Skip to content

Commit

Permalink
fix PropertyDeclaration emit missing exclamationToken
Browse files Browse the repository at this point in the history
  • Loading branch information
王文璐 committed Feb 2, 2018
1 parent 71ad30e commit 57b1033
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,7 @@ namespace ts {
emitModifiers(node, node.modifiers);
emit(node.name);
emitIfPresent(node.questionToken);
emitIfPresent(node.exclamationToken);
emitTypeAnnotation(node.type);
emitInitializer(node.initializer);
writeSemicolon();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace ts.codefix {
fixIds: [fixId],
getAllCodeActions: context => {
const seenNames = createMap<true>();
const newLineCharacter = getNewLineOrDefaultFromHost(context.host, context.formatContext.options);

return codeFixAll(context, errorCodes, (changes, diag) => {
const info = getInfo(diag.file!, diag.start!);
if (!info) return undefined;
Expand All @@ -22,7 +24,7 @@ namespace ts.codefix {
return;
}

addDefiniteAssignmentAssertion(changes, diag.file, propertyDeclaration);
addDefiniteAssignmentAssertion(changes, diag.file, propertyDeclaration, newLineCharacter);
});
},
});
Expand All @@ -43,13 +45,16 @@ namespace ts.codefix {
}

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

function addDefiniteAssignmentAssertion(changeTracker: textChanges.ChangeTracker, propertyDeclarationSourceFile: SourceFile, propertyDeclaration: PropertyDeclaration): void {
changeTracker.insertTokenAfter(propertyDeclarationSourceFile, SyntaxKind.ExclamationToken, propertyDeclaration.name);
function addDefiniteAssignmentAssertion(changeTracker: textChanges.ChangeTracker, propertyDeclarationSourceFile: SourceFile, propertyDeclaration: PropertyDeclaration, newLineCharacter: string): void {
const property = clone(propertyDeclaration);
property.exclamationToken = createToken(SyntaxKind.ExclamationToken);
changeTracker.replaceNode(propertyDeclarationSourceFile, propertyDeclaration, property, { suffix: newLineCharacter });
}
}
5 changes: 0 additions & 5 deletions src/services/textChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,6 @@ namespace ts.textChanges {
this.replaceWithSingle(sourceFile, pos, pos, createToken(modifier), { suffix: " " });
}

public insertTokenAfter(sourceFile: SourceFile, token: SyntaxKind, after: Node) {
const pos = after.getEnd();
this.replaceWithSingle(sourceFile, pos, pos, createToken(token), {});
}

public changeIdentifierToPropertyAccess(sourceFile: SourceFile, prefix: string, node: Identifier): void {
const startPosition = getAdjustedStartPosition(sourceFile, node, {}, Position.Start);
this.replaceWithSingle(sourceFile, startPosition, startPosition, createPropertyAccess(createIdentifier(prefix), ""), {});
Expand Down

0 comments on commit 57b1033

Please sign in to comment.