@@ -4734,14 +4734,49 @@ module ts {
47344734 return type;
47354735 }
47364736 }
4737+ /*Transitively mark all linked imports as referenced*/
4738+ function markLinkedImportsAsReferenced(node: ImportDeclaration): void {
4739+ var nodeLinks = getNodeLinks(node);
4740+ while (nodeLinks.importOnRightSide) {
4741+ var rightSide = nodeLinks.importOnRightSide;
4742+ nodeLinks.importOnRightSide = undefined;
4743+
4744+ getSymbolLinks(rightSide).referenced = true;
4745+ Debug.assert((rightSide.flags & SymbolFlags.Import) !== 0);
4746+
4747+ nodeLinks = getNodeLinks(getDeclarationOfKind(rightSide, SyntaxKind.ImportDeclaration))
4748+ }
4749+ }
47374750
47384751 function checkIdentifier(node: Identifier): Type {
47394752 var symbol = getResolvedSymbol(node);
47404753
47414754 if (symbol.flags & SymbolFlags.Import) {
4742- // Mark the import as referenced so that we emit it in the final .js file.
4743- // exception: identifiers that appear in type queries, const enums, modules that contain only const enums
4744- getSymbolLinks(symbol).referenced = getSymbolLinks(symbol).referenced || (!isInTypeQuery(node) && !isConstEnumOrConstEnumOnlyModule(resolveImport(symbol)));
4755+ var symbolLinks = getSymbolLinks(symbol);
4756+ if (!symbolLinks.referenced) {
4757+ var importOrExportAssignment = getLeftSideOfImportOrExportAssignment(node);
4758+
4759+ // decision about whether import is referenced can be made now if
4760+ // - import that are used anywhere except right side of import declarations
4761+ // - imports that are used on the right side of exported import declarations
4762+ // for other cases defer decision until the check of left side
4763+ if (!importOrExportAssignment ||
4764+ (importOrExportAssignment.flags & NodeFlags.Export) ||
4765+ (importOrExportAssignment.kind === SyntaxKind.ExportAssignment)) {
4766+ // Mark the import as referenced so that we emit it in the final .js file.
4767+ // exception: identifiers that appear in type queries, const enums, modules that contain only const enums
4768+ symbolLinks.referenced = !isInTypeQuery(node) && !isConstEnumOrConstEnumOnlyModule(resolveImport(symbol));
4769+ }
4770+ else {
4771+ var nodeLinks = getNodeLinks(importOrExportAssignment);
4772+ Debug.assert(!nodeLinks.importOnRightSide);
4773+ nodeLinks.importOnRightSide = symbol;
4774+ }
4775+ }
4776+
4777+ if (symbolLinks.referenced) {
4778+ markLinkedImportsAsReferenced(<ImportDeclaration>getDeclarationOfKind(symbol, SyntaxKind.ImportDeclaration));
4779+ }
47454780 }
47464781
47474782 checkCollisionWithCapturedSuperVariable(node, node);
@@ -8872,6 +8907,8 @@ module ts {
88728907 if (symbol && symbol.flags & SymbolFlags.Import) {
88738908 // Mark the import as referenced so that we emit it in the final .js file.
88748909 getSymbolLinks(symbol).referenced = true;
8910+ // mark any import declarations that depend upon this import as referenced
8911+ markLinkedImportsAsReferenced(<ImportDeclaration>getDeclarationOfKind(symbol, SyntaxKind.ImportDeclaration))
88758912 }
88768913 }
88778914
@@ -9097,19 +9134,24 @@ module ts {
90979134 return false;
90989135 }
90999136
9100- function isInRightSideOfImportOrExportAssignment(node : EntityName) {
9101- while (node .parent.kind === SyntaxKind.QualifiedName) {
9102- node = <QualifiedName>node .parent;
9137+ function getLeftSideOfImportOrExportAssignment(nodeOnRightSide : EntityName): ImportDeclaration | ExportAssignment {
9138+ while (nodeOnRightSide .parent.kind === SyntaxKind.QualifiedName) {
9139+ nodeOnRightSide = <QualifiedName>nodeOnRightSide .parent;
91039140 }
91049141
9105- if (node .parent.kind === SyntaxKind.ImportDeclaration) {
9106- return (<ImportDeclaration>node .parent).moduleReference === node ;
9142+ if (nodeOnRightSide .parent.kind === SyntaxKind.ImportDeclaration) {
9143+ return (<ImportDeclaration>nodeOnRightSide .parent).moduleReference === nodeOnRightSide && <ImportDeclaration>nodeOnRightSide.parent ;
91079144 }
9108- if (node.parent.kind === SyntaxKind.ExportAssignment) {
9109- return (<ExportAssignment>node.parent).exportName === node;
9145+
9146+ if (nodeOnRightSide.parent.kind === SyntaxKind.ExportAssignment) {
9147+ return (<ExportAssignment>nodeOnRightSide.parent).exportName === nodeOnRightSide && <ExportAssignment>nodeOnRightSide.parent;
91109148 }
91119149
9112- return false;
9150+ return undefined;
9151+ }
9152+
9153+ function isInRightSideOfImportOrExportAssignment(node: EntityName) {
9154+ return getLeftSideOfImportOrExportAssignment(node) !== undefined;
91139155 }
91149156
91159157 function isRightSideOfQualifiedNameOrPropertyAccess(node: Node) {
0 commit comments