diff --git a/src/services/codefixes/fixForgottenThisPropertyAccess.ts b/src/services/codefixes/fixForgottenThisPropertyAccess.ts index 19610da0b15..837487f1b8c 100644 --- a/src/services/codefixes/fixForgottenThisPropertyAccess.ts +++ b/src/services/codefixes/fixForgottenThisPropertyAccess.ts @@ -21,6 +21,8 @@ namespace ts.codefix { } function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, token: Identifier): void { - changes.replaceNode(sourceFile, token, createPropertyAccess(createThis(), token)); + // TODO (https://github.com/Microsoft/TypeScript/issues/21246): use shared helper + suppressLeadingAndTrailingTrivia(token); + changes.replaceRange(sourceFile, { pos: token.getStart(), end: token.end }, createPropertyAccess(createThis(), token)); } } \ No newline at end of file diff --git a/tests/cases/fourslash/codeFixForgottenThisPropertyAccess03.ts b/tests/cases/fourslash/codeFixForgottenThisPropertyAccess03.ts new file mode 100644 index 00000000000..2dd1c47b6f7 --- /dev/null +++ b/tests/cases/fourslash/codeFixForgottenThisPropertyAccess03.ts @@ -0,0 +1,15 @@ +/// + +////class C { +//// foo: number; +//// constructor() {[| +//// /* a comment */foo = 10; +//// |]} +////} + +verify.codeFix({ + description: "Add 'this.' to unresolved variable", + newRangeContent: ` + /* a comment */this.foo = 10; + ` +});