Skip to content

Commit 0d8e152

Browse files
committed
Merge branch 'transforms-transformer-es6' into transforms-transformer-module
2 parents 0b64048 + 78dfab8 commit 0d8e152

16 files changed

Lines changed: 478 additions & 399 deletions

File tree

Jakefile.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ function concatenateFiles(destinationFile, sourceFiles) {
239239

240240
var useDebugMode = true;
241241
var useTransforms = process.env.USE_TRANSFORMS || false;
242+
var useTransformCompat = false;
242243
var host = (process.env.host || process.env.TYPESCRIPT_HOST || "node");
243244
var compilerFilename = "tsc.js";
244245
var LKGCompiler = path.join(LKGDirectory, compilerFilename);
@@ -301,6 +302,9 @@ function compileFile(outFile, sources, prereqs, prefixes, useBuiltCompiler, noOu
301302
if (useBuiltCompiler && useTransforms) {
302303
options += " --experimentalTransforms"
303304
}
305+
else if (useBuiltCompiler && useTransformCompat) {
306+
options += " --transformCompatibleEmit"
307+
}
304308

305309
var cmd = host + " " + compilerPath + " " + options + " ";
306310
cmd = cmd + sources.join(" ");
@@ -429,6 +433,10 @@ task("setTransforms", function() {
429433
useTransforms = true;
430434
});
431435

436+
task("setTransformCompat", function() {
437+
useTransformCompat = true;
438+
});
439+
432440
task("configure-nightly", [configureNightlyJs], function() {
433441
var cmd = host + " " + configureNightlyJs + " " + packageJson + " " + programTs;
434442
console.log(cmd);

src/compiler/commandLineParser.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,18 @@ namespace ts {
322322
description: Diagnostics.Allow_default_imports_from_modules_with_no_default_export_This_does_not_affect_code_emit_just_typechecking
323323
},
324324
{
325+
// this option will be removed when this is merged with master and exists solely
326+
// to enable the tree transforming emitter side-by-side with the existing emitter.
325327
name: "experimentalTransforms",
326328
type: "boolean",
327329
experimental: true
330+
},
331+
{
332+
// this option will be removed when this is merged with master and exists solely
333+
// to enable the tree transforming emitter side-by-side with the existing emitter.
334+
name: "transformCompatibleEmit",
335+
type: "boolean",
336+
experimental: true
328337
}
329338
];
330339

src/compiler/emitter.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,6 +1915,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
19151915

19161916
if (multiLine) {
19171917
decreaseIndent();
1918+
if (!compilerOptions.transformCompatibleEmit) {
1919+
writeLine();
1920+
}
19181921
}
19191922

19201923
write(")");
@@ -4331,7 +4334,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
43314334
writeLine();
43324335
emitStart(restParam);
43334336
emitNodeWithCommentsAndWithoutSourcemap(restParam.name);
4334-
write(restIndex > 0
4337+
write(restIndex > 0 || !compilerOptions.transformCompatibleEmit
43354338
? `[${tempName} - ${restIndex}] = arguments[${tempName}];`
43364339
: `[${tempName}] = arguments[${tempName}];`);
43374340
emitEnd(restParam);
@@ -5353,7 +5356,7 @@ const _super = (function (geti, seti) {
53535356
const isClassExpressionWithStaticProperties = staticProperties.length > 0 && node.kind === SyntaxKind.ClassExpression;
53545357
let tempVariable: Identifier;
53555358

5356-
if (isClassExpressionWithStaticProperties) {
5359+
if (isClassExpressionWithStaticProperties && compilerOptions.transformCompatibleEmit) {
53575360
tempVariable = createAndRecordTempVariable(TempFlags.Auto);
53585361
write("(");
53595362
increaseIndent();
@@ -5390,6 +5393,11 @@ const _super = (function (geti, seti) {
53905393
writeLine();
53915394
emitConstructor(node, baseTypeNode);
53925395
emitMemberFunctionsForES5AndLower(node);
5396+
if (!compilerOptions.transformCompatibleEmit) {
5397+
emitPropertyDeclarations(node, staticProperties);
5398+
writeLine();
5399+
emitDecoratorsOfClass(node, /*decoratedClassAlias*/ undefined);
5400+
}
53935401
writeLine();
53945402
emitToken(SyntaxKind.CloseBraceToken, node.members.end, () => {
53955403
write("return ");
@@ -5416,11 +5424,13 @@ const _super = (function (geti, seti) {
54165424
write("))");
54175425
if (node.kind === SyntaxKind.ClassDeclaration) {
54185426
write(";");
5419-
emitPropertyDeclarations(node, staticProperties);
5420-
writeLine();
5421-
emitDecoratorsOfClass(node, /*decoratedClassAlias*/ undefined);
5427+
if (compilerOptions.transformCompatibleEmit) {
5428+
emitPropertyDeclarations(node, staticProperties);
5429+
writeLine();
5430+
emitDecoratorsOfClass(node, /*decoratedClassAlias*/ undefined);
5431+
}
54225432
}
5423-
else if (isClassExpressionWithStaticProperties) {
5433+
else if (isClassExpressionWithStaticProperties && compilerOptions.transformCompatibleEmit) {
54245434
for (const property of staticProperties) {
54255435
write(",");
54265436
writeLine();

src/compiler/factory.ts

Lines changed: 55 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ namespace ts {
250250
node.decorators = undefined;
251251
node.modifiers = undefined;
252252
node.typeParameters = undefined;
253-
node.parameters = createSynthesizedNodeArray(parameters);
253+
node.parameters = createNodeArray(parameters);
254254
node.type = undefined;
255255
node.body = body;
256256
return node;
@@ -286,15 +286,15 @@ namespace ts {
286286
node.name = typeof name === "string" ? createIdentifier(name) : name;
287287
node.questionToken = undefined;
288288
node.type = undefined;
289-
node.initializer = initializer;
289+
node.initializer = initializer ? parenthesizeExpressionForList(initializer) : undefined;
290290
return node;
291291
}
292292

293293
// Expression
294294

295295
export function createArrayLiteral(elements?: Expression[]) {
296296
const node = <ArrayLiteralExpression>createNode(SyntaxKind.ArrayLiteralExpression);
297-
node.elements = createNodeArray(elements);
297+
node.elements = parenthesizeListElements(createNodeArray(elements));
298298
return node;
299299
}
300300

@@ -322,14 +322,16 @@ namespace ts {
322322
export function createCall(expression: Expression, argumentsArray: Expression[], location?: TextRange) {
323323
const node = <CallExpression>createNode(SyntaxKind.CallExpression, location);
324324
node.expression = parenthesizeForAccess(expression);
325-
node.arguments = createNodeArray(argumentsArray);
325+
node.arguments = parenthesizeListElements(createNodeArray(argumentsArray));
326326
return node;
327327
}
328328

329329
export function createNew(expression: Expression, argumentsArray: Expression[], location?: TextRange) {
330330
const node = <NewExpression>createNode(SyntaxKind.NewExpression, location);
331331
node.expression = parenthesizeForAccess(expression);
332-
node.arguments = argumentsArray ? createNodeArray(argumentsArray) : undefined;
332+
node.arguments = argumentsArray
333+
? parenthesizeListElements(createNodeArray(argumentsArray))
334+
: undefined;
333335
return node;
334336
}
335337

@@ -358,7 +360,7 @@ namespace ts {
358360
node.parameters = createNodeArray(parameters);
359361
node.type = undefined;
360362
node.equalsGreaterThanToken = createNode(SyntaxKind.EqualsGreaterThanToken);
361-
node.body = body;
363+
node.body = parenthesizeConciseBody(body);
362364
return node;
363365
}
364366

@@ -414,7 +416,7 @@ namespace ts {
414416

415417
export function createSpread(expression: Expression) {
416418
const node = <SpreadElementExpression>createNode(SyntaxKind.SpreadElementExpression);
417-
node.expression = expression;
419+
node.expression = parenthesizeExpressionForList(expression);
418420
return node;
419421
}
420422

@@ -424,8 +426,8 @@ namespace ts {
424426
node.modifiers = undefined;
425427
node.name = name;
426428
node.typeParameters = undefined;
427-
node.heritageClauses = createSynthesizedNodeArray(heritageClauses);
428-
node.members = createSynthesizedNodeArray(members);
429+
node.heritageClauses = createNodeArray(heritageClauses);
430+
node.members = createNodeArray(members);
429431
return node;
430432
}
431433

@@ -469,7 +471,7 @@ namespace ts {
469471
export function createVariableDeclaration(name: string | BindingPattern | Identifier, initializer?: Expression, location?: TextRange): VariableDeclaration {
470472
const node = <VariableDeclaration>createNode(SyntaxKind.VariableDeclaration, location);
471473
node.name = typeof name === "string" ? createIdentifier(name) : name;
472-
node.initializer = initializer;
474+
node.initializer = initializer !== undefined ? parenthesizeExpressionForList(initializer) : undefined;
473475
return node;
474476
}
475477

@@ -479,7 +481,7 @@ namespace ts {
479481

480482
export function createStatement(expression: Expression, location?: TextRange): ExpressionStatement {
481483
const node = <ExpressionStatement>createNode(SyntaxKind.ExpressionStatement, location);
482-
node.expression = expression;
484+
node.expression = parenthesizeExpressionForExpressionStatement(expression);
483485
return node;
484486
}
485487

@@ -562,8 +564,8 @@ namespace ts {
562564
setModifiers(node, modifiers);
563565
node.name = name;
564566
node.typeParameters = undefined;
565-
node.heritageClauses = createSynthesizedNodeArray(heritageClauses);
566-
node.members = createSynthesizedNodeArray(members);
567+
node.heritageClauses = createNodeArray(heritageClauses);
568+
node.members = createNodeArray(members);
567569
return node;
568570
}
569571

@@ -599,7 +601,14 @@ namespace ts {
599601
export function createHeritageClause(token: SyntaxKind, types: ExpressionWithTypeArguments[], location?: TextRange) {
600602
const node = <HeritageClause>createNode(SyntaxKind.HeritageClause, location);
601603
node.token = token;
602-
node.types = createSynthesizedNodeArray(types);
604+
node.types = createNodeArray(types);
605+
return node;
606+
}
607+
608+
export function createCaseClause(expression: Expression, statements: Statement[], location?: TextRange) {
609+
const node = <CaseClause>createNode(SyntaxKind.CaseClause, location);
610+
node.expression = parenthesizeExpressionForList(expression);
611+
node.statements = createNodeArray(statements);
603612
return node;
604613
}
605614

@@ -609,7 +618,7 @@ namespace ts {
609618
const node = <PropertyAssignment>createNode(SyntaxKind.PropertyAssignment, location);
610619
node.name = typeof name === "string" ? createIdentifier(name) : name;
611620
node.questionToken = undefined;
612-
node.initializer = initializer;
621+
node.initializer = initializer !== undefined ? parenthesizeExpressionForList(initializer) : undefined;
613622
return node;
614623
}
615624

@@ -734,7 +743,7 @@ namespace ts {
734743
export function createJsxCreateElement(reactNamespace: string, tagName: Expression, props: Expression, children: Expression[]): LeftHandSideExpression {
735744
const argumentsList = [tagName];
736745
if (props) {
737-
argumentsList.push(props)
746+
argumentsList.push(props);
738747
}
739748

740749
if (children && children.length > 0) {
@@ -834,7 +843,7 @@ namespace ts {
834843
addPropertyAssignment(properties, "enumerable", enumerable, preferNewLine);
835844
addPropertyAssignment(properties, "configurable", configurable, preferNewLine);
836845
addPropertyAssignment(properties, "writable", writable, preferNewLine);
837-
return createObjectLiteral(properties, location)
846+
return createObjectLiteral(properties, location);
838847
}
839848

840849
function addPropertyAssignment(properties: ObjectLiteralElement[], name: string, value: boolean | Expression, preferNewLine: boolean) {
@@ -848,7 +857,7 @@ namespace ts {
848857
startOnNewLine(property);
849858
}
850859

851-
addNode(properties, property);
860+
properties.push(property);
852861
}
853862
}
854863

@@ -1205,6 +1214,26 @@ namespace ts {
12051214
: createParen(operand, /*location*/ operand);
12061215
}
12071216

1217+
function parenthesizeListElements(elements: NodeArray<Expression>) {
1218+
let result: Expression[];
1219+
for (let i = 0; i < elements.length; i++) {
1220+
const element = parenthesizeExpressionForList(elements[i]);
1221+
if (result !== undefined || element !== elements[i]) {
1222+
if (result === undefined) {
1223+
result = elements.slice(0, i);
1224+
}
1225+
1226+
result.push(element);
1227+
}
1228+
}
1229+
1230+
if (result !== undefined) {
1231+
return createNodeArray(result, elements, elements.hasTrailingComma);
1232+
}
1233+
1234+
return elements;
1235+
}
1236+
12081237
export function parenthesizeExpressionForList(expression: Expression) {
12091238
const expressionPrecedence = getExpressionPrecedence(expression);
12101239
const commaPrecedence = getOperatorPrecedence(SyntaxKind.BinaryExpression, SyntaxKind.CommaToken);
@@ -1230,6 +1259,14 @@ namespace ts {
12301259
return expression;
12311260
}
12321261

1262+
export function parenthesizeConciseBody(body: ConciseBody): ConciseBody {
1263+
if (body.kind === SyntaxKind.ObjectLiteralExpression) {
1264+
return createParen(<Expression>body, /*location*/ body);
1265+
}
1266+
1267+
return body;
1268+
}
1269+
12331270
function getLeftmostExpression(node: Expression): Expression {
12341271
while (true) {
12351272
switch (node.kind) {
@@ -1266,20 +1303,6 @@ namespace ts {
12661303
return node;
12671304
}
12681305

1269-
/**
1270-
* Skips past any TypeAssertionExpression or AsExpression nodes to their inner expression.
1271-
*
1272-
* @param node The expression node.
1273-
*/
1274-
function skipAssertions(node: Expression) {
1275-
while (node.kind === SyntaxKind.TypeAssertionExpression
1276-
|| node.kind === SyntaxKind.AsExpression) {
1277-
node = (<AssertionExpression>node).expression;
1278-
}
1279-
1280-
return node;
1281-
}
1282-
12831306
export function startOnNewLine<T extends Node>(node: T): T {
12841307
node.startsOnNewLine = true;
12851308
return node;

src/compiler/printer.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ const _super = (function (geti, seti) {
137137
const comments = createCommentWriter(host, writer, sourceMap);
138138
const {
139139
getLeadingComments,
140-
getLeadingCommentsOfPosition,
141140
getTrailingComments,
142141
getTrailingCommentsOfPosition,
143142
emitLeadingComments,
@@ -2421,7 +2420,7 @@ const _super = (function (geti, seti) {
24212420
function createBracketsMap() {
24222421
const brackets: string[][] = [];
24232422
brackets[ListFormat.Braces] = ["{", "}"];
2424-
brackets[ListFormat.Parenthesis] = ["(",")"];
2423+
brackets[ListFormat.Parenthesis] = ["(", ")"];
24252424
brackets[ListFormat.AngleBrackets] = ["<", ">"];
24262425
brackets[ListFormat.SquareBrackets] = ["[", "]"];
24272426
return brackets;

src/compiler/program.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/// <reference path="sys.ts" />
22
/// <reference path="emitter.ts" />
33
/// <reference path="core.ts" />
4+
/// <reference path="printer.ts" />
45

56
namespace ts {
67
/* @internal */ export let programTime = 0;

src/compiler/sourcemap.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ namespace ts {
6767
// Source map data
6868
let sourceMapData: SourceMapData;
6969

70+
// This keeps track of the number of times `disable` has been called without a
71+
// corresponding call to `enable`. As long as this value is non-zero, mappings will not
72+
// be recorded.
73+
// This is primarily used to provide a better experience when debugging binding
74+
// patterns and destructuring assignments for simple expressions.
7075
let disableDepth: number;
7176

7277
return {
@@ -160,12 +165,18 @@ namespace ts {
160165
disableDepth = 0;
161166
}
162167

168+
/**
169+
* Re-enables the recording of mappings.
170+
*/
163171
function enable() {
164172
if (disableDepth > 0) {
165173
disableDepth--;
166174
}
167175
}
168176

177+
/**
178+
* Disables the recording of mappings.
179+
*/
169180
function disable() {
170181
disableDepth++;
171182
}

0 commit comments

Comments
 (0)