Skip to content

Commit add85e1

Browse files
authored
fix: add corresponding message for override error in js files (#45656)
* fix: add override errors for js class members * fix: test failures of js override error message * update diagnostic messages * fix: test errors * fix: lint errors
1 parent b2f9432 commit add85e1

File tree

6 files changed

+63
-22
lines changed

6 files changed

+63
-22
lines changed

src/compiler/checker.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38042,6 +38042,7 @@ namespace ts {
3804238042
function checkClassMember(member: ClassElement | ParameterPropertyDeclaration, memberIsParameterProperty?: boolean) {
3804338043
const hasOverride = hasOverrideModifier(member);
3804438044
const hasStatic = isStatic(member);
38045+
const isJs = isInJSFile(member);
3804538046
if (baseWithThis && (hasOverride || compilerOptions.noImplicitOverride)) {
3804638047
const declaredProp = member.name && getSymbolAtLocation(member.name) || getSymbolAtLocation(member);
3804738048
if (!declaredProp) {
@@ -38057,8 +38058,19 @@ namespace ts {
3805738058
if (prop && !baseProp && hasOverride) {
3805838059
const suggestion = getSuggestedSymbolForNonexistentClassMember(symbolName(declaredProp), baseType);
3805938060
suggestion ?
38060-
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)) :
38061-
error(member, Diagnostics.This_member_cannot_have_an_override_modifier_because_it_is_not_declared_in_the_base_class_0, baseClassName);
38061+
error(
38062+
member,
38063+
isJs ?
38064+
Diagnostics.This_member_cannot_have_a_JSDoc_comment_with_an_override_tag_because_it_is_not_declared_in_the_base_class_0_Did_you_mean_1 :
38065+
Diagnostics.This_member_cannot_have_an_override_modifier_because_it_is_not_declared_in_the_base_class_0_Did_you_mean_1,
38066+
baseClassName,
38067+
symbolToString(suggestion)) :
38068+
error(
38069+
member,
38070+
isJs ?
38071+
Diagnostics.This_member_cannot_have_a_JSDoc_comment_with_an_override_tag_because_it_is_not_declared_in_the_base_class_0 :
38072+
Diagnostics.This_member_cannot_have_an_override_modifier_because_it_is_not_declared_in_the_base_class_0,
38073+
baseClassName);
3806238074
}
3806338075
else if (prop && baseProp?.declarations && compilerOptions.noImplicitOverride && !nodeInAmbientContext) {
3806438076
const baseHasAbstract = some(baseProp.declarations, hasAbstractModifier);
@@ -38068,8 +38080,12 @@ namespace ts {
3806838080

3806938081
if (!baseHasAbstract) {
3807038082
const diag = memberIsParameterProperty ?
38071-
Diagnostics.This_parameter_property_must_have_an_override_modifier_because_it_overrides_a_member_in_base_class_0 :
38072-
Diagnostics.This_member_must_have_an_override_modifier_because_it_overrides_a_member_in_the_base_class_0;
38083+
isJs ?
38084+
Diagnostics.This_parameter_property_must_have_a_JSDoc_comment_with_an_override_tag_because_it_overrides_a_member_in_the_base_class_0 :
38085+
Diagnostics.This_parameter_property_must_have_an_override_modifier_because_it_overrides_a_member_in_base_class_0 :
38086+
isJs ?
38087+
Diagnostics.This_member_must_have_a_JSDoc_comment_with_an_override_tag_because_it_overrides_a_member_in_the_base_class_0 :
38088+
Diagnostics.This_member_must_have_an_override_modifier_because_it_overrides_a_member_in_the_base_class_0;
3807338089
error(member, diag, baseClassName);
3807438090
}
3807538091
else if (hasAbstractModifier(member) && baseHasAbstract) {
@@ -38079,7 +38095,12 @@ namespace ts {
3807938095
}
3808038096
else if (hasOverride) {
3808138097
const className = typeToString(type);
38082-
error(member, Diagnostics.This_member_cannot_have_an_override_modifier_because_its_containing_class_0_does_not_extend_another_class, className);
38098+
error(
38099+
member,
38100+
isJs ?
38101+
Diagnostics.This_member_cannot_have_a_JSDoc_comment_with_an_override_tag_because_its_containing_class_0_does_not_extend_another_class :
38102+
Diagnostics.This_member_cannot_have_an_override_modifier_because_its_containing_class_0_does_not_extend_another_class,
38103+
className);
3808338104
}
3808438105
}
3808538106
}

src/compiler/diagnosticMessages.json

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3709,7 +3709,26 @@
37093709
"category": "Error",
37103710
"code": 4118
37113711
},
3712-
3712+
"This member must have a JSDoc comment with an '@override' tag because it overrides a member in the base class '{0}'.": {
3713+
"category": "Error",
3714+
"code": 4119
3715+
},
3716+
"This parameter property must have a JSDoc comment with an '@override' tag because it overrides a member in the base class '{0}'.": {
3717+
"category": "Error",
3718+
"code": 4120
3719+
},
3720+
"This member cannot have a JSDoc comment with an '@override' tag because its containing class '{0}' does not extend another class.": {
3721+
"category": "Error",
3722+
"code": 4121
3723+
},
3724+
"This member cannot have a JSDoc comment with an '@override' tag because it is not declared in the base class '{0}'.": {
3725+
"category": "Error",
3726+
"code": 4122
3727+
},
3728+
"This member cannot have a JSDoc comment with an 'override' tag because it is not declared in the base class '{0}'. Did you mean '{1}'?": {
3729+
"category": "Error",
3730+
"code": 4123
3731+
},
37133732
"The current host does not support the '{0}' option.": {
37143733
"category": "Error",
37153734
"code": 5001

src/services/codefixes/fixSpelling.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace ts.codefix {
1010
Diagnostics.Cannot_find_name_0_Did_you_mean_the_static_member_1_0.code,
1111
Diagnostics._0_has_no_exported_member_named_1_Did_you_mean_2.code,
1212
Diagnostics.This_member_cannot_have_an_override_modifier_because_it_is_not_declared_in_the_base_class_0_Did_you_mean_1.code,
13+
Diagnostics.This_member_cannot_have_a_JSDoc_comment_with_an_override_tag_because_it_is_not_declared_in_the_base_class_0_Did_you_mean_1.code,
1314
// for JSX class components
1415
Diagnostics.No_overload_matches_this_call.code,
1516
// for JSX FC

tests/baselines/reference/jsdocOverrideTag1.errors.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
tests/cases/conformance/jsdoc/0.js(27,5): error TS4114: This member must have an 'override' modifier because it overrides a member in the base class 'A'.
2-
tests/cases/conformance/jsdoc/0.js(32,5): error TS4113: This member cannot have an 'override' modifier because it is not declared in the base class 'A'.
3-
tests/cases/conformance/jsdoc/0.js(40,5): error TS4112: This member cannot have an 'override' modifier because its containing class 'C' does not extend another class.
1+
tests/cases/conformance/jsdoc/0.js(27,5): error TS4119: This member must have a JSDoc comment with an '@override' tag because it overrides a member in the base class 'A'.
2+
tests/cases/conformance/jsdoc/0.js(32,5): error TS4122: This member cannot have a JSDoc comment with an '@override' tag because it is not declared in the base class 'A'.
3+
tests/cases/conformance/jsdoc/0.js(40,5): error TS4121: This member cannot have a JSDoc comment with an '@override' tag because its containing class 'C' does not extend another class.
44

55

66
==== tests/cases/conformance/jsdoc/0.js (3 errors) ====
@@ -32,14 +32,14 @@ tests/cases/conformance/jsdoc/0.js(40,5): error TS4112: This member cannot have
3232

3333
bar () {
3434
~~~
35-
!!! error TS4114: This member must have an 'override' modifier because it overrides a member in the base class 'A'.
35+
!!! error TS4119: This member must have a JSDoc comment with an '@override' tag because it overrides a member in the base class 'A'.
3636

3737
}
3838

3939
/** @override */
4040
baz () {
4141
~~~
42-
!!! error TS4113: This member cannot have an 'override' modifier because it is not declared in the base class 'A'.
42+
!!! error TS4122: This member cannot have a JSDoc comment with an '@override' tag because it is not declared in the base class 'A'.
4343

4444
}
4545
}
@@ -49,7 +49,7 @@ tests/cases/conformance/jsdoc/0.js(40,5): error TS4112: This member cannot have
4949
/** @override */
5050
foo () {
5151
~~~
52-
!!! error TS4112: This member cannot have an 'override' modifier because its containing class 'C' does not extend another class.
52+
!!! error TS4121: This member cannot have a JSDoc comment with an '@override' tag because its containing class 'C' does not extend another class.
5353

5454
}
5555
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
tests/cases/conformance/override/a.js(7,5): error TS4114: This member must have an 'override' modifier because it overrides a member in the base class 'B'.
2-
tests/cases/conformance/override/a.js(11,5): error TS4113: This member cannot have an 'override' modifier because it is not declared in the base class 'B'.
3-
tests/cases/conformance/override/a.js(17,5): error TS4112: This member cannot have an 'override' modifier because its containing class 'C' does not extend another class.
4-
tests/cases/conformance/override/a.js(19,5): error TS4112: This member cannot have an 'override' modifier because its containing class 'C' does not extend another class.
1+
tests/cases/conformance/override/a.js(7,5): error TS4119: This member must have a JSDoc comment with an '@override' tag because it overrides a member in the base class 'B'.
2+
tests/cases/conformance/override/a.js(11,5): error TS4122: This member cannot have a JSDoc comment with an '@override' tag because it is not declared in the base class 'B'.
3+
tests/cases/conformance/override/a.js(17,5): error TS4121: This member cannot have a JSDoc comment with an '@override' tag because its containing class 'C' does not extend another class.
4+
tests/cases/conformance/override/a.js(19,5): error TS4121: This member cannot have a JSDoc comment with an '@override' tag because its containing class 'C' does not extend another class.
55

66

77
==== tests/cases/conformance/override/a.js (4 errors) ====
@@ -13,23 +13,23 @@ tests/cases/conformance/override/a.js(19,5): error TS4112: This member cannot ha
1313
class D extends B {
1414
foo (v) {}
1515
~~~
16-
!!! error TS4114: This member must have an 'override' modifier because it overrides a member in the base class 'B'.
16+
!!! error TS4119: This member must have a JSDoc comment with an '@override' tag because it overrides a member in the base class 'B'.
1717
/** @override */
1818
fooo (v) {}
1919
/** @override */
2020
bar(v) {}
2121
~~~
22-
!!! error TS4113: This member cannot have an 'override' modifier because it is not declared in the base class 'B'.
22+
!!! error TS4122: This member cannot have a JSDoc comment with an '@override' tag because it is not declared in the base class 'B'.
2323
}
2424

2525
class C {
2626
foo () {}
2727
/** @override */
2828
fooo (v) {}
2929
~~~~
30-
!!! error TS4112: This member cannot have an 'override' modifier because its containing class 'C' does not extend another class.
30+
!!! error TS4121: This member cannot have a JSDoc comment with an '@override' tag because its containing class 'C' does not extend another class.
3131
/** @override */
3232
bar(v) {}
3333
~~~
34-
!!! error TS4112: This member cannot have an 'override' modifier because its containing class 'C' does not extend another class.
34+
!!! error TS4121: This member cannot have a JSDoc comment with an '@override' tag because its containing class 'C' does not extend another class.
3535
}

tests/baselines/reference/override_js4.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/conformance/override/a.js(7,5): error TS4117: This member cannot have an 'override' modifier because it is not declared in the base class 'A'. Did you mean 'doSomething'?
1+
tests/cases/conformance/override/a.js(7,5): error TS4123: This member cannot have a JSDoc comment with an 'override' tag because it is not declared in the base class 'A'. Did you mean 'doSomething'?
22

33

44
==== tests/cases/conformance/override/a.js (1 errors) ====
@@ -10,6 +10,6 @@ tests/cases/conformance/override/a.js(7,5): error TS4117: This member cannot hav
1010
/** @override */
1111
doSomethang() {}
1212
~~~~~~~~~~~
13-
!!! error TS4117: This member cannot have an 'override' modifier because it is not declared in the base class 'A'. Did you mean 'doSomething'?
13+
!!! error TS4123: This member cannot have a JSDoc comment with an 'override' tag because it is not declared in the base class 'A'. Did you mean 'doSomething'?
1414
}
1515

0 commit comments

Comments
 (0)