• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { Code } from '../utils/constant';
17import { StringUtils } from '../utils/stringUtils';
18import { AstNodeHelper, RawSourceCodeInfoImpl } from './coreImpls';
19import type { Context, ISourceCodeProcessor, ProcessResult, sourceParser } from './typedef';
20import type { comment } from './typedef';
21
22export class RawSourceCodeProcessor implements ISourceCodeProcessor, sourceParser.INodeVisitorCallback {
23  rawSourceCodeInfo?: RawSourceCodeInfoImpl;
24  async process(context: Context, content: string): Promise<ProcessResult> {
25    const sourceParser: sourceParser.SourceCodeParser = context.getSourceParser(content);
26    this.rawSourceCodeInfo = new RawSourceCodeInfoImpl(content);
27    sourceParser.visitEachNodeComment(this, false);
28    context.setRawSourceInfo(this.rawSourceCodeInfo);
29    return {
30      code: Code.OK,
31      content: content
32    };
33  }
34
35  onVisitNode(node: comment.CommentNode): void {
36    if (node.astNode) {
37      const nodeSignature = AstNodeHelper.getNodeSignature(node.astNode);
38      if (StringUtils.isEmpty(nodeSignature)) {
39        return;
40      }
41      const sourceFile = node.astNode.getSourceFile();
42      if (!sourceFile) {
43        return;
44      }
45      const { line, character } = sourceFile.getLineAndCharacterOfPosition(node.astNode.getStart());
46      this.rawSourceCodeInfo?.addRawNodeInfo(nodeSignature, node.astNode, line + 1, character);
47    }
48  }
49}