Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/harness/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ namespace FourSlash {
const name = typeof include === "string" ? include : include.name;
const found = nameToEntries.get(name);
if (!found) throw this.raiseError(`No completion ${name} found`);
assert(found.length === 1); // Must use 'exact' for multiple completions with same name
assert(found.length === 1, `Multiple completions named '${name}' were found. Using 'exact' is required.`);
this.verifyCompletionEntry(ts.first(found), include);
}
}
Expand Down
15 changes: 12 additions & 3 deletions src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ namespace ts.Completions {
// Based on the order we add things we will always see locals first, then globals, then module exports.
// So adding a completion for a local will prevent us from adding completions for external module exports sharing the same name.
const uniques = createMap<true>();
const uniqueGlobals = createMap<true>();
for (const symbol of symbols) {
const origin = symbolToOriginInfoMap ? symbolToOriginInfoMap[getSymbolId(symbol)] : undefined;
const entry = createCompletionEntry(symbol, location, sourceFile, typeChecker, target, kind, origin, recommendedCompletion, propertyAccessToConvert, isJsxInitializer, preferences);
Expand All @@ -285,12 +286,20 @@ namespace ts.Completions {
continue;
}

// Latter case tests whether this is a global variable.
if (!origin && !(symbol.parent === undefined && !some(symbol.declarations, d => d.getSourceFile() === location!.getSourceFile()))) { // TODO: GH#18217
const isExternalModule = !!origin;
const isTopLevel = symbol.parent === undefined;
const hasDeclarationInFile = some(symbol.declarations, d => d.getSourceFile() === location!.getSourceFile());
if (!isExternalModule && !(isTopLevel && !hasDeclarationInFile)) {
uniques.set(name, true);
}
else if (isTopLevel && !isExternalModule && !hasDeclarationInFile) {
uniqueGlobals.set(name, true);
}

entries.push(entry);
const shouldBeShadowedByGlobal = isExternalModule && uniqueGlobals.has(name);
if (!shouldBeShadowedByGlobal) {
entries.push(entry);
}
}

log("getCompletionsAtPosition: getCompletionEntriesFromSymbols: " + (timestamp() - start));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
// @module: esnext
// @noLib: true

// @Filename: /global.d.ts
// A local variable would prevent import completions (see `completionsImport_shadowedByLocal.ts`), but a global doesn't.
////declare var foo: number;

// @Filename: /a.ts
////export const foo = 0;

Expand All @@ -21,7 +17,6 @@ verify.completions({
marker: "",
exact: [
"globalThis",
{ name: "foo", text: "var foo: number", kind: "var", kindModifiers: "declare" },
Comment thread
andrewbranch marked this conversation as resolved.
"undefined",
{
name: "foo",
Expand Down
14 changes: 14 additions & 0 deletions tests/cases/fourslash/completionsImport_shadowedByGlobal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// <reference path="fourslash.ts" />

// @Filename: /a.ts
//// export const parseInt = 0;

// @Filename: /b.ts
////parseI/**/

verify.completions({
marker: "",
// The real parseInt is in 'globalVars', the fake one exported from a.ts is missing
exact: ["globalThis", ...completion.globalsVars, "undefined", ...completion.statementKeywordsWithTypes],
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sandersn What's going on with globalThis here?

preferences: { includeCompletionsForModuleExports: true },
});