Expose transformations as public API

This commit is contained in:
Ron Buckton
2017-02-07 14:36:15 -08:00
parent a7728f8fa1
commit 2f624f5df3
25 changed files with 459 additions and 77 deletions

View File

@@ -77,8 +77,8 @@
"../services/codefixes/helpers.ts",
"../services/codefixes/importFixes.ts",
"../services/codefixes/unusedIdentifierFixes.ts",
"../services/harness.ts",
"harness.ts",
"sourceMapRecorder.ts",
"harnessLanguageService.ts",
"fourslash.ts",
@@ -119,6 +119,8 @@
"./unittests/compileOnSave.ts",
"./unittests/typingsInstaller.ts",
"./unittests/projectErrors.ts",
"./unittests/printer.ts"
"./unittests/printer.ts",
"./unittests/transform.ts",
"./unittests/customTransforms.ts"
]
}

View File

@@ -0,0 +1,86 @@
/// <reference path="..\..\compiler\emitter.ts" />
/// <reference path="..\harness.ts" />
namespace ts {
describe("customTransforms", () => {
function emitsCorrectly(name: string, sources: { file: string, text: string }[], customTransformers: CustomTransformers) {
it(name, () => {
const roots = sources.map(source => createSourceFile(source.file, source.text, ScriptTarget.ES2015));
const fileMap = arrayToMap(roots, file => file.fileName);
const outputs = createMap<string>();
const options: CompilerOptions = {};
const host: CompilerHost = {
getSourceFile: (fileName) => fileMap.get(fileName),
getDefaultLibFileName: () => "lib.d.ts",
getCurrentDirectory: () => "",
getDirectories: () => [],
getCanonicalFileName: (fileName) => fileName,
useCaseSensitiveFileNames: () => true,
getNewLine: () => "\n",
fileExists: (fileName) => fileMap.has(fileName),
readFile: (fileName) => fileMap.has(fileName) ? fileMap.get(fileName).text : undefined,
writeFile: (fileName, text) => outputs.set(fileName, text),
};
const program = createProgram(arrayFrom(fileMap.keys()), options, host);
program.emit(/*targetSourceFile*/ undefined, host.writeFile, /*cancellationToken*/ undefined, /*emitOnlyDtsFiles*/ false, customTransformers);
Harness.Baseline.runBaseline(`customTransforms/${name}.js`, () => {
let content = "";
for (const [file, text] of arrayFrom(outputs.entries())) {
if (content) content += "\n\n";
content += `// [${file}]\n`;
content += text;
}
return content;
});
});
}
const sources = [{
file: "source.ts",
text: `
function f1() { }
class c() { }
enum e { }
// leading
function f2() { } // trailing
`
}];
const before: Transformer = context => {
return file => visitEachChild(file, visit, context);
function visit(node: Node): VisitResult<Node> {
switch (node.kind) {
case SyntaxKind.FunctionDeclaration:
return visitFunction(<FunctionDeclaration>node);
default:
return visitEachChild(node, visit, context);
}
}
function visitFunction(node: FunctionDeclaration) {
addSyntheticLeadingComment(node, SyntaxKind.MultiLineCommentTrivia, "@before", /*hasTrailingNewLine*/ true);
return node;
}
};
const after: Transformer = context => {
return file => visitEachChild(file, visit, context);
function visit(node: Node): VisitResult<Node> {
switch (node.kind) {
case SyntaxKind.VariableStatement:
return visitVariableStatement(<VariableStatement>node);
default:
return visitEachChild(node, visit, context);
}
}
function visitVariableStatement(node: VariableStatement) {
addSyntheticLeadingComment(node, SyntaxKind.SingleLineCommentTrivia, "@after");
return node;
}
};
emitsCorrectly("before", sources, { before: [before] });
emitsCorrectly("after", sources, { after: [after] });
emitsCorrectly("both", sources, { before: [before], after: [after] });
});
}

View File

@@ -0,0 +1,43 @@
/// <reference path="..\..\services\transform.ts" />
/// <reference path="..\harness.ts" />
namespace ts {
describe("TransformAPI", () => {
function transformsCorrectly(name: string, source: string, transformers: Transformer[]) {
it(name, () => {
Harness.Baseline.runBaseline(`transformApi/transformsCorrectly.${name}.js`, () => {
const transformed = transform(createSourceFile("source.ts", source, ScriptTarget.ES2015), transformers);
const printer = createPrinter({ newLine: NewLineKind.CarriageReturnLineFeed }, {
onEmitNode: transformed.emitNodeWithNotification,
onSubstituteNode: transformed.emitNodeWithSubstitution
});
const result = printer.printBundle(createBundle(transformed.transformed));
transformed.dispose();
return result;
});
});
}
transformsCorrectly("substitution", `
var a = undefined;
`, [
context => {
const previousOnSubstituteNode = context.onSubstituteNode;
context.enableSubstitution(SyntaxKind.Identifier);
context.onSubstituteNode = (hint, node) => {
node = previousOnSubstituteNode(hint, node);
if (hint === EmitHint.Expression && node.kind === SyntaxKind.Identifier && (<Identifier>node).text === "undefined") {
node = createPartiallyEmittedExpression(
addSyntheticTrailingComment(
setTextRange(
createVoidZero(),
node),
SyntaxKind.MultiLineCommentTrivia, "undefined"));
}
return node;
};
return file => file;
}
]);
});
}