|
| 1 | +/* eslint-disable @typescript-eslint/no-unsafe-return */ |
| 2 | +/* eslint-disable @typescript-eslint/no-unsafe-call */ |
| 3 | +/* eslint-disable @typescript-eslint/no-unsafe-member-access */ |
| 4 | +/* eslint-disable @typescript-eslint/no-unsafe-assignment */ |
| 5 | +import { |
| 6 | + ApexLexer, |
| 7 | + CommonTokenStream, |
| 8 | + ApexParser, |
| 9 | + CaseInsensitiveInputStream, |
| 10 | + ApexParserListener, |
| 11 | + ClassDeclarationContext, |
| 12 | + DotExpressionContext, |
| 13 | + VariableDeclaratorContext, |
| 14 | + CompilationUnitContext, |
| 15 | +} from '@apexdevtools/apex-parser'; |
| 16 | +import { CharStreams, Token } from 'antlr4ts'; |
| 17 | +import { ParseTreeWalker } from 'antlr4ts/tree/ParseTreeWalker'; |
| 18 | + |
| 19 | +export class ApexASTParser { |
| 20 | + private apexFileContent: string; |
| 21 | + private implementsInterface: Map<string, Token> = new Map(); |
| 22 | + // private callsMethods: Map<string, Token[]>; |
| 23 | + private interfaceName: string; |
| 24 | + private methodName: string; |
| 25 | + // private className: string; |
| 26 | + private astListener: ApexParserListener; |
| 27 | + |
| 28 | + public get implemementsInterface(): Map<string, Token> { |
| 29 | + return this.implementsInterface; |
| 30 | + } |
| 31 | + |
| 32 | + public constructor(apexFileContent: string, interfaceName: string, methodName: string) { |
| 33 | + this.apexFileContent = apexFileContent; |
| 34 | + this.interfaceName = interfaceName; |
| 35 | + this.methodName = methodName; |
| 36 | + this.astListener = this.createASTListener(); |
| 37 | + } |
| 38 | + |
| 39 | + public parse(): CompilationUnitContext { |
| 40 | + const lexer = new ApexLexer(new CaseInsensitiveInputStream(CharStreams.fromString(this.apexFileContent))); |
| 41 | + const tokens = new CommonTokenStream(lexer); |
| 42 | + const parser = new ApexParser(tokens); |
| 43 | + const context = parser.compilationUnit(); |
| 44 | + // parser.addParseListener(new interfaceVisitor() as ApexParserListener); |
| 45 | + ParseTreeWalker.DEFAULT.walk(this.astListener, context); |
| 46 | + return context; |
| 47 | + } |
| 48 | + |
| 49 | + private createASTListener(): ApexParserListener { |
| 50 | + class ApexMigrationListener implements ApexParserListener { |
| 51 | + public constructor(private parser: ApexASTParser) {} |
| 52 | + public enterClassDeclaration(ctx: ClassDeclarationContext): void { |
| 53 | + const interfaceToBeSearched = this.parser.interfaceName; |
| 54 | + if (!interfaceToBeSearched) return; |
| 55 | + if (!ctx.typeList() || !ctx.typeList().typeRef()) return; |
| 56 | + for (const typeRefContext of ctx.typeList().typeRef()) |
| 57 | + for (const typeNameContext of typeRefContext.typeName()) { |
| 58 | + if (!typeNameContext.id() || !typeNameContext.id().Identifier()) continue; |
| 59 | + if (typeNameContext.id().Identifier().symbol.text === interfaceToBeSearched) { |
| 60 | + this.parser.implementsInterface.set(interfaceToBeSearched, typeNameContext.id().Identifier().symbol); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public enterDotExpression(ctx: DotExpressionContext): void { |
| 66 | + // console.log('*********'); |
| 67 | + // console.log(ctx.expression().start.text); |
| 68 | + if (ctx.dotMethodCall() && this.parser.methodName) { |
| 69 | + // console.log(ctx.dotMethodCall().anyId().Identifier().symbol.text); |
| 70 | + // ctx.dotMethodCall().expressionList().expression(1).children[0].children[0].children[0]; |
| 71 | + // console.log(ctx.dotMethodCall().expressionList().expression(1).children[0]); |
| 72 | + } |
| 73 | + // console.log('*********'); |
| 74 | + } |
| 75 | + |
| 76 | + public enterVariableDeclarator(ctx: VariableDeclaratorContext): void { |
| 77 | + if (ctx.id().Identifier().symbol.text === 'DRName') { |
| 78 | + // console.log(ctx.expression()); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + return new ApexMigrationListener(this); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// const filePath = '/Users/abhinavkumar2/company/plugin-omnistudio-migration-tool/test/FormulaParserService.cls'; |
| 87 | +// new ApexASTParser(filePath, 'callable', '').parse(filePath); |
| 88 | + |
| 89 | +// console.log(ast); |
0 commit comments