feat: add a codefix to fix class to className in react & add spelling suggest for JSX attributes#37907
feat: add a codefix to fix class to className in react & add spelling suggest for JSX attributes#37907DanielRosenwasser merged 15 commits intomicrosoft:mastermicrosoft/TypeScript:masterfrom Jack-Works:feat/class-to-classnameJack-Works/TypeScript:feat/class-to-classnameCopy head branch name to clipboard
Conversation
|
I don't know how JSX name resolution works here, but I'm surprised that it's a type assignability error instead of a name resolution error. If it were the latter, you could probably just special-case the spelling correction algorithm and use the existing did-you-mean codefix. Switching which error is issued actually might be the right fix for this. |
|
I manually do this all the time! |
Yes, in my last try I tried to use it but it seems like JSX never emit diag 2551 ( // The following 2 property both misspelled with doubled last letter
const x = <a onCompositionUpdateCapturee={() => {}} />;
// Property '...' does not exist on type '...'.ts(2322)
window.AbortControllerr
// Property '...' does not exist on type '...'. Did you mean '...'?ts(2551)I'll try to investigate this again |
|
Oh I found a comment: https://github.com/microsoft/TypeScript/blob/c219fdae08607e07ec875d2137fcba7c2d93792c/src/compiler/checker.ts#L15984 🤔 let me try it |
1513326 to
769c52c
Compare
|
@andrewbranch |
|
Done. I'll add some test for it later |
|
Test added. ready for review |
|
@sheetalkamat are you OK with this change now? @andrewbranch likes it at least =) |
I think it's cross-language interference. This is like an editor that lets you paste in Norwegian and offers to translate it to Swedish. |
This reverts commit 1513326.
78c5f67 to
4fce495
Compare
| const propName = symbolToString(prop); | ||
| const suggestionSymbol = getSuggestedSymbolForNonexistentJSXAttribute(propName, errorTarget); | ||
| const suggestion = suggestionSymbol ? symbolToString(suggestionSymbol) : undefined; | ||
| if (suggestion) reportError(Diagnostics.Property_0_does_not_exist_on_type_1_Did_you_mean_2, propName, typeToString(errorTarget), suggestion); |
There was a problem hiding this comment.
There's technically the possibility of an empty symbol name here, but not clear when that comes up.
Also, as a nit, whenever we have an if/else pair we generally wrap those in curlies.
There was a problem hiding this comment.
how to handle a symbol without name?
There was a problem hiding this comment.
if (suggestion !== undefined)
|
Thanks @Jack-Works! |



Add a code fix to fix
classin JSX (toclassName) orforin JSX (tohtmlFor). This is useful for copy-paste HTML code into a JSX file.Before:

After (fix all):
