forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfactory.ts
More file actions
86 lines (79 loc) · 5.43 KB
/
factory.ts
File metadata and controls
86 lines (79 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
namespace ts {
describe("unittests:: FactoryAPI", () => {
function assertSyntaxKind(node: Node, expected: SyntaxKind) {
assert.strictEqual(node.kind, expected, `Actual: ${Debug.formatSyntaxKind(node.kind)} Expected: ${Debug.formatSyntaxKind(expected)}`);
}
describe("factory.createExportAssignment", () => {
it("parenthesizes default export if necessary", () => {
function checkExpression(expression: Expression) {
const node = factory.createExportAssignment(
/*decorators*/ undefined,
/*modifiers*/ undefined,
/*isExportEquals*/ false,
expression,
);
assertSyntaxKind(node.expression, SyntaxKind.ParenthesizedExpression);
}
const clazz = factory.createClassExpression(/*decorators*/ undefined, /*modifiers*/ undefined, "C", /*typeParameters*/ undefined, /*heritageClauses*/ undefined, [
factory.createPropertyDeclaration(/*decorators*/ undefined, [factory.createToken(SyntaxKind.StaticKeyword)], "prop", /*questionOrExclamationToken*/ undefined, /*type*/ undefined, factory.createStringLiteral("1")),
]);
checkExpression(clazz);
checkExpression(factory.createPropertyAccessExpression(clazz, "prop"));
const func = factory.createFunctionExpression(/*modifiers*/ undefined, /*asteriskToken*/ undefined, "fn", /*typeParameters*/ undefined, /*parameters*/ undefined, /*type*/ undefined, factory.createBlock([]));
checkExpression(func);
checkExpression(factory.createCallExpression(func, /*typeArguments*/ undefined, /*argumentsArray*/ undefined));
checkExpression(factory.createTaggedTemplateExpression(func, /*typeArguments*/ undefined, factory.createNoSubstitutionTemplateLiteral("")));
checkExpression(factory.createBinaryExpression(factory.createStringLiteral("a"), SyntaxKind.CommaToken, factory.createStringLiteral("b")));
checkExpression(factory.createCommaListExpression([factory.createStringLiteral("a"), factory.createStringLiteral("b")]));
});
});
describe("factory.createArrowFunction", () => {
it("parenthesizes concise body if necessary", () => {
function checkBody(body: ConciseBody) {
const node = factory.createArrowFunction(
/*modifiers*/ undefined,
/*typeParameters*/ undefined,
[],
/*type*/ undefined,
/*equalsGreaterThanToken*/ undefined,
body,
);
assertSyntaxKind(node.body, SyntaxKind.ParenthesizedExpression);
}
checkBody(factory.createObjectLiteralExpression());
checkBody(factory.createPropertyAccessExpression(factory.createObjectLiteralExpression(), "prop"));
checkBody(factory.createAsExpression(factory.createPropertyAccessExpression(factory.createObjectLiteralExpression(), "prop"), factory.createTypeReferenceNode("T", /*typeArguments*/ undefined)));
checkBody(factory.createNonNullExpression(factory.createPropertyAccessExpression(factory.createObjectLiteralExpression(), "prop")));
checkBody(factory.createCommaListExpression([factory.createStringLiteral("a"), factory.createStringLiteral("b")]));
checkBody(factory.createBinaryExpression(factory.createStringLiteral("a"), SyntaxKind.CommaToken, factory.createStringLiteral("b")));
});
});
describe("createBinaryExpression", () => {
it("parenthesizes arrow function in RHS if necessary", () => {
const lhs = factory.createIdentifier("foo");
const rhs = factory.createArrowFunction(
/*modifiers*/ undefined,
/*typeParameters*/ undefined,
[],
/*type*/ undefined,
/*equalsGreaterThanToken*/ undefined,
factory.createBlock([]),
);
function checkRhs(operator: BinaryOperator, expectParens: boolean) {
const node = factory.createBinaryExpression(lhs, operator, rhs);
assertSyntaxKind(node.right, expectParens ? SyntaxKind.ParenthesizedExpression : SyntaxKind.ArrowFunction);
}
checkRhs(SyntaxKind.CommaToken, /*expectParens*/ false);
checkRhs(SyntaxKind.EqualsToken, /*expectParens*/ false);
checkRhs(SyntaxKind.PlusEqualsToken, /*expectParens*/ false);
checkRhs(SyntaxKind.BarBarToken, /*expectParens*/ true);
checkRhs(SyntaxKind.AmpersandAmpersandToken, /*expectParens*/ true);
checkRhs(SyntaxKind.QuestionQuestionToken, /*expectParens*/ true);
checkRhs(SyntaxKind.EqualsEqualsToken, /*expectParens*/ true);
checkRhs(SyntaxKind.BarBarEqualsToken, /*expectParens*/ false);
checkRhs(SyntaxKind.AmpersandAmpersandEqualsToken, /*expectParens*/ false);
checkRhs(SyntaxKind.QuestionQuestionEqualsToken, /*expectParens*/ false);
});
});
});
}