Skip to content

Commit f050750

Browse files
authored
Don’t auto-import undefined (microsoft#35504)
1 parent 27616dd commit f050750

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

src/harness/fourslash.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -854,9 +854,9 @@ namespace FourSlash {
854854
}
855855
}
856856

857-
assert.equal(actual.hasAction, hasAction);
858-
assert.equal(actual.isRecommended, isRecommended);
859-
assert.equal(actual.source, source);
857+
assert.equal(actual.hasAction, hasAction, `Expected 'hasAction' value '${actual.hasAction}' to equal '${hasAction}'`);
858+
assert.equal(actual.isRecommended, isRecommended, `Expected 'isRecommended' value '${actual.source}' to equal '${isRecommended}'`);
859+
assert.equal(actual.source, source, `Expected 'source' value '${actual.source}' to equal '${source}'`);
860860
assert.equal(actual.sortText, sortText || ts.Completions.SortText.LocationPriority, this.messageAtLastKnownMarker(`Actual entry: ${JSON.stringify(actual)}`));
861861

862862
if (text !== undefined) {

src/services/completions.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,12 @@ namespace ts.Completions {
117117
// If the symbol/moduleSymbol was a merged symbol, it will have a new identity
118118
// in the checker, even though the symbols to merge are the same (guaranteed by
119119
// cache invalidation in synchronizeHostData).
120-
if (suggestion.symbol.declarations) {
120+
if (suggestion.symbol.declarations?.length) {
121121
suggestion.symbol = checker.getMergedSymbol(suggestion.origin.isDefaultExport
122-
? suggestion.symbol.declarations[0].localSymbol || suggestion.symbol.declarations[0].symbol
122+
? suggestion.symbol.declarations[0].localSymbol ?? suggestion.symbol.declarations[0].symbol
123123
: suggestion.symbol.declarations[0].symbol);
124124
}
125-
if (suggestion.origin.moduleSymbol.declarations) {
125+
if (suggestion.origin.moduleSymbol.declarations?.length) {
126126
suggestion.origin.moduleSymbol = checker.getMergedSymbol(suggestion.origin.moduleSymbol.declarations[0].symbol);
127127
}
128128
});
@@ -1624,6 +1624,9 @@ namespace ts.Completions {
16241624
if (isDefaultExport) {
16251625
symbol = getLocalSymbolForExportDefault(symbol) || symbol;
16261626
}
1627+
if (typeChecker.isUndefinedSymbol(symbol)) {
1628+
return;
1629+
}
16271630
addToSeen(resultSymbolIds, getSymbolId(symbol));
16281631
const origin: SymbolOriginInfoExport = { kind: SymbolOriginInfoKind.Export, moduleSymbol, isDefaultExport };
16291632
results.push({
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/// <reference path="../fourslash.ts" />
2+
3+
// @Filename: /tsconfig.json
4+
////{ "compilerOptions": { "module": "esnext" } }
5+
6+
// @Filename: /undefined.ts
7+
////export = undefined;
8+
9+
// @Filename: /undefinedAlias.ts
10+
////const x = undefined;
11+
////export = x;
12+
13+
// @Filename: /index.ts
14+
//// /**/
15+
16+
// Would throw error if undefined appears twice
17+
goTo.marker("");
18+
verify.completions({
19+
includes: [{
20+
name: "x",
21+
hasAction: true,
22+
sortText: completion.SortText.AutoImportSuggestions,
23+
source: "/undefinedAlias"
24+
}],
25+
preferences: {
26+
includeCompletionsForModuleExports: true,
27+
includeInsertTextCompletions: true
28+
}
29+
});
30+
31+
// Do it again for cache test
32+
verify.completions({
33+
includes: [{
34+
name: "x",
35+
hasAction: true,
36+
sortText: completion.SortText.AutoImportSuggestions,
37+
source: "/undefinedAlias"
38+
}],
39+
preferences: {
40+
includeCompletionsForModuleExports: true,
41+
includeInsertTextCompletions: true
42+
}
43+
});

0 commit comments

Comments
 (0)