Skip to content

Commit 0d86090

Browse files
feat: apex parser
1 parent b3b944b commit 0d86090

File tree

3 files changed

+64
-5
lines changed

3 files changed

+64
-5
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"@types/lodash.chunk": "^4.2.9",
1414
"lodash.chunk": "^4.2.0",
1515
"open": "^8.4.2",
16-
"tslib": "^2"
16+
"tslib": "^2",
17+
"@apexdevtools/apex-parser": "^4.1.0"
1718
},
1819
"devDependencies": {
1920
"@oclif/dev-cli": "^1",
@@ -114,4 +115,4 @@
114115
"pre-push": "sf-husky-pre-push"
115116
}
116117
}
117-
}
118+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import fs from 'fs';
2+
import {
3+
ApexLexer,
4+
CommonTokenStream,
5+
ApexParser,
6+
CaseInsensitiveInputStream,
7+
ApexParserListener,
8+
ClassDeclarationContext,
9+
DotExpressionContext,
10+
VariableDeclaratorContext,
11+
CompilationUnitContext,
12+
} from '@apexdevtools/apex-parser';
13+
import { CharStreams } from 'antlr4ts';
14+
import { ParseTreeWalker } from 'antlr4ts/tree/ParseTreeWalker';
15+
16+
export class ApexASTTraverser {
17+
public static parse(apexClass: string): CompilationUnitContext {
18+
const lexer = new ApexLexer(new CaseInsensitiveInputStream(CharStreams.fromString(apexClass)));
19+
const tokens = new CommonTokenStream(lexer);
20+
const parser = new ApexParser(tokens);
21+
const context = parser.compilationUnit();
22+
// parser.addParseListener(new interfaceVisitor() as ApexParserListener);
23+
ParseTreeWalker.DEFAULT.walk(new interfaceVisitor() as ApexParserListener, context);
24+
return context;
25+
}
26+
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[] = [];
35+
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());
53+
}
54+
}
55+
}
56+
57+
const filePath = '/Users/abhinavkumar2/company/plugin-omnistudio-migration-tool/test/FormulaParserService.cls';
58+
ApexASTTraverser.traverse(filePath);
59+
60+
// console.log(ast);

test/commands/omnistudio/migration/info.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ describe('omnistudio:migration:info', () => {
2121
.stdout()
2222
.command(['omnistudio:migration:info', '--targetusername', '[email protected]', '--allversions'])
2323
.it('runs omnistudio:migration:info --targetusername [email protected] --allversions', (ctx) => {
24-
expect(ctx.stdout).to.contain(
25-
'Hello world! This is org: Super Awesome Org and I will be around until Tue Mar 20 2018!'
26-
);
24+
expect(ctx.stdout).to.contain('Hello world! This is org: Super Awesome Org');
2725
});
2826
});

0 commit comments

Comments
 (0)