diff --git a/packages/ts-morph/src/compiler/ast/base/TypeArgumentedNode.ts b/packages/ts-morph/src/compiler/ast/base/TypeArgumentedNode.ts index 4c60e16f6..60e5a0b2c 100644 --- a/packages/ts-morph/src/compiler/ast/base/TypeArgumentedNode.ts +++ b/packages/ts-morph/src/compiler/ast/base/TypeArgumentedNode.ts @@ -86,7 +86,12 @@ export function TypeArgumentedNode { @@ -86,6 +86,29 @@ describe("TypeArgumentedNode", () => { it("should add a type arg", () => { doTest("@dec<1, 2>()\nclass T {}", "3", "@dec<1, 2, 3>()\nclass T {}"); }); + + // Issue #1228: addTypeArgument crashes on property access expressions + describe("property access expressions", () => { + function doTestPropertyAccess(code: string, text: string, expectedCode: string) { + const { sourceFile } = getInfoFromText(code); + const callExpr = sourceFile.getDescendantsOfKind(SyntaxKind.CallExpression)[0]; + const result = callExpr.addTypeArgument(text); + expect(result.getText()).to.equal(text); + expect(sourceFile.getFullText()).to.equal(expectedCode); + } + + it("should add type argument to property access on this", () => { + doTestPropertyAccess("this.foo();", "string", "this.foo();"); + }); + + it("should add type argument to property access on constructor", () => { + doTestPropertyAccess("new Test().foo();", "string", "new Test().foo();"); + }); + + it("should add type argument to property access on object", () => { + doTestPropertyAccess("obj.method();", "string", "obj.method();"); + }); + }); }); describe(nameof("removeTypeArgument"), () => {