Handle the new js binding element alias symbols in JS declaration emit

This commit is contained in:
Wesley Wigham
2020-10-21 11:23:02 -07:00
parent 672861abc6
commit f4255dd237
5 changed files with 241 additions and 0 deletions

View File

@@ -6783,6 +6783,37 @@ namespace ts {
const targetName = getInternalSymbolName(target, verbatimTargetName);
includePrivateSymbol(target); // the target may be within the same scope - attempt to serialize it first
switch (node.kind) {
case SyntaxKind.BindingElement:
if (node.parent?.parent?.kind === SyntaxKind.VariableDeclaration) {
// const { SomeClass } = require('./lib');
const specifier = getSpecifierForModuleSymbol(target.parent || target, context); // 'y'
addResult(factory.createImportDeclaration(
/*decorators*/ undefined,
/*modifiers*/ undefined,
factory.createImportClause(/*isTypeOnly*/ false, /*name*/ undefined, factory.createNamedImports([factory.createImportSpecifier(
(node as BindingElement).propertyName && isIdentifier((node as BindingElement).propertyName!) ? factory.createIdentifier(idText((node as BindingElement).propertyName as Identifier)) : undefined,
factory.createIdentifier(localName)
)])),
factory.createStringLiteral(specifier)
), ModifierFlags.None);
break;
}
// At present, the below case should be entirely unhit, as, generally speaking, the below case is *usually* bound
// such that the `BinaryExpression` is the declaration rather than the specific, nested binding element
// (because we don't seek to emit an alias in these forms yet). As such, the `BinaryExpression` switch case
// will be what actually handles this form. _However_, in anticipation of binding the below with proper
// alias symbols, I'm _pretty comfortable_ including the case here, even though it is not yet live.
if (node.parent?.parent?.kind === SyntaxKind.BinaryExpression) {
// module.exports = { SomeClass }
serializeExportSpecifier(
unescapeLeadingUnderscores(symbol.escapedName),
targetName
);
break;
}
// We don't know how to serialize this (nested?) binding element
Debug.failBadSyntaxKind(node.parent?.parent || node, "Unhandled binding element grandparent kind in declaration serialization");
break;
case SyntaxKind.VariableDeclaration:
// commonjs require: const x = require('y')
if (isPropertyAccessExpression((node as VariableDeclaration).initializer!)) {