Skip to content

Commit 061cd27

Browse files
committed
Add more check
1 parent 6cb7f5e commit 061cd27

File tree

9 files changed

+335
-127
lines changed

9 files changed

+335
-127
lines changed

src/compiler/checker.ts

+22-21
Original file line numberDiff line numberDiff line change
@@ -34375,7 +34375,9 @@ namespace ts {
3437534375
}
3437634376
}
3437734377

34378-
issueMemberWithOverride(node, type, typeWithThis);
34378+
if (compilerOptions.pedanticOverride) {
34379+
issueMemberWithOverride(node, type, typeWithThis);
34380+
}
3437934381

3438034382
const implementedTypeNodes = getEffectiveImplementsTypeNodes(node);
3438134383
if (implementedTypeNodes) {
@@ -34411,32 +34413,31 @@ namespace ts {
3441134413
}
3441234414
}
3441334415

34414-
function issueMemberWithOverride (node: ClassLikeDeclaration, type: InterfaceType, typeWithThis: Type) {
34416+
function issueMemberWithOverride(node: ClassLikeDeclaration, type: InterfaceType, typeWithThis: Type) {
3441534417
const baseTypeNode = getEffectiveBaseTypeNode(node);
34418+
const baseTypes = baseTypeNode && getBaseTypes(type);
34419+
const baseWithThis = baseTypes?.length ? getTypeWithThisArgument(first(baseTypes), type.thisType) : undefined;
34420+
3441634421
for (const member of node.members) {
3441734422
const hasOverride = hasOverrideModifier(member);
34418-
if (hasOverride && !baseTypeNode) {
34419-
error(member, Diagnostics._0_is_deprecated, 'has override but not extended')
34420-
}
34421-
else if (baseTypeNode) {
34422-
const baseTypes = getBaseTypes(type);
34423-
if (baseTypes.length) {
34424-
const baseType = first(baseTypes);
34425-
const baseWithThis = getTypeWithThisArgument(baseType, type.thisType);
34426-
34427-
const declaredProp = member.name && getSymbolAtLocation(member.name) || getSymbolAtLocation(member);
34428-
if (declaredProp) {
34429-
const prop = getPropertyOfType(typeWithThis, declaredProp.escapedName);
34430-
const baseProp = getPropertyOfType(baseWithThis, declaredProp.escapedName);
34431-
if (prop && !baseProp && hasOverride) {
34432-
error(member, Diagnostics._0_is_deprecated, 'not founded in base class')
34433-
}
34434-
else if (prop && baseProp && !hasOverride) {
34435-
error(member, Diagnostics._0_is_deprecated, 'need override')
34436-
}
34423+
if (baseWithThis) {
34424+
const declaredProp = member.name && getSymbolAtLocation(member.name) || getSymbolAtLocation(member);
34425+
if (declaredProp) {
34426+
const baseClassName = typeToString(baseWithThis);
34427+
const prop = getPropertyOfType(typeWithThis, declaredProp.escapedName);
34428+
const baseProp = getPropertyOfType(baseWithThis, declaredProp.escapedName);
34429+
if (prop && !baseProp && hasOverride) {
34430+
error(member, Diagnostics.Method_cannot_have_override_modifier_because_it_s_not_existed_in_the_base_class_0, baseClassName);
34431+
}
34432+
else if (prop && baseProp && !hasOverride) {
34433+
error(member, Diagnostics.Method_must_have_override_modifier_because_it_s_override_the_base_class_0, baseClassName);
3443734434
}
3443834435
}
3443934436
}
34437+
else if (hasOverride) {
34438+
const className = typeToString(type);
34439+
error(member, Diagnostics.Method_cannot_have_override_modifier_because_class_0_does_not_extended_another_class, className);
34440+
}
3444034441
}
3444134442
}
3444234443

src/compiler/commandLineParser.ts

+8
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,14 @@ namespace ts {
606606
category: Diagnostics.Additional_Checks,
607607
description: Diagnostics.Report_errors_for_fallthrough_cases_in_switch_statement
608608
},
609+
{
610+
name: "pedanticOverride",
611+
type: "boolean",
612+
affectsSemanticDiagnostics: true,
613+
showInSimplifiedHelpView: false,
614+
category: Diagnostics.Additional_Checks,
615+
description: Diagnostics.Required_override_modifier_in_class_element
616+
},
609617

610618
// Module Resolution
611619
{

src/compiler/diagnosticMessages.json

+16
Original file line numberDiff line numberDiff line change
@@ -3361,6 +3361,18 @@
33613361
"category": "Error",
33623362
"code": 4110
33633363
},
3364+
"Method cannot have override modifier because class '{0}' does not extended another class.": {
3365+
"category": "Error",
3366+
"code": 4111
3367+
},
3368+
"Method cannot have override modifier because it's not existed in the base class '{0}'.": {
3369+
"category": "Error",
3370+
"code": 4112
3371+
},
3372+
"Method must have override modifier because it's override the base class '{0}'.": {
3373+
"category": "Error",
3374+
"code": 4113
3375+
},
33643376

33653377
"The current host does not support the '{0}' option.": {
33663378
"category": "Error",
@@ -4653,6 +4665,10 @@
46534665
"category": "Error",
46544666
"code": 6504
46554667
},
4668+
"Required override modifier in class element": {
4669+
"category": "Error",
4670+
"code": 6801
4671+
},
46564672

46574673
"Variable '{0}' implicitly has an '{1}' type.": {
46584674
"category": "Error",

src/compiler/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5719,6 +5719,7 @@ namespace ts {
57195719
paths?: MapLike<string[]>;
57205720
/*@internal*/ plugins?: PluginImport[];
57215721
preserveConstEnums?: boolean;
5722+
pedanticOverride?: boolean;
57225723
preserveSymlinks?: boolean;
57235724
/* @internal */ preserveWatchOutput?: boolean;
57245725
project?: string;
+54-23
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,71 @@
1-
tests/cases/conformance/override/override1.ts(15,5): suggestion TS6385: 'need override' is deprecated
2-
tests/cases/conformance/override/override1.ts(19,14): suggestion TS6385: 'not founded in base class' is deprecated
3-
tests/cases/conformance/override/override1.ts(25,14): suggestion TS6385: 'has override but not extended' is deprecated
1+
tests/cases/conformance/override/override1.ts(9,5): error TS4113: Method must have override modifier because it's override the base class 'B'.
2+
tests/cases/conformance/override/override1.ts(11,14): error TS4112: Method cannot have override modifier because it's not existed in the base class 'B'.
3+
tests/cases/conformance/override/override1.ts(15,14): error TS4111: Method cannot have override modifier because class 'C' does not extended another class.
4+
tests/cases/conformance/override/override1.ts(22,9): error TS4113: Method must have override modifier because it's override the base class 'B'.
5+
tests/cases/conformance/override/override1.ts(24,18): error TS4112: Method cannot have override modifier because it's not existed in the base class 'B'.
6+
tests/cases/conformance/override/override1.ts(33,5): error TS4113: Method must have override modifier because it's override the base class '(Anonymous class)'.
7+
tests/cases/conformance/override/override1.ts(37,14): error TS4112: Method cannot have override modifier because it's not existed in the base class '(Anonymous class)'.
8+
tests/cases/conformance/override/override1.ts(42,18): error TS4111: Method cannot have override modifier because class '(Anonymous class)' does not extended another class.
49

510

6-
==== tests/cases/conformance/override/override1.ts (3 errors) ====
11+
==== tests/cases/conformance/override/override1.ts (8 errors) ====
712
class B {
8-
foo (v: string) {
9-
10-
}
11-
fooo (v: string) {
12-
13-
}
13+
foo (v: string) {}
14+
fooo (v: string) {}
1415
}
1516

1617
class D extends B {
17-
override foo (v: string) {
18+
override foo (v: string) {}
1819

19-
}
20-
21-
fooo (v: string) {
20+
fooo (v: string) {}
2221
~~~~
23-
!!! suggestion TS6385: 'need override' is deprecated
24-
25-
}
22+
!!! error TS4113: Method must have override modifier because it's override the base class 'B'.
2623

27-
override bar(v: string) {
24+
override bar(v: string) {}
2825
~~~
29-
!!! suggestion TS6385: 'not founded in base class' is deprecated
30-
31-
}
26+
!!! error TS4112: Method cannot have override modifier because it's not existed in the base class 'B'.
3227
}
3328

3429
class C {
35-
override foo(v: string) {
30+
override foo(v: string) {}
3631
~~~
37-
!!! suggestion TS6385: 'has override but not extended' is deprecated
32+
!!! error TS4111: Method cannot have override modifier because class 'C' does not extended another class.
33+
}
34+
35+
function f () {
36+
return class extends B {
37+
override foo (v: string) {}
38+
39+
fooo (v: string) {}
40+
~~~~
41+
!!! error TS4113: Method must have override modifier because it's override the base class 'B'.
42+
43+
override bar(v: string) {}
44+
~~~
45+
!!! error TS4112: Method cannot have override modifier because it's not existed in the base class 'B'.
46+
}
47+
}
48+
49+
class E extends (class {
50+
foo () { }
51+
bar () { }
52+
}) {
53+
override foo () { }
54+
bar () { }
55+
~~~
56+
!!! error TS4113: Method must have override modifier because it's override the base class '(Anonymous class)'.
57+
58+
baz() {}
59+
60+
override bazz () {}
61+
~~~~
62+
!!! error TS4112: Method cannot have override modifier because it's not existed in the base class '(Anonymous class)'.
63+
}
3864

65+
function ff () {
66+
return class {
67+
override foo () {}
68+
~~~
69+
!!! error TS4111: Method cannot have override modifier because class '(Anonymous class)' does not extended another class.
3970
}
4071
}
+74-27
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,46 @@
11
//// [override1.ts]
22
class B {
3-
foo (v: string) {
4-
5-
}
6-
fooo (v: string) {
7-
8-
}
3+
foo (v: string) {}
4+
fooo (v: string) {}
95
}
106

117
class D extends B {
12-
override foo (v: string) {
8+
override foo (v: string) {}
139

14-
}
10+
fooo (v: string) {}
1511

16-
fooo (v: string) {
17-
18-
}
12+
override bar(v: string) {}
13+
}
1914

20-
override bar(v: string) {
21-
15+
class C {
16+
override foo(v: string) {}
17+
}
18+
19+
function f () {
20+
return class extends B {
21+
override foo (v: string) {}
22+
23+
fooo (v: string) {}
24+
25+
override bar(v: string) {}
2226
}
2327
}
2428

25-
class C {
26-
override foo(v: string) {
29+
class E extends (class {
30+
foo () { }
31+
bar () { }
32+
}) {
33+
override foo () { }
34+
bar () { }
35+
36+
baz() {}
2737

38+
override bazz () {}
39+
}
40+
41+
function ff () {
42+
return class {
43+
override foo () {}
2844
}
2945
}
3046

@@ -45,29 +61,60 @@ var __extends = (this && this.__extends) || (function () {
4561
var B = /** @class */ (function () {
4662
function B() {
4763
}
48-
B.prototype.foo = function (v) {
49-
};
50-
B.prototype.fooo = function (v) {
51-
};
64+
B.prototype.foo = function (v) { };
65+
B.prototype.fooo = function (v) { };
5266
return B;
5367
}());
5468
var D = /** @class */ (function (_super) {
5569
__extends(D, _super);
5670
function D() {
5771
return _super !== null && _super.apply(this, arguments) || this;
5872
}
59-
D.prototype.foo = function (v) {
60-
};
61-
D.prototype.fooo = function (v) {
62-
};
63-
D.prototype.bar = function (v) {
64-
};
73+
D.prototype.foo = function (v) { };
74+
D.prototype.fooo = function (v) { };
75+
D.prototype.bar = function (v) { };
6576
return D;
6677
}(B));
6778
var C = /** @class */ (function () {
6879
function C() {
6980
}
70-
C.prototype.foo = function (v) {
71-
};
81+
C.prototype.foo = function (v) { };
7282
return C;
7383
}());
84+
function f() {
85+
return /** @class */ (function (_super) {
86+
__extends(class_1, _super);
87+
function class_1() {
88+
return _super !== null && _super.apply(this, arguments) || this;
89+
}
90+
class_1.prototype.foo = function (v) { };
91+
class_1.prototype.fooo = function (v) { };
92+
class_1.prototype.bar = function (v) { };
93+
return class_1;
94+
}(B));
95+
}
96+
var E = /** @class */ (function (_super) {
97+
__extends(E, _super);
98+
function E() {
99+
return _super !== null && _super.apply(this, arguments) || this;
100+
}
101+
E.prototype.foo = function () { };
102+
E.prototype.bar = function () { };
103+
E.prototype.baz = function () { };
104+
E.prototype.bazz = function () { };
105+
return E;
106+
}((/** @class */ (function () {
107+
function class_2() {
108+
}
109+
class_2.prototype.foo = function () { };
110+
class_2.prototype.bar = function () { };
111+
return class_2;
112+
}()))));
113+
function ff() {
114+
return /** @class */ (function () {
115+
function class_3() {
116+
}
117+
class_3.prototype.foo = function () { };
118+
return class_3;
119+
}());
120+
}

0 commit comments

Comments
 (0)