Skip to content

Commit 5a26747

Browse files
Merge pull request #27004 from RyanCavanaugh/noJsNewModuleCompletions
Don't offer module completions in non-module JS files
2 parents f2a1a42 + 95ba73e commit 5a26747

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

src/services/completions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,10 +1268,10 @@ namespace ts.Completions {
12681268
if (sourceFile.externalModuleIndicator) return true;
12691269
// If already using commonjs, don't introduce ES6.
12701270
if (sourceFile.commonJsModuleIndicator) return false;
1271-
// If some file is using ES6 modules, assume that it's OK to add more.
1272-
if (programContainsEs6Modules(program)) return true;
12731271
// For JS, stay on the safe side.
12741272
if (isUncheckedFile) return false;
1273+
// If some file is using ES6 modules, assume that it's OK to add more.
1274+
if (programContainsEs6Modules(program)) return true;
12751275
// If module transpilation is enabled or we're targeting es6 or above, or not emitting, OK.
12761276
return compilerOptionsIndicateEs6Modules(program.getCompilerOptions());
12771277
}

tests/cases/fourslash/completionsImport_importType.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
// @allowJs: true
44

55
// @Filename: /a.js
6-
////export const x = 0;
7-
////export class C {}
8-
/////** @typedef {number} T */
6+
//// export const x = 0;
7+
//// export class C {}
8+
//// /** @typedef {number} T */
99

1010
// @Filename: /b.js
11-
/////** @type {/*0*/} */
12-
/////** @type {/*1*/} */
11+
//// export const m = 0;
12+
//// /** @type {/*0*/} */
13+
//// /** @type {/*1*/} */
1314

1415
verify.completions({
1516
marker: ["0", "1"],
@@ -43,6 +44,7 @@ verify.applyCodeActionFromCompletion("0", {
4344
newFileContent:
4445
`import { C } from "./a";
4546
47+
export const m = 0;
4648
/** @type {} */
4749
/** @type {} */`,
4850
});
@@ -55,6 +57,7 @@ verify.applyCodeActionFromCompletion("1", {
5557
newFileContent:
5658
`import { C } from "./a";
5759
60+
export const m = 0;
5861
/** @type {} */
5962
/** @type {import("./a").} */`,
6063
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @allowJs: true
4+
// @module: esnext
5+
6+
// @Filename: /node_modules/foo/index.d.ts
7+
//// export const fail: number;
8+
9+
// @Filename: /a.js
10+
//// export const x = 0;
11+
//// export class C {}
12+
////
13+
14+
// @Filename: /b.js
15+
//// /**/
16+
17+
goTo.file("/b.js");
18+
goTo.marker();
19+
verify.not.completionListContains("fail", undefined, undefined, undefined, undefined, undefined, { includeCompletionsForModuleExports: true });
20+
edit.insert("export const k = 10;\r\nf");
21+
verify.completionListContains(
22+
{ name: "fail", source: "/node_modules/foo/index" },
23+
"const fail: number",
24+
"",
25+
"const",
26+
undefined,
27+
true,
28+
{
29+
includeCompletionsForModuleExports: true,
30+
sourceDisplay: "./node_modules/foo/index"
31+
});

0 commit comments

Comments
 (0)