Skip to content

Commit fc30bd1

Browse files
author
Andy
authored
Suggestion to convert to ES6 module should only trigger in projects which express some intent to use ES6 (microsoft#23576)
1 parent 905f9a0 commit fc30bd1

31 files changed

Lines changed: 61 additions & 8 deletions

File tree

src/services/completions.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,14 +1201,11 @@ namespace ts.Completions {
12011201
// If already using commonjs, don't introduce ES6.
12021202
if (sourceFile.commonJsModuleIndicator) return false;
12031203
// If some file is using ES6 modules, assume that it's OK to add more.
1204-
if (program.getSourceFiles().some(s => !s.isDeclarationFile && !program.isSourceFileFromExternalLibrary(s) && !!s.externalModuleIndicator)) {
1205-
return true;
1206-
}
1204+
if (programContainsEs6Modules(program)) return true;
12071205
// For JS, stay on the safe side.
12081206
if (isSourceFileJavaScript(sourceFile)) return false;
12091207
// If module transpilation is enabled or we're targeting es6 or above, or not emitting, OK.
1210-
const compilerOptions = program.getCompilerOptions();
1211-
return !!compilerOptions.module || compilerOptions.target >= ScriptTarget.ES2015 || !!compilerOptions.noEmit;
1208+
return compilerOptionsIndicateEs6Modules(program.getCompilerOptions());
12121209
}
12131210

12141211
function isSnippetScope(scopeNode: Node): boolean {

src/services/suggestionDiagnostics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace ts {
55
const checker = program.getDiagnosticsProducingTypeChecker();
66
const diags: Diagnostic[] = [];
77

8-
if (sourceFile.commonJsModuleIndicator) {
8+
if (sourceFile.commonJsModuleIndicator && (programContainsEs6Modules(program) || compilerOptionsIndicateEs6Modules(program.getCompilerOptions()))) {
99
diags.push(createDiagnosticForNode(getErrorNodeFromCommonJsIndicator(sourceFile.commonJsModuleIndicator), Diagnostics.File_is_a_CommonJS_module_it_may_be_converted_to_an_ES6_module));
1010
}
1111

src/services/utilities.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,13 @@ namespace ts {
12141214
: getTextOfIdentifierOrLiteral(name);
12151215
}
12161216

1217+
export function programContainsEs6Modules(program: Program): boolean {
1218+
return program.getSourceFiles().some(s => !s.isDeclarationFile && !program.isSourceFileFromExternalLibrary(s) && !!s.externalModuleIndicator);
1219+
}
1220+
export function compilerOptionsIndicateEs6Modules(compilerOptions: CompilerOptions): boolean {
1221+
return !!compilerOptions.module || compilerOptions.target >= ScriptTarget.ES2015 || !!compilerOptions.noEmit;
1222+
}
1223+
12171224
export function hostUsesCaseSensitiveFileNames(host: LanguageServiceHost): boolean {
12181225
return host.useCaseSensitiveFileNames ? host.useCaseSensitiveFileNames() : false;
12191226
}

tests/cases/fourslash/refactorConvertToEs6Module_export_alias.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/// <reference path='fourslash.ts' />
22

33
// @allowJs: true
4+
// @target: esnext
45

56
// @Filename: /a.js
67
////const exportsAlias = exports;

tests/cases/fourslash/refactorConvertToEs6Module_export_dotDefault.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Test that we leave it alone if the name is a keyword.
44

55
// @allowJs: true
6+
// @target: esnext
67

78
// @Filename: /a.js
89
////exports.default = 0;

tests/cases/fourslash/refactorConvertToEs6Module_export_invalidName.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Test that we leave it alone if the name is a keyword.
44

55
// @allowJs: true
6+
// @target: esnext
67

78
// @Filename: /a.js
89
////exports.class = 0;

tests/cases/fourslash/refactorConvertToEs6Module_export_moduleDotExports.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/// <reference path='fourslash.ts' />
22

33
// @allowJs: true
4+
// @target: esnext
45

56
// @Filename: /a.js
67
////module.exports = function() {}

tests/cases/fourslash/refactorConvertToEs6Module_export_moduleDotExportsEqualsRequire.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/// <reference path='fourslash.ts' />
22

33
// @allowJs: true
4+
// @target: esnext
45

56
// @Filename: /a.d.ts
67
////export const x: number;

tests/cases/fourslash/refactorConvertToEs6Module_export_moduleDotExports_changesImports.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/// <reference path='fourslash.ts' />
22

33
// @allowJs: true
4+
// @target: esnext
45

56
// @Filename: /a.js
67
////module.exports = 0;

tests/cases/fourslash/refactorConvertToEs6Module_export_named.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/// <reference path='fourslash.ts' />
22

33
// @allowJs: true
4+
// @target: esnext
45

56
// @Filename: /a.js
67
////[|exports.f|] = function() {};

0 commit comments

Comments
 (0)