Skip to content

Commit 6d27336

Browse files
committed
Merged some changes from other branches.
1 parent 90a317f commit 6d27336

9 files changed

Lines changed: 432 additions & 219 deletions

File tree

Jakefile.js

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

224224
var useDebugMode = true;
225+
var useTransforms = process.env.USE_TRANSFORMS || false;
225226
var host = (process.env.host || process.env.TYPESCRIPT_HOST || "node");
226227
var compilerFilename = "tsc.js";
227228
var LKGCompiler = path.join(LKGDirectory, compilerFilename);
@@ -281,6 +282,10 @@ function compileFile(outFile, sources, prereqs, prefixes, useBuiltCompiler, noOu
281282
options += " --stripInternal"
282283
}
283284

285+
if (useBuiltCompiler && useTransforms) {
286+
options += " --experimentalTransforms"
287+
}
288+
284289
var cmd = host + " " + compilerPath + " " + options + " ";
285290
cmd = cmd + sources.join(" ");
286291
console.log(cmd + "\n");
@@ -404,6 +409,10 @@ task("setDebugMode", function() {
404409
useDebugMode = true;
405410
});
406411

412+
task("setTransforms", function() {
413+
useTransforms = true;
414+
});
415+
407416
task("configure-nightly", [configureNightlyJs], function() {
408417
var cmd = host + " " + configureNightlyJs + " " + packageJson + " " + programTs;
409418
console.log(cmd);

src/compiler/comments.ts

Lines changed: 87 additions & 53 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);
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 as `/*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 as `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) {
@@ -182,7 +216,7 @@ namespace ts {
182216
// get the leading comments from detachedPos
183217
const pos = lastOrUndefined(detachedCommentsInfo).detachedCommentEndPos;
184218
const leadingComments = getLeadingCommentRanges(currentText, pos);
185-
if (detachedCommentsInfo.length > 1) {
219+
if (detachedCommentsInfo.length - 1) {
186220
detachedCommentsInfo.pop();
187221
}
188222
else {

src/compiler/declarationEmitter.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ namespace ts {
9191
// Emit reference in dts, if the file reference was not already emitted
9292
if (referencedFile && !contains(emittedReferencedFiles, referencedFile)) {
9393
// Add a reference to generated dts file,
94-
// global file reference is added only
94+
// global file reference is added only
9595
// - if it is not bundled emit (because otherwise it would be self reference)
9696
// - and it is not already added
9797
if (writeReferencePath(referencedFile, !isBundledEmit && !addedGlobalFileReference)) {
@@ -144,7 +144,7 @@ namespace ts {
144144

145145
if (!isBundledEmit && isExternalModule(sourceFile) && sourceFile.moduleAugmentations.length && !resultHasExternalModuleIndicator) {
146146
// if file was external module with augmentations - this fact should be preserved in .d.ts as well.
147-
// in case if we didn't write any external module specifiers in .d.ts we need to emit something
147+
// in case if we didn't write any external module specifiers in .d.ts we need to emit something
148148
// that will force compiler to think that this file is an external module - 'export {}' is a reasonable choice here.
149149
write("export {};");
150150
writeLine();
@@ -349,7 +349,7 @@ namespace ts {
349349
const jsDocComments = getJsDocCommentsFromText(declaration, currentText);
350350
emitNewLineBeforeLeadingComments(currentLineMap, writer, declaration, jsDocComments);
351351
// jsDoc comments are emitted at /*leading comment1 */space/*leading comment*/space
352-
emitComments(currentText, currentLineMap, writer, jsDocComments, /*trailingSeparator*/ true, newLine, writeCommentRange);
352+
emitComments(currentText, currentLineMap, writer, jsDocComments, /*leadingSeparator*/ false, /*trailingSeparator*/ true, newLine, writeCommentRange);
353353
}
354354
}
355355

@@ -736,7 +736,7 @@ namespace ts {
736736

737737
function emitExternalModuleSpecifier(parent: ImportEqualsDeclaration | ImportDeclaration | ExportDeclaration | ModuleDeclaration) {
738738
// emitExternalModuleSpecifier is usually called when we emit something in the.d.ts file that will make it an external module (i.e. import/export declarations).
739-
// the only case when it is not true is when we call it to emit correct name for module augmentation - d.ts files with just module augmentations are not considered
739+
// the only case when it is not true is when we call it to emit correct name for module augmentation - d.ts files with just module augmentations are not considered
740740
// external modules since they are indistingushable from script files with ambient modules. To fix this in such d.ts files we'll emit top level 'export {}'
741741
// so compiler will treat them as external modules.
742742
resultHasExternalModuleIndicator = resultHasExternalModuleIndicator || parent.kind !== SyntaxKind.ModuleDeclaration;

0 commit comments

Comments
 (0)