|
| 1 | +/* eslint-disable @typescript-eslint/no-unsafe-call */ |
| 2 | +/* eslint-disable @typescript-eslint/no-unsafe-assignment */ |
1 | 3 | /* eslint-disable no-console */ |
2 | 4 | /* eslint-disable @typescript-eslint/explicit-member-accessibility */ |
3 | | -import * as fs from 'fs'; |
4 | | -import * as parser from '@babel/parser'; |
5 | | -import traverse, { NodePath } from '@babel/traverse'; |
6 | | -import * as t from '@babel/types'; // Import all types from @babel/types |
| 5 | +// import * as fs from 'fs'; |
| 6 | +// import { parse, type ParseResult } from '@babel/parser'; // Import all types from @babel/types |
7 | 7 |
|
8 | 8 | class JavaScriptParser { |
9 | | - private fileContent: string; |
10 | | - private ast: parser.ParseResult<t.File> | null; // Specify the generic type argument |
| 9 | + // private fileContent: string; |
| 10 | + private ast: File | null = null; // Specify the generic type argument |
11 | 11 |
|
12 | 12 | constructor(filePath: string) { |
13 | | - this.fileContent = fs.readFileSync(filePath, 'utf-8'); |
| 13 | + // this.fileContent = fs.readFileSync(filePath, 'utf-8'); |
14 | 14 | this.ast = null; |
15 | 15 | } |
16 | 16 |
|
17 | | - public parseFile(): void { |
18 | | - this.ast = parser.parse(this.fileContent, { |
19 | | - sourceType: 'module', |
20 | | - plugins: ['decorators'], |
21 | | - }); |
22 | | - } |
| 17 | + // public parseCode(): void { |
| 18 | + // const parseResult: File = parse(this.fileContent, { |
| 19 | + // sourceType: 'module', // Use 'script' if you're parsing non-module code |
| 20 | + // plugins: ['jsx', 'typescript'], // Add plugins as needed |
| 21 | + // }); |
| 22 | + |
| 23 | + // if (parseResult.type === 'File') { |
| 24 | + // this.ast = parseResult; |
| 25 | + // } else { |
| 26 | + // throw new Error("Parsing did not return a 'File' node as expected."); |
| 27 | + // } |
| 28 | + // } |
23 | 29 |
|
24 | | - public traverseAST(): string[] { |
25 | | - // eslint-disable-next-line @typescript-eslint/no-explicit-any |
26 | | - const labels: string[] = []; |
| 30 | + // Method to get the AST as a string |
| 31 | + getAST(): string | null { |
27 | 32 | if (!this.ast) { |
28 | | - throw new Error('AST has not been generated. Call parseFile() first.'); |
| 33 | + console.error('AST is not available. Please parse the code first.'); |
| 34 | + return null; |
29 | 35 | } |
| 36 | + return JSON.stringify(this.ast, null, 2); |
| 37 | + } |
30 | 38 |
|
31 | | - traverse(this.ast, { |
32 | | - ImportDeclaration(path: NodePath<t.ImportDeclaration>) { |
33 | | - console.log('Import found:', path.node.source.value); |
34 | | - labels.push(path.node.source.value); |
35 | | - }, |
36 | | - }); |
37 | | - // eslint-disable-next-line @typescript-eslint/no-unsafe-return |
38 | | - return labels; |
| 39 | + // Main method to process the file |
| 40 | + processFile(): void { |
| 41 | + // this.parseCode(); // Parse the JavaScript code |
| 42 | + const astString = this.getAST(); // Get the AST as a string |
| 43 | + if (astString) { |
| 44 | + console.log(astString); // Output the AST |
| 45 | + } |
39 | 46 | } |
40 | 47 | } |
41 | 48 |
|
|
0 commit comments