Skip to content

Fix emit for object rest on a module export #32699

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,10 +436,10 @@ task("runtests-parallel").flags = {
" --shardId": "1-based ID of this shard (default: 1)",
};

task("diff", () => exec(getDiffTool(), [refBaseline, localBaseline], { ignoreExitCode: true }));
task("diff", () => exec(getDiffTool(), [refBaseline, localBaseline], { ignoreExitCode: true, waitForExit: false }));
task("diff").description = "Diffs the compiler baselines using the diff tool specified by the 'DIFF' environment variable";

task("diff-rwc", () => exec(getDiffTool(), [refRwcBaseline, localRwcBaseline], { ignoreExitCode: true }));
task("diff-rwc", () => exec(getDiffTool(), [refRwcBaseline, localRwcBaseline], { ignoreExitCode: true, waitForExit: false }));
task("diff-rwc").description = "Diffs the RWC baselines using the diff tool specified by the 'DIFF' environment variable";

/**
Expand Down
38 changes: 23 additions & 15 deletions scripts/build/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,45 @@ const isWindows = /^win/.test(process.platform);
* @property {boolean} [ignoreExitCode]
* @property {import("prex").CancellationToken} [cancelToken]
* @property {boolean} [hidePrompt]
* @property {boolean} [waitForExit=true]
*/
function exec(cmd, args, options = {}) {
return /**@type {Promise<{exitCode: number}>}*/(new Promise((resolve, reject) => {
const { ignoreExitCode, cancelToken = CancellationToken.none } = options;
const { ignoreExitCode, cancelToken = CancellationToken.none, waitForExit = true } = options;
cancelToken.throwIfCancellationRequested();

// TODO (weswig): Update child_process types to add windowsVerbatimArguments to the type definition
const subshellFlag = isWindows ? "/c" : "-c";
const command = isWindows ? [possiblyQuote(cmd), ...args] : [`${cmd} ${args.join(" ")}`];

if (!options.hidePrompt) log(`> ${chalk.green(cmd)} ${args.join(" ")}`);
const proc = spawn(isWindows ? "cmd" : "/bin/sh", [subshellFlag, ...command], { stdio: "inherit", windowsVerbatimArguments: true });
const proc = spawn(isWindows ? "cmd" : "/bin/sh", [subshellFlag, ...command], { stdio: waitForExit ? "inherit" : "ignore", windowsVerbatimArguments: true });
const registration = cancelToken.register(() => {
log(`${chalk.red("killing")} '${chalk.green(cmd)} ${args.join(" ")}'...`);
proc.kill("SIGINT");
proc.kill("SIGTERM");
reject(new CancelError());
});
proc.on("exit", exitCode => {
registration.unregister();
if (exitCode === 0 || ignoreExitCode) {
resolve({ exitCode });
}
else {
reject(new Error(`Process exited with code: ${exitCode}`));
}
});
proc.on("error", error => {
registration.unregister();
reject(error);
});
if (waitForExit) {
proc.on("exit", exitCode => {
registration.unregister();
if (exitCode === 0 || ignoreExitCode) {
resolve({ exitCode });
}
else {
reject(new Error(`Process exited with code: ${exitCode}`));
}
});
proc.on("error", error => {
registration.unregister();
reject(error);
});
}
else {
proc.unref();
// wait a short period in order to allow the process to start successfully before Node exits.
setTimeout(() => resolve({ exitCode: undefined }), 100);
}
}));
}
exports.exec = exec;
Expand Down
30 changes: 29 additions & 1 deletion src/compiler/transformers/es2018.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace ts {
const previousOnSubstituteNode = context.onSubstituteNode;
context.onSubstituteNode = onSubstituteNode;

let exportedVariableStatement = false;
let enabledSubstitutions: ESNextSubstitutionFlags;
let enclosingFunctionFlags: FunctionFlags;
let enclosingSuperContainerFlags: NodeCheckFlags = 0;
Expand All @@ -40,6 +41,7 @@ namespace ts {
return node;
}

exportedVariableStatement = false;
const visited = visitEachChild(node, visitor, context);
addEmitHelpers(visited, context.readEmitHelpers());
return visited;
Expand Down Expand Up @@ -79,6 +81,8 @@ namespace ts {
return visitBinaryExpression(node as BinaryExpression, noDestructuringValue);
case SyntaxKind.CatchClause:
return visitCatchClause(node as CatchClause);
case SyntaxKind.VariableStatement:
return visitVariableStatement(node as VariableStatement);
case SyntaxKind.VariableDeclaration:
return visitVariableDeclaration(node as VariableDeclaration);
case SyntaxKind.ForOfStatement:
Expand Down Expand Up @@ -321,19 +325,43 @@ namespace ts {
return visitEachChild(node, visitor, context);
}

function visitVariableStatement(node: VariableStatement): VisitResult<VariableStatement> {
if (hasModifier(node, ModifierFlags.Export)) {
const savedExportedVariableStatement = exportedVariableStatement;
exportedVariableStatement = true;
const visited = visitEachChild(node, visitor, context);
exportedVariableStatement = savedExportedVariableStatement;
return visited;
}
return visitEachChild(node, visitor, context);
}

/**
* Visits a VariableDeclaration node with a binding pattern.
*
* @param node A VariableDeclaration node.
*/
function visitVariableDeclaration(node: VariableDeclaration): VisitResult<VariableDeclaration> {
if (exportedVariableStatement) {
const savedExportedVariableStatement = exportedVariableStatement;
exportedVariableStatement = false;
const visited = visitVariableDeclarationWorker(node, /*exportedVariableStatement*/ true);
exportedVariableStatement = savedExportedVariableStatement;
return visited;
}
return visitVariableDeclarationWorker(node, /*exportedVariableStatement*/ false);
}

function visitVariableDeclarationWorker(node: VariableDeclaration, exportedVariableStatement: boolean): VisitResult<VariableDeclaration> {
// If we are here it is because the name contains a binding pattern with a rest somewhere in it.
if (isBindingPattern(node.name) && node.name.transformFlags & TransformFlags.ContainsObjectRestOrSpread) {
return flattenDestructuringBinding(
node,
visitor,
context,
FlattenLevel.ObjectRest
FlattenLevel.ObjectRest,
/*rval*/ undefined,
exportedVariableStatement
);
}
return visitEachChild(node, visitor, context);
Expand Down
2 changes: 2 additions & 0 deletions src/harness/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@ namespace Harness {
includeBuiltFile?: string;
baselineFile?: string;
libFiles?: string;
noTypesAndSymbols?: boolean;
}

// Additional options not already in ts.optionDeclarations
Expand All @@ -742,6 +743,7 @@ namespace Harness {
{ name: "currentDirectory", type: "string" },
{ name: "symlink", type: "string" },
{ name: "link", type: "string" },
{ name: "noTypesAndSymbols", type: "boolean" },
// Emitted js baseline will print full paths for every output file
{ name: "fullEmitPaths", type: "boolean" }
];
Expand Down
49 changes: 39 additions & 10 deletions src/testRunner/compilerRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,18 @@ class CompilerBaselineRunner extends RunnerBase {
}

public checkTestCodeOutput(fileName: string, test?: CompilerFileBasedTest) {
if (test && test.configurations) {
if (test && ts.some(test.configurations)) {
test.configurations.forEach(configuration => {
describe(`${this.testSuiteName} tests for ${fileName}${configuration ? ` (${Harness.getFileBasedTestConfigurationDescription(configuration)})` : ``}`, () => {
this.runSuite(fileName, test, configuration);
});
});
}
describe(`${this.testSuiteName} tests for ${fileName}`, () => {
this.runSuite(fileName, test);
});
else {
describe(`${this.testSuiteName} tests for ${fileName}`, () => {
this.runSuite(fileName, test);
});
}
}

private runSuite(fileName: string, test?: CompilerFileBasedTest, configuration?: Harness.FileBasedTestConfiguration) {
Expand Down Expand Up @@ -112,6 +114,7 @@ class CompilerBaselineRunner extends RunnerBase {
class CompilerTest {
private fileName: string;
private justName: string;
private configuredName: string;
private lastUnit: Harness.TestCaseParser.TestUnitData;
private harnessSettings: Harness.TestCaseParser.CompilerSettings;
private hasNonDtsFiles: boolean;
Expand All @@ -126,6 +129,25 @@ class CompilerTest {
constructor(fileName: string, testCaseContent?: Harness.TestCaseParser.TestCaseContent, configurationOverrides?: Harness.TestCaseParser.CompilerSettings) {
this.fileName = fileName;
this.justName = vpath.basename(fileName);
this.configuredName = this.justName;
if (configurationOverrides) {
let configuredName = "";
const keys = Object
.keys(configurationOverrides)
.map(k => k.toLowerCase())
.sort();
for (const key of keys) {
if (configuredName) {
configuredName += ",";
}
configuredName += `${key}=${configurationOverrides[key].toLowerCase()}`;
}
if (configuredName) {
const extname = vpath.extname(this.justName);
const basename = vpath.basename(this.justName, extname, /*ignoreCase*/ true);
this.configuredName = `${basename}(${configuredName})${extname}`;
}
}

const rootDir = fileName.indexOf("conformance") === -1 ? "tests/cases/compiler/" : ts.getDirectoryPath(fileName) + "/";

Expand Down Expand Up @@ -205,15 +227,15 @@ class CompilerTest {
public verifyDiagnostics() {
// check errors
Harness.Compiler.doErrorBaseline(
this.justName,
this.configuredName,
this.tsConfigFiles.concat(this.toBeCompiled, this.otherFiles),
this.result.diagnostics,
!!this.options.pretty);
}

public verifyModuleResolution() {
if (this.options.traceResolution) {
Harness.Baseline.runBaseline(this.justName.replace(/\.tsx?$/, ".trace.json"),
Harness.Baseline.runBaseline(this.configuredName.replace(/\.tsx?$/, ".trace.json"),
JSON.stringify(this.result.traces.map(utils.sanitizeTraceResolutionLogEntry), undefined, 4));
}
}
Expand All @@ -225,14 +247,14 @@ class CompilerTest {
// Because of the noEmitOnError option no files are created. We need to return null because baselining isn't required.
? null // tslint:disable-line no-null-keyword
: record;
Harness.Baseline.runBaseline(this.justName.replace(/\.tsx?$/, ".sourcemap.txt"), baseline);
Harness.Baseline.runBaseline(this.configuredName.replace(/\.tsx?$/, ".sourcemap.txt"), baseline);
}
}

public verifyJavaScriptOutput() {
if (this.hasNonDtsFiles) {
Harness.Compiler.doJsEmitBaseline(
this.justName,
this.configuredName,
this.fileName,
this.options,
this.result,
Expand All @@ -245,7 +267,7 @@ class CompilerTest {

public verifySourceMapOutput() {
Harness.Compiler.doSourcemapBaseline(
this.justName,
this.configuredName,
this.options,
this.result,
this.harnessSettings);
Expand All @@ -256,8 +278,15 @@ class CompilerTest {
return;
}

const noTypesAndSymbols =
this.harnessSettings.noTypesAndSymbols &&
this.harnessSettings.noTypesAndSymbols.toLowerCase() === "true";
if (noTypesAndSymbols) {
return;
}

Harness.Compiler.doTypeAndSymbolBaseline(
this.justName,
this.configuredName,
this.result.program!,
this.toBeCompiled.concat(this.otherFiles).filter(file => !!this.result.program!.getSourceFile(file.unitName)),
/*opts*/ undefined,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//// [exportEmptyArrayBindingPattern.ts]
export const [] = [];

//// [exportEmptyArrayBindingPattern.js]
define(["require", "exports"], function (require, exports) {
"use strict";
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports._b = _a = [];
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//// [exportEmptyArrayBindingPattern.ts]
export const [] = [];

//// [exportEmptyArrayBindingPattern.js]
define(["require", "exports"], function (require, exports) {
"use strict";
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
_a = [];
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//// [exportEmptyArrayBindingPattern.ts]
export const [] = [];

//// [exportEmptyArrayBindingPattern.js]
"use strict";
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports._b = _a = [];
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//// [exportEmptyArrayBindingPattern.ts]
export const [] = [];

//// [exportEmptyArrayBindingPattern.js]
"use strict";
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
_a = [];
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//// [exportEmptyArrayBindingPattern.ts]
export const [] = [];

//// [exportEmptyArrayBindingPattern.js]
var _a;
export var _b = _a = [];
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//// [exportEmptyArrayBindingPattern.ts]
export const [] = [];

//// [exportEmptyArrayBindingPattern.js]
export const [] = [];
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//// [exportEmptyArrayBindingPattern.ts]
export const [] = [];

//// [exportEmptyArrayBindingPattern.js]
var _a;
export var _b = _a = [];
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//// [exportEmptyArrayBindingPattern.ts]
export const [] = [];

//// [exportEmptyArrayBindingPattern.js]
export const [] = [];
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//// [exportEmptyArrayBindingPattern.ts]
export const [] = [];

//// [exportEmptyArrayBindingPattern.js]
System.register([], function (exports_1, context_1) {
"use strict";
var _a, _b;
var __moduleName = context_1 && context_1.id;
return {
setters: [],
execute: function () {
exports_1("_b", _b = _a = []);
}
};
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//// [exportEmptyArrayBindingPattern.ts]
export const [] = [];

//// [exportEmptyArrayBindingPattern.js]
System.register([], function (exports_1, context_1) {
"use strict";
var _a;
var __moduleName = context_1 && context_1.id;
return {
setters: [],
execute: function () {
_a = [];
}
};
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//// [exportEmptyObjectBindingPattern.ts]
export const {} = {};

//// [exportEmptyObjectBindingPattern.js]
define(["require", "exports"], function (require, exports) {
"use strict";
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports._b = _a = {};
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//// [exportEmptyObjectBindingPattern.ts]
export const {} = {};

//// [exportEmptyObjectBindingPattern.js]
define(["require", "exports"], function (require, exports) {
"use strict";
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
_a = {};
});
Loading