replace element access if suggession is not a valid identifier

This commit is contained in:
王文璐
2018-02-20 21:33:01 +08:00
parent 7e908dbe91
commit 0a9f32cede
2 changed files with 21 additions and 6 deletions

View File

@@ -8,18 +8,18 @@ namespace ts.codefix {
registerCodeFix({
errorCodes,
getCodeActions(context) {
const { sourceFile } = context;
const { sourceFile, host } = context;
const info = getInfo(sourceFile, context.span.start, context.program.getTypeChecker());
if (!info) return undefined;
const { node, suggestion } = info;
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, node, suggestion));
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, node, suggestion, host.getCompilationSettings().target));
const description = formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Change_spelling_to_0), [suggestion]);
return [{ description, changes, fixId }];
},
fixIds: [fixId],
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => {
const info = getInfo(diag.file!, diag.start!, context.program.getTypeChecker());
if (info) doChange(changes, context.sourceFile, info.node, info.suggestion);
if (info) doChange(changes, context.sourceFile, info.node, info.suggestion, context.host.getCompilationSettings().target);
}),
});
@@ -45,8 +45,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 {

View File

@@ -4,6 +4,11 @@
//// s.toStrang();
//// s.toStrong();
////}
////
////type A = { "foo-bar": number; "barbaz": string; };
////var a: A;
////a.foobar;
////a.barbz
verify.codeFixAll({
fixId: "fixSpelling",
@@ -11,5 +16,10 @@ verify.codeFixAll({
`function f(s: string) {
s.toString();
s.toString();
}`,
}
type A = { "foo-bar": number; "barbaz": string; };
var a: A;
a["foo-bar"];
a.barbaz`,
});