8000 Merge branch 'transforms-printer' into transforms-transformer-ts · prmdeveloper/TypeScript@0a325ee · GitHub
[go: up one dir, main page]

Skip to content

Commit 0a325ee

Browse files
committed
Merge branch 'transforms-printer' into transforms-transformer-ts
2 parents c4dc2ae + 64e7aa8 commit 0a325ee

19 files changed

+990
-474
lines changed

Jakefile.js

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

240240
var useDebugMode = true;
241+
var useTransforms = process.env.USE_TRANSFORMS || false;
241242
var host = (process.env.host || process.env.TYPESCRIPT_HOST || "node");
242243
var compilerFilename = "tsc.js";
243244
var LKGCompiler = path.join(LKGDirectory, compilerFilename);
@@ -297,6 +298,10 @@ function compileFile(outFile, sources, prereqs, prefixes, useBuiltCompiler, noOu
297298
options += " --stripInternal"
298299
}
299300

301+
if (useBuiltCompiler && useTransforms) {
302+
options += " --experimentalTransforms"
303+
}
304+
300305
var cmd = host + " " + compilerPath + " " + options + " ";
301306
cmd = cmd + sources.join(" ");
302307
console.log(cmd + "\n");
@@ -420,6 +425,10 @@ task("setDebugMode", function() {
420425
useDebugMode = true;
421426
});
422427

428+
task("setTransforms", function() {
429+
useTransforms = true;
430+
});
431+
423432
task("configure-nightly", [configureNightlyJs], function() {
424433
var cmd = host + " " + configureNightlyJs + " " + packageJson + " " + programTs;
425434
console.log(cmd);

src/compiler/binder.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,7 +1798,7 @@ namespace ts {
17981798
* @param node The node to analyze
17991799
* @param subtreeFlags Transform flags computed for this node's subtree
18001800
*/
1801-
export function computeTransformFlagsForNode(node: Node, subtreeFlags: TransformFlags) {
1801+
export function computeTransformFlagsForNode(node: Node, subtreeFlags: TransformFlags): TransformFlags {
18021802
// Ambient nodes are TypeScript syntax and the flags of their subtree are ignored.
18031803
if (node.flags & NodeFlags.Ambient) {
18041804
return (node.transformFlags = TransformFlags.AssertTypeScript)
@@ -1971,20 +1971,6 @@ namespace ts {
19711971

19721972
break;
19731973

1974-
case SyntaxKind.ExpressionStatement:
1975-
if (nodeIsSynthesized(node)) {
1976-
const expression = (<ExpressionStatement>node).expression;
1977-
if (nodeIsSynthesized(expression)
1978-
&& isCallExpression(expression)
1979-
&& expression.expression.kind === SyntaxKind.SuperKeyword) {
1980-
// A synthesized call to `super` should be transformed to a cleaner emit
1981-
// when transpiling to ES5/3.
1982-
transformFlags |= TransformFlags.AssertES6;
1983-
}
1984-
}
1985-
1986-
break;
1987-
19881974
case SyntaxKind.BinaryExpression:
19891975
if (isDestructuringAssignment(node)) {
19901976
// Destructuring assignments are ES6 syntax.
@@ -2093,7 +2079,7 @@ namespace ts {
20932079
case SyntaxKind.VariableDeclarationList:
20942080
// If a VariableDeclarationList is `let` or `const`, then it is ES6 syntax.
20952081
if (node.flags & NodeFlags.BlockScoped) {
2096-
transformFlags |= TransformFlags.AssertES6;
2082+
transformFlags |= TransformFlags.AssertES6 | TransformFlags.ContainsBlockScopedBinding;
20972083
}
20982084

20992085
break;
@@ -2106,6 +2092,26 @@ namespace ts {
21062092

21072093
break;
21082094

2095+
case SyntaxKind.LabeledStatement:
2096+
// A labeled statement containing a block scoped binding *may* need to be transformed from ES6.
2097+
if (subtreeFlags & TransformFlags.ContainsBlockScopedBinding
2098+
&& isIterationStatement(this, /*lookInLabeledStatements*/ true)) {
2099+
transformFlags |= TransformFlags.AssertES6;
2100+
}
2101+
2102+
break;
2103+
2104+
case SyntaxKind.DoStatement:
2105+
case SyntaxKind.WhileStatement:
2106+
case SyntaxKind.ForStatement:
2107+
case SyntaxKind.ForInStatement:
2108+
// A loop containing a block scoped binding *may* need to be transformed from ES6.
2109+
if (subtreeFlags & TransformFlags.ContainsBlockScopedBinding) {
2110+
transformFlags |= TransformFlags.AssertES6;
2111+
}
2112+
2113+
break;
2114+
21092115
case SyntaxKind.ClassDeclaration:
21102116
case SyntaxKind.ClassExpression:
21112117
// A ClassDeclarations or ClassExpression is ES6 syntax.

src/compiler/commandLineParser.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,11 @@ namespace ts {
320320
name: "allowSyntheticDefaultImports",
321321
type: "boolean",
322322
description: Diagnostics.Allow_default_imports_from_modules_with_no_default_export_This_does_not_affect_code_emit_just_typechecking
323+
},
324+
{
325+
name: "experimentalTransforms",
326+
type: "boolean",
327+
experimental: true
323328
}
324329
];
325330

src/compiler/comments.ts

Lines changed: 86 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ namespace ts {
55
export interface CommentWriter {
66
reset(): void;
77
setSourceFile(sourceFile: SourceFile): void;
8-
getLeadingCommentsToEmit(node: TextRange): CommentRange[];
9-
getTrailingCommentsToEmit(node: TextRange): CommentRange[];
10-
emitDetachedComments(node: TextRange): void;
11-
emitLeadingComments(node: TextRange, comments?: CommentRange[]): void;
12-
emitTrailingComments(node: TextRange, comments?: CommentRange[]): void;
8+
getLeadingComments(range: Node, getAdditionalRange?: (range: Node) => Node): CommentRange[];
9+
getLeadingComments(range: TextRange): CommentRange[];
10+
getLeadingCommentsOfPosition(pos: number): CommentRange[];
11+
getTrailingComments(range: Node, getAdditionalRange?: (range: Node) => Node): CommentRange[];
12+
getTrailingComments(range: TextRange): CommentRange[];
13+
getTrailingCommentsOfPosition(pos: number): CommentRange[];
14+
emitLeadingComments(range: TextRange, comments?: CommentRange[]): void;
15+
emitTrailingComments(range: TextRange, comments?: CommentRange[]): void;
16+
emitDetachedComments(range: TextRange): void;
1317
}
1418

1519
export function createCommentWriter(host: EmitHost, writer: EmitTextWriter, sourceMap: SourceMapWriter): CommentWriter {
@@ -25,8 +29,8 @@ namespace ts {
2529
// This maps start->end for a comment range. See `hasConsumedCommentRange` and
2630
// `consumeCommentRange` for usage.
2731
let consumedCommentRanges: number[];
28-
let leadingCommentRangeNodeStarts: boolean[];
29-
let trailingCommentRangeNodeEnds: boolean[];
32+
let leadingCommentRangePositions: boolean[];
33+
let trailingCommentRangePositions: boolean[];
3034

3135
return compilerOptions.removeComments
3236
? createCommentRemovingWriter()
@@ -36,11 +40,13 @@ namespace ts {
3640
return {
3741
reset,
3842
setSourceFile,
39-
getLeadingCommentsToEmit(node: TextRange): CommentRange[] { return undefined; },
40-
getTrailingCommentsToEmit(node: TextRange): CommentRange[] { return undefined; },
43+
getLeadingComments(range: TextRange, getAdditionalRange?: (range: TextRange) => TextRange): CommentRange[] { return undefined; },
44+
getLeadingCommentsOfPosition(pos: number): CommentRange[] { return undefined; },
45+
getTrailingComments(range: TextRange, getAdditionalRange?: (range: TextRange) => TextRange): CommentRange[] { return undefined; },
46+
getTrailingCommentsOfPosition(pos: number): CommentRange[] { return undefined; },
47+
emitLeadingComments(range: TextRange, comments?: CommentRange[]): void { },
48+
emitTrailingComments(range: TextRange, comments?: CommentRange[]): void { },
4149
emitDetachedComments,
42-
emitLeadingComments(node: TextRange, comments?: CommentRange[]): void { },
43-
emitTrailingComments(node: TextRange, comments?: CommentRange[]): void { },
4450
};
4551

4652
function emitDetachedComments(node: TextRange): void {
@@ -53,41 +59,85 @@ namespace ts {
5359
return {
5460
reset,
5561
setSourceFile,
56-
getLeadingCommentsToEmit,
57-
getTrailingCommentsToEmit,
58-
emitDetachedComments,
62+
getLeadingComments,
63+
getLeadingCommentsOfPosition,
64+
getTrailingComments,
65+
getTrailingCommentsOfPosition,
5966
emitLeadingComments,
6067
emitTrailingComments,
68+
emitDetachedComments,
6169
};
6270

63-
function getLeadingCommentsToEmit(node: TextRange) {
64-
if (nodeIsSynthesized(node)) {
65-
return;
71+
function getLeadingComments(range: TextRange | Node, getAdditionalRange?: (range: Node) => Node) {
72+
let comments = getLeadingCommentsOfPosition(range.pos);
73+
if (getAdditionalRange) {
74+
let additionalRange = getAdditionalRange(<Node>range);
75+
while (additionalRange) {
76+
comments = concatenate(
77+
getLeadingCommentsOfPosition(additionalRange.pos),
78+
comments
79+
);
80+
81+
additionalRange = getAdditionalRange(additionalRange);
82+
}
6683
}
6784

68-
if (!leadingCommentRangeNodeStarts[node.pos]) {
69-
leadingCommentRangeNodeStarts[node.pos] = true;
70-
const comments = hasDetachedComments(node.pos)
71-
? getLeadingCommentsWithoutDetachedComments()
72-
: getLeadingCommentRanges(currentText, node.pos);
73-
return consumeCommentRanges(comments);
85+
return comments;
86+
}
87+
88+
function getTrailingComments(range: TextRange | Node, getAdditionalRange?: (range: Node) => Node) {
89+
let comments = getTrailingCommentsOfPosition(range.end);
90+
if (getAdditionalRange) {
91+
let additionalRange = getAdditionalRange(<Node>range);
92+
while (additionalRange) {
93+
comments = concatenate(
94+
comments,
95+
getTrailingCommentsOfPosition(additionalRange.end)
96+
);
97+
98+
additionalRange = getAdditionalRange(additionalRange);
99+
}
74100
}
75101

76-
return noComments;
102+
return comments;
77103
}
78104

79-
function getTrailingCommentsToEmit(node: TextRange) {
80-
if (nodeIsSynthesized(node)) {
81-
return;
105+
function getLeadingCommentsOfPosition(pos: number) {
106+
if (positionIsSynthesized(pos) || leadingCommentRangePositions[pos]) {
107+
return undefined;
82108
}
83109

84-
if (!trailingCommentRangeNodeEnds[node.end]) {
85-
trailingCommentRangeNodeEnds[node.end] = true;
86-
const comments = getTrailingCommentRanges(currentText, node.end);
87-
return consumeCommentRanges(comments);
110+
leadingCommentRangePositions[pos] = true;
111+
const comments = hasDetachedComments(pos)
112+
? getLeadingCommentsWithoutDetachedComments()
113+
: getLeadingCommentRanges(currentText, pos);
114+
return consumeCommentRanges(comments);
115+
}
116+
117+
function getTrailingCommentsOfPosition(pos: number) {
118+
if (positionIsSynthesized(pos) || trailingCommentRangePositions[pos]) {
119+
return undefined;
88120
}
89121

90-
return noComments;
122+
trailingCommentRangePositions[pos] = true;
123+
const comments = getTrailingCommentRanges(currentText, pos);
124+
return consumeCommentRanges(comments);
125+
}
126+
127+
function emitLeadingComments(range: TextRange, comments = getLeadingComments(range)) {
128+
emitNewLineBeforeLeadingComments(currentLineMap, writer, range, comments);< 10000 /div>
129+
130+
// Leading comments are emitted at /*leading comment1 */space/*leading comment*/space
131+
emitComments(currentText, currentLineMap, writer, comments, /*leadingSeparator*/ false, /*trailingSeparator*/ true, newLine, writeComment);
132+
}
133+
134+
function emitTrailingComments(range: TextRange, comments = getTrailingComments(range)) {
135+
// trailing comments are emitted at space/*trailing comment1 */space/*trailing comment*/
136+
emitComments(currentText, currentLineMap, writer, comments, /*leadingSeparator*/ true, /*trailingSeparator*/ false, newLine, writeComment);
137+
}
138+
139+
function emitDetachedComments(range: TextRange) {
140+
emitDetachedCommentsAndUpdateCommentsInfo(range, /*removeComments*/ false);
91141
}
92142

93143
function hasConsumedCommentRange(comment: CommentRange) {
@@ -136,22 +186,6 @@ namespace ts {
136186

137187
return noComments;
138188
}
139-
140-
function emitLeadingComments(range: TextRange, leadingComments: CommentRange[] = getLeadingCommentsToEmit(range)) {
141-
emitNewLineBeforeLeadingComments(currentLineMap, writer, range, leadingComments);
142-
143-
// Leading comments are emitted at /*leading comment1 */space/*leading comment*/space
144-
emitComments(currentText, currentLineMap, writer, leadingComments, /*trailingSeparator*/ true, newLine, writeComment);
145-
}
146-
147-
function emitTrailingComments(range: TextRange, trailingComments = getTrailingCommentsToEmit(range)) {
148-
// trailing comments are emitted at space/*trailing comment1 */space/*trailing comment*/
149-
emitComments(currentText, currentLineMap, writer, trailingComments, /*trailingSeparator*/ false, newLine, writeComment);
150-
}
151-
152-
function emitDetachedComments(range: TextRange) {
153-
emitDetachedCommentsAndUpdateCommentsInfo(range, /*removeComments*/ false);
154-
}
155189
}
156190

157191
function reset() {
@@ -160,8 +194,8 @@ namespace ts {
160194
currentLineMap = undefined;
161195
detachedCommentsInfo = undefined;
162196
consumedCommentRanges = undefined;
163-
trailingCommentRangeNodeEnds = undefined;
164-
leadingCommentRangeNodeStarts = undefined;
197+
trailingCommentRangePositions = undefined;
198+
leadingCommentRangePositions = undefined;
165199
}
166200

167201
function setSourceFile(sourceFile: SourceFile) {
@@ -170,8 +204,8 @@ namespace ts {
170204
currentLineMap = getLineStarts(sourceFile);
171205
detachedCommentsInfo = undefined;
172206
consumedCommentRanges = [];
173-
leadingCommentRangeNodeStarts = [];
174-
trailingCommentRangeNodeEnds = [];
207+
leadingCommentRangePositions = [];
208+
trailingCommentRangePositions = [];
175209
}
176210

177211
function hasDetachedComments(pos: number) {

0 commit comments

Comments
 (0)
0