Skip to content

Commit 4559855

Browse files
authored
feat(44190): check misspelled base members in override checks (microsoft#44213)
1 parent b7b4856 commit 4559855

File tree

7 files changed

+103
-1
lines changed

7 files changed

+103
-1
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37308,7 +37308,10 @@ namespace ts {
3730837308

3730937309
const baseClassName = typeToString(baseWithThis);
3731037310
if (prop && !baseProp && hasOverride) {
37311-
error(member, Diagnostics.This_member_cannot_have_an_override_modifier_because_it_is_not_declared_in_the_base_class_0, baseClassName);
37311+
const suggestion = getSpellingSuggestionForName(symbolName(declaredProp), arrayFrom(getMembersOfSymbol(baseType.symbol).values()), SymbolFlags.ClassMember);
37312+
suggestion ?
37313+
error(member, Diagnostics.This_member_cannot_have_an_override_modifier_because_it_is_not_declared_in_the_base_class_0_Did_you_mean_1, baseClassName, symbolToString(suggestion)) :
37314+
error(member, Diagnostics.This_member_cannot_have_an_override_modifier_because_it_is_not_declared_in_the_base_class_0, baseClassName);
3731237315
}
3731337316
else if (prop && baseProp?.valueDeclaration && compilerOptions.noImplicitOverride && !nodeInAmbientContext) {
3731437317
const baseHasAbstract = hasAbstractModifier(baseProp.valueDeclaration);

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3733,6 +3733,10 @@
37333733
"category": "Error",
37343734
"code": 4116
37353735
},
3736+
"This member cannot have an 'override' modifier because it is not declared in the base class '{0}'. Did you mean '{1}'?": {
3737+
"category": "Error",
3738+
"code": 4117
3739+
},
37363740

37373741
"The current host does not support the '{0}' option.": {
37383742
"category": "Error",
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
tests/cases/conformance/override/override15.ts(6,14): error TS4117: This member cannot have an 'override' modifier because it is not declared in the base class 'A'. Did you mean 'doSomething'?
2+
3+
4+
==== tests/cases/conformance/override/override15.ts (1 errors) ====
5+
class A {
6+
doSomething() {}
7+
}
8+
9+
class B extends A {
10+
override doSomethang() {}
11+
~~~~~~~~~~~
12+
!!! error TS4117: This member cannot have an 'override' modifier because it is not declared in the base class 'A'. Did you mean 'doSomething'?
13+
}
14+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//// [override15.ts]
2+
class A {
3+
doSomething() {}
4+
}
5+
6+
class B extends A {
7+
override doSomethang() {}
8+
}
9+
10+
11+
//// [override15.js]
12+
var __extends = (this && this.__extends) || (function () {
13+
var extendStatics = function (d, b) {
14+
extendStatics = Object.setPrototypeOf ||
15+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
16+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
17+
return extendStatics(d, b);
18+
};
19+
return function (d, b) {
20+
if (typeof b !== "function" && b !== null)
21+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
22+
extendStatics(d, b);
23+
function __() { this.constructor = d; }
24+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
25+
};
26+
})();
27+
var A = /** @class */ (function () {
28+
function A() {
29+
}
30+
A.prototype.doSomething = function () { };
31+
return A;
32+
}());
33+
var B = /** @class */ (function (_super) {
34+
__extends(B, _super);
35+
function B() {
36+
return _super !== null && _super.apply(this, arguments) || this;
37+
}
38+
B.prototype.doSomethang = function () { };
39+
return B;
40+
}(A));
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/conformance/override/override15.ts ===
2+
class A {
3+
>A : Symbol(A, Decl(override15.ts, 0, 0))
4+
5+
doSomething() {}
6+
>doSomething : Symbol(A.doSomething, Decl(override15.ts, 0, 9))
7+
}
8+
9+
class B extends A {
10+
>B : Symbol(B, Decl(override15.ts, 2, 1))
11+
>A : Symbol(A, Decl(override15.ts, 0, 0))
12+
13+
override doSomethang() {}
14+
>doSomethang : Symbol(B.doSomethang, Decl(override15.ts, 4, 19))
15+
}
16+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/conformance/override/override15.ts ===
2+
class A {
3+
>A : A
4+
5+
doSomething() {}
6+
>doSomething : () => void
7+
}
8+
9+
class B extends A {
10+
>B : B
11+
>A : A
12+
13+
override doSomethang() {}
14+
>doSomethang : () => void
15+
}
16+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @noImplicitOverride: true
2+
3+
class A {
4+
doSomething() {}
5+
}
6+
7+
class B extends A {
8+
override doSomethang() {}
9+
}

0 commit comments

Comments
 (0)