1import { Code } from '../utils/constant'; 2import { StringUtils } from '../utils/stringUtils'; 3import { AstNodeHelper, RawSourceCodeInfoImpl } from './coreImpls'; 4import type { Context, ISourceCodeProcessor, ProcessResult, sourceParser } from './typedef'; 5import { comment } from './typedef'; 6 7export class RawSourceCodeProcessor implements ISourceCodeProcessor, sourceParser.INodeVisitorCallback { 8 rawSourceCodeInfo?: RawSourceCodeInfoImpl; 9 async process(context: Context, content: string): Promise<ProcessResult> { 10 const sourceParser: sourceParser.SourceCodeParser = context.getSourceParser(content); 11 this.rawSourceCodeInfo = new RawSourceCodeInfoImpl(content); 12 sourceParser.visitEachNodeComment(this, false); 13 context.setRawSourceInfo(this.rawSourceCodeInfo); 14 return { 15 code: Code.OK, 16 content: content 17 }; 18 } 19 20 onVisitNode(node: comment.CommentNode): void { 21 if (node.astNode) { 22 const nodeSignature = AstNodeHelper.getNodeSignature(node.astNode); 23 if (StringUtils.isEmpty(nodeSignature)) { 24 return; 25 } 26 const sourceFile = node.astNode.getSourceFile(); 27 if (!sourceFile) { 28 return; 29 } 30 const { line, character } = sourceFile.getLineAndCharacterOfPosition(node.astNode.getStart()); 31 this.rawSourceCodeInfo?.addRawNodeInfo(nodeSignature, node.astNode, line + 1, character); 32 } 33 } 34}