Skip to content

Commit 3020f3d

Browse files
committed
Handle trivia in code fix forgotten-this
Fixes microsoft#21433
1 parent b0ea899 commit 3020f3d

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

src/services/codefixes/fixForgottenThisPropertyAccess.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ namespace ts.codefix {
2121
}
2222

2323
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, token: Identifier): void {
24-
changes.replaceNode(sourceFile, token, createPropertyAccess(createThis(), token));
24+
// TODO (https://github.com/Microsoft/TypeScript/issues/21246): use shared helper
25+
suppressLeadingAndTrailingTrivia(token);
26+
changes.replaceRange(sourceFile, { pos: token.getStart(), end: token.end }, createPropertyAccess(createThis(), token));
2527
}
2628
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////class C {
4+
//// foo: number;
5+
//// constructor() {[|
6+
//// /* a comment */foo = 10;
7+
//// |]}
8+
////}
9+
10+
verify.codeFix({
11+
description: "Add 'this.' to unresolved variable",
12+
newRangeContent: `
13+
/* a comment */this.foo = 10;
14+
`
15+
});

0 commit comments

Comments
 (0)