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 */
15import fs from 'fs' ;
26import {
37 ApexLexer ,
@@ -10,51 +14,79 @@ import {
1014 VariableDeclaratorContext ,
1115 CompilationUnitContext ,
1216} from '@apexdevtools/apex-parser' ;
13- import { CharStreams } from 'antlr4ts' ;
17+ import { CharStreams , Token } from 'antlr4ts' ;
1418import { ParseTreeWalker } from 'antlr4ts/tree/ParseTreeWalker' ;
1519
16- export class ApexASTTraverser {
17- public static parse ( apexClass : string ) : CompilationUnitContext {
18- const lexer = new ApexLexer ( new CaseInsensitiveInputStream ( CharStreams . fromString ( apexClass ) ) ) ;
20+ export class ApexASTParser {
21+ private apexFilePath : string ;
22+ private implementsInterface : Map < string , Token > ;
23+ private callsMethods : Map < string , Token [ ] > ;
24+ private interfaceName : string ;
25+ private methodName : string ;
26+ private className : string ;
27+ private astListener : ApexParserListener ;
28+
29+ public get implemementsInterface ( ) : Map < string , Token > {
30+ return this . implementsInterface ;
31+ }
32+
33+ public constructor ( apexFilePath : string , interfaceName : string , methodName : string ) {
34+ this . apexFilePath = apexFilePath ;
35+ this . interfaceName = interfaceName ;
36+ this . methodName = methodName ;
37+ this . astListener = this . createASTListener ( ) ;
38+ }
39+
40+ public parse ( filePath : string ) : CompilationUnitContext {
41+ const fileContent = fs . readFileSync ( filePath ) . toString ( ) ;
42+ const lexer = new ApexLexer ( new CaseInsensitiveInputStream ( CharStreams . fromString ( fileContent ) ) ) ;
1943 const tokens = new CommonTokenStream ( lexer ) ;
2044 const parser = new ApexParser ( tokens ) ;
2145 const context = parser . compilationUnit ( ) ;
2246 // parser.addParseListener(new interfaceVisitor() as ApexParserListener);
23- ParseTreeWalker . DEFAULT . walk ( new interfaceVisitor ( ) as ApexParserListener , context ) ;
47+ ParseTreeWalker . DEFAULT . walk ( this . astListener , context ) ;
2448 return context ;
2549 }
2650
27- public static traverse ( filePath : string ) : CompilationUnitContext {
28- const fileContent = fs . readFileSync ( filePath ) . toString ( ) ;
29- const ast = this . parse ( fileContent ) ;
30- return ast ;
31- }
32- }
33- export class interfaceVisitor implements ApexParserListener {
34- private interfaceImplementations : string [ ] = [ ] ;
51+ private createASTListener ( ) : ApexParserListener {
52+ class ApexMigrationListener implements ApexParserListener {
53+ public constructor ( private parser : ApexASTParser ) { }
3554
36- public enterClassDeclaration ( ctx : ClassDeclarationContext ) : void {
37- this . interfaceImplementations . push ( ctx . typeList ( ) . typeRef ( 0 ) . typeName ( 0 ) . id ( ) . Identifier ( ) . symbol . text ) ;
38- }
39- public enterDotExpression ( ctx : DotExpressionContext ) : void {
40- // console.log('*********');
41- // console.log(ctx.expression().start.text);
42-
43- if ( ctx . dotMethodCall ( ) ) {
44- // console.log(ctx.dotMethodCall().anyId().Identifier().symbol.text);
45- // ctx.dotMethodCall().expressionList().expression(1).children[0].children[0].children[0];
46- // console.log(ctx.dotMethodCall().expressionList().expression(1).children[0]);
47- }
48- // console.log('*********');
49- }
50- public enterVariableDeclarator ( ctx : VariableDeclaratorContext ) : void {
51- if ( ctx . id ( ) . Identifier ( ) . symbol . text === 'DRName' ) {
52- // console.log(ctx.expression());
55+ public enterClassDeclaration ( ctx : ClassDeclarationContext ) : void {
56+ const interfaceToBeSearched = this . parser . interfaceName ;
57+ if ( ! interfaceToBeSearched ) return ;
58+ if ( ! ctx . typeList ( ) || ! ctx . typeList ( ) . typeRef ( ) ) return ;
59+ for ( const typeRefContext of ctx . typeList ( ) . typeRef ( ) )
60+ for ( const typeNameContext of typeRefContext . typeName ( ) ) {
61+ if ( ! typeNameContext . id ( ) || ! typeNameContext . id ( ) . Identifier ( ) ) continue ;
62+ if ( typeNameContext . id ( ) . Identifier ( ) . symbol . text === interfaceToBeSearched ) {
63+ this . parser . implementsInterface . set ( interfaceToBeSearched , typeNameContext . id ( ) . Identifier ( ) . symbol ) ;
64+ }
65+ }
66+ }
67+
68+ public enterDotExpression ( ctx : DotExpressionContext ) : void {
69+ // console.log('*********');
70+ // console.log(ctx.expression().start.text);
71+ if ( ctx . dotMethodCall ( ) ) {
72+ // console.log(ctx.dotMethodCall().anyId().Identifier().symbol.text);
73+ // ctx.dotMethodCall().expressionList().expression(1).children[0].children[0].children[0];
74+ // console.log(ctx.dotMethodCall().expressionList().expression(1).children[0]);
75+ }
76+ // console.log('*********');
77+ }
78+
79+ public enterVariableDeclarator ( ctx : VariableDeclaratorContext ) : void {
80+ if ( ctx . id ( ) . Identifier ( ) . symbol . text === 'DRName' ) {
81+ // console.log(ctx.expression());
82+ }
83+ }
5384 }
85+ return new ApexMigrationListener ( this ) ;
5486 }
5587}
5688
57- const filePath = '/Users/abhinavkumar2/company/plugin-omnistudio-migration-tool/test/FormulaParserService.cls' ;
58- ApexASTTraverser . traverse ( filePath ) ;
89+ // const filePath = '/Users/abhinavkumar2/company/plugin-omnistudio-migration-tool/test/FormulaParserService.cls';
90+ // new ApexASTParser(filePath, 'callable', '').parse (filePath);
5991
6092// console.log(ast);
0 commit comments