Skip to content

Commit 528c72a

Browse files
committed
feat: update js_parser to support .mjs files
1 parent 69640d8 commit 528c72a

1 file changed

Lines changed: 25 additions & 5 deletions

File tree

test-app/build-tools/jsparser/js_parser.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ function readInterfaceNames(data, err) {
184184
}
185185

186186
/*
187-
* Traverses a given input directory and attempts to visit every ".js" file.
187+
* Traverses a given input directory and attempts to visit every ".js" and ".mjs" file.
188188
* It passes each found file down the line.
189189
*/
190190
function traverseAndAnalyseFilesDir(inputDir, err) {
@@ -201,7 +201,7 @@ function traverseFiles(filesToTraverse) {
201201
}
202202
for (let i = 0; i < filesToTraverse.length; i += 1) {
203203
const fp = filesToTraverse[i];
204-
logger.info("Visiting JavaScript file: " + fp);
204+
logger.info("Visiting JavaScript/ES Module file: " + fp);
205205

206206
readFile(fp)
207207
.then(astFromFileContent.bind(null, fp))
@@ -233,6 +233,7 @@ const readFile = function (filePath, err) {
233233

234234
/*
235235
* Get's the AST (https://en.wikipedia.org/wiki/Abstract_syntax_tree) from the file content and passes it down the line.
236+
* Supports both CommonJS (.js) and ES modules (.mjs) files.
236237
*/
237238
const astFromFileContent = function (path, data, err) {
238239
return new Promise(function (resolve, reject) {
@@ -241,13 +242,28 @@ const astFromFileContent = function (path, data, err) {
241242
return reject(err);
242243
}
243244

244-
const ast = babelParser.parse(data.data, {
245+
// Determine if this is an ES module based on file extension
246+
const isESModule = path.endsWith('.mjs');
247+
248+
// Configure Babel parser based on file type
249+
const parserOptions = {
245250
minify: false,
246251
plugins: [
247252
["@babel/plugin-proposal-decorators", { decoratorsBeforeExport: true }],
248253
"objectRestSpread",
249254
],
250-
});
255+
};
256+
257+
// For ES modules, set sourceType to 'module'
258+
if (isESModule) {
259+
parserOptions.sourceType = 'module';
260+
logger.info(`Parsing ES module: ${path}`);
261+
} else {
262+
// For regular JS files, keep existing behavior (default sourceType is 'script')
263+
logger.info(`Parsing CommonJS file: ${path}`);
264+
}
265+
266+
const ast = babelParser.parse(data.data, parserOptions);
251267
data.ast = ast;
252268
return resolve(data);
253269
});
@@ -271,14 +287,18 @@ const visitAst = function (path, data, err) {
271287

272288
traverse.default(data.ast, {
273289
enter: function (path) {
290+
// Determine file extension length to properly strip it from the path
291+
const fileExtension = data.filePath.endsWith('.mjs') ? '.mjs' : '.js';
292+
const extensionLength = fileExtension.length;
293+
274294
const decoratorConfig = {
275295
logger: logger,
276296
extendDecoratorName: extendDecoratorName,
277297
interfacesDecoratorName: interfacesDecoratorName,
278298
filePath:
279299
data.filePath.substring(
280300
inputDir.length + 1,
281-
data.filePath.length - 3
301+
data.filePath.length - extensionLength
282302
) || "",
283303
fullPathName: data.filePath
284304
.substring(inputDir.length + 1)

0 commit comments

Comments
 (0)