Merge pull request #22049 from Kingwl/spelling-fix

replace element access if suggession is not a valid identifier
This commit is contained in:
Mohamed Hegazy
2018-04-03 13:29:43 -07:00
committed by GitHub
2 changed files with 22 additions and 5 deletions

View File

@@ -13,13 +13,15 @@ namespace ts.codefix {
const info = getInfo(sourceFile, context.span.start, context);
if (!info) return undefined;
const { node, suggestion } = info;
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, node, suggestion));
const { target } = context.host.getCompilationSettings();
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, node, suggestion, target));
return [createCodeFixAction(changes, [Diagnostics.Change_spelling_to_0, suggestion], fixId, Diagnostics.Fix_all_detected_spelling_errors)];
},
fixIds: [fixId],
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => {
const info = getInfo(diag.file!, diag.start!, context);
if (info) doChange(changes, context.sourceFile, info.node, info.suggestion);
const { target } = context.host.getCompilationSettings();
if (info) doChange(changes, context.sourceFile, info.node, info.suggestion, target);
}),
});
@@ -54,8 +56,13 @@ namespace ts.codefix {
return suggestion === undefined ? undefined : { node, suggestion };
}
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, node: Node, suggestion: string) {
changes.replaceNode(sourceFile, node, createIdentifier(suggestion));
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, node: Node, suggestion: string, target: ScriptTarget) {
if (!isIdentifierText(suggestion, target) && isPropertyAccessExpression(node.parent)) {
changes.replaceNode(sourceFile, node.parent, createElementAccess(node.parent.expression, createLiteral(suggestion)));
}
else {
changes.replaceNode(sourceFile, node, createIdentifier(suggestion));
}
}
function convertSemanticMeaningToSymbolFlags(meaning: SemanticMeaning): SymbolFlags {