@@ -4734,14 +4734,47 @@ 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+ nodeLinks = getNodeLinks(rightSide.declarations[0])
4746+ }
4747+ }
47374748
47384749 function checkIdentifier(node: Identifier): Type {
47394750 var symbol = getResolvedSymbol(node);
47404751
47414752 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)));
4753+ var symbolLinks = getSymbolLinks(symbol);
4754+ if (!symbolLinks.referenced) {
4755+ var importOrExportAssignment = getLeftSideOfImportOrExportAssignment(node);
4756+
4757+ // decision about whether import is referenced can be made now if
4758+ // - import that are used anywhere except right side of import declarations
4759+ // - imports that are used on the right side of exported import declarations
4760+ // for other cases defer decision until the check of left side
4761+ if (!importOrExportAssignment ||
4762+ (importOrExportAssignment.flags & NodeFlags.Export) ||
4763+ (importOrExportAssignment.kind === SyntaxKind.ExportAssignment)) {
4764+ // Mark the import as referenced so that we emit it in the final .js file.
4765+ // exception: identifiers that appear in type queries, const enums, modules that contain only const enums
4766+ symbolLinks.referenced = !isInTypeQuery(node) && !isConstEnumOrConstEnumOnlyModule(resolveImport(symbol));
4767+ }
4768+ else {
4769+ var nodeLinks = getNodeLinks(importOrExportAssignment);
4770+ Debug.assert(!nodeLinks.importOnRightSide);
4771+ nodeLinks.importOnRightSide = symbol;
4772+ }
4773+ }
4774+
4775+ if (symbolLinks.referenced) {
4776+ markLinkedImportsAsReferenced(<ImportDeclaration>symbol.declarations[0]);
4777+ }
47454778 }
47464779
47474780 checkCollisionWithCapturedSuperVariable(node, node);
@@ -8872,6 +8905,8 @@ module ts {
88728905 if (symbol && symbol.flags & SymbolFlags.Import) {
88738906 // Mark the import as referenced so that we emit it in the final .js file.
88748907 getSymbolLinks(symbol).referenced = true;
8908+ // mark any import declarations that depend upon this import as referenced
8909+ markLinkedImportsAsReferenced(<ImportDeclaration>symbol.declarations[0])
88758910 }
88768911 }
88778912
@@ -9097,19 +9132,24 @@ module ts {
90979132 return false;
90989133 }
90999134
9100- function isInRightSideOfImportOrExportAssignment(node : EntityName) {
9101- while (node .parent.kind === SyntaxKind.QualifiedName) {
9102- node = <QualifiedName>node .parent;
9135+ function getLeftSideOfImportOrExportAssignment(nodeOnRightSide : EntityName): ImportDeclaration | ExportAssignment {
9136+ while (nodeOnRightSide .parent.kind === SyntaxKind.QualifiedName) {
9137+ nodeOnRightSide = <QualifiedName>nodeOnRightSide .parent;
91039138 }
91049139
9105- if (node .parent.kind === SyntaxKind.ImportDeclaration) {
9106- return (<ImportDeclaration>node .parent).moduleReference === node ;
9140+ if (nodeOnRightSide .parent.kind === SyntaxKind.ImportDeclaration) {
9141+ return (<ImportDeclaration>nodeOnRightSide .parent).moduleReference === nodeOnRightSide && <ImportDeclaration>nodeOnRightSide.parent ;
91079142 }
9108- if (node.parent.kind === SyntaxKind.ExportAssignment) {
9109- return (<ExportAssignment>node.parent).exportName === node;
9143+
9144+ if (nodeOnRightSide.parent.kind === SyntaxKind.ExportAssignment) {
9145+ return (<ExportAssignment>nodeOnRightSide.parent).exportName === nodeOnRightSide && <ExportAssignment>nodeOnRightSide.parent;
91109146 }
91119147
9112- return false;
9148+ return undefined;
9149+ }
9150+
9151+ function isInRightSideOfImportOrExportAssignment(node: EntityName) {
9152+ return getLeftSideOfImportOrExportAssignment(node) !== undefined;
91139153 }
91149154
91159155 function isRightSideOfQualifiedNameOrPropertyAccess(node: Node) {
0 commit comments