Skip to content

Commit 57b1033

Browse files
author
王文璐
committed
fix PropertyDeclaration emit missing exclamationToken
1 parent 71ad30e commit 57b1033

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

src/compiler/emitter.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,7 @@ namespace ts {
10201020
emitModifiers(node, node.modifiers);
10211021
emit(node.name);
10221022
emitIfPresent(node.questionToken);
1023+
emitIfPresent(node.exclamationToken);
10231024
emitTypeAnnotation(node.type);
10241025
emitInitializer(node.initializer);
10251026
writeSemicolon();

src/services/codefixes/fixAddMissingDefiniteAssignmentAssertions.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ namespace ts.codefix {
1313
fixIds: [fixId],
1414
getAllCodeActions: context => {
1515
const seenNames = createMap<true>();
16+
const newLineCharacter = getNewLineOrDefaultFromHost(context.host, context.formatContext.options);
17+
1618
return codeFixAll(context, errorCodes, (changes, diag) => {
1719
const info = getInfo(diag.file!, diag.start!);
1820
if (!info) return undefined;
@@ -22,7 +24,7 @@ namespace ts.codefix {
2224
return;
2325
}
2426

25-
addDefiniteAssignmentAssertion(changes, diag.file, propertyDeclaration);
27+
addDefiniteAssignmentAssertion(changes, diag.file, propertyDeclaration, newLineCharacter);
2628
});
2729
},
2830
});
@@ -43,13 +45,16 @@ namespace ts.codefix {
4345
}
4446

4547
function getActionsForAddMissingDefiniteAssignmentAssertion (context: CodeFixContext, token: Identifier, propertyDeclaration: PropertyDeclaration): CodeFixAction[] | undefined {
48+
const newLineCharacter = getNewLineOrDefaultFromHost(context.host, context.formatContext.options);
4649
const description = formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Declare_property_0), [token.text]);
47-
const changes = textChanges.ChangeTracker.with(context, t => addDefiniteAssignmentAssertion(t, context.sourceFile, propertyDeclaration));
50+
const changes = textChanges.ChangeTracker.with(context, t => addDefiniteAssignmentAssertion(t, context.sourceFile, propertyDeclaration, newLineCharacter));
4851
const action = { description, changes, fixId };
4952
return [ action ];
5053
}
5154

52-
function addDefiniteAssignmentAssertion(changeTracker: textChanges.ChangeTracker, propertyDeclarationSourceFile: SourceFile, propertyDeclaration: PropertyDeclaration): void {
53-
changeTracker.insertTokenAfter(propertyDeclarationSourceFile, SyntaxKind.ExclamationToken, propertyDeclaration.name);
55+
function addDefiniteAssignmentAssertion(changeTracker: textChanges.ChangeTracker, propertyDeclarationSourceFile: SourceFile, propertyDeclaration: PropertyDeclaration, newLineCharacter: string): void {
56+
const property = clone(propertyDeclaration);
57+
property.exclamationToken = createToken(SyntaxKind.ExclamationToken);
58+
changeTracker.replaceNode(propertyDeclarationSourceFile, propertyDeclaration, property, { suffix: newLineCharacter });
5459
}
5560
}

src/services/textChanges.ts

-5
Original file line numberDiff line numberDiff line change
@@ -350,11 +350,6 @@ namespace ts.textChanges {
350350
this.replaceWithSingle(sourceFile, pos, pos, createToken(modifier), { suffix: " " });
351351
}
352352

353-
public insertTokenAfter(sourceFile: SourceFile, token: SyntaxKind, after: Node) {
354-
const pos = after.getEnd();
355-
this.replaceWithSingle(sourceFile, pos, pos, createToken(token), {});
356-
}
357-
358353
public changeIdentifierToPropertyAccess(sourceFile: SourceFile, prefix: string, node: Identifier): void {
359354
const startPosition = getAdjustedStartPosition(sourceFile, node, {}, Position.Start);
360355
this.replaceWithSingle(sourceFile, startPosition, startPosition, createPropertyAccess(createIdentifier(prefix), ""), {});

0 commit comments

Comments
 (0)