Skip to content

Commit

Permalink
More did-you-mean errors on classes in plain JS
Browse files Browse the repository at this point in the history
Classes
- that are declared with class declarations or expressions, but not
  constructor functions
- and which have nothing in their heritage clauses

Now provide spelling suggestions on misspelled property accesses.

Because of JS support for assignment-as-declaration, it's still easy
to mistakenly declare a property with a typo and not get any
suggestions.
  • Loading branch information
sandersn committed Jul 7, 2022
1 parent 0f86803 commit c850390
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29277,9 +29277,10 @@ m2: ${(this.mapper2 as unknown as DebugTypeMapper).__debugToString().split("\n")
if (file) {
if (compilerOptions.checkJs === undefined && file.checkJsDirective === undefined && (file.scriptKind === ScriptKind.JS || file.scriptKind === ScriptKind.JSX)) {
const declarationFile = forEach(suggestion?.declarations, getSourceFileOfNode);
const suggestionHasNoExtends = !suggestion?.valueDeclaration || !isClassLike(suggestion.valueDeclaration) || suggestion.valueDeclaration.heritageClauses?.length
return !(file !== declarationFile && !!declarationFile && isGlobalSourceFile(declarationFile))
&& !(excludeClasses && suggestion && suggestion.flags & SymbolFlags.Class)
&& !(!!node && excludeClasses && isPropertyAccessExpression(node) && node.expression.kind === SyntaxKind.ThisKeyword);
&& !(excludeClasses && suggestion && suggestion.flags & SymbolFlags.Class && suggestionHasNoExtends)
&& !(!!node && excludeClasses && isPropertyAccessExpression(node) && node.expression.kind === SyntaxKind.ThisKeyword && suggestionHasNoExtends);
}
}
return false;
Expand Down
56 changes: 49 additions & 7 deletions tests/cases/fourslash/codeFixSpellingJs9.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,52 @@
/// <reference path='fourslash.ts' />
// @allowJs: true
// @Filename: codeFixSpellingJs9.js
//// class C {
//// numble = 1
//// mumble() {
//// return this.[|numbles|]
//// }
//// }
//// class D extends C { }
//// const c = new C()
//// c.[|numbles|] = 3
//// c.[|mumbles|]()
//// const d = new D()
//// d.[|numbles|] = 4
//// d.[|mumbles()|]
//// class Person {
//// getFavoriteColor() {
////
//// }
//// }
////
//// const person = new Person();
//// person.[|getFavoriteColour|]();
//// person.[|getFavoriteColoxr|]();
verify.codeFixAll({
fixId: "fixSpelling",
fixAllDescription: "Fix all detected spelling errors",
newFileContent:
`class C {
numble = 1
mumble() {
return this.numble
}
}
class D extends C { }
const c = new C()
c.numble = 3
c.mumble()
const d = new D()
d.numbles = 4
d.mumbles()
class Person {
getFavoriteColor() {
// @allowjs: true
// @noEmit: true
}
}
// @filename: noSuggestionWithoutDidYouMean.js
//// let a = {};
//// console.log(a.apple);
verify.noErrors()
verify.getSuggestionDiagnostics([])
const person = new Person();
person.getFavoriteColor();
person.getFavoriteColor();`,
});

0 comments on commit c850390

Please sign in to comment.