Skip to content

Fix completions of exports elsewhere in same file #43755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 26, 2021
Merged
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
35 changes: 25 additions & 10 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25043,12 +25043,11 @@ namespace ts {
switch (kind) {
case AssignmentDeclarationKind.None:
return getTypeOfExpression(binaryExpression.left);
case AssignmentDeclarationKind.ThisProperty:
return getContextualTypeForThisPropertyAssignment(binaryExpression);
case AssignmentDeclarationKind.Property:
case AssignmentDeclarationKind.ExportsProperty:
case AssignmentDeclarationKind.Prototype:
case AssignmentDeclarationKind.PrototypeProperty:
if (isPossiblyAliasedThisProperty(binaryExpression, kind)) {
return getContextualTypeForThisPropertyAssignment(binaryExpression, kind);
return getContextualTypeForThisPropertyAssignment(binaryExpression);
}
// If `binaryExpression.left` was assigned a symbol, then this is a new declaration; otherwise it is an assignment to an existing declaration.
// See `bindStaticPropertyAssignment` in `binder.ts`.
Expand Down Expand Up @@ -25081,9 +25080,15 @@ namespace ts {
}
return isInJSFile(decl) ? undefined : getTypeOfExpression(binaryExpression.left);
}
case AssignmentDeclarationKind.ExportsProperty:
case AssignmentDeclarationKind.Prototype:
case AssignmentDeclarationKind.PrototypeProperty:
let valueDeclaration = binaryExpression.left.symbol?.valueDeclaration;
// falls through
case AssignmentDeclarationKind.ModuleExports:
case AssignmentDeclarationKind.ThisProperty:
return getContextualTypeForThisPropertyAssignment(binaryExpression, kind);
valueDeclaration ||= binaryExpression.symbol?.valueDeclaration;
Copy link
Member

Choose a reason for hiding this comment

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

switch scope rules are boooooooooo

const annotated = valueDeclaration && getEffectiveTypeAnnotationNode(valueDeclaration);
return annotated ? getTypeFromTypeNode(annotated) : undefined;
case AssignmentDeclarationKind.ObjectDefinePropertyValue:
case AssignmentDeclarationKind.ObjectDefinePropertyExports:
case AssignmentDeclarationKind.ObjectDefinePrototypeProperty:
Expand All @@ -25105,7 +25110,7 @@ namespace ts {
return isThisInitializedDeclaration(symbol?.valueDeclaration);
}

function getContextualTypeForThisPropertyAssignment(binaryExpression: BinaryExpression, kind: AssignmentDeclarationKind): Type | undefined {
function getContextualTypeForThisPropertyAssignment(binaryExpression: BinaryExpression): Type | undefined {
if (!binaryExpression.symbol) return getTypeOfExpression(binaryExpression.left);
if (binaryExpression.symbol.valueDeclaration) {
const annotated = getEffectiveTypeAnnotationNode(binaryExpression.symbol.valueDeclaration);
Expand All @@ -25116,7 +25121,6 @@ namespace ts {
}
}
}
if (kind === AssignmentDeclarationKind.ModuleExports) return undefined;
const thisAccess = cast(binaryExpression.left, isAccessExpression);
if (!isObjectLiteralMethod(getThisContainer(thisAccess.expression, /*includeArrowFunctions*/ false))) {
return undefined;
Expand Down Expand Up @@ -38825,10 +38829,10 @@ namespace ts {

switch (location.kind) {
case SyntaxKind.SourceFile:
if (!isExternalOrCommonJsModule(<SourceFile>location)) break;
if (!isExternalModule(<SourceFile>location)) break;
// falls through
case SyntaxKind.ModuleDeclaration:
copySymbols(getSymbolOfNode(location as ModuleDeclaration | SourceFile).exports!, meaning & SymbolFlags.ModuleMember);
copyLocallyVisibleExportSymbols(getSymbolOfNode(location as ModuleDeclaration | SourceFile).exports!, meaning & SymbolFlags.ModuleMember);
break;
case SyntaxKind.EnumDeclaration:
copySymbols(getSymbolOfNode(location as EnumDeclaration).exports!, meaning & SymbolFlags.EnumMember);
Expand Down Expand Up @@ -38897,6 +38901,17 @@ namespace ts {
});
}
}

function copyLocallyVisibleExportSymbols(source: SymbolTable, meaning: SymbolFlags): void {
if (meaning) {
source.forEach(symbol => {
// Similar condition as in `resolveNameHelper`
if (!getDeclarationOfKind(symbol, SyntaxKind.ExportSpecifier) && !getDeclarationOfKind(symbol, SyntaxKind.NamespaceExport)) {
copySymbol(symbol, meaning);
}
});
}
}
}

function isTypeDeclarationName(name: Node): boolean {
Expand Down
3 changes: 2 additions & 1 deletion src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2992,7 +2992,8 @@ namespace ts.Completions {
if (type) {
return type;
}
if (isBinaryExpression(node.parent) && node.parent.operatorToken.kind === SyntaxKind.EqualsToken) {
if (isBinaryExpression(node.parent) && node.parent.operatorToken.kind === SyntaxKind.EqualsToken && node === node.parent.left) {
// Object literal is assignment pattern: ({ | } = x)
return typeChecker.getTypeAtLocation(node.parent);
}
return undefined;
Expand Down
17 changes: 17 additions & 0 deletions tests/cases/fourslash/completionsExternalModuleRenamedExports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/// <reference path="fourslash.ts" />

// @Filename: other.ts
//// export {};

// @Filename: index.ts
//// const c = 0;
//// export { c as yeahThisIsTotallyInScopeHuh };
//// export * as alsoNotInScope from "./other";
////
//// /**/

verify.completions({
marker: "",
includes: "c",
excludes: ["yeahThisIsTotallyInScopeHuh", "alsoNotInScope"],
});
17 changes: 17 additions & 0 deletions tests/cases/fourslash/completionsObjectLiteralModuleExports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/// <reference path="fourslash.ts" />

// @allowJs: true
// @checkJs: true

// @Filename: index.js
//// const almanac = 0;
//// module.exports = {
//// a/**/
//// };

verify.completions({
marker: "",
isNewIdentifierLocation: true,
includes: "almanac",
excludes: "a",
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/// <reference path="fourslash.ts" />

// @checkJs: true
// @allowJs: true

// @Filename: index.js
//// module.exports = {
//// a/*1*/
//// }
////
//// exports.foo = {
//// a/*2*/
//// }
////
//// function F() {
//// this.blah = {
//// a/*3*/
//// };
//// }
////
//// F.foo = {
//// a/*4*/
//// }
////
//// F.prototype = {
//// a/*5*/
//// }
////
//// F.prototype.x = {
//// a/*6*/
//// }

[1, 2, 3, 4, 5, 6].forEach(marker => {
verify.completions({
marker: `${marker}`,
excludes: "a",
isNewIdentifierLocation: true,
});
});