Skip to content

Adds the transformFiles API for tree transformations #6986

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 109 commits into from
Mar 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
109 commits
Select commit Hold shift + click to select a range
1e35593
Adds the transformFiles API
rbuckton Feb 5, 2016
cbb910a
Merge branch 'transforms-flags' into transforms-transformer
rbuckton Feb 8, 2016
6fa4002
Merge branch 'transforms-flags' into transforms-transformer
rbuckton Feb 9, 2016
0f2bbb1
Moved LexicalEnvironment to types.ts, minor fixes
rbuckton Feb 9, 2016
6b381ec
Added printer
rbuckton Feb 9, 2016
fb19e81
Fixed linter error
rbuckton Feb 9, 2016
e545f1b
Merge branch 'transforms-transformer' into transforms-printer
rbuckton Feb 9, 2016
49d2d93
Adds destructuring logic and placeholders for each transformer.
rbuckton Feb 9, 2016
f948b14
Adds the TypeScript transformer
rbuckton Feb 12, 2016
ceae78b
Merge branch 'transforms-flags' into transforms-transformer
rbuckton Feb 12, 2016
f8ed021
PR feedback and cleanup
rbuckton Feb 13, 2016
ab811f9
Updates to TransformationContext and transformer.ts
rbuckton Feb 13, 2016
c634a36
Merge branch 'transforms-transformer' into transforms-printer
rbuckton Feb 13, 2016
0f16e68
PR Feedback
rbuckton Feb 17, 2016
8ec3932
Merge branch 'transforms-transformer' into transforms-printer
rbuckton Feb 17, 2016
357171f
PR feedback, switched to getEmitScriptTarget/getEmitModuleKind
rbuckton Feb 17, 2016
c4dc2ae
Merge branch 'transforms-transformer' into transforms-transformer-ts
rbuckton Feb 17, 2016
6c0551f
Adds initial jsx transformer
rbuckton Feb 17, 2016
1ceb02a
Added initial ES7 transformer
rbuckton Feb 17, 2016
dd2dc78
Enables source maps for exceptions thrown in the compiler (when avail…
rbuckton Feb 20, 2016
c759b63
Adds ES6 transformer
rbuckton Feb 20, 2016
39628d7
Added CommonJS/AMD/UMD module transformer
rbuckton Feb 20, 2016
5564537
Added initial SystemJS transformer.
rbuckton Feb 22, 2016
024eff1
Merge branch 'transforms-flags' into transforms-transformer
rbuckton Feb 22, 2016
11d54ba
Merged some changes from other branches.
rbuckton Feb 22, 2016
8e0d28e
Merge branch 'transforms-flags' into transforms-transformer
rbuckton Feb 22, 2016
7d72c18
Merge branch 'transforms-flags' into transforms-transformer
rbuckton Feb 22, 2016
90a317f
Merge branch 'transforms-transformer' into transforms-printer
rbuckton Feb 22, 2016
6d27336
Merged some changes from other branches.
rbuckton Feb 23, 2016
f52a30b
Merged some changes from other branches.
rbuckton Feb 23, 2016
64e7aa8
PR Feedback.
rbuckton Feb 23, 2016
0a325ee
Merge branch 'transforms-printer' into transforms-transformer-ts
rbuckton Feb 23, 2016
3d8cb51
Merge branch 'transforms-transformer-ts' into transforms-transformer-jsx
rbuckton Feb 23, 2016
0145009
Merge branch 'transforms-transformer-jsx' into transforms-transformer…
rbuckton Feb 23, 2016
dc0b043
Merge branch 'transforms-transformer-es7' into transforms-transformer…
rbuckton Feb 23, 2016
0b64048
Merge branch 'transforms-transformer-es6' into transforms-transformer…
rbuckton Feb 23, 2016
f3179da
Fixed issue with test dependencies
rbuckton Feb 23, 2016
617e511
Merge branch 'transforms-transformer' into transforms-printer
rbuckton Feb 23, 2016
61fe61b
Merge branch 'transforms-printer' into transforms-transformer-ts
rbuckton Feb 23, 2016
c166a2d
Merge branch 'transforms-transformer-ts' into transforms-transformer-jsx
rbuckton Feb 23, 2016
5b7bd63
Merge branch 'transforms-transformer-jsx' into transforms-transformer…
rbuckton Feb 23, 2016
6d111e3
Merge branch 'transforms-transformer-es7' into transforms-transformer…
rbuckton Feb 23, 2016
4a81dde
Fixed issue with smartindenter
rbuckton Feb 23, 2016
2e47e2e
Merge branch 'transforms-transformer' into transforms-printer
rbuckton Feb 23, 2016
e5cd8fe
Added temporary compat flag to control emit output to fix tests
rbuckton Feb 23, 2016
b3adade
PR Feedback
rbuckton Feb 23, 2016
61f3ba6
Merge branch 'transforms-transformer' into transforms-printer
rbuckton Feb 23, 2016
ad314b0
Merge branch 'transforms-printer' into transforms-transformer-ts
rbuckton Feb 23, 2016
0937cec
Added additional auto-parenthesizing to visitor.
rbuckton Feb 23, 2016
98ab964
Merge branch 'transforms-transformer' into transforms-printer
rbuckton Feb 23, 2016
52c62d0
Merge branch 'transforms-printer' into transforms-transformer-ts
rbuckton Feb 23, 2016
f484ff4
Reenabled transforms.
rbuckton Feb 23, 2016
635347e
Fixed typo in visitEachChild
rbuckton Feb 23, 2016
9871d3b
Merge branch 'transforms-transformer' into transforms-printer
rbuckton Feb 23, 2016
de9866f
Merge branch 'transforms-printer' into transforms-transformer-ts
rbuckton Feb 23, 2016
02b85f8
PR feedback
rbuckton Feb 23, 2016
f92c24b
Removed unused liftNode function
rbuckton Feb 23, 2016
40b61fb
Merge branch 'transforms-transformer' into transforms-printer
rbuckton Feb 23, 2016
4f03b31
Updated lift behavior
rbuckton Feb 23, 2016
75b2181
Merge branch 'transforms-transformer' into transforms-printer
rbuckton Feb 23, 2016
951ce55
Merge branch 'transforms-printer' into transforms-transformer-ts
rbuckton Feb 23, 2016
08036b7
Fixed linter warnings, added comments and minor cleanup.
rbuckton Feb 24, 2016
608822d
Merge branch 'transforms-transformer-ts' into transforms-transformer-jsx
rbuckton Feb 24, 2016
c9f5253
Fixed linter warnings
rbuckton Feb 24, 2016
25f4102
Reverted change to emitter to force use of printFiles
rbuckton Feb 24, 2016
cde3b34
Merge branch 'transforms-transformer-ts' into transforms-transformer-jsx
rbuckton Feb 24, 2016
5a9b131
Merge branch 'transforms-transformer-jsx' into transforms-transformer…
rbuckton Feb 24, 2016
78dfab8
Merge branch 'transforms-transformer-es7' into transforms-transformer…
rbuckton Feb 24, 2016
0d8e152
Merge branch 'transforms-transformer-es6' into transforms-transformer…
rbuckton Feb 24, 2016
72bfd2f
Fixed linter warnings.
rbuckton Feb 24, 2016
cd2cf7d
PR Feedback and defer makeUniqueName/getGeneratedNameForNode to printer.
rbuckton Feb 29, 2016
b1d8828
Remove transformCompatibleEmit flag and make it the default.
rbuckton Feb 29, 2016
8e5e5f8
Merge branch 'transforms-printer' into transforms-transformer-ts
rbuckton Feb 29, 2016
359875b
PR Feedback
rbuckton Feb 29, 2016
fe7ad5f
Minor tweaks to naming
rbuckton Feb 29, 2016
a0dbe76
PR Feedback
rbuckton Mar 1, 2016
a7f9cda
Merge branch 'transforms-transformer-ts' into transforms-transformer-jsx
rbuckton Mar 1, 2016
7d05ba2
Fixed visitJsxText, plus PR Feedback
rbuckton Mar 1, 2016
186f5c8
PR Feedback
rbuckton Mar 1, 2016
2c9cd2e
Merge branch 'transforms-transformer-jsx' into transforms-transformer…
rbuckton Mar 1, 2016
88e1772
Merge branch 'transforms-transformer-es7' into transforms-transformer…
rbuckton Mar 1, 2016
30433c2
ES6 cleanup
rbuckton Mar 2, 2016
2d2709f
Fixed typo in visitCallExpression
rbuckton Mar 2, 2016
70cbb9b
Merge branch 'transforms-transformer-es6' into transforms-transformer…
rbuckton Mar 2, 2016
ebb4764
Switch from onBefore/onAfterEmitNode to onEmitNode
rbuckton Mar 2, 2016
e66e51d
Merge branch 'transforms-printer' into transforms-transformer-ts
rbuckton Mar 2, 2016
8b35af4
Switched to onEmitNode
rbuckton Mar 2, 2016
1fdaf74
Merge branch 'transforms-transformer-ts' into transforms-transformer-jsx
rbuckton Mar 2, 2016
593fbd4
Merge branch 'transforms-transformer-jsx' into transforms-transformer…
rbuckton Mar 2, 2016
7b28b48
Merge branch 'transforms-transformer-es7' into transforms-transformer…
rbuckton Mar 2, 2016
1c73818
Switched to onEmitNode
rbuckton Mar 2, 2016
c4a75ba
Merge branch 'transforms-transformer-es6' into transforms-transformer…
rbuckton Mar 2, 2016
72eebdb
Switched to onEmitNode
rbuckton Mar 2, 2016
1cf183b
Fixed invalid assertion in ts transformer
rbuckton Mar 4, 2016
d89e21a
General fixes and cleanup
rbuckton Mar 8, 2016
5b8cf96
Moved modifier related flags to separate enum
rbuckton Mar 10, 2016
99e6ad8
Removed ModifiersArray
rbuckton Mar 10, 2016
47cdfbe
Add support for array return values from visitors
rbuckton Mar 10, 2016
2699bf9
Removed NodeArrayNode in favor of arrays
rbuckton Mar 10, 2016
94018a1
cleaned up assertions and removed flattenNodes.
rbuckton Mar 10, 2016
ad0dd4e
Fixed perf issue in binder, plus PR feedback
rbuckton Mar 18, 2016
44ca7d4
Updated baselines
rbuckton Mar 18, 2016
ae7843d
PR Feedback
rbuckton Mar 18, 2016
3c5f170
Merge pull request #7206 from Microsoft/transforms-transformer-module
rbuckton Mar 18, 2016
07a3d18
Merge pull request #7194 from Microsoft/transforms-transformer-es6
rbuckton Mar 18, 2016
cc00f4c
Merge pull request #7193 from Microsoft/transforms-transformer-es7
rbuckton Mar 18, 2016
829a9df
Merge pull request #7192 from Microsoft/transforms-transformer-jsx
rbuckton Mar 18, 2016
ca9148e
Merge pull request #7191 from Microsoft/transforms-transformer-ts
rbuckton Mar 18, 2016
85ae53d
Merge pull request #6987 from Microsoft/transforms-printer
rbuckton Mar 18, 2016
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
31 changes: 31 additions & 0 deletions Jakefile.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,18 @@ var compilerSources = [
"checker.ts",
"factory.ts",
"visitor.ts",
"transformers/destructuring.ts",
"transformers/ts.ts",
"transformers/module/es6.ts",
"transformers/module/system.ts",
"transformers/module/module.ts",
"transformers/jsx.ts",
"transformers/es7.ts",
"transformers/es6.ts",
"transformer.ts",
"sourcemap.ts",
"comments.ts",
"printer.ts",
"declarationEmitter.ts",
"emitter.ts",
"program.ts",
Expand All @@ -64,7 +75,18 @@ var servicesSources = [
"checker.ts",
"factory.ts",
"visitor.ts",
"transformers/destructuring.ts",
"transformers/ts.ts",
"transformers/module/es6.ts",
"transformers/module/system.ts",
"transformers/module/module.ts",
"transformers/jsx.ts",
"transformers/es7.ts",
"transformers/es6.ts",
"transformer.ts",
"sourcemap.ts",
"comments.ts",
"printer.ts",
"declarationEmitter.ts",
"emitter.ts",
"program.ts",
Expand Down Expand Up @@ -216,6 +238,7 @@ function concatenateFiles(destinationFile, sourceFiles) {
}

var useDebugMode = true;
var useTransforms = process.env.USE_TRANSFORMS || false;
var host = (process.env.host || process.env.TYPESCRIPT_HOST || "node");
var compilerFilename = "tsc.js";
var LKGCompiler = path.join(LKGDirectory, compilerFilename);
Expand Down Expand Up @@ -275,6 +298,10 @@ function compileFile(outFile, sources, prereqs, prefixes, useBuiltCompiler, noOu
options += " --stripInternal"
}

if (useBuiltCompiler && useTransforms) {
options += " --experimentalTransforms"
}

var cmd = host + " " + compilerPath + " " + options + " ";
cmd = cmd + sources.join(" ");
console.log(cmd + "\n");
Expand Down Expand Up @@ -398,6 +425,10 @@ task("setDebugMode", function() {
useDebugMode = true;
});

task("setTransforms", function() {
useTransforms = true;
});

task("configure-nightly", [configureNightlyJs], function() {
var cmd = host + " " + configureNightlyJs + " " + packageJson + " " + programTs;
console.log(cmd);
Expand Down
529 changes: 316 additions & 213 deletions src/compiler/binder.ts

Large diffs are not rendered by default.

261 changes: 133 additions & 128 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,13 @@ namespace ts {
name: "allowSyntheticDefaultImports",
type: "boolean",
description: Diagnostics.Allow_default_imports_from_modules_with_no_default_export_This_does_not_affect_code_emit_just_typechecking
},
{
// this option will be removed when this is merged with master and exists solely
// to enable the tree transforming emitter side-by-side with the existing emitter.
name: "experimentalTransforms",
type: "boolean",
experimental: true
}
];

Expand Down
248 changes: 248 additions & 0 deletions src/compiler/comments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
/// <reference path="sourcemap.ts" />

/* @internal */
namespace ts {
export interface CommentWriter {
reset(): void;
setSourceFile(sourceFile: SourceFile): void;
getLeadingComments(range: Node, getAdditionalRange?: (range: Node) => Node): CommentRange[];
getLeadingComments(range: TextRange): CommentRange[];
getLeadingCommentsOfPosition(pos: number): CommentRange[];
getTrailingComments(range: Node, getAdditionalRange?: (range: Node) => Node): CommentRange[];
getTrailingComments(range: TextRange): CommentRange[];
getTrailingCommentsOfPosition(pos: number): CommentRange[];
emitLeadingComments(range: TextRange, comments?: CommentRange[]): void;
emitTrailingComments(range: TextRange, comments?: CommentRange[]): void;
emitDetachedComments(range: TextRange): void;
}

export function createCommentWriter(host: EmitHost, writer: EmitTextWriter, sourceMap: SourceMapWriter): CommentWriter {
const compilerOptions = host.getCompilerOptions();
const newLine = host.getNewLine();
const { emitPos } = sourceMap;

let currentSourceFile: SourceFile;
let currentText: string;
let currentLineMap: number[];
let detachedCommentsInfo: { nodePos: number, detachedCommentEndPos: number}[];

// This maps start->end for a comment range. See `hasConsumedCommentRange` and
// `consumeCommentRange` for usage.
let consumedCommentRanges: number[];
let leadingCommentRangePositions: boolean[];
let trailingCommentRangePositions: boolean[];

return compilerOptions.removeComments
? createCommentRemovingWriter()
: createCommentPreservingWriter();

function createCommentRemovingWriter(): CommentWriter {
return {
reset,
setSourceFile,
getLeadingComments(range: TextRange, getAdditionalRange?: (range: TextRange) => TextRange): CommentRange[] { return undefined; },
getLeadingCommentsOfPosition(pos: number): CommentRange[] { return undefined; },
getTrailingComments(range: TextRange, getAdditionalRange?: (range: TextRange) => TextRange): CommentRange[] { return undefined; },
getTrailingCommentsOfPosition(pos: number): CommentRange[] { return undefined; },
emitLeadingComments(range: TextRange, comments?: CommentRange[]): void { },
emitTrailingComments(range: TextRange, comments?: CommentRange[]): void { },
emitDetachedComments,
};

function emitDetachedComments(node: TextRange): void {
emitDetachedCommentsAndUpdateCommentsInfo(node, /*removeComments*/ true);
}
}

function createCommentPreservingWriter(): CommentWriter {
const noComments: CommentRange[] = [];
return {
reset,
setSourceFile,
getLeadingComments,
getLeadingCommentsOfPosition,
getTrailingComments,
getTrailingCommentsOfPosition,
emitLeadingComments,
emitTrailingComments,
emitDetachedComments,
};

function getLeadingComments(range: TextRange | Node, getAdditionalRange?: (range: Node) => Node) {
let comments = getLeadingCommentsOfPosition(range.pos);
if (getAdditionalRange) {
let additionalRange = getAdditionalRange(<Node>range);
while (additionalRange) {
comments = concatenate(
getLeadingCommentsOfPosition(additionalRange.pos),
comments
);

additionalRange = getAdditionalRange(additionalRange);
}
}

return comments;
}

function getTrailingComments(range: TextRange | Node, getAdditionalRange?: (range: Node) => Node) {
let comments = getTrailingCommentsOfPosition(range.end);
if (getAdditionalRange) {
let additionalRange = getAdditionalRange(<Node>range);
while (additionalRange) {
comments = concatenate(
comments,
getTrailingCommentsOfPosition(additionalRange.end)
);

additionalRange = getAdditionalRange(additionalRange);
}
}

return comments;
}

function getLeadingCommentsOfPosition(pos: number) {
if (positionIsSynthesized(pos) || leadingCommentRangePositions[pos]) {
return undefined;
}

leadingCommentRangePositions[pos] = true;
const comments = hasDetachedComments(pos)
? getLeadingCommentsWithoutDetachedComments()
: getLeadingCommentRanges(currentText, pos);
return consumeCommentRanges(comments);
}

function getTrailingCommentsOfPosition(pos: number) {
if (positionIsSynthesized(pos) || trailingCommentRangePositions[pos]) {
return undefined;
}

trailingCommentRangePositions[pos] = true;
const comments = getTrailingCommentRanges(currentText, pos);
return consumeCommentRanges(comments);
}

function emitLeadingComments(range: TextRange, comments = getLeadingComments(range)) {
emitNewLineBeforeLeadingComments(currentLineMap, writer, range, comments);

// Leading comments are emitted at /*leading comment1 */space/*leading comment*/space
emitComments(currentText, currentLineMap, writer, comments, /*leadingSeparator*/ false, /*trailingSeparator*/ true, newLine, writeComment);
}

function emitTrailingComments(range: TextRange, comments = getTrailingComments(range)) {
// trailing comments are emitted at space/*trailing comment1 */space/*trailing comment*/
emitComments(currentText, currentLineMap, writer, comments, /*leadingSeparator*/ true, /*trailingSeparator*/ false, newLine, writeComment);
}

function emitDetachedComments(range: TextRange) {
emitDetachedCommentsAndUpdateCommentsInfo(range, /*removeComments*/ false);
}

function hasConsumedCommentRange(comment: CommentRange) {
return comment.end === consumedCommentRanges[comment.pos];
}

function consumeCommentRange(comment: CommentRange) {
if (!hasConsumedCommentRange(comment)) {
consumedCommentRanges[comment.pos] = comment.end;
return true;
}

return false;
}

function consumeCommentRanges(comments: CommentRange[]) {
let consumed: CommentRange[];
if (comments) {
let commentsSkipped = 0;
let commentsConsumed = 0;
for (let i = 0; i < comments.length; i++) {
const comment = comments[i];
if (consumeCommentRange(comment)) {
commentsConsumed++;
if (commentsSkipped !== 0) {
if (consumed === undefined) {
consumed = [comment];
}
else {
consumed.push(comment);
}
}
}
else {
commentsSkipped++;
if (commentsConsumed !== 0 && consumed === undefined) {
consumed = comments.slice(0, i);
}
}
}

if (commentsConsumed) {
return consumed || comments;
}
}

return noComments;
}
}

function reset() {
currentSourceFile = undefined;
currentText = undefined;
currentLineMap = undefined;
detachedCommentsInfo = undefined;
consumedCommentRanges = undefined;
trailingCommentRangePositions = undefined;
leadingCommentRangePositions = undefined;
}

function setSourceFile(sourceFile: SourceFile) {
currentSourceFile = sourceFile;
currentText = sourceFile.text;
currentLineMap = getLineStarts(sourceFile);
detachedCommentsInfo = undefined;
consumedCommentRanges = [];
leadingCommentRangePositions = [];
trailingCommentRangePositions = [];
}

function hasDetachedComments(pos: number) {
return detachedCommentsInfo !== undefined && lastOrUndefined(detachedCommentsInfo).nodePos === pos;
}

function getLeadingCommentsWithoutDetachedComments() {
// get the leading comments from detachedPos
const pos = lastOrUndefined(detachedCommentsInfo).detachedCommentEndPos;
const leadingComments = getLeadingCommentRanges(currentText, pos);
if (detachedCommentsInfo.length - 1) {
detachedCommentsInfo.pop();
}
else {
detachedCommentsInfo = undefined;
}

return leadingComments;
}

function emitDetachedCommentsAndUpdateCommentsInfo(node: TextRange, removeComments: boolean) {
const currentDetachedCommentInfo = emitDetachedComments(currentText, currentLineMap, writer, writeComment, node, newLine, removeComments);

if (currentDetachedCommentInfo) {
if (detachedCommentsInfo) {
detachedCommentsInfo.push(currentDetachedCommentInfo);
}
else {
detachedCommentsInfo = [currentDetachedCommentInfo];
}
}
}

function writeComment(text: string, lineMap: number[], writer: EmitTextWriter, comment: CommentRange, newLine: string) {
emitPos(comment.pos);
writeCommentRange(text, lineMap, writer, comment, newLine);
emitPos(comment.end);
}
}
}
Loading