|
| 1 | +/* eslint-disable @typescript-eslint/restrict-template-expressions */ |
1 | 2 | /* eslint-disable @typescript-eslint/no-unsafe-call */ |
2 | | -/* eslint-disable @typescript-eslint/no-unsafe-assignment */ |
| 3 | +/* eslint-disable @typescript-eslint/no-unsafe-return */ |
| 4 | +/* eslint-disable @typescript-eslint/no-unsafe-member-access */ |
3 | 5 | /* eslint-disable no-console */ |
4 | | -/* eslint-disable @typescript-eslint/explicit-member-accessibility */ |
5 | | -// import * as fs from 'fs'; |
6 | | -// import { parse, type ParseResult } from '@babel/parser'; // Import all types from @babel/types |
| 6 | +import * as fs from 'fs'; |
| 7 | +import * as parser from '@babel/parser'; |
| 8 | +import traverse from '@babel/traverse'; |
| 9 | +import generate from '@babel/generator'; |
| 10 | +import * as t from '@babel/types'; |
7 | 11 |
|
8 | | -class JavaScriptParser { |
9 | | - // private fileContent: string; |
10 | | - private ast: File | null = null; // Specify the generic type argument |
| 12 | +const DEFAULT_NAMESPACE = 'c'; |
11 | 13 |
|
12 | | - constructor(filePath: string) { |
13 | | - // this.fileContent = fs.readFileSync(filePath, 'utf-8'); |
14 | | - this.ast = null; |
15 | | - } |
| 14 | +export class JavaScriptParser { |
| 15 | + // Function to replace strings in import declarations and write back to file |
| 16 | + public replaceImportSource(filePath: string, oldSource: string): string { |
| 17 | + // Read the JavaScript file |
| 18 | + const code = fs.readFileSync(filePath, 'utf-8'); |
16 | 19 |
|
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 | | - // }); |
| 20 | + // Parse the code into an AST (Abstract Syntax Tree) |
| 21 | + const ast = parser.parse(code, { |
| 22 | + sourceType: 'module', // Specify that we are parsing an ES module |
| 23 | + plugins: ['decorators'], // Include any relevant plugins if necessary (e.g., 'jsx', 'flow', etc.) |
| 24 | + }); |
22 | 25 |
|
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 | | - // } |
| 26 | + // Traverse the AST and modify import declarations |
| 27 | + traverse(ast, { |
| 28 | + ImportDeclaration(path) { |
| 29 | + const importSource = path.node.source.value; |
29 | 30 |
|
30 | | - // Method to get the AST as a string |
31 | | - getAST(): string | null { |
32 | | - if (!this.ast) { |
33 | | - console.error('AST is not available. Please parse the code first.'); |
34 | | - return null; |
35 | | - } |
36 | | - return JSON.stringify(this.ast, null, 2); |
| 31 | + // Check if the import source contains the old substring |
| 32 | + if (importSource.includes(oldSource + '/')) { |
| 33 | + // Replace the old substring with the new substring |
| 34 | + const updatedSource = importSource.replace(oldSource, DEFAULT_NAMESPACE); |
| 35 | + // Update the AST with the new source |
| 36 | + path.node.source = t.stringLiteral(updatedSource); |
| 37 | + } |
| 38 | + }, |
| 39 | + }); |
| 40 | + return generate(ast, {}, code).code; |
37 | 41 | } |
38 | 42 |
|
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 |
| 43 | + // Method to save modified HTML back to a file |
| 44 | + public saveToFile(filePath: string, output: string): void { |
| 45 | + try { |
| 46 | + fs.writeFileSync(filePath, output, 'utf-8'); |
| 47 | + console.log(`Replaced import 'oldSource' with 'c' in file: ${filePath}`); |
| 48 | + } catch (error) { |
| 49 | + console.error(`Error writing file to disk: ${error}`); |
| 50 | + throw error; |
45 | 51 | } |
46 | 52 | } |
47 | 53 | } |
48 | | - |
49 | | -export default JavaScriptParser; |
|
0 commit comments