Skip to content

Commit c850390

Browse files
committed
More did-you-mean errors on classes in plain JS
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.
1 parent 0f86803 commit c850390

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29277,9 +29277,10 @@ m2: ${(this.mapper2 as unknown as DebugTypeMapper).__debugToString().split("\n")
2927729277
if (file) {
2927829278
if (compilerOptions.checkJs === undefined && file.checkJsDirective === undefined && (file.scriptKind === ScriptKind.JS || file.scriptKind === ScriptKind.JSX)) {
2927929279
const declarationFile = forEach(suggestion?.declarations, getSourceFileOfNode);
29280+
const suggestionHasNoExtends = !suggestion?.valueDeclaration || !isClassLike(suggestion.valueDeclaration) || suggestion.valueDeclaration.heritageClauses?.length
2928029281
return !(file !== declarationFile && !!declarationFile && isGlobalSourceFile(declarationFile))
29281-
&& !(excludeClasses && suggestion && suggestion.flags & SymbolFlags.Class)
29282-
&& !(!!node && excludeClasses && isPropertyAccessExpression(node) && node.expression.kind === SyntaxKind.ThisKeyword);
29282+
&& !(excludeClasses && suggestion && suggestion.flags & SymbolFlags.Class && suggestionHasNoExtends)
29283+
&& !(!!node && excludeClasses && isPropertyAccessExpression(node) && node.expression.kind === SyntaxKind.ThisKeyword && suggestionHasNoExtends);
2928329284
}
2928429285
}
2928529286
return false;
Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,52 @@
11
/// <reference path='fourslash.ts' />
2+
// @allowJs: true
3+
// @Filename: codeFixSpellingJs9.js
4+
//// class C {
5+
//// numble = 1
6+
//// mumble() {
7+
//// return this.[|numbles|]
8+
//// }
9+
//// }
10+
//// class D extends C { }
11+
//// const c = new C()
12+
//// c.[|numbles|] = 3
13+
//// c.[|mumbles|]()
14+
//// const d = new D()
15+
//// d.[|numbles|] = 4
16+
//// d.[|mumbles()|]
17+
//// class Person {
18+
//// getFavoriteColor() {
19+
////
20+
//// }
21+
//// }
22+
////
23+
//// const person = new Person();
24+
//// person.[|getFavoriteColour|]();
25+
//// person.[|getFavoriteColoxr|]();
26+
verify.codeFixAll({
27+
fixId: "fixSpelling",
28+
fixAllDescription: "Fix all detected spelling errors",
29+
newFileContent:
30+
`class C {
31+
numble = 1
32+
mumble() {
33+
return this.numble
34+
}
35+
}
36+
class D extends C { }
37+
const c = new C()
38+
c.numble = 3
39+
c.mumble()
40+
const d = new D()
41+
d.numbles = 4
42+
d.mumbles()
43+
class Person {
44+
getFavoriteColor() {
245
3-
// @allowjs: true
4-
// @noEmit: true
46+
}
47+
}
548
6-
// @filename: noSuggestionWithoutDidYouMean.js
7-
//// let a = {};
8-
//// console.log(a.apple);
9-
verify.noErrors()
10-
verify.getSuggestionDiagnostics([])
49+
const person = new Person();
50+
person.getFavoriteColor();
51+
person.getFavoriteColor();`,
52+
});

0 commit comments

Comments
 (0)