Skip to content

Commit abf270a

Browse files
committed
do not look into nested es6 exports / imports when collecting external modules
1 parent eda6eca commit abf270a

2 files changed

Lines changed: 42 additions & 47 deletions

File tree

src/compiler/program.ts

Lines changed: 41 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -689,61 +689,56 @@ namespace ts {
689689

690690
let imports: LiteralExpression[];
691691
for (let node of file.statements) {
692-
collect(node, /* allowRelativeModuleNames */ true);
692+
collect(node, /* allowRelativeModuleNames */ true, /* collectOnlyRequireCalls */ false);
693693
}
694694

695695
file.imports = imports || emptyArray;
696696

697-
function collect(node: Node, allowRelativeModuleNames: boolean): void {
698-
switch (node.kind) {
699-
case SyntaxKind.ImportDeclaration:
700-
case SyntaxKind.ImportEqualsDeclaration:
701-
case SyntaxKind.ExportDeclaration:
702-
let moduleNameExpr = getExternalModuleName(node);
703-
if (!moduleNameExpr || moduleNameExpr.kind !== SyntaxKind.StringLiteral) {
704-
break;
705-
}
706-
if (!(<LiteralExpression>moduleNameExpr).text) {
707-
break;
708-
}
697+
return;
709698

710-
if (allowRelativeModuleNames || !isExternalModuleNameRelative((<LiteralExpression>moduleNameExpr).text)) {
711-
(imports || (imports = [])).push(<LiteralExpression>moduleNameExpr);
712-
}
713-
break;
714-
case SyntaxKind.CallExpression:
715-
if (isJavaScriptFile && isRequireCall(node)) {
716-
717-
let jsImports = (<CallExpression>node).arguments;
718-
if (jsImports) {
719-
imports = (imports || []);
720-
for (var i = 0; i < jsImports.length; i++) {
721-
if (jsImports[i].kind === SyntaxKind.StringLiteral) {
722-
imports.push(<StringLiteral>jsImports[i]);
723-
}
724-
}
699+
function collect(node: Node, allowRelativeModuleNames: boolean, collectOnlyRequireCalls: boolean): void {
700+
if (!collectOnlyRequireCalls) {
701+
switch (node.kind) {
702+
case SyntaxKind.ImportDeclaration:
703+
case SyntaxKind.ImportEqualsDeclaration:
704+
case SyntaxKind.ExportDeclaration:
705+
let moduleNameExpr = getExternalModuleName(node);
706+
if (!moduleNameExpr || moduleNameExpr.kind !== SyntaxKind.StringLiteral) {
707+
break;
725708
}
726-
}
727-
break;
728-
case SyntaxKind.ModuleDeclaration:
729-
if ((<ModuleDeclaration>node).name.kind === SyntaxKind.StringLiteral && (node.flags & NodeFlags.Ambient || isDeclarationFile(file))) {
730-
// TypeScript 1.0 spec (April 2014): 12.1.6
731-
// An AmbientExternalModuleDeclaration declares an external module.
732-
// This type of declaration is permitted only in the global module.
733-
// The StringLiteral must specify a top - level external module name.
734-
// Relative external module names are not permitted
735-
forEachChild((<ModuleDeclaration>node).body, node => {
709+
if (!(<LiteralExpression>moduleNameExpr).text) {
710+
break;
711+
}
712+
713+
if (allowRelativeModuleNames || !isExternalModuleNameRelative((<LiteralExpression>moduleNameExpr).text)) {
714+
(imports || (imports = [])).push(<LiteralExpression>moduleNameExpr);
715+
}
716+
break;
717+
case SyntaxKind.ModuleDeclaration:
718+
if ((<ModuleDeclaration>node).name.kind === SyntaxKind.StringLiteral && (node.flags & NodeFlags.Ambient || isDeclarationFile(file))) {
736719
// TypeScript 1.0 spec (April 2014): 12.1.6
737-
// An ExternalImportDeclaration in anAmbientExternalModuleDeclaration may reference other external modules
738-
// only through top - level external module names. Relative external module names are not permitted.
739-
collect(node, /* allowRelativeModuleNames */ false);
740-
});
741-
}
742-
break;
720+
// An AmbientExternalModuleDeclaration declares an external module.
721+
// This type of declaration is permitted only in the global module.
722+
// The StringLiteral must specify a top - level external module name.
723+
// Relative external module names are not permitted
724+
forEachChild((<ModuleDeclaration>node).body, node => {
725+
// TypeScript 1.0 spec (April 2014): 12.1.6
726+
// An ExternalImportDeclaration in anAmbientExternalModuleDeclaration may reference other external modules
727+
// only through top - level external module names. Relative external module names are not permitted.
728+
collect(node, /* allowRelativeModuleNames */ false, collectOnlyRequireCalls);
729+
});
730+
}
731+
break;
732+
}
743733
}
744734

745-
if (isSourceFileJavaScript(file)) {
746-
forEachChild(node, node => collect(node, allowRelativeModuleNames));
735+
if (isJavaScriptFile) {
736+
if (isRequireCall(node)) {
737+
(imports || (imports = [])).push(<StringLiteral>(<CallExpression>node).arguments[0]);
738+
}
739+
else {
740+
forEachChild(node, node => collect(node, allowRelativeModuleNames, /* collectOnlyRequireCalls */ true));
741+
}
747742
}
748743
}
749744
}

src/compiler/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,7 @@ namespace ts {
10451045

10461046
/**
10471047
* Returns true if the node is a CallExpression to the identifier 'require' with
1048-
* exactly one argument.
1048+
* exactly one string literal argument.
10491049
* This function does not test if the node is in a JavaScript file or not.
10501050
*/
10511051
export function isRequireCall(expression: Node): expression is CallExpression {

0 commit comments

Comments
 (0)