Skip to content

Commit

Permalink
feat: make stripComments optional for syntax detection(#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jan 11, 2024
1 parent 4ffc601 commit c29618f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
32 changes: 25 additions & 7 deletions src/syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,35 @@ 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(COMMENT_RE, ""));
export type DetectSyntaxOptions = { stripComments?: boolean };

export function hasESMSyntax(
code: string,
opts: DetectSyntaxOptions = {},
): boolean {
if (opts.stripComments) {
code = code.replace(COMMENT_RE, "");
}
return ESM_RE.test(code);
}

export function hasCJSSyntax(code: string): boolean {
return CJS_RE.test(code.replace(COMMENT_RE, ""));
export function hasCJSSyntax(
code: string,
opts: DetectSyntaxOptions = {},
): boolean {
if (opts.stripComments) {
code = code.replace(COMMENT_RE, "");
}
return CJS_RE.test(code);
}

export function detectSyntax(code: string) {
const hasESM = hasESMSyntax(code);
const hasCJS = hasCJSSyntax(code);
export function detectSyntax(code: string, opts: DetectSyntaxOptions = {}) {
if (opts.stripComments) {
code = code.replace(COMMENT_RE, "");
}
// We strip comments once hence not passing opts down to hasESMSyntax and hasCJSSyntax
const hasESM = hasESMSyntax(code, {});
const hasCJS = hasCJSSyntax(code, {});

return {
hasESM,
Expand Down
18 changes: 18 additions & 0 deletions test/syntax.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ const staticTests = {
isMixed: false,
},
"export class": { hasESM: true, hasCJS: false, isMixed: false },
"const start = '/* ';import foo from 'bar';const end = ' */'": {
hasESM: true,
hasCJS: false,
isMixed: false,
},
// CJS
"exports.c={}": { hasESM: false, hasCJS: true, isMixed: false },
"const b=true;module.exports={b};": {
Expand All @@ -82,6 +87,9 @@ const staticTests = {
isMixed: false,
},
"const a={};": { hasESM: false, hasCJS: false, isMixed: false },
};

const staticTestsWithComments = {
'// They\'re exposed using "export import" so that types are passed along as expected\nmodule.exports={};':
{ hasESM: false, hasCJS: true, isMixed: false },
};
Expand All @@ -94,6 +102,16 @@ describe("detectSyntax", () => {
}
});

describe("detectSyntax (with comment)", () => {
for (const [input, result] of Object.entries(staticTestsWithComments)) {
it(input, () => {
expect(detectSyntax(input, { stripComments: true })).to.deep.equal(
result,
);
});
}
});

const nodeImportTests = {
"node:fs": true,
fs: true,
Expand Down

0 comments on commit c29618f

Please sign in to comment.