Skip to content

CommonJS imports support destructuring+property access #40702

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
Sep 24, 2020
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
39 changes: 24 additions & 15 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2439,10 +2439,11 @@ namespace ts {
}

function getTargetOfImportEqualsDeclaration(node: ImportEqualsDeclaration | VariableDeclaration, dontResolveAlias: boolean): Symbol | undefined {
if (isVariableDeclaration(node) && node.initializer && isPropertyAccessExpression(node.initializer)) {
const name = (getLeftmostAccessExpression(node.initializer.expression) as CallExpression).arguments[0] as StringLiteral;
return isIdentifier(node.initializer.name)
? resolveSymbol(getPropertyOfType(resolveExternalModuleTypeByLiteral(name), node.initializer.name.escapedText))
const commonJSPropertyAccess = getCommonJSPropertyAccess(node);
if (commonJSPropertyAccess) {
const name = (getLeftmostAccessExpression(commonJSPropertyAccess.expression) as CallExpression).arguments[0] as StringLiteral;
return isIdentifier(commonJSPropertyAccess.name)
? resolveSymbol(getPropertyOfType(resolveExternalModuleTypeByLiteral(name), commonJSPropertyAccess.name.escapedText))
: undefined;
}
if (isVariableDeclaration(node) || node.moduleReference.kind === SyntaxKind.ExternalModuleReference) {
Expand Down Expand Up @@ -2637,12 +2638,8 @@ namespace ts {
return result;
}

function getExportOfModule(symbol: Symbol, specifier: ImportOrExportSpecifier | BindingElement, dontResolveAlias: boolean): Symbol | undefined {
function getExportOfModule(symbol: Symbol, name: Identifier, specifier: Declaration, dontResolveAlias: boolean): Symbol | undefined {
if (symbol.flags & SymbolFlags.Module) {
const name = specifier.propertyName ?? specifier.name;
if (!isIdentifier(name)) {
return undefined;
}
const exportSymbol = getExportsOfSymbol(symbol).get(name.escapedText);
const resolved = resolveSymbol(exportSymbol, dontResolveAlias);
markSymbolOfAliasDeclarationIfTypeOnly(specifier, exportSymbol, resolved, /*overwriteEmpty*/ false);
Expand All @@ -2659,10 +2656,10 @@ namespace ts {
}
}

function getExternalModuleMember(node: ImportDeclaration | ExportDeclaration | VariableDeclaration, specifier: ImportOrExportSpecifier | BindingElement, dontResolveAlias = false): Symbol | undefined {
function getExternalModuleMember(node: ImportDeclaration | ExportDeclaration | VariableDeclaration, specifier: ImportOrExportSpecifier | BindingElement | PropertyAccessExpression, dontResolveAlias = false): Symbol | undefined {
const moduleSpecifier = getExternalModuleRequireArgument(node) || (node as ImportDeclaration | ExportDeclaration).moduleSpecifier!;
const moduleSymbol = resolveExternalModuleName(node, moduleSpecifier)!; // TODO: GH#18217
const name = specifier.propertyName || specifier.name;
const name = !isPropertyAccessExpression(specifier) && specifier.propertyName || specifier.name;
if (!isIdentifier(name)) {
return undefined;
}
Expand All @@ -2682,10 +2679,10 @@ namespace ts {
else {
symbolFromVariable = getPropertyOfVariable(targetSymbol, name.escapedText);
}

// if symbolFromVariable is export - get its final target
symbolFromVariable = resolveSymbol(symbolFromVariable, dontResolveAlias);
let symbolFromModule = getExportOfModule(targetSymbol, specifier, dontResolveAlias);

let symbolFromModule = getExportOfModule(targetSymbol, name, specifier, dontResolveAlias);
if (symbolFromModule === undefined && name.escapedText === InternalSymbolName.Default) {
const file = find(moduleSymbol.declarations, isSourceFile);
if (canHaveSyntheticDefault(file, moduleSymbol, dontResolveAlias)) {
Expand Down Expand Up @@ -2773,11 +2770,23 @@ namespace ts {
}

function getTargetOfImportSpecifier(node: ImportSpecifier | BindingElement, dontResolveAlias: boolean): Symbol | undefined {
const resolved = getExternalModuleMember(isBindingElement(node) ? getRootDeclaration(node) as VariableDeclaration : node.parent.parent.parent, node, dontResolveAlias);
const root = isBindingElement(node) ? getRootDeclaration(node) as VariableDeclaration : node.parent.parent.parent;
const commonJSPropertyAccess = getCommonJSPropertyAccess(root);
const resolved = getExternalModuleMember(root, commonJSPropertyAccess || node, dontResolveAlias);
const name = node.propertyName || node.name;
if (commonJSPropertyAccess && resolved && isIdentifier(name)) {
return getPropertyOfType(getTypeOfSymbol(resolved), name.escapedText);
}
markSymbolOfAliasDeclarationIfTypeOnly(node, /*immediateTarget*/ undefined, resolved, /*overwriteEmpty*/ false);
return resolved;
}

function getCommonJSPropertyAccess(node: Node) {
if (isVariableDeclaration(node) && node.initializer && isPropertyAccessExpression(node.initializer)) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if(isVariableDeclaration(node) && node.initializer && isPropertyAccessExpression(node.initializer)) {
if (isVariableDeclaration(node) && node.initializer && isPropertyAccessExpression(node.initializer)) {

Copy link
Member Author

Choose a reason for hiding this comment

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

I tried to commit your suggestion directly but github kept claiming the PR was recently updated =(

return node.initializer;
}
}

function getTargetOfNamespaceExportDeclaration(node: NamespaceExportDeclaration, dontResolveAlias: boolean): Symbol {
const resolved = resolveExternalModuleSymbol(node.parent.symbol, dontResolveAlias);
markSymbolOfAliasDeclarationIfTypeOnly(node, /*immediateTarget*/ undefined, resolved, /*overwriteEmpty*/ false);
Expand Down Expand Up @@ -2931,7 +2940,7 @@ namespace ts {
finalTarget: Symbol | undefined,
overwriteEmpty: boolean,
): boolean {
if (!aliasDeclaration) return false;
if (!aliasDeclaration || isPropertyAccessExpression(aliasDeclaration)) return false;

// If the declaration itself is type-only, mark it and return.
// No need to check what it resolves to.
Expand Down
23 changes: 0 additions & 23 deletions tests/baselines/reference/jsDeclarationsTypeReferences2.errors.txt

This file was deleted.

2 changes: 1 addition & 1 deletion tests/baselines/reference/jsDeclarationsTypeReferences2.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ export declare const o: {
m: number;
};
//// [index.d.ts]
export const thing: any;
export const thing: number;
24 changes: 12 additions & 12 deletions tests/baselines/reference/jsDeclarationsTypeReferences2.types
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
=== tests/cases/conformance/jsdoc/declarations/index.js ===
const{ a, m } = require("./something").o;
>a : any
>m : any
>a : number
>m : number
>require("./something").o : { a: number; m: number; }
>require("./something") : typeof import("tests/cases/conformance/jsdoc/declarations/something")
>require : any
>"./something" : "./something"
>o : { a: number; m: number; }

const thing = a + m
>thing : any
>a + m : any
>a : any
>m : any
>thing : number
>a + m : number
>a : number
>m : number

module.exports = {
>module.exports = { thing} : { thing: any; }
>module.exports : { thing: any; }
>module : { "\"tests/cases/conformance/jsdoc/declarations/index\"": { thing: any; }; }
>exports : { thing: any; }
>{ thing} : { thing: any; }
>module.exports = { thing} : { thing: number; }
>module.exports : { thing: number; }
>module : { "\"tests/cases/conformance/jsdoc/declarations/index\"": { thing: number; }; }
>exports : { thing: number; }
>{ thing} : { thing: number; }

thing
>thing : any
>thing : number

};

Expand Down