8000 Merge branch 'transforms-transformer-es6' into transforms-transformer… · prmdeveloper/TypeScript@70cbb9b · GitHub
[go: up one dir, main page]

Skip to content

Commit 70cbb9b

Browse files
committed
Merge branch 'transforms-transformer-es6' into transforms-transformer-module
2 parents 72bfd2f + 2d2709f commit 70cbb9b

File tree

184 files changed

+2761
-2126
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

184 files changed

+2761
-2126
lines changed

Jakefile.js

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

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

309305
var cmd = host + " " + compilerPath + " " + options + " ";
310306
cmd = cmd + sources.join(" ");
@@ -433,10 +429,6 @@ task("setTransforms", function() {
433429
useTransforms = true;
434430
});
435431

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

src/compiler/binder.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1930,7 +1930,8 @@ namespace ts {
19301930
case SyntaxKind.CallExpression:
19311931
excludeFlags = TransformFlags.ArrayLiteralOrCallOrNewExcludes;
19321932
if (subtreeFlags & TransformFlags.ContainsSpreadElementExpression
1933-
|| isSuperCall(node)) {
1933+
|| isSuperCall(node)
1934+
|| isSuperPropertyCall(node)) {
19341935
// If the this node contains a SpreadElementExpression, or is a super call, then it is an ES6
19351936
// node.
19361937
transformFlags |= TransformFlags.AssertES6;
@@ -1969,12 +1970,18 @@ namespace ts {
19691970
}
19701971
}
19711972

1973+
// If the expression of a ParenthesizedExpression is a destructuring assignment,
1974+
// then the ParenthesizedExpression is a destructuring assignment.
1975+
if ((<ParenthesizedExpression>node).expression.transformFlags & TransformFlags.DestructuringAssignment) {
1976+
transformFlags |= TransformFlags.DestructuringAssignment;
1977+
}
1978+
19721979
break;
19731980

19741981
case SyntaxKind.BinaryExpression:
19751982
if (isDestructuringAssignment(node)) {
19761983
// Destructuring assignments are ES6 syntax.
1977-
transformFlags |= TransformFlags.AssertES6;
1984+
transformFlags |= TransformFlags.AssertES6 | TransformFlags.DestructuringAssignment;
19781985
}
19791986
else if ((<BinaryExpression>node).operatorToken.kind === SyntaxKind.AsteriskAsteriskToken
19801987
|| (<BinaryExpression>node).operatorToken.kind === SyntaxKind.AsteriskAsteriskEqualsToken) {
@@ -1984,6 +1991,16 @@ namespace ts {
19841991

19851992
break;
19861993

1994+
case SyntaxKind.ExpressionStatement:
1995+
// If the expression of an expression statement is a destructuring assignment,
1996+
// then we treat the statement as ES6 so that we can indicate that we do not
1997+
// need to hold on to the right-hand side.
1998+
if ((<ExpressionStatement>node).expression.transformFlags & TransformFlags.DestructuringAssignment) {
1999+
transformFlags |= TransformFlags.AssertES6;
2000+
}
2001+
2002+
break;
2003+
19872004
case SyntaxKind.Parameter:
19882005
// If the parameter has a question token, then it is TypeScript syntax.
19892006
if ((<ParameterDeclaration>node).questionToken) {

src/compiler/checker.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4846,7 +4846,7 @@ namespace ts {
48464846
const parent = container && container.parent;
48474847
if (parent && (isClassLike(parent) || parent.kind === SyntaxKind.InterfaceDeclaration)) {
48484848
if (!(container.flags & NodeFlags.Static) &&
4849-
(container.kind !== SyntaxKind.Constructor || isNodeDescendentOf(node, (<ConstructorDeclaration>container).body))) {
4849+
(container.kind !== SyntaxKind.Constructor || isNodeDescendantOf(node, (<ConstructorDeclaration>container).body))) {
48504850
return getDeclaredTypeOfClassOrInterface(getSymbolOfNode(parent)).thisType;
48514851
}
48524852
}
@@ -7206,8 +7206,8 @@ namespace ts {
72067206
let container = getContainingClass(node);
72077207
while (container !== undefined) {
72087208
if (container === localOrExportSymbol.valueDeclaration && container.name !== node) {
7209-
getNodeLinks(container).flags |= NodeCheckFlags.ClassWithBodyScopedClassBinding;
7210-
getNodeLinks(node).flags |= NodeCheckFlags.BodyScopedClassBinding;
7209+
getNodeLinks(container).flags |= NodeCheckFlags.DecoratedClassWithSelfReference;
7210+
getNodeLinks(node).flags |= NodeCheckFlags.SelfReferenceInDecoratedClass;
72117211
break;
72127212
}
72137213

@@ -7495,7 +7495,7 @@ namespace ts {
74957495
// This is required for destructuring assignments, as a call expression cannot be used as the target of a destructuring assignment
74967496
// while a property access can.
74977497
if (container.kind === SyntaxKind.MethodDeclaration && container.flags & NodeFlags.Async) {
7498-
if (isSuperPropertyOrElementAccess(node.parent) && isAssignmentTarget(node.parent)) {
7498+
if (isSuperProperty(node.parent) && isAssignmentTarget(node.parent)) {
74997499
getNodeLinks(container).flags |= NodeCheckFlags.AsyncMethodWithSuperBinding;
75007500
}
75017501
else {

src/compiler/commandLineParser.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -327,13 +327,6 @@ namespace ts {
327327
name: "experimentalTransforms",
328328
type: "boolean",
329329
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
337330
}
338331
];
339332

src/compiler/core.ts

Lines changed: 100 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,26 +171,119 @@ namespace ts {
171171
return result;
172172
}
173173

174+
/**
175+
* Flattens an array containing a mix of array or non-array elements.
176+
*
177+
* @param array The array to flatten.
178+
*/
179+
export function flatten<T>(array: (T | T[])[]): T[] {
180+
let result: T[];
181+
if (array) {
182+
result = [];
183+
for (const v of array) {
184+
if (v) {
185+
if (isArray(v)) {
186+
addRange(result, v);
187+
}
188+
else {
189+
result.push(v);
190+
}
191+
}
192+
}
193+
}
194+
195+
return result;
196+
}
197+
174198
/**
175199
* Maps an array. If the mapped value is an array, it is spread into the result.
200+
*
201+
* @param array The array to map.
202+
* @param mapfn The callback used to map the result into one or more values.
176203
*/
177-
export function flatMap<T, U>(array: T[], f: (x: T, i: number) => U | U[]): U[] {
204+
export function flatMap<T, U>(array: T[], mapfn: (x: T, i: number) => U | U[]): U[] {
178205
let result: U[];
179206
if (array) {
180207
result = [];
181208
for (let i = 0; i < array.length; i++) {
182-
const v = array[i];
183-
const ar = f(v, i);
184-
if (ar) {
185-
// We cast to <U> here to leverage the behavior of Array#concat
186-
// which will append a single value here.
187-
result = result.concat(<U[]>ar);
209+
const v = mapfn(array[i], i);
210+
if (v) {
211+
if (isArray(v)) {
212+
addRange(result, v);
213+
}
214+
else {
215+
result.push(v);
216+
}
188217
}
189218
}
190219
}
191220
return result;
192221
}
193222

223+
/**
224+
* Computes the first matching span of elements and returns a tuple of the first span
225+
* and the remaining elements.
226+
*/
227+
export function span<T>(array: T[], f: (x: T, i: number) => boolean): [T[], T[]] {
228+
if (array) {
229+
for (let i = 0; i < array.length; i++) {
230+
if (!f(array[i], i)) {
231+
return [array.slice(0, i), array.slice(i)];
232+
}
233+
}
234+
return [array.slice(0), []];
235+
}
236+
237+
return undefined;
238+
}
239+
240+
/**
241+
* Maps contiguous spans of values with the same key.
242+
*
243+
* @param array The array to map.
244+
* @param keyfn A callback used to select the key for an element.
245+
* @param mapfn A callback used to map a contiguous chunk of values to a single value.
246+
*/
247+
export function spanMap<T, K, U>(array: T[], keyfn: (x: T, i: number) => K, mapfn: (chunk: T[], key: K) => U): U[] {
248+
let result: U[];
249+
if (array) {
250+
result = [];
251+
const len = array.length;
252+
let previousKey: K;
253+
let key: K;
254+
let start = 0;
255+
let pos = 0;
256+
while (start < len) {
257+
while (pos < len) {
258+
const value = array[pos];
259+
key = keyfn(value, pos);
260+
if (pos === 0) {
261+
previousKey = key;
262+
}
263+
else if (key !== previousKey) {
264+
break;
265+
}
266+
267+
pos++;
268+
}
269+
270+
if (start < pos) {
271+
const v = mapfn(array.slice(start, pos), previousKey);
272+
if (v) {
273+
result.push(v);
274+
}
275+
276+
start = pos;
277+
}
278+
279+
previousKey = key;
280+
pos++;
281+
}
282+
}
283+
284+
return result;
285+
}
286+
194287
export function concatenate<T>(array1: T[], array2: T[]): T[] {
195288
if (!array2 || !array2.length) return array1;
196289
if (!array1 || !array1.length) return array2;

src/compiler/emitter.ts

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
346346
};
347347

348348
function isUniqueLocalName(name: string, container: Node): boolean {
349-
for (let node = container; isNodeDescendentOf(node, container); node = node.nextContainer) {
349+
for (let node = container; isNodeDescendantOf(node, container); node = node.nextContainer) {
350350
if (node.locals && hasProperty(node.locals, name)) {
351351
// We conservatively include alias symbols to cover cases where they're emitted as locals
352352
if (node.locals[name].flags & (SymbolFlags.Value | SymbolFlags.ExportValue | SymbolFlags.Alias)) {
@@ -1529,7 +1529,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
15291529
return;
15301530
}
15311531
}
1532-
else if (resolver.getNodeCheckFlags(node) & NodeCheckFlags.BodyScopedClassBinding) {
1532+
else if (resolver.getNodeCheckFlags(node) & NodeCheckFlags.SelfReferenceInDecoratedClass) {
15331533
// Due to the emit for class decorators, any reference to the class from inside of the class body
15341534
// must instead be rewritten to point to a temporary variable to avoid issues with the double-bind
15351535
// behavior of class names in ES6.
@@ -1915,9 +1915,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
19151915

19161916
if (multiLine) {
19171917
decreaseIndent();
1918-
if (!compilerOptions.transformCompatibleEmit) {
1919-
writeLine();
1920-
}
19211918
}
19221919

19231920
write(")");
@@ -2237,7 +2234,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
22372234
return forEach(elements, e => e.kind === SyntaxKind.SpreadElementExpression);
22382235
}
22392236

2240-
function skipParentheses(node: Expression): Expression {
2237+
function skipParenthesesAndAssertions(node: Expression): Expression {
22412238
while (node.kind === SyntaxKind.ParenthesizedExpression || node.kind === SyntaxKind.TypeAssertionExpression || node.kind === SyntaxKind.AsExpression) {
22422239
node = (<ParenthesizedExpression | AssertionExpression>node).expression;
22432240
}
@@ -2268,7 +2265,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
22682265

22692266
function emitCallWithSpread(node: CallExpression) {
22702267
let target: Expression;
2271-
const expr = skipParentheses(node.expression);
2268+
const expr = skipParenthesesAndAssertions(node.expression);
22722269
if (expr.kind === SyntaxKind.PropertyAccessExpression) {
22732270
// Target will be emitted as "this" argument
22742271
target = emitCallTarget((<PropertyAccessExpression>expr).expression);
@@ -2342,7 +2339,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
23422339
superCall = true;
23432340
}
23442341
else {
2345-
superCall = isSuperPropertyOrElementAccess(expression);
2342+
superCall = isSuperProperty(expression);
23462343
isAsyncMethodWithSuper = superCall && isInAsyncMethodWithSuperInES6(node);
23472344
emit(expression);
23482345
}
@@ -4334,9 +4331,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
43344331
writeLine();
43354332
emitStart(restParam);
43364333
emitNodeWithCommentsAndWithoutSourcemap(restParam.name);
4337-
write(restIndex > 0 || !compilerOptions.transformCompatibleEmit
4338-
? `[${tempName} - ${restIndex}] = arguments[${tempName}];`
4339-
: `[${tempName}] = arguments[${tempName}];`);
4334+
write(`[${tempName} - ${restIndex}] = arguments[${tempName}];`);
43404335
emitEnd(restParam);
43414336
decreaseIndent();
43424337
writeLine();
@@ -5208,7 +5203,7 @@ const _super = (function (geti, seti) {
52085203
// [Example 4]
52095204
//
52105205

5211-
if (resolver.getNodeCheckFlags(node) & NodeCheckFlags.ClassWithBodyScopedClassBinding) {
5206+
if (resolver.getNodeCheckFlags(node) & NodeCheckFlags.DecoratedClassWithSelfReference) {
52125207
decoratedClassAlias = unescapeIdentifier(makeUniqueName(node.name ? node.name.text : "default"));
52135208
decoratedClassAliases[getNodeId(node)] = decoratedClassAlias;
52145209
write(`let ${decoratedClassAlias};`);
@@ -5356,7 +5351,7 @@ const _super = (function (geti, seti) {
53565351
const isClassExpressionWithStaticProperties = staticProperties.length > 0 && node.kind === SyntaxKind.ClassExpression;
53575352
let tempVariable: Identifier;
53585353

5359-
if (isClassExpressionWithStaticProperties && compilerOptions.transformCompatibleEmit) {
5354+
if (isClassExpressionWithStaticProperties) {
53605355
tempVariable = createAndRecordTempVariable(TempFlags.Auto);
53615356
write("(");
53625357
increaseIndent();
@@ -5393,11 +5388,6 @@ const _super = (function (geti, seti) {
53935388
writeLine();
53945389
emitConstructor(node, baseTypeNode);
53955390
emitMemberFunctionsForES5AndLower(node);
5396-
if (!compilerOptions.transformCompatibleEmit) {
5397-
emitPropertyDeclarations(node, staticProperties);
5398-
writeLine();
5399-
emitDecoratorsOfClass(node, /*decoratedClassAlias*/ undefined);
5400-
}
54015391
writeLine();
54025392
emitToken(SyntaxKind.CloseBraceToken, node.members.end, () => {
54035393
write("return ");
@@ -5424,13 +5414,10 @@ const _super = (function (geti, seti) {
54245414
write("))");
54255415
if (node.kind === SyntaxKind.ClassDeclaration) {
54265416
write(";");
5427-
if (compilerOptions.transformCompatibleEmit) {
5428-
emitPropertyDeclarations(node, staticProperties);
5429-
writeLine();
5430-
emitDecoratorsOfClass(node, /*decoratedClassAlias*/ undefined);
5431-
}
5417+
emitPropertyDeclarations(node, staticProperties);
5418+
emitDecoratorsOfClass(node, /*decoratedClassAlias*/ undefined);
54325419
}
5433-
else if (isClassExpressionWithStaticProperties && compilerOptions.transformCompatibleEmit) {
5420+
else if (isClassExpressionWithStaticProperties) {
54345421
for (const property of staticProperties) {
54355422
write(",");
54365423
writeLine();

0 commit comments

Comments
 (0)
0