Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make stripComments optional for syntax detection #217

Merged
merged 1 commit into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 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, "");
}

Check warning on line 25 in src/syntax.ts

View check run for this annotation

Codecov / codecov/patch

src/syntax.ts#L24-L25

Added lines #L24 - L25 were not covered by tests
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, "");
}

Check warning on line 35 in src/syntax.ts

View check run for this annotation

Codecov / codecov/patch

src/syntax.ts#L34-L35

Added lines #L34 - L35 were not covered by tests
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