Skip to content

Commit b996287

Browse files
authored
fix(48191): Duplicates comments on "Add definite assignment assertion to property" (microsoft#48299)
* Suppress leading and trailing comments for adding missing definite assignment assertion action * Suppress leading and trailing comments for adding missing initalizer action * Test for initializer property action
1 parent 546a87f commit b996287

5 files changed

+66
-0
lines changed

Diff for: src/services/codefixes/fixStrictClassInitialization.ts

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ namespace ts.codefix {
6767
}
6868

6969
function addDefiniteAssignmentAssertion(changeTracker: textChanges.ChangeTracker, propertyDeclarationSourceFile: SourceFile, propertyDeclaration: PropertyDeclaration): void {
70+
suppressLeadingAndTrailingTrivia(propertyDeclaration);
7071
const property = factory.updatePropertyDeclaration(
7172
propertyDeclaration,
7273
propertyDeclaration.decorators,
@@ -108,6 +109,7 @@ namespace ts.codefix {
108109
}
109110

110111
function addInitializer(changeTracker: textChanges.ChangeTracker, propertyDeclarationSourceFile: SourceFile, propertyDeclaration: PropertyDeclaration, initializer: Expression): void {
112+
suppressLeadingAndTrailingTrivia(propertyDeclaration);
111113
const property = factory.updatePropertyDeclaration(
112114
propertyDeclaration,
113115
propertyDeclaration.decorators,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @strict: true
4+
5+
//// class T {
6+
//// // comment
7+
//// a: string;
8+
//// }
9+
10+
verify.codeFix({
11+
description: `Add definite assignment assertion to property 'a: string;'`,
12+
newFileContent: `class T {
13+
// comment
14+
a!: string;
15+
}`,
16+
index: 1
17+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @strict: true
4+
5+
//// class T {
6+
//// a: string; // comment
7+
//// }
8+
9+
verify.codeFix({
10+
description: `Add definite assignment assertion to property 'a: string;'`,
11+
newFileContent: `class T {
12+
a!: string; // comment
13+
}`,
14+
index: 1
15+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @strict: true
4+
5+
//// class T {
6+
//// // comment
7+
//// a: 2;
8+
//// }
9+
10+
verify.codeFix({
11+
description: `Add initializer to property 'a'`,
12+
newFileContent: `class T {
13+
// comment
14+
a: 2 = 2;
15+
}`,
16+
index: 2
17+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @strict: true
4+
5+
//// class T {
6+
//// a: 2; // comment
7+
//// }
8+
9+
verify.codeFix({
10+
description: `Add initializer to property 'a'`,
11+
newFileContent: `class T {
12+
a: 2 = 2; // comment
13+
}`,
14+
index: 2
15+
})

0 commit comments

Comments
 (0)