Skip to content

Commit b467cd8

Browse files
authored
Move property errors to the name for the error span (#23865)
1 parent 616e6e6 commit b467cd8

File tree

52 files changed

+617
-294
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+617
-294
lines changed

src/compiler/utilities.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,8 @@ namespace ts {
688688
case SyntaxKind.GetAccessor:
689689
case SyntaxKind.SetAccessor:
690690
case SyntaxKind.TypeAliasDeclaration:
691+
case SyntaxKind.PropertyDeclaration:
692+
case SyntaxKind.PropertySignature:
691693
errorNode = (<NamedDeclaration>node).name;
692694
break;
693695
case SyntaxKind.ArrowFunction:
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
tests/cases/compiler/ClassDeclarationWithInvalidConstOnPropertyDeclaration.ts(2,3): error TS1248: A class member cannot have the 'const' keyword.
1+
tests/cases/compiler/ClassDeclarationWithInvalidConstOnPropertyDeclaration.ts(2,16): error TS1248: A class member cannot have the 'const' keyword.
22

33

44
==== tests/cases/compiler/ClassDeclarationWithInvalidConstOnPropertyDeclaration.ts (1 errors) ====
55
class AtomicNumbers {
66
static const H = 1;
7-
~~~~~~~~~~~~~~~~~~~
7+
~
88
!!! error TS1248: A class member cannot have the 'const' keyword.
99
}

tests/baselines/reference/circularIndexedAccessErrors.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ tests/cases/conformance/types/keyof/circularIndexedAccessErrors.ts(37,30): error
99
==== tests/cases/conformance/types/keyof/circularIndexedAccessErrors.ts (6 errors) ====
1010
type T1 = {
1111
x: T1["x"]; // Error
12-
~~~~~~~~~~~
12+
~
1313
!!! error TS2502: 'x' is referenced directly or indirectly in its own type annotation.
1414
};
1515

1616
type T2<K extends "x" | "y"> = {
1717
x: T2<K>[K]; // Error
18-
~~~~~~~~~~~~
18+
~
1919
!!! error TS2502: 'x' is referenced directly or indirectly in its own type annotation.
2020
y: number;
2121
}
@@ -29,13 +29,13 @@ tests/cases/conformance/types/keyof/circularIndexedAccessErrors.ts(37,30): error
2929

3030
interface T4<T extends T4<T>> {
3131
x: T4<T>["x"]; // Error
32-
~~~~~~~~~~~~~~
32+
~
3333
!!! error TS2502: 'x' is referenced directly or indirectly in its own type annotation.
3434
}
3535

3636
class C1 {
3737
x: C1["x"]; // Error
38-
~~~~~~~~~~~
38+
~
3939
!!! error TS2502: 'x' is referenced directly or indirectly in its own type annotation.
4040
}
4141

tests/baselines/reference/classIndexer2.errors.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ tests/cases/compiler/classIndexer2.ts(4,5): error TS2411: Property 'y' of type '
66
[s: string]: number;
77
x: number;
88
y: string;
9-
~~~~~~~~~~
9+
~
1010
!!! error TS2411: Property 'y' of type 'string' is not assignable to string index type 'number'.
1111
constructor() {
1212
}

tests/baselines/reference/classIndexer3.errors.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ tests/cases/compiler/classIndexer3.ts(9,5): error TS2411: Property 'y' of type '
1111
class D123 extends C123 {
1212
x: number;
1313
y: string;
14-
~~~~~~~~~~
14+
~
1515
!!! error TS2411: Property 'y' of type 'string' is not assignable to string index type 'number'.
1616
}

tests/baselines/reference/classIndexer4.errors.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ tests/cases/compiler/classIndexer4.ts(9,5): error TS2411: Property 'y' of type '
1111
interface D123 extends C123 {
1212
x: number;
1313
y: string;
14-
~~~~~~~~~~
14+
~
1515
!!! error TS2411: Property 'y' of type 'string' is not assignable to string index type 'number'.
1616
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
tests/cases/compiler/classPropertyErrorOnNameOnly.ts(7,3): error TS2322: Type '(val: Values) => "1" | "2" | "3" | "4" | "5" | undefined' is not assignable to type 'FuncType'.
2+
Type '"1" | "2" | "3" | "4" | "5" | undefined' is not assignable to type 'string'.
3+
Type 'undefined' is not assignable to type 'string'.
4+
tests/cases/compiler/classPropertyErrorOnNameOnly.ts(24,7): error TS2322: Type '(val: Values) => "1" | "2" | "3" | "4" | "5" | undefined' is not assignable to type 'FuncType'.
5+
Type '"1" | "2" | "3" | "4" | "5" | undefined' is not assignable to type 'string'.
6+
Type 'undefined' is not assignable to type 'string'.
7+
8+
9+
==== tests/cases/compiler/classPropertyErrorOnNameOnly.ts (2 errors) ====
10+
type Values = 1 | 2 | 3 | 4 | 5 | 6
11+
12+
type FuncType = (arg: Values) => string
13+
14+
// turn on strictNullChecks
15+
class Example {
16+
insideClass: FuncType = function(val) { // error span goes from here
17+
~~~~~~~~~~~
18+
!!! error TS2322: Type '(val: Values) => "1" | "2" | "3" | "4" | "5" | undefined' is not assignable to type 'FuncType'.
19+
!!! error TS2322: Type '"1" | "2" | "3" | "4" | "5" | undefined' is not assignable to type 'string'.
20+
!!! error TS2322: Type 'undefined' is not assignable to type 'string'.
21+
switch (val) {
22+
case 1:
23+
return "1";
24+
case 2:
25+
return "2";
26+
case 3:
27+
return "3"
28+
case 4:
29+
return "4"
30+
case 5:
31+
return "5"
32+
// forgot case 6
33+
}
34+
} // all the way to here
35+
}
36+
37+
const outsideClass: FuncType = function(val) { // compare to errors only on this line in this case
38+
~~~~~~~~~~~~
39+
!!! error TS2322: Type '(val: Values) => "1" | "2" | "3" | "4" | "5" | undefined' is not assignable to type 'FuncType'.
40+
!!! error TS2322: Type '"1" | "2" | "3" | "4" | "5" | undefined' is not assignable to type 'string'.
41+
!!! error TS2322: Type 'undefined' is not assignable to type 'string'.
42+
switch (val) {
43+
case 1:
44+
return "1";
45+
case 2:
46+
return "2";
47+
case 3:
48+
return "3"
49+
case 4:
50+
return "4"
51+
case 5:
52+
return "5"
53+
// forgot case 6
54+
}
55+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//// [classPropertyErrorOnNameOnly.ts]
2+
type Values = 1 | 2 | 3 | 4 | 5 | 6
3+
4+
type FuncType = (arg: Values) => string
5+
6+
// turn on strictNullChecks
7+
class Example {
8+
insideClass: FuncType = function(val) { // error span goes from here
9+
switch (val) {
10+
case 1:
11+
return "1";
12+
case 2:
13+
return "2";
14+
case 3:
15+
return "3"
16+
case 4:
17+
return "4"
18+
case 5:
19+
return "5"
20+
// forgot case 6
21+
}
22+
} // all the way to here
23+
}
24+
25+
const outsideClass: FuncType = function(val) { // compare to errors only on this line in this case
26+
switch (val) {
27+
case 1:
28+
return "1";
29+
case 2:
30+
return "2";
31+
case 3:
32+
return "3"
33+
case 4:
34+
return "4"
35+
case 5:
36+
return "5"
37+
// forgot case 6
38+
}
39+
}
40+
41+
//// [classPropertyErrorOnNameOnly.js]
42+
"use strict";
43+
// turn on strictNullChecks
44+
var Example = /** @class */ (function () {
45+
function Example() {
46+
this.insideClass = function (val) {
47+
switch (val) {
48+
case 1:
49+
return "1";
50+
case 2:
51+
return "2";
52+
case 3:
53+
return "3";
54+
case 4:
55+
return "4";
56+
case 5:
57+
return "5";
58+
// forgot case 6
59+
}
60+
}; // all the way to here
61+
}
62+
return Example;
63+
}());
64+
var outsideClass = function (val) {
65+
switch (val) {
66+
case 1:
67+
return "1";
68+
case 2:
69+
return "2";
70+
case 3:
71+
return "3";
72+
case 4:
73+
return "4";
74+
case 5:
75+
return "5";
76+
// forgot case 6
77+
}
78+
};
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
=== tests/cases/compiler/classPropertyErrorOnNameOnly.ts ===
2+
type Values = 1 | 2 | 3 | 4 | 5 | 6
3+
>Values : Symbol(Values, Decl(classPropertyErrorOnNameOnly.ts, 0, 0))
4+
5+
type FuncType = (arg: Values) => string
6+
>FuncType : Symbol(FuncType, Decl(classPropertyErrorOnNameOnly.ts, 0, 35))
7+
>arg : Symbol(arg, Decl(classPropertyErrorOnNameOnly.ts, 2, 17))
8+
>Values : Symbol(Values, Decl(classPropertyErrorOnNameOnly.ts, 0, 0))
9+
10+
// turn on strictNullChecks
11+
class Example {
12+
>Example : Symbol(Example, Decl(classPropertyErrorOnNameOnly.ts, 2, 39))
13+
14+
insideClass: FuncType = function(val) { // error span goes from here
15+
>insideClass : Symbol(Example.insideClass, Decl(classPropertyErrorOnNameOnly.ts, 5, 15))
16+
>FuncType : Symbol(FuncType, Decl(classPropertyErrorOnNameOnly.ts, 0, 35))
17+
>val : Symbol(val, Decl(classPropertyErrorOnNameOnly.ts, 6, 35))
18+
19+
switch (val) {
20+
>val : Symbol(val, Decl(classPropertyErrorOnNameOnly.ts, 6, 35))
21+
22+
case 1:
23+
return "1";
24+
case 2:
25+
return "2";
26+
case 3:
27+
return "3"
28+
case 4:
29+
return "4"
30+
case 5:
31+
return "5"
32+
// forgot case 6
33+
}
34+
} // all the way to here
35+
}
36+
37+
const outsideClass: FuncType = function(val) { // compare to errors only on this line in this case
38+
>outsideClass : Symbol(outsideClass, Decl(classPropertyErrorOnNameOnly.ts, 23, 5))
39+
>FuncType : Symbol(FuncType, Decl(classPropertyErrorOnNameOnly.ts, 0, 35))
40+
>val : Symbol(val, Decl(classPropertyErrorOnNameOnly.ts, 23, 40))
41+
42+
switch (val) {
43+
>val : Symbol(val, Decl(classPropertyErrorOnNameOnly.ts, 23, 40))
44+
45+
case 1:
46+
return "1";
47+
case 2:
48+
return "2";
49+
case 3:
50+
return "3"
51+
case 4:
52+
return "4"
53+
case 5:
54+
return "5"
55+
// forgot case 6
56+
}
57+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
=== tests/cases/compiler/classPropertyErrorOnNameOnly.ts ===
2+
type Values = 1 | 2 | 3 | 4 | 5 | 6
3+
>Values : Values
4+
5+
type FuncType = (arg: Values) => string
6+
>FuncType : FuncType
7+
>arg : Values
8+
>Values : Values
9+
10+
// turn on strictNullChecks
11+
class Example {
12+
>Example : Example
13+
14+
insideClass: FuncType = function(val) { // error span goes from here
15+
>insideClass : FuncType
16+
>FuncType : FuncType
17+
>function(val) { // error span goes from here switch (val) { case 1: return "1"; case 2: return "2"; case 3: return "3" case 4: return "4" case 5: return "5" // forgot case 6 } } : (val: Values) => "1" | "2" | "3" | "4" | "5" | undefined
18+
>val : Values
19+
20+
switch (val) {
21+
>val : Values
22+
23+
case 1:
24+
>1 : 1
25+
26+
return "1";
27+
>"1" : "1"
28+
29+
case 2:
30+
>2 : 2
31+
32+
return "2";
33+
>"2" : "2"
34+
35+
case 3:
36+
>3 : 3
37+
38+
return "3"
39+
>"3" : "3"
40+
41+
case 4:
42+
>4 : 4
43+
44+
return "4"
45+
>"4" : "4"
46+
47+
case 5:
48+
>5 : 5
49+
50+
return "5"
51+
>"5" : "5"
52+
53+
// forgot case 6
54+
}
55+
} // all the way to here
56+
}
57+
58+
const outsideClass: FuncType = function(val) { // compare to errors only on this line in this case
59+
>outsideClass : FuncType
60+
>FuncType : FuncType
61+
>function(val) { // compare to errors only on this line in this case switch (val) { case 1: return "1"; case 2: return "2"; case 3: return "3" case 4: return "4" case 5: return "5" // forgot case 6 }} : (val: Values) => "1" | "2" | "3" | "4" | "5" | undefined
62+
>val : Values
63+
64+
switch (val) {
65+
>val : Values
66+
67+
case 1:
68+
>1 : 1
69+
70+
return "1";
71+
>"1" : "1"
72+
73+
case 2:
74+
>2 : 2
75+
76+
return "2";
77+
>"2" : "2"
78+
79+
case 3:
80+
>3 : 3
81+
82+
return "3"
83+
>"3" : "3"
84+
85+
case 4:
86+
>4 : 4
87+
88+
return "4"
89+
>"4" : "4"
90+
91+
case 5:
92+
>5 : 5
93+
94+
return "5"
95+
>"5" : "5"
96+
97+
// forgot case 6
98+
}
99+
}

tests/baselines/reference/computedPropertyNames42_ES5.errors.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ tests/cases/conformance/es6/computedProperties/computedPropertyNames42_ES5.ts(8,
1010

1111
// Computed properties
1212
[""]: Foo;
13-
~~~~~~~~~~
13+
~~~~
1414
!!! error TS2411: Property '[""]' of type 'Foo' is not assignable to string index type 'Foo2'.
1515
}

tests/baselines/reference/computedPropertyNames42_ES6.errors.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ tests/cases/conformance/es6/computedProperties/computedPropertyNames42_ES6.ts(8,
1010

1111
// Computed properties
1212
[""]: Foo;
13-
~~~~~~~~~~
13+
~~~~
1414
!!! error TS2411: Property '[""]' of type 'Foo' is not assignable to string index type 'Foo2'.
1515
}

0 commit comments

Comments
 (0)