Hello,
I need to parse some Aurelia source code files.
For this purpose I chose Babel.js 7+.
The AST result completely covers my needs.
import babel = require("@babel/parser");
let ast = babel.parse(code, {
sourceType: "module",
plugins: [
'jsx',
'typescript',
'classProperties',
['decorators', { decoratorsBeforeExport: true }]
]
});
The source code is:
export class RgbToHexValueConverter {
toView(rgb: IColor): string {
return "#" + (
(1 << 24) + (rgb.r << 16) + (rgb.g << 8) + rgb.b).toString(16).slice(1);
}
fromView(hex: string): IColor {
let exp = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i,
result = exp.exec(hex);
return {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
};
}
get fullName(): string {
return "THENAME";
}
}
class Test {
getInfo(input: any): any {}
getData(input: any): any {}
}
interface IColor {
r: number;
g: number;
b: number;
}
To achieve the result I found a way that is named jsonpath
$..program.body[0].declaration.body.body[0].key.name
But I want to use native Visitors. Is it possible?
I want to implement a scenario with visitors :
If you foundExportNamedDeclaration then go to declaration to find ClassDeclaration type.
If you don’t, Are you in ClassDeclaration ?
If yes, then go to body and find ClassBody then again body
And for each ClassMethod go to the key and return the name.
With this you can get all name methods in a class.
ExportNamedDeclaration > ClassDeclaration > ClassBody >
ClassMethod > Identifier > [getInfo, getData]
How can I get above result by visitors? Or what is the best/correct way to get info form AST in Babel.js 7+?

