Skip to content

Commit 1f815b6

Browse files
committed
add unit test
1 parent 8ee5610 commit 1f815b6

File tree

2 files changed

+67
-40
lines changed

2 files changed

+67
-40
lines changed

src/harness/unittests/transform.ts

Lines changed: 66 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,74 @@
33

44
namespace ts {
55
describe("TransformAPI", () => {
6-
function transformsCorrectly(name: string, source: string, transformers: TransformerFactory<SourceFile>[]) {
7-
it(name, () => {
8-
Harness.Baseline.runBaseline(`transformApi/transformsCorrectly.${name}.js`, () => {
9-
const transformed = transform(createSourceFile("source.ts", source, ScriptTarget.ES2015), transformers);
10-
const printer = createPrinter({ newLine: NewLineKind.CarriageReturnLineFeed }, {
11-
onEmitNode: transformed.emitNodeWithNotification,
12-
substituteNode: transformed.substituteNode
13-
});
14-
const result = printer.printBundle(createBundle(transformed.transformed));
15-
transformed.dispose();
16-
return result;
17-
});
6+
function replaceUndefinedWithVoid0(context: ts.TransformationContext) {
7+
const previousOnSubstituteNode = context.onSubstituteNode;
8+
context.enableSubstitution(SyntaxKind.Identifier);
9+
context.onSubstituteNode = (hint, node) => {
10+
node = previousOnSubstituteNode(hint, node);
11+
if (hint === EmitHint.Expression && node.kind === SyntaxKind.Identifier && (<Identifier>node).text === "undefined") {
12+
node = createPartiallyEmittedExpression(
13+
addSyntheticTrailingComment(
14+
setTextRange(
15+
createVoidZero(),
16+
node),
17+
SyntaxKind.MultiLineCommentTrivia, "undefined"));
18+
}
19+
return node;
20+
};
21+
return (file: ts.SourceFile) => file;
22+
}
23+
24+
function replaceIdentifiersNamedOldNameWithNewName(context: ts.TransformationContext) {
25+
const previousOnSubstituteNode = context.onSubstituteNode;
26+
context.enableSubstitution(SyntaxKind.Identifier);
27+
context.onSubstituteNode = (hint, node) => {
28+
node = previousOnSubstituteNode(hint, node);
29+
if (node.kind === SyntaxKind.Identifier && (<Identifier>node).text === "oldName") {
30+
node = setTextRange(createIdentifier("newName"), node);
31+
}
32+
return node;
33+
};
34+
return (file: ts.SourceFile) => file;
35+
}
36+
37+
function transformSourceFile(sourceText: string, transformers: TransformerFactory<SourceFile>[]) {
38+
const transformed = transform(createSourceFile("source.ts", sourceText, ScriptTarget.ES2015), transformers);
39+
const printer = createPrinter({ newLine: NewLineKind.CarriageReturnLineFeed }, {
40+
onEmitNode: transformed.emitNodeWithNotification,
41+
substituteNode: transformed.substituteNode
42+
});
43+
const result = printer.printBundle(createBundle(transformed.transformed));
44+
transformed.dispose();
45+
return result;
46+
}
47+
48+
function testBaseline(testName: string, test: () => string) {
49+
it(testName, () => {
50+
Harness.Baseline.runBaseline(`transformApi/transformsCorrectly.${testName}.js`, test);
1851
});
1952
}
2053

21-
transformsCorrectly("substitution", `
22-
var a = undefined;
23-
`, [
24-
context => {
25-
const previousOnSubstituteNode = context.onSubstituteNode;
26-
context.enableSubstitution(SyntaxKind.Identifier);
27-
context.onSubstituteNode = (hint, node) => {
28-
node = previousOnSubstituteNode(hint, node);
29-
if (hint === EmitHint.Expression && node.kind === SyntaxKind.Identifier && (<Identifier>node).text === "undefined") {
30-
node = createPartiallyEmittedExpression(
31-
addSyntheticTrailingComment(
32-
setTextRange(
33-
createVoidZero(),
34-
node),
35-
SyntaxKind.MultiLineCommentTrivia, "undefined"));
36-
}
37-
return node;
38-
};
39-
return file => file;
40-
}
41-
]);
42-
43-
// https://github.com/Microsoft/TypeScript/issues/15192
44-
transformsCorrectly("types", `let a: () => void`, [
45-
context => file => visitNode(file, function visitor(node: Node): VisitResult<Node> {
46-
return visitEachChild(node, visitor, context);
47-
})
48-
]);
54+
testBaseline("substitution", () => {
55+
return transformSourceFile(`var a = undefined;`, [replaceUndefinedWithVoid0]);
56+
});
57+
58+
testBaseline("types", () => {
59+
return transformSourceFile(`let a: () => void`, [
60+
context => file => visitNode(file, function visitor(node: Node): VisitResult<Node> {
61+
return visitEachChild(node, visitor, context);
62+
})
63+
]);
64+
});
65+
66+
testBaseline("fromTranspileModule", () => {
67+
return ts.transpileModule(`var oldName = undefined;`, {
68+
transformers: {
69+
before: [replaceUndefinedWithVoid0],
70+
after: [replaceIdentifiersNamedOldNameWithNewName]
71+
}
72+
}).outputText;
73+
});
4974
});
5075
}
76+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var newName = void 0 /*undefined*/;

0 commit comments

Comments
 (0)