|
3 | 3 |
|
4 | 4 | namespace ts {
|
5 | 5 | 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); |
18 | 51 | });
|
19 | 52 | }
|
20 | 53 |
|
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 | + }); |
49 | 74 | });
|
50 | 75 | }
|
| 76 | + |
0 commit comments