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 /*modifiers*/ undefined, 11 /*isExportEquals*/ false, 12 expression, 13 ); 14 assertSyntaxKind(node.expression, SyntaxKind.ParenthesizedExpression); 15 } 16 17 const clazz = factory.createClassExpression(/*modifiers*/ undefined, "C", /*typeParameters*/ undefined, /*heritageClauses*/ undefined, [ 18 factory.createPropertyDeclaration([factory.createToken(SyntaxKind.StaticKeyword)], "prop", /*questionOrExclamationToken*/ undefined, /*type*/ undefined, factory.createStringLiteral("1")), 19 ]); 20 checkExpression(clazz); 21 checkExpression(factory.createPropertyAccessExpression(clazz, "prop")); 22 23 const func = factory.createFunctionExpression(/*modifiers*/ undefined, /*asteriskToken*/ undefined, "fn", /*typeParameters*/ undefined, /*parameters*/ undefined, /*type*/ undefined, factory.createBlock([])); 24 checkExpression(func); 25 checkExpression(factory.createCallExpression(func, /*typeArguments*/ undefined, /*argumentsArray*/ undefined)); 26 checkExpression(factory.createTaggedTemplateExpression(func, /*typeArguments*/ undefined, factory.createNoSubstitutionTemplateLiteral(""))); 27 28 checkExpression(factory.createBinaryExpression(factory.createStringLiteral("a"), SyntaxKind.CommaToken, factory.createStringLiteral("b"))); 29 checkExpression(factory.createCommaListExpression([factory.createStringLiteral("a"), factory.createStringLiteral("b")])); 30 }); 31 }); 32 33 describe("factory.createArrowFunction", () => { 34 it("parenthesizes concise body if necessary", () => { 35 function checkBody(body: ConciseBody) { 36 const node = factory.createArrowFunction( 37 /*modifiers*/ undefined, 38 /*typeParameters*/ undefined, 39 [], 40 /*type*/ undefined, 41 /*equalsGreaterThanToken*/ undefined, 42 body, 43 ); 44 assertSyntaxKind(node.body, SyntaxKind.ParenthesizedExpression); 45 } 46 47 checkBody(factory.createObjectLiteralExpression()); 48 checkBody(factory.createPropertyAccessExpression(factory.createObjectLiteralExpression(), "prop")); 49 checkBody(factory.createAsExpression(factory.createPropertyAccessExpression(factory.createObjectLiteralExpression(), "prop"), factory.createTypeReferenceNode("T", /*typeArguments*/ undefined))); 50 checkBody(factory.createNonNullExpression(factory.createPropertyAccessExpression(factory.createObjectLiteralExpression(), "prop"))); 51 checkBody(factory.createCommaListExpression([factory.createStringLiteral("a"), factory.createStringLiteral("b")])); 52 checkBody(factory.createBinaryExpression(factory.createStringLiteral("a"), SyntaxKind.CommaToken, factory.createStringLiteral("b"))); 53 }); 54 }); 55 56 describe("createBinaryExpression", () => { 57 it("parenthesizes arrow function in RHS if necessary", () => { 58 const lhs = factory.createIdentifier("foo"); 59 const rhs = factory.createArrowFunction( 60 /*modifiers*/ undefined, 61 /*typeParameters*/ undefined, 62 [], 63 /*type*/ undefined, 64 /*equalsGreaterThanToken*/ undefined, 65 factory.createBlock([]), 66 ); 67 function checkRhs(operator: BinaryOperator, expectParens: boolean) { 68 const node = factory.createBinaryExpression(lhs, operator, rhs); 69 assertSyntaxKind(node.right, expectParens ? SyntaxKind.ParenthesizedExpression : SyntaxKind.ArrowFunction); 70 } 71 72 checkRhs(SyntaxKind.CommaToken, /*expectParens*/ false); 73 checkRhs(SyntaxKind.EqualsToken, /*expectParens*/ false); 74 checkRhs(SyntaxKind.PlusEqualsToken, /*expectParens*/ false); 75 checkRhs(SyntaxKind.BarBarToken, /*expectParens*/ true); 76 checkRhs(SyntaxKind.AmpersandAmpersandToken, /*expectParens*/ true); 77 checkRhs(SyntaxKind.QuestionQuestionToken, /*expectParens*/ true); 78 checkRhs(SyntaxKind.EqualsEqualsToken, /*expectParens*/ true); 79 checkRhs(SyntaxKind.BarBarEqualsToken, /*expectParens*/ false); 80 checkRhs(SyntaxKind.AmpersandAmpersandEqualsToken, /*expectParens*/ false); 81 checkRhs(SyntaxKind.QuestionQuestionEqualsToken, /*expectParens*/ false); 82 }); 83 }); 84 85 describe("deprecations", () => { 86 beforeEach(() => { 87 Debug.enableDeprecationWarnings = false; 88 }); 89 90 afterEach(() => { 91 Debug.enableDeprecationWarnings = true; 92 }); 93 94 // https://github.com/microsoft/TypeScript/issues/50259 95 it("deprecated createConstructorDeclaration overload does not throw", () => { 96 const body = factory.createBlock([]); 97 assert.doesNotThrow(() => factory.createConstructorDeclaration( 98 /*decorators*/ undefined, 99 /*modifiers*/ undefined, 100 /*parameters*/ [], 101 body, 102 )); 103 }); 104 105 // https://github.com/microsoft/TypeScript/issues/50259 106 it("deprecated updateConstructorDeclaration overload does not throw", () => { 107 const body = factory.createBlock([]); 108 const ctor = factory.createConstructorDeclaration(/*modifiers*/ undefined, [], body); 109 assert.doesNotThrow(() => factory.updateConstructorDeclaration( 110 ctor, 111 ctor.decorators, 112 ctor.modifiers, 113 ctor.parameters, 114 ctor.body, 115 )); 116 }); 117 }); 118 119 }); 120} 121