@@ -35,8 +35,6 @@ namespace ts {
35
35
context . onEmitNode = onEmitNode ;
36
36
context . enableSubstitution ( SyntaxKind . Identifier ) ; // Substitutes expression identifiers with imported/exported symbols.
37
37
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.
40
38
context . enableSubstitution ( SyntaxKind . PrefixUnaryExpression ) ; // Substitutes updates to exported symbols.
41
39
context . enableSubstitution ( SyntaxKind . PostfixUnaryExpression ) ; // Substitutes updates to exported symbols.
42
40
context . enableSubstitution ( SyntaxKind . ShorthandPropertyAssignment ) ; // Substitutes shorthand property assignments for imported/exported symbols.
@@ -49,7 +47,6 @@ namespace ts {
49
47
let currentModuleInfo : ExternalModuleInfo ; // The ExternalModuleInfo for the current file.
50
48
let noSubstitution : boolean [ ] ; // Set of nodes for which substitution rules should be ignored.
51
49
let needUMDDynamicImportHelper : boolean ;
52
- let bindingReferenceCache : ESMap < Node , Identifier | SourceFile | ImportClause | ImportSpecifier | undefined > | undefined ;
53
50
54
51
return chainBundle ( context , transformSourceFile ) ;
55
52
@@ -1746,10 +1743,6 @@ namespace ts {
1746
1743
return substituteExpressionIdentifier ( < Identifier > node ) ;
1747
1744
case SyntaxKind . BinaryExpression :
1748
1745
return substituteBinaryExpression ( < BinaryExpression > node ) ;
1749
- case SyntaxKind . CallExpression :
1750
- return substituteCallExpression ( < CallExpression > node ) ;
1751
- case SyntaxKind . TaggedTemplateExpression :
1752
- return substituteTaggedTemplateExpression ( < TaggedTemplateExpression > node ) ;
1753
1746
case SyntaxKind . PostfixUnaryExpression :
1754
1747
case SyntaxKind . PrefixUnaryExpression :
1755
1748
return substituteUnaryExpression ( < PrefixUnaryExpression | PostfixUnaryExpression > node ) ;
@@ -1759,124 +1752,54 @@ namespace ts {
1759
1752
}
1760
1753
1761
1754
/**
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.
1768
1759
*/
1769
- function getImportOrExportBindingReferenceWorker ( node : Identifier ) : Identifier | SourceFile | ImportClause | ImportSpecifier | undefined {
1760
+ function substituteExpressionIdentifier ( node : Identifier ) : Expression {
1770
1761
if ( getEmitFlags ( node ) & EmitFlags . HelperName ) {
1771
1762
const externalHelpersModuleName = getExternalHelpersModuleName ( currentSourceFile ) ;
1772
1763
if ( externalHelpersModuleName ) {
1773
- return externalHelpersModuleName ;
1764
+ return factory . createPropertyAccessExpression ( externalHelpersModuleName , node ) ;
1774
1765
}
1766
+ return node ;
1775
1767
}
1776
1768
else if ( ! ( isGeneratedIdentifier ( node ) && ! ( node . autoGenerateFlags & GeneratedIdentifierFlags . AllowNameSubstitution ) ) && ! isLocalName ( node ) ) {
1777
1769
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 ) {
1853
1771
return setTextRange (
1854
1772
factory . createPropertyAccessExpression (
1855
1773
factory . createIdentifier ( "exports" ) ,
1856
1774
factory . cloneNode ( node )
1857
1775
) ,
1858
1776
/*location*/ node
1859
1777
) ;
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
+ }
1879
1801
}
1802
+ return node ;
1880
1803
}
1881
1804
1882
1805
/**
0 commit comments