mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-05 08:11:30 -06:00
Merge pull request #22049 from Kingwl/spelling-fix
replace element access if suggession is not a valid identifier
This commit is contained in:
commit
84548d5c9b
@ -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 {
|
||||
|
||||
@ -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",
|
||||
@ -12,5 +17,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`,
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user