fix(48191): Duplicates comments on "Add definite assignment assertion to property" (#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
This commit is contained in:
Minh Quy
2022-03-17 17:57:10 +01:00
committed by GitHub
parent 546a87fa31
commit b996287e00
5 changed files with 66 additions and 0 deletions

View File

@@ -67,6 +67,7 @@ namespace ts.codefix {
}
function addDefiniteAssignmentAssertion(changeTracker: textChanges.ChangeTracker, propertyDeclarationSourceFile: SourceFile, propertyDeclaration: PropertyDeclaration): void {
suppressLeadingAndTrailingTrivia(propertyDeclaration);
const property = factory.updatePropertyDeclaration(
propertyDeclaration,
propertyDeclaration.decorators,
@@ -108,6 +109,7 @@ namespace ts.codefix {
}
function addInitializer(changeTracker: textChanges.ChangeTracker, propertyDeclarationSourceFile: SourceFile, propertyDeclaration: PropertyDeclaration, initializer: Expression): void {
suppressLeadingAndTrailingTrivia(propertyDeclaration);
const property = factory.updatePropertyDeclaration(
propertyDeclaration,
propertyDeclaration.decorators,

View File

@@ -0,0 +1,17 @@
/// <reference path='fourslash.ts' />
// @strict: true
//// class T {
//// // comment
//// a: string;
//// }
verify.codeFix({
description: `Add definite assignment assertion to property 'a: string;'`,
newFileContent: `class T {
// comment
a!: string;
}`,
index: 1
})

View File

@@ -0,0 +1,15 @@
/// <reference path='fourslash.ts' />
// @strict: true
//// class T {
//// a: string; // comment
//// }
verify.codeFix({
description: `Add definite assignment assertion to property 'a: string;'`,
newFileContent: `class T {
a!: string; // comment
}`,
index: 1
})

View File

@@ -0,0 +1,17 @@
/// <reference path='fourslash.ts' />
// @strict: true
//// class T {
//// // comment
//// a: 2;
//// }
verify.codeFix({
description: `Add initializer to property 'a'`,
newFileContent: `class T {
// comment
a: 2 = 2;
}`,
index: 2
})

View File

@@ -0,0 +1,15 @@
/// <reference path='fourslash.ts' />
// @strict: true
//// class T {
//// a: 2; // comment
//// }
verify.codeFix({
description: `Add initializer to property 'a'`,
newFileContent: `class T {
a: 2 = 2; // comment
}`,
index: 2
})