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