From e80f69381fff3b03d342f25422a6ed1eb301fe63 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Wed, 12 Nov 2025 10:11:01 -0800 Subject: [PATCH] fix: `InvalidOperationError` in `addTypeArgument` Fix `addTypeArgument` for various forms of `PropertyAccessExpression`. Fixes https://github.com/dsherret/ts-morph/issues/1228. --- .../compiler/ast/base/TypeArgumentedNode.ts | 7 ++++- .../ast/base/typeArgumentedNodeTests.ts | 27 +++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) 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"), () => {