44/* eslint-disable @typescript-eslint/no-unsafe-member-access */
55/* eslint-disable no-console */
66import * as fs from 'fs' ;
7- import * as parser from '@babel/parser' ;
7+ import { parse } from '@babel/parser' ;
88import traverse from '@babel/traverse' ;
9- import * as t from '@babel/types' ;
109import { FileConstant } from '../fileutils/FileConstant' ;
1110
1211const DEFAULT_NAMESPACE = 'c' ;
1312
1413export class JavaScriptParser {
1514 // Function to replace strings in import declarations and write back to file
16- public replaceImportSource ( filePath : string , oldSource : string ) : Map < string , string > {
15+ public replaceImportSource ( filePath : string , oldSource : string ) : Map < string , string > | null {
1716 const jsContentMap = new Map < string , string > ( ) ;
17+
1818 // Read the JavaScript file
1919 const code = fs . readFileSync ( filePath , 'utf-8' ) ;
20+
21+ // Skip processing if the file has specific markers or doesn't include the oldSource
2022 if ( code . includes ( 'Generated class DO NOT MODIFY' ) || ! code . includes ( oldSource + '/' ) ) {
2123 return null ;
2224 }
25+
26+ // Store the original file content
2327 jsContentMap . set ( FileConstant . BASE_CONTENT , code ) ;
24- // Parse the code into an AST (Abstract Syntax Tree)
25- const ast = parser . parse ( code , {
28+
29+ // Parse the code into an AST
30+ const ast = parse ( code , {
2631 sourceType : 'module' , // Specify that we are parsing an ES module
27- plugins : [ 'decorators' ] , // Include any relevant plugins if necessary (e.g., 'jsx', 'flow', etc.)
32+ plugins : [ 'decorators' ] , // Include any relevant plugins if necessary
2833 } ) ;
2934
30- // Traverse the AST and modify import declarations
35+ // Array to store replacement operations
36+ const replacements : Array < { original : string ; updated : string } > = [ ] ;
37+
38+ // Traverse the AST and identify import declarations
3139 traverse ( ast , {
3240 ImportDeclaration ( path ) {
3341 const importSource = path . node . source . value ;
@@ -36,14 +44,20 @@ export class JavaScriptParser {
3644 if ( importSource . includes ( oldSource + '/' ) ) {
3745 // Replace the old substring with the new substring
3846 const updatedSource = importSource . replace ( oldSource , DEFAULT_NAMESPACE ) ;
39- // Update the AST with the new source
40- path . node . source = t . stringLiteral ( updatedSource ) ;
47+ replacements . push ( { original : importSource , updated : updatedSource } ) ;
4148 }
4249 } ,
4350 } ) ;
44- // jsContentMap.set(FileConstant.MODIFIED_CONTENT, generate(ast, {}, code).code);
45- jsContentMap . set ( FileConstant . MODIFIED_CONTENT , code . replace ( oldSource , DEFAULT_NAMESPACE ) ) ;
46- // return generate(ast, {}, code).code;
51+
52+ // Apply replacements directly to the original code
53+ let modifiedCode = code ;
54+ replacements . forEach ( ( { original, updated } ) => {
55+ const importRegex = new RegExp ( `(["'])${ original } (["'])` , 'g' ) ; // Match the import string with quotes
56+ modifiedCode = modifiedCode . replace ( importRegex , `$1${ updated } $2` ) ;
57+ } ) ;
58+
59+ // Store the modified content
60+ jsContentMap . set ( FileConstant . MODIFIED_CONTENT , modifiedCode ) ;
4761 return jsContentMap ;
4862 }
4963
0 commit comments