diff --git a/src/syntax.ts b/src/syntax.ts index cc3769f..b518ea1 100644 --- a/src/syntax.ts +++ b/src/syntax.ts @@ -10,7 +10,8 @@ const ESM_RE = const CJS_RE = /([\s;]|^)(module.exports\b|exports\.\w|require\s*\(|global\.\w)/m; -const COMMENT_RE = /\/\*.+?\*\/|\/\/.*(?=[nr])/g; +const MULTI_LINE_COMMENT_RE = /\/\*.+?\*\//gs; +const SINGLE_LINE_COMMENT_RE = /\/\/.*/g; const BUILTIN_EXTENSIONS = new Set([".mjs", ".cjs", ".node", ".wasm"]); @@ -37,7 +38,9 @@ export function hasESMSyntax( opts: DetectSyntaxOptions = {}, ): boolean { if (opts.stripComments) { - code = code.replace(COMMENT_RE, ""); + code = code + .replace(SINGLE_LINE_COMMENT_RE, "") + .replace(MULTI_LINE_COMMENT_RE, ""); } return ESM_RE.test(code); } @@ -54,7 +57,9 @@ export function hasCJSSyntax( opts: DetectSyntaxOptions = {}, ): boolean { if (opts.stripComments) { - code = code.replace(COMMENT_RE, ""); + code = code + .replace(SINGLE_LINE_COMMENT_RE, "") + .replace(MULTI_LINE_COMMENT_RE, ""); } return CJS_RE.test(code); } @@ -68,7 +73,9 @@ export function hasCJSSyntax( */ export function detectSyntax(code: string, opts: DetectSyntaxOptions = {}) { if (opts.stripComments) { - code = code.replace(COMMENT_RE, ""); + code = code + .replace(SINGLE_LINE_COMMENT_RE, "") + .replace(MULTI_LINE_COMMENT_RE, ""); } // We strip comments once hence not passing opts down to hasESMSyntax and hasCJSSyntax const hasESM = hasESMSyntax(code, {}); diff --git a/test/syntax.test.ts b/test/syntax.test.ts index 785cb07..1895fca 100644 --- a/test/syntax.test.ts +++ b/test/syntax.test.ts @@ -92,6 +92,8 @@ const staticTests = { const staticTestsWithComments = { '// They\'re exposed using "export import" so that types are passed along as expected\nmodule.exports={};': { hasESM: false, hasCJS: true, isMixed: false }, + "/* export * */": { hasESM: false, hasCJS: false, isMixed: false }, + "/* \n export * \n */": { hasESM: false, hasCJS: false, isMixed: false }, }; describe("detectSyntax", () => {