@@ -35,8 +35,6 @@ namespace ts {
3535 context . onEmitNode = onEmitNode ;
3636 context . enableSubstitution ( SyntaxKind . Identifier ) ; // Substitutes expression identifiers with imported/exported symbols.
3737 context . enableSubstitution ( SyntaxKind . BinaryExpression ) ; // Substitutes assignments to exported symbols.
38- context . enableSubstitution ( SyntaxKind . CallExpression ) ; // Substitutes expression identifiers with imported/exported symbols.
39- context . enableSubstitution ( SyntaxKind . TaggedTemplateExpression ) ; // Substitutes expression identifiers with imported/exported symbols.
4038 context . enableSubstitution ( SyntaxKind . PrefixUnaryExpression ) ; // Substitutes updates to exported symbols.
4139 context . enableSubstitution ( SyntaxKind . PostfixUnaryExpression ) ; // Substitutes updates to exported symbols.
4240 context . enableSubstitution ( SyntaxKind . ShorthandPropertyAssignment ) ; // Substitutes shorthand property assignments for imported/exported symbols.
@@ -49,7 +47,6 @@ namespace ts {
4947 let currentModuleInfo : ExternalModuleInfo ; // The ExternalModuleInfo for the current file.
5048 let noSubstitution : boolean [ ] ; // Set of nodes for which substitution rules should be ignored.
5149 let needUMDDynamicImportHelper : boolean ;
52- let bindingReferenceCache : ESMap < Node , Identifier | SourceFile | ImportClause | ImportSpecifier | undefined > | undefined ;
5350
5451 return chainBundle ( context , transformSourceFile ) ;
5552
@@ -1746,10 +1743,6 @@ namespace ts {
17461743 return substituteExpressionIdentifier ( < Identifier > node ) ;
17471744 case SyntaxKind . BinaryExpression :
17481745 return substituteBinaryExpression ( < BinaryExpression > node ) ;
1749- case SyntaxKind . CallExpression :
1750- return substituteCallExpression ( < CallExpression > node ) ;
1751- case SyntaxKind . TaggedTemplateExpression :
1752- return substituteTaggedTemplateExpression ( < TaggedTemplateExpression > node ) ;
17531746 case SyntaxKind . PostfixUnaryExpression :
17541747 case SyntaxKind . PrefixUnaryExpression :
17551748 return substituteUnaryExpression ( < PrefixUnaryExpression | PostfixUnaryExpression > node ) ;
@@ -1759,124 +1752,54 @@ namespace ts {
17591752 }
17601753
17611754 /**
1762- * For an Identifier, gets the import or export binding that it references.
1763- * @returns One of the following:
1764- * - An `Identifier` if node references an external helpers module (i.e., `tslib`).
1765- * - A `SourceFile` if the node references an export in the file.
1766- * - An `ImportClause` or `ImportSpecifier` if the node references an import binding.
1767- * - Otherwise, `undefined`.
1755+ * Substitution for an Identifier expression that may contain an imported or exported
1756+ * symbol.
1757+ *
1758+ * @param node The node to substitute.
17681759 */
1769- function getImportOrExportBindingReferenceWorker ( node : Identifier ) : Identifier | SourceFile | ImportClause | ImportSpecifier | undefined {
1760+ function substituteExpressionIdentifier ( node : Identifier ) : Expression {
17701761 if ( getEmitFlags ( node ) & EmitFlags . HelperName ) {
17711762 const externalHelpersModuleName = getExternalHelpersModuleName ( currentSourceFile ) ;
17721763 if ( externalHelpersModuleName ) {
1773- return externalHelpersModuleName ;
1764+ return factory . createPropertyAccessExpression ( externalHelpersModuleName , node ) ;
17741765 }
1766+ return node ;
17751767 }
17761768 else if ( ! ( isGeneratedIdentifier ( node ) && ! ( node . autoGenerateFlags & GeneratedIdentifierFlags . AllowNameSubstitution ) ) && ! isLocalName ( node ) ) {
17771769 const exportContainer = resolver . getReferencedExportContainer ( node , isExportName ( node ) ) ;
1778- if ( exportContainer ?. kind === SyntaxKind . SourceFile ) {
1779- return exportContainer ;
1780- }
1781- const importDeclaration = resolver . getReferencedImportDeclaration ( node ) ;
1782- if ( importDeclaration && ( isImportClause ( importDeclaration ) || isImportSpecifier ( importDeclaration ) ) ) {
1783- return importDeclaration ;
1784- }
1785- }
1786- return undefined ;
1787- }
1788-
1789- /**
1790- * For an Identifier, gets the import or export binding that it references.
1791- * @param removeEntry When `false`, the result is cached to avoid recomputing the result in a later substitution.
1792- * When `true`, any cached result for the node is removed.
1793- * @returns One of the following:
1794- * - An `Identifier` if node references an external helpers module (i.e., `tslib`).
1795- * - A `SourceFile` if the node references an export in the file.
1796- * - An `ImportClause` or `ImportSpecifier` if the node references an import binding.
1797- * - Otherwise, `undefined`.
1798- */
1799- function getImportOrExportBindingReference ( node : Identifier , removeEntry : boolean ) : Identifier | SourceFile | ImportClause | ImportSpecifier | undefined {
1800- let result = bindingReferenceCache ?. get ( node ) ;
1801- if ( ! result && ! bindingReferenceCache ?. has ( node ) ) {
1802- result = getImportOrExportBindingReferenceWorker ( node ) ;
1803- if ( ! removeEntry ) {
1804- bindingReferenceCache ||= new Map ( ) ;
1805- bindingReferenceCache . set ( node , result ) ;
1806- }
1807- }
1808- else if ( removeEntry ) {
1809- bindingReferenceCache ?. delete ( node ) ;
1810- }
1811- return result ;
1812- }
1813-
1814- function substituteCallExpression ( node : CallExpression ) {
1815- if ( isIdentifier ( node . expression ) && getImportOrExportBindingReference ( node . expression , /*removeEntry*/ false ) ) {
1816- return isCallChain ( node ) ?
1817- factory . updateCallChain ( node ,
1818- setTextRange ( factory . createComma ( factory . createNumericLiteral ( 0 ) , node . expression ) , node . expression ) ,
1819- node . questionDotToken ,
1820- /*typeArguments*/ undefined ,
1821- node . arguments ) :
1822- factory . updateCallExpression ( node ,
1823- setTextRange ( factory . createComma ( factory . createNumericLiteral ( 0 ) , node . expression ) , node . expression ) ,
1824- /*typeArguments*/ undefined ,
1825- node . arguments ) ;
1826- }
1827- return node ;
1828- }
1829-
1830- function substituteTaggedTemplateExpression ( node : TaggedTemplateExpression ) {
1831- if ( isIdentifier ( node . tag ) && getImportOrExportBindingReference ( node . tag , /*removeEntry*/ false ) ) {
1832- return factory . updateTaggedTemplateExpression (
1833- node ,
1834- setTextRange ( factory . createComma ( factory . createNumericLiteral ( 0 ) , node . tag ) , node . tag ) ,
1835- /*typeArguments*/ undefined ,
1836- node . template ) ;
1837- }
1838- return node ;
1839- }
1840-
1841- /**
1842- * Substitution for an Identifier expression that may contain an imported or exported
1843- * symbol.
1844- *
1845- * @param node The node to substitute.
1846- */
1847- function substituteExpressionIdentifier ( node : Identifier ) : Expression {
1848- const result = getImportOrExportBindingReference ( node , /*removeEntry*/ true ) ;
1849- switch ( result ?. kind ) {
1850- case SyntaxKind . Identifier : // tslib import
1851- return factory . createPropertyAccessExpression ( result , node ) ;
1852- case SyntaxKind . SourceFile : // top-level export
1770+ if ( exportContainer && exportContainer . kind === SyntaxKind . SourceFile ) {
18531771 return setTextRange (
18541772 factory . createPropertyAccessExpression (
18551773 factory . createIdentifier ( "exports" ) ,
18561774 factory . cloneNode ( node )
18571775 ) ,
18581776 /*location*/ node
18591777 ) ;
1860- case SyntaxKind . ImportClause :
1861- return setTextRange (
1862- factory . createPropertyAccessExpression (
1863- factory . getGeneratedNameForNode ( result . parent ) ,
1864- factory . createIdentifier ( "default" )
1865- ) ,
1866- /*location*/ node
1867- ) ;
1868- case SyntaxKind . ImportSpecifier :
1869- const name = result . propertyName || result . name ;
1870- return setTextRange (
1871- factory . createPropertyAccessExpression (
1872- factory . getGeneratedNameForNode ( result . parent ?. parent ?. parent || result ) ,
1873- factory . cloneNode ( name )
1874- ) ,
1875- /*location*/ node
1876- ) ;
1877- default :
1878- return node ;
1778+ }
1779+ const importDeclaration = resolver . getReferencedImportDeclaration ( node ) ;
1780+ if ( importDeclaration ) {
1781+ if ( isImportClause ( importDeclaration ) ) {
1782+ return setTextRange (
1783+ factory . createPropertyAccessExpression (
1784+ factory . getGeneratedNameForNode ( importDeclaration . parent ) ,
1785+ factory . createIdentifier ( "default" )
1786+ ) ,
1787+ /*location*/ node
1788+ ) ;
1789+ }
1790+ else if ( isImportSpecifier ( importDeclaration ) ) {
1791+ const name = importDeclaration . propertyName || importDeclaration . name ;
1792+ return setTextRange (
1793+ factory . createPropertyAccessExpression (
1794+ factory . getGeneratedNameForNode ( importDeclaration . parent ?. parent ?. parent || importDeclaration ) ,
1795+ factory . cloneNode ( name )
1796+ ) ,
1797+ /*location*/ node
1798+ ) ;
1799+ }
1800+ }
18791801 }
1802+ return node ;
18801803 }
18811804
18821805 /**
0 commit comments