Skip to content

Commit 5030d0f

Browse files
author
Arthur Ozga
committed
Use factory for all fixes
1 parent 21fc30f commit 5030d0f

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

src/services/codefixes/fixAddMissingMember.ts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,19 @@ namespace ts.codefix {
8282

8383
const className = classDeclaration.name.getText();
8484

85+
const staticInitialization = createStatement(createAssignment(
86+
createPropertyAccess(createIdentifier(className), tokenName),
87+
createIdentifier("undefined")));
88+
89+
const staticInitializationChangeTracker = textChanges.ChangeTracker.fromCodeFixContext(context);
90+
staticInitializationChangeTracker.insertNodeAfter(
91+
classDeclarationSourceFile,
92+
classDeclaration,
93+
staticInitialization,
94+
{ suffix: context.newLineCharacter });
8595
const initializeStaticAction = {
8696
description: formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Initialize_static_property_0), [tokenName]),
87-
changes: [{
88-
fileName: classDeclarationSourceFile.fileName,
89-
textChanges: [{
90-
span: { start: classDeclaration.getEnd(), length: 0 },
91-
newText: `${context.newLineCharacter}${className}.${tokenName} = undefined;${context.newLineCharacter}`
92-
}]
93-
}]
97+
changes: staticInitializationChangeTracker.getChanges()
9498
};
9599

96100
(actions || (actions = [])).push(initializeStaticAction);
@@ -102,15 +106,20 @@ namespace ts.codefix {
102106
return actions;
103107
}
104108

109+
const propertyInitialization = createStatement(createAssignment(
110+
createPropertyAccess(createThis(), tokenName),
111+
createIdentifier("undefined")));
112+
113+
const propertyInitializationChangeTracker = textChanges.ChangeTracker.fromCodeFixContext(context);
114+
propertyInitializationChangeTracker.insertNodeAt(
115+
classDeclarationSourceFile,
116+
classConstructor.body.getEnd() - 1,
117+
propertyInitialization,
118+
{ prefix: context.newLineCharacter, suffix: context.newLineCharacter });
119+
105120
const initializeAction = {
106121
description: formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Initialize_property_0_in_the_constructor), [tokenName]),
107-
changes: [{
108-
fileName: classDeclarationSourceFile.fileName,
109-
textChanges: [{
110-
span: { start: classConstructor.body.getEnd() - 1, length: 0 },
111-
newText: `this.${tokenName} = undefined;${context.newLineCharacter}`
112-
}]
113-
}]
122+
changes: propertyInitializationChangeTracker.getChanges()
114123
};
115124

116125
(actions || (actions = [])).push(initializeAction);

tests/cases/fourslash/codeFixAddMissingMember5.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
////}
1212
////|]
1313

14-
verify.rangeAfterCodeFix(`class C {
14+
verify.applyCodeFix(/*errorCode*/ undefined, /*index*/ 0);
15+
verify.currentFileContentIs(`class C {
1516
static method() {
1617
()=>{ this.foo === 10 };
1718
}
1819
}
19-
C.foo = undefined;`, /*includeWhiteSpace*/false, /*errorCode*/ undefined, /*index*/ 0);
20+
C.foo = undefined;
21+
`);

tests/cases/fourslash/codeFixAddMissingMember7.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
////}
1010
////|]
1111

12-
verify.rangeAfterCodeFix(`class C {
12+
verify.applyCodeFix(/*errorCode*/ undefined, /*index*/ 2)
13+
verify.currentFileContentIs(`class C {
1314
static p = ()=>{ this.foo === 10 };
1415
}
15-
C.foo = undefined;`, /*includeWhiteSpace*/false, /*errorCode*/ undefined, /*index*/ 2);
16+
C.foo = undefined;
17+
`);

0 commit comments

Comments
 (0)