Merge pull request #37907 from Jack-Works/feat/class-to-classname

feat: add a codefix to fix class to className in react & add spelling suggest for JSX attributes
This commit is contained in:
Daniel Rosenwasser
2020-06-22 18:32:24 -07:00
committed by GitHub
11 changed files with 314 additions and 14 deletions

View File

@@ -7,12 +7,16 @@ namespace ts.codefix {
Diagnostics.Cannot_find_name_0_Did_you_mean_the_instance_member_this_0.code,
Diagnostics.Cannot_find_name_0_Did_you_mean_the_static_member_1_0.code,
Diagnostics.Module_0_has_no_exported_member_1_Did_you_mean_2.code,
// for JSX class components
Diagnostics.No_overload_matches_this_call.code,
// for JSX FC
Diagnostics.Type_0_is_not_assignable_to_type_1.code,
];
registerCodeFix({
errorCodes,
getCodeActions(context) {
const { sourceFile } = context;
const info = getInfo(sourceFile, context.span.start, context);
const { sourceFile, errorCode } = context;
const info = getInfo(sourceFile, context.span.start, context, errorCode);
if (!info) return undefined;
const { node, suggestedSymbol } = info;
const { target } = context.host.getCompilationSettings();
@@ -21,18 +25,23 @@ namespace ts.codefix {
},
fixIds: [fixId],
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => {
const info = getInfo(diag.file, diag.start, context);
const info = getInfo(diag.file, diag.start, context, diag.code);
const { target } = context.host.getCompilationSettings();
if (info) doChange(changes, context.sourceFile, info.node, info.suggestedSymbol, target!);
}),
});
function getInfo(sourceFile: SourceFile, pos: number, context: CodeFixContextBase): { node: Node, suggestedSymbol: Symbol } | undefined {
function getInfo(sourceFile: SourceFile, pos: number, context: CodeFixContextBase, errorCode: number): { node: Node, suggestedSymbol: Symbol } | undefined {
// This is the identifier of the misspelled word. eg:
// this.speling = 1;
// ^^^^^^^
const node = getTokenAtPosition(sourceFile, pos);
const parent = node.parent;
// Only fix spelling for No_overload_matches_this_call emitted on the React class component
if ((
errorCode === Diagnostics.No_overload_matches_this_call.code ||
errorCode === Diagnostics.Type_0_is_not_assignable_to_type_1.code) &&
!isJsxAttribute(parent)) return undefined;
const checker = context.program.getTypeChecker();
let suggestedSymbol: Symbol | undefined;
@@ -52,6 +61,12 @@ namespace ts.codefix {
suggestedSymbol = checker.getSuggestedSymbolForNonexistentModule(node, resolvedSourceFile.symbol);
}
}
else if (isJsxAttribute(parent) && parent.name === node) {
Debug.assertNode(node, isIdentifier, "Expected an identifier for JSX attribute");
const tag = findAncestor(node, isJsxOpeningLikeElement)!;
const props = checker.getContextualTypeForArgumentAtIndex(tag, 0);
suggestedSymbol = checker.getSuggestedSymbolForNonexistentJSXAttribute(node, props!);
}
else {
const meaning = getMeaningFromLocation(node);
const name = getTextOfNode(node);