diff --git a/src/services/suggestionDiagnostics.ts b/src/services/suggestionDiagnostics.ts index ebefddfd80dec..1ca10245c72f1 100644 --- a/src/services/suggestionDiagnostics.ts +++ b/src/services/suggestionDiagnostics.ts @@ -6,8 +6,10 @@ namespace ts { program.getSemanticDiagnostics(sourceFile, cancellationToken); const diags: DiagnosticWithLocation[] = []; const checker = program.getTypeChecker(); + const isCommonJSFile = sourceFile.impliedNodeFormat === ModuleKind.CommonJS || fileExtensionIsOneOf(sourceFile.fileName, [Extension.Cts, Extension.Cjs]) ; - if (sourceFile.commonJsModuleIndicator && + if (!isCommonJSFile && + sourceFile.commonJsModuleIndicator && (programContainsEs6Modules(program) || compilerOptionsIndicateEs6Modules(program.getCompilerOptions())) && containsTopLevelCommonjs(sourceFile)) { diags.push(createDiagnosticForNode(getErrorNodeFromCommonJsIndicator(sourceFile.commonJsModuleIndicator), Diagnostics.File_is_a_CommonJS_module_it_may_be_converted_to_an_ES6_module)); diff --git a/tests/cases/fourslash/refactorConvertToEs6Module_inCommonJsFile.ts b/tests/cases/fourslash/refactorConvertToEs6Module_inCommonJsFile.ts new file mode 100644 index 0000000000000..d56bdc303fbff --- /dev/null +++ b/tests/cases/fourslash/refactorConvertToEs6Module_inCommonJsFile.ts @@ -0,0 +1,31 @@ +/// + +// @allowJs: true +// @target: esnext + +// @Filename: /a.cjs +////module.exports = 0; + +// @Filename: /b.cts +////module.exports = 0; + +// @Filename: /c.ts +////module.exports = 0; + +// @Filename: /d.js +////module.exports = 0; + +goTo.file("/a.cjs"); +verify.codeFixAvailable([]); + +goTo.file("/b.cts"); +verify.codeFixAvailable([]); + +goTo.file("/c.ts"); +verify.codeFixAvailable([]); + +goTo.file("/d.js"); +verify.codeFix({ + description: "Convert to ES6 module", + newFileContent: 'export default 0;', +}); \ No newline at end of file diff --git a/tests/cases/fourslash/refactorConvertToEs6Module_module_node12.ts b/tests/cases/fourslash/refactorConvertToEs6Module_module_node12.ts new file mode 100644 index 0000000000000..a7be99ba1572f --- /dev/null +++ b/tests/cases/fourslash/refactorConvertToEs6Module_module_node12.ts @@ -0,0 +1,29 @@ +/// + +// @allowJs: true +// @target: esnext +// @module: node12 + +// @Filename: /a.js +////module.exports = 0; + +// @Filename: /b.ts +////module.exports = 0; + +// @Filename: /c.cjs +////module.exports = 0; + +// @Filename: /d.cts +////module.exports = 0; + +goTo.file("/a.js"); +verify.codeFixAvailable([]); + +goTo.file("/b.ts"); +verify.codeFixAvailable([]); + +goTo.file("/c.cjs"); +verify.codeFixAvailable([]); + +goTo.file("/d.cts"); +verify.codeFixAvailable([]); \ No newline at end of file diff --git a/tests/cases/fourslash/refactorConvertToEs6Module_module_nodenext.ts b/tests/cases/fourslash/refactorConvertToEs6Module_module_nodenext.ts new file mode 100644 index 0000000000000..622c3168b2fe7 --- /dev/null +++ b/tests/cases/fourslash/refactorConvertToEs6Module_module_nodenext.ts @@ -0,0 +1,29 @@ +/// + +// @allowJs: true +// @target: esnext +// @module: nodenext + +// @Filename: /a.js +////module.exports = 0; + +// @Filename: /b.ts +////module.exports = 0; + +// @Filename: /c.cjs +////module.exports = 0; + +// @Filename: /d.cts +////module.exports = 0; + +goTo.file("/a.js"); +verify.codeFixAvailable([]); + +goTo.file("/b.ts"); +verify.codeFixAvailable([]); + +goTo.file("/c.cjs"); +verify.codeFixAvailable([]); + +goTo.file("/d.cts"); +verify.codeFixAvailable([]); \ No newline at end of file