Skip to content

Commit 6e86b13

Browse files
committed
Merge pull request microsoft#7681 from Microsoft/transforms-fixES6ImportElision
Fixes ES6 import elision for transformers
2 parents af4580b + 6ba1961 commit 6e86b13

4 files changed

Lines changed: 54 additions & 7 deletions

File tree

Jakefile.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,7 @@ function runTestsAndWriteOutput(file) {
695695
cleanTestDirs();
696696
var tests = process.env.test || process.env.tests || process.env.t;
697697
var light = process.env.light || false;
698+
var beep = process.env.beep;
698699
var testConfigFile = 'test.config';
699700
if (fs.existsSync(testConfigFile)) {
700701
fs.unlinkSync(testConfigFile);
@@ -726,13 +727,15 @@ function runTestsAndWriteOutput(file) {
726727
var tapNotOk = /^not\sok/;
727728
var tapComment = /^#/;
728729
var typeError = /^\s+TypeError:/;
730+
var debugError = /^\s+Error:\sDebug\sFailure\./;
729731
var progress = new ProgressBar("Running tests...");
730732
var expectedTestCount = 0;
731733
var testCount = 0;
732734
var failureCount = 0;
733735
var successCount = 0;
734736
var comments = [];
735737
var typeErrorCount = 0;
738+
var debugErrorCount = 0;
736739

737740
ex.addListener("stdout", function (output) {
738741
var m = tapRange.exec(output);
@@ -756,6 +759,9 @@ function runTestsAndWriteOutput(file) {
756759
else if (typeError.test(output)) {
757760
typeErrorCount++;
758761
}
762+
else if (debugError.test(output)) {
763+
debugErrorCount++;
764+
}
759765
return;
760766
}
761767

@@ -790,6 +796,8 @@ function runTestsAndWriteOutput(file) {
790796
console.log(comments.join(os.EOL));
791797
deleteTemporaryProjectOutput();
792798
complete();
799+
800+
if (beep) process.stdout.write("\u0007");
793801
});
794802
ex.addListener("error", function (e, status) {
795803
if (progress.visible) {
@@ -803,7 +811,12 @@ function runTestsAndWriteOutput(file) {
803811
console.log("# type errors: %s", typeErrorCount);
804812
}
805813

814+
if (debugErrorCount) {
815+
console.log("# debug errors: %s", debugErrorCount);
816+
}
817+
806818
deleteTemporaryProjectOutput();
819+
if (beep) process.stdout.write("\u0007");
807820
fail("Process exited with code " + status);
808821
});
809822
ex.run();

src/compiler/transformers/module/es6.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,35 @@
44
/*@internal*/
55
namespace ts {
66
export function transformES6Module(context: TransformationContext) {
7+
const compilerOptions = context.getCompilerOptions();
8+
const resolver = context.getEmitResolver();
9+
10+
let currentSourceFile: SourceFile;
11+
712
return transformSourceFile;
813

914
function transformSourceFile(node: SourceFile) {
15+
if (isExternalModule(node) || compilerOptions.isolatedModules) {
16+
currentSourceFile = node;
17+
return visitEachChild(node, visitor, context);
18+
}
19+
return node;
20+
}
21+
22+
function visitor(node: Node) {
23+
switch (node.kind) {
24+
case SyntaxKind.ImportDeclaration:
25+
return visitImportDeclaration(<ImportDeclaration>node);
26+
}
27+
28+
return node;
29+
}
30+
31+
function visitImportDeclaration(node: ImportDeclaration) {
32+
if (node.importClause && !resolver.isReferencedAliasDeclaration(node.importClause, /*checkChildren*/ true)) {
33+
return undefined;
34+
}
35+
1036
return node;
1137
}
1238
}

src/compiler/transformers/ts.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,11 +1793,14 @@ namespace ts {
17931793
return undefined;
17941794
}
17951795

1796-
return createMethod(
1797-
visitNodes(node.modifiers, visitor, isModifier),
1798-
visitPropertyNameOfClassElement(node),
1799-
visitNodes(node.parameters, visitor, isParameter),
1800-
transformFunctionBody(node),
1796+
return setOriginalNode(
1797+
createMethod(
1798+
visitNodes(node.modifiers, visitor, isModifier),
1799+
visitPropertyNameOfClassElement(node),
1800+
visitNodes(node.parameters, visitor, isParameter),
1801+
transformFunctionBody(node),
1802+
node
1803+
),
18011804
node
18021805
);
18031806
}
@@ -1900,6 +1903,10 @@ namespace ts {
19001903
* @param node The function expression node.
19011904
*/
19021905
function visitFunctionExpression(node: FunctionExpression) {
1906+
if (nodeIsMissing(node.body)) {
1907+
return createNode(SyntaxKind.OmittedExpression);
1908+
}
1909+
19031910
return createFunctionExpression(
19041911
node.asteriskToken,
19051912
node.name,
@@ -1916,7 +1923,7 @@ namespace ts {
19161923
* @param node The declaration node.
19171924
*/
19181925
function shouldElideFunctionLikeDeclaration(node: FunctionLikeDeclaration) {
1919-
return node.body === undefined
1926+
return nodeIsMissing(node.body)
19201927
|| hasModifier(node, ModifierFlags.Abstract | ModifierFlags.Ambient);
19211928
}
19221929

@@ -2004,7 +2011,7 @@ namespace ts {
20042011
)
20052012
);
20062013

2007-
const block = createBlock(statements, /*location*/ node.body);
2014+
const block = createBlock(statements, /*location*/ node.body, /*multiLine*/ true);
20082015

20092016
// Minor optimization, emit `_super` helper to capture `super` access in an arrow.
20102017
// This step isn't needed if we eventually transform this to ES5.

src/compiler/utilities.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3405,6 +3405,7 @@ namespace ts {
34053405
|| kind === SyntaxKind.TypeAliasDeclaration
34063406
|| kind === SyntaxKind.EnumDeclaration
34073407
|| kind === SyntaxKind.ModuleDeclaration
3408+
|| kind === SyntaxKind.ImportDeclaration
34083409
|| kind === SyntaxKind.ImportEqualsDeclaration
34093410
|| kind === SyntaxKind.ExportDeclaration
34103411
|| kind === SyntaxKind.ExportAssignment;

0 commit comments

Comments
 (0)