From 1abbf273b9c057aa4607224fc66ef84c6fdd2f31 Mon Sep 17 00:00:00 2001 From: julien huang Date: Sat, 23 Sep 2023 14:10:43 +0200 Subject: [PATCH 1/3] fix: strip comment for syntax detection --- src/syntax.ts | 4 ++-- test/syntax.test.ts | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/syntax.ts b/src/syntax.ts index 05d12f5..4c88846 100644 --- a/src/syntax.ts +++ b/src/syntax.ts @@ -10,13 +10,13 @@ const ESM_RE = const BUILTIN_EXTENSIONS = new Set([".mjs", ".cjs", ".node", ".wasm"]); export function hasESMSyntax(code: string): boolean { - return ESM_RE.test(code); + return ESM_RE.test(code.replace(/\/\*.+?\*\/|\/\/.*(?=[nr])/g, '')); } const CJS_RE = /([\s;]|^)(module.exports\b|exports\.\w|require\s*\(|global\.\w)/m; export function hasCJSSyntax(code: string): boolean { - return CJS_RE.test(code); + return CJS_RE.test(code.replace(/\/\*.+?\*\/|\/\/.*(?=[nr])/g, '')); } export function detectSyntax(code: string) { diff --git a/test/syntax.test.ts b/test/syntax.test.ts index c679880..ce98b82 100644 --- a/test/syntax.test.ts +++ b/test/syntax.test.ts @@ -82,6 +82,7 @@ const staticTests = { isMixed: false, }, "const a={};": { hasESM: false, hasCJS: false, isMixed: false }, + "// They're exposed using \"export import\" so that types are passed along as expected\nmodule.exports={};": { hasESM: false, hasCJS: true , isMixed: false } }; describe("detectSyntax", () => { From b6f0452ce212228d05df1e2eeedd3825bb0096ca Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Thu, 11 Jan 2024 11:32:39 +0100 Subject: [PATCH 2/3] lint --- src/syntax.ts | 4 ++-- test/syntax.test.ts | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/syntax.ts b/src/syntax.ts index 4c88846..2b808c7 100644 --- a/src/syntax.ts +++ b/src/syntax.ts @@ -10,13 +10,13 @@ const ESM_RE = const BUILTIN_EXTENSIONS = new Set([".mjs", ".cjs", ".node", ".wasm"]); export function hasESMSyntax(code: string): boolean { - return ESM_RE.test(code.replace(/\/\*.+?\*\/|\/\/.*(?=[nr])/g, '')); + return ESM_RE.test(code.replace(/\/\*.+?\*\/|\/\/.*(?=[nr])/g, "")); } const CJS_RE = /([\s;]|^)(module.exports\b|exports\.\w|require\s*\(|global\.\w)/m; export function hasCJSSyntax(code: string): boolean { - return CJS_RE.test(code.replace(/\/\*.+?\*\/|\/\/.*(?=[nr])/g, '')); + return CJS_RE.test(code.replace(/\/\*.+?\*\/|\/\/.*(?=[nr])/g, "")); } export function detectSyntax(code: string) { diff --git a/test/syntax.test.ts b/test/syntax.test.ts index ce98b82..4e8c286 100644 --- a/test/syntax.test.ts +++ b/test/syntax.test.ts @@ -82,7 +82,8 @@ const staticTests = { isMixed: false, }, "const a={};": { hasESM: false, hasCJS: false, isMixed: false }, - "// They're exposed using \"export import\" so that types are passed along as expected\nmodule.exports={};": { hasESM: false, hasCJS: true , isMixed: false } + '// They\'re exposed using "export import" so that types are passed along as expected\nmodule.exports={};': + { hasESM: false, hasCJS: true, isMixed: false }, }; describe("detectSyntax", () => { From 7305f39380e69f7dd88e76c6066732d7133b103d Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Thu, 11 Jan 2024 11:36:08 +0100 Subject: [PATCH 3/3] refactor: extract comment regex --- src/syntax.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/syntax.ts b/src/syntax.ts index 2b808c7..7a5855e 100644 --- a/src/syntax.ts +++ b/src/syntax.ts @@ -7,16 +7,19 @@ import { isNodeBuiltin, getProtocol } from "./utils"; const ESM_RE = /([\s;]|^)(import[\s\w*,{}]*from|import\s*["'*{]|export\b\s*(?:[*{]|default|class|type|function|const|var|let|async function)|import\.meta\b)/m; +const CJS_RE = + /([\s;]|^)(module.exports\b|exports\.\w|require\s*\(|global\.\w)/m; + +const COMMENT_RE = /\/\*.+?\*\/|\/\/.*(?=[nr])/g; + const BUILTIN_EXTENSIONS = new Set([".mjs", ".cjs", ".node", ".wasm"]); export function hasESMSyntax(code: string): boolean { - return ESM_RE.test(code.replace(/\/\*.+?\*\/|\/\/.*(?=[nr])/g, "")); + return ESM_RE.test(code.replace(COMMENT_RE, "")); } -const CJS_RE = - /([\s;]|^)(module.exports\b|exports\.\w|require\s*\(|global\.\w)/m; export function hasCJSSyntax(code: string): boolean { - return CJS_RE.test(code.replace(/\/\*.+?\*\/|\/\/.*(?=[nr])/g, "")); + return CJS_RE.test(code.replace(COMMENT_RE, "")); } export function detectSyntax(code: string) {