From fa7eceff0be99cdc4590f4b0b0c385b160e0abea Mon Sep 17 00:00:00 2001 From: julien huang Date: Wed, 27 Mar 2024 00:19:31 +0100 Subject: [PATCH 1/2] feat: add stripCommentsFromCode option to isValidNodeImport --- src/syntax.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/syntax.ts b/src/syntax.ts index fe78deb..669de85 100644 --- a/src/syntax.ts +++ b/src/syntax.ts @@ -63,6 +63,12 @@ export interface ValidNodeImportOptions extends ResolveOptions { * Default: ['node', 'file', 'data'] */ allowedProtocols?: Array; + /** + * Whether to strip comments from the code before checking for ESM syntax. + * + * Default: false + */ + stripCommentsFromCode?: boolean; } const validNodeImportDefaults: ValidNodeImportOptions = { @@ -115,5 +121,5 @@ export async function isValidNodeImport( (await fsp.readFile(resolvedPath, "utf8").catch(() => {})) || ""; - return !hasESMSyntax(code); + return !hasESMSyntax(code, { stripComments: options.stripCommentsFromCode }); } From b39f7cf505f6ba9aed2ec73aeaaaed07d8c4d3ab Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 27 Mar 2024 11:37:32 +0100 Subject: [PATCH 2/2] Apply suggestions from code review --- src/syntax.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/syntax.ts b/src/syntax.ts index 669de85..d08c3d5 100644 --- a/src/syntax.ts +++ b/src/syntax.ts @@ -68,7 +68,7 @@ export interface ValidNodeImportOptions extends ResolveOptions { * * Default: false */ - stripCommentsFromCode?: boolean; + stripComments?: boolean; } const validNodeImportDefaults: ValidNodeImportOptions = { @@ -121,5 +121,5 @@ export async function isValidNodeImport( (await fsp.readFile(resolvedPath, "utf8").catch(() => {})) || ""; - return !hasESMSyntax(code, { stripComments: options.stripCommentsFromCode }); + return !hasESMSyntax(code, { stripComments: options.stripComments }); }