@@ -280,7 +280,7 @@ namespace ts {
280280 if ( moduleKind !== ModuleKind . AMD ) {
281281 if ( ! node . importClause ) {
282282 // import "mod";
283- addNode ( statements ,
283+ statements . push (
284284 createStatement (
285285 createRequireCall ( node ) ,
286286 /*location*/ node
@@ -291,7 +291,7 @@ namespace ts {
291291 const variables : VariableDeclaration [ ] = [ ] ;
292292 if ( namespaceDeclaration && ! isDefaultImport ( node ) ) {
293293 // import * as n from "mod";
294- addNode ( variables ,
294+ variables . push (
295295 createVariableDeclaration (
296296 getSynthesizedClone ( namespaceDeclaration . name ) ,
297297 createRequireCall ( node )
@@ -303,15 +303,15 @@ namespace ts {
303303 // import { x, y } from "mod";
304304 // import d, { x, y } from "mod";
305305 // import d, * as n from "mod";
306- addNode ( variables ,
306+ variables . push (
307307 createVariableDeclaration (
308308 getGeneratedNameForNode ( node ) ,
309309 createRequireCall ( node )
310310 )
311311 ) ;
312312
313313 if ( namespaceDeclaration && isDefaultImport ( node ) ) {
314- addNode ( variables ,
314+ variables . push (
315315 createVariableDeclaration (
316316 getSynthesizedClone ( namespaceDeclaration . name ) ,
317317 getGeneratedNameForNode ( node )
@@ -320,7 +320,7 @@ namespace ts {
320320 }
321321 }
322322
323- addNode ( statements ,
323+ statements . push (
324324 createVariableStatement (
325325 /*modifiers*/ undefined ,
326326 createVariableDeclarationList ( variables ) ,
@@ -331,7 +331,7 @@ namespace ts {
331331 }
332332 else if ( namespaceDeclaration && isDefaultImport ( node ) ) {
333333 // import d, * as n from "mod";
334- addNode ( statements ,
334+ statements . push (
335335 createVariableStatement (
336336 /*modifiers*/ undefined ,
337337 createVariableDeclarationList ( [
@@ -346,7 +346,7 @@ namespace ts {
346346 }
347347
348348 addExportImportAssignments ( statements , node ) ;
349- return statements ;
349+ return singleOrMany ( statements ) ;
350350 }
351351
352352 function visitImportEqualsDeclaration ( node : ImportEqualsDeclaration ) : VisitResult < Statement > {
@@ -409,7 +409,7 @@ namespace ts {
409409 const statements : Statement [ ] = [ ] ;
410410 // export { x, y } from "mod";
411411 if ( moduleKind !== ModuleKind . AMD ) {
412- addNode ( statements ,
412+ statements . push (
413413 createVariableStatement (
414414 /*modifiers*/ undefined ,
415415 createVariableDeclarationList ( [
@@ -428,7 +428,7 @@ namespace ts {
428428 generatedName ,
429429 specifier . propertyName || specifier . name
430430 ) ;
431- addNode ( statements ,
431+ statements . push (
432432 createStatement (
433433 createExportAssignment ( specifier . name , exportedValue ) ,
434434 /*location*/ specifier
@@ -437,7 +437,7 @@ namespace ts {
437437 }
438438 }
439439
440- return statements ;
440+ return singleOrMany ( statements ) ;
441441 }
442442 else {
443443 // export * from "mod";
@@ -466,8 +466,9 @@ namespace ts {
466466 }
467467
468468 function addExportDefault ( statements : Statement [ ] , expression : Expression , location : TextRange ) : void {
469- addNode ( statements , tryCreateExportDefaultCompat ( ) ) ;
470- addNode ( statements ,
469+ tryAddExportDefaultCompat ( statements ) ;
470+
471+ statements . push (
471472 createStatement (
472473 createExportAssignment (
473474 createIdentifier ( "default" ) ,
@@ -478,25 +479,29 @@ namespace ts {
478479 ) ;
479480 }
480481
481- function tryCreateExportDefaultCompat ( ) : Statement {
482+ function tryAddExportDefaultCompat ( statements : Statement [ ] ) {
482483 const original = getOriginalNode ( currentSourceFile ) ;
483484 Debug . assert ( original . kind === SyntaxKind . SourceFile ) ;
484485
485- if ( ! ( < SourceFile > original ) . symbol . exports [ "___esModule" ] ) {
486+ if ( ! original . symbol . exports [ "___esModule" ] ) {
486487 if ( languageVersion === ScriptTarget . ES3 ) {
487- return createStatement (
488- createExportAssignment (
489- createIdentifier ( "__esModule" ) ,
490- createLiteral ( true )
488+ statements . push (
489+ createStatement (
490+ createExportAssignment (
491+ createIdentifier ( "__esModule" ) ,
492+ createLiteral ( true )
493+ )
491494 )
492495 ) ;
493496 }
494497 else {
495- return createStatement (
496- createObjectDefineProperty (
497- createIdentifier ( "exports" ) ,
498- createLiteral ( "__esModule" ) ,
499- { value : createLiteral ( true ) }
498+ statements . push (
499+ createStatement (
500+ createObjectDefineProperty (
501+ createIdentifier ( "exports" ) ,
502+ createLiteral ( "__esModule" ) ,
503+ { value : createLiteral ( true ) }
504+ )
500505 )
501506 ) ;
502507 }
@@ -653,13 +658,48 @@ namespace ts {
653658 }
654659
655660 function substituteExpressionIdentifier ( node : Identifier ) : Expression {
656- const container = resolver . getReferencedExportContainer ( node ) ;
657- if ( container && container . kind === SyntaxKind . SourceFile ) {
658- return createPropertyAccess (
659- createIdentifier ( "exports" ) ,
660- getSynthesizedClone ( node ) ,
661- /*location*/ node
662- ) ;
661+ const original = getOriginalNode ( node ) ;
662+ if ( isIdentifier ( original ) ) {
663+ const container = resolver . getReferencedExportContainer ( original ) ;
664+ if ( container ) {
665+ if ( container . kind === SyntaxKind . SourceFile ) {
666+ return createPropertyAccess (
667+ createIdentifier ( "exports" ) ,
668+ getSynthesizedClone ( node ) ,
669+ /*location*/ node
670+ ) ;
671+ }
672+ }
673+ else {
674+ const declaration = resolver . getReferencedImportDeclaration ( original ) ;
675+ if ( declaration ) {
676+ if ( declaration . kind === SyntaxKind . ImportClause ) {
677+ if ( languageVersion >= ScriptTarget . ES5 ) {
678+ return createPropertyAccess (
679+ getGeneratedNameForNode ( declaration . parent ) ,
680+ createIdentifier ( "default" ) ,
681+ /*location*/ node
682+ ) ;
683+ }
684+ else {
685+ return createElementAccess (
686+ getGeneratedNameForNode ( declaration . parent ) ,
687+ createLiteral ( "default" ) ,
688+ /*location*/ node
689+ ) ;
690+ }
691+ }
692+ else if ( declaration . kind === SyntaxKind . ImportSpecifier ) {
693+ const name = ( < ImportSpecifier > declaration ) . propertyName
694+ || ( < ImportSpecifier > declaration ) . name ;
695+ return createPropertyAccess (
696+ getGeneratedNameForNode ( declaration . parent . parent . parent ) ,
697+ getSynthesizedClone ( name ) ,
698+ /*location*/ node
699+ ) ;
700+ }
701+ }
702+ }
663703 }
664704
665705 return node ;
0 commit comments