Skip to content

Commit

Permalink
refactor: extract comment regex
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jan 11, 2024
1 parent b6f0452 commit 7305f39
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 7305f39

Please sign in to comment.