Skip to content

Commit f3fe501

Browse files
committed
feat: restrict-template-expressions: move type to labeled range (#724)
The current error message is very awkwardly worded, moving the type to a labeled range makes it read more clearly I think.
1 parent 187c803 commit f3fe501

File tree

3 files changed

+179
-29
lines changed

3 files changed

+179
-29
lines changed

e2e/__snapshots__/snapshot.test.ts.snap

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3338,8 +3338,17 @@ exports[`TSGoLint E2E Snapshot Tests > should generate consistent diagnostics sn
33383338
{
33393339
"file_path": "fixtures/basic/rules/restrict-template-expressions/index.ts",
33403340
"kind": 0,
3341+
"labeled_ranges": [
3342+
{
3343+
"label": "Type: { prop: any; }",
3344+
"range": {
3345+
"end": 247,
3346+
"pos": 244,
3347+
},
3348+
},
3349+
],
33413350
"message": {
3342-
"description": "Invalid type "{ prop: any; }" of template literal expression.",
3351+
"description": "Invalid type used in template literal expression.",
33433352
"id": "invalidType",
33443353
},
33453354
"range": {
@@ -3351,8 +3360,17 @@ exports[`TSGoLint E2E Snapshot Tests > should generate consistent diagnostics sn
33513360
{
33523361
"file_path": "fixtures/basic/rules/restrict-template-expressions/index.ts",
33533362
"kind": 0,
3363+
"labeled_ranges": [
3364+
{
3365+
"label": "Type: symbol",
3366+
"range": {
3367+
"end": 319,
3368+
"pos": 316,
3369+
},
3370+
},
3371+
],
33543372
"message": {
3355-
"description": "Invalid type "symbol" of template literal expression.",
3373+
"description": "Invalid type used in template literal expression.",
33563374
"id": "invalidType",
33573375
},
33583376
"range": {
@@ -3364,8 +3382,17 @@ exports[`TSGoLint E2E Snapshot Tests > should generate consistent diagnostics sn
33643382
{
33653383
"file_path": "fixtures/basic/rules/restrict-template-expressions/index.ts",
33663384
"kind": 0,
3385+
"labeled_ranges": [
3386+
{
3387+
"label": "Type: Function",
3388+
"range": {
3389+
"end": 400,
3390+
"pos": 398,
3391+
},
3392+
},
3393+
],
33673394
"message": {
3368-
"description": "Invalid type "Function" of template literal expression.",
3395+
"description": "Invalid type used in template literal expression.",
33693396
"id": "invalidType",
33703397
},
33713398
"range": {
@@ -3377,8 +3404,17 @@ exports[`TSGoLint E2E Snapshot Tests > should generate consistent diagnostics sn
33773404
{
33783405
"file_path": "fixtures/basic/rules/restrict-template-expressions/index.ts",
33793406
"kind": 0,
3407+
"labeled_ranges": [
3408+
{
3409+
"label": "Type: number[]",
3410+
"range": {
3411+
"end": 468,
3412+
"pos": 465,
3413+
},
3414+
},
3415+
],
33803416
"message": {
3381-
"description": "Invalid type "number[]" of template literal expression.",
3417+
"description": "Invalid type used in template literal expression.",
33823418
"id": "invalidType",
33833419
},
33843420
"range": {
@@ -4830,8 +4866,17 @@ exports[`TSGoLint E2E Snapshot Tests > should handle circular project references
48304866
{
48314867
"file_path": "fixtures/circular-project-references/project2/src/demo/index.ts",
48324868
"kind": 0,
4869+
"labeled_ranges": [
4870+
{
4871+
"label": "Type: { x: number; }",
4872+
"range": {
4873+
"end": 66,
4874+
"pos": 60,
4875+
},
4876+
},
4877+
],
48334878
"message": {
4834-
"description": "Invalid type "{ x: number; }" of template literal expression.",
4879+
"description": "Invalid type used in template literal expression.",
48354880
"id": "invalidType",
48364881
},
48374882
"range": {
Lines changed: 110 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,167 +1,258 @@
11

22
[TestRestrictTemplateExpressionsRule/invalid-0 - 1]
33
Diagnostic 1: invalidType (2:30 - 2:32)
4-
Message: Invalid type "123" of template literal expression.
4+
Message: Invalid type used in template literal expression.
55
1 |
66
2 | const msg = `arg = ${123}`;
77
| ~~~
88
3 |
9+
Label: Type: 123 (2:30 - 2:32)
10+
1 |
11+
2 | const msg = `arg = ${123}`;
12+
| ^^^ Type: 123
13+
3 |
914
---
1015

1116
[TestRestrictTemplateExpressionsRule/invalid-1 - 1]
1217
Diagnostic 1: invalidType (2:30 - 2:34)
13-
Message: Invalid type "false" of template literal expression.
18+
Message: Invalid type used in template literal expression.
1419
1 |
1520
2 | const msg = `arg = ${false}`;
1621
| ~~~~~
1722
3 |
23+
Label: Type: false (2:30 - 2:34)
24+
1 |
25+
2 | const msg = `arg = ${false}`;
26+
| ^^^^^ Type: false
27+
3 |
1828
---
1929

2030
[TestRestrictTemplateExpressionsRule/invalid-10 - 1]
2131
Diagnostic 1: invalidType (3:30 - 3:32)
22-
Message: Invalid type "{}" of template literal expression.
32+
Message: Invalid type used in template literal expression.
2333
2 | const arg = {};
2434
3 | const msg = `arg = ${arg}`;
2535
| ~~~
2636
4 |
37+
Label: Type: {} (3:30 - 3:32)
38+
2 | const arg = {};
39+
3 | const msg = `arg = ${arg}`;
40+
| ^^^ Type: {}
41+
4 |
2742
---
2843

2944
[TestRestrictTemplateExpressionsRule/invalid-11 - 1]
3045
Diagnostic 1: invalidType (3:30 - 3:32)
31-
Message: Invalid type "{ a: string; } & { b: string; }" of template literal expression.
46+
Message: Invalid type used in template literal expression.
3247
2 | declare const arg: { a: string } & { b: string };
3348
3 | const msg = `arg = ${arg}`;
3449
| ~~~
3550
4 |
51+
Label: Type: { a: string; } & { b: string; } (3:30 - 3:32)
52+
2 | declare const arg: { a: string } & { b: string };
53+
3 | const msg = `arg = ${arg}`;
54+
| ^^^ Type: { a: string; } & { b: string; }
55+
4 |
3656
---
3757

3858
[TestRestrictTemplateExpressionsRule/invalid-12 - 1]
3959
Diagnostic 1: invalidType (3:27 - 3:29)
40-
Message: Invalid type "{}" of template literal expression.
60+
Message: Invalid type used in template literal expression.
4161
2 | function test<T extends {}>(arg: T) {
4262
3 | return `arg = ${arg}`;
4363
| ~~~
4464
4 | }
65+
Label: Type: {} (3:27 - 3:29)
66+
2 | function test<T extends {}>(arg: T) {
67+
3 | return `arg = ${arg}`;
68+
| ^^^ Type: {}
69+
4 | }
4570
---
4671

4772
[TestRestrictTemplateExpressionsRule/invalid-13 - 1]
4873
Diagnostic 1: invalidType (3:27 - 3:29)
49-
Message: Invalid type "T" of template literal expression.
74+
Message: Invalid type used in template literal expression.
5075
2 | function test<TWithNoConstraint>(arg: T) {
5176
3 | return `arg = ${arg}`;
5277
| ~~~
5378
4 | }
79+
Label: Type: T (3:27 - 3:29)
80+
2 | function test<TWithNoConstraint>(arg: T) {
81+
3 | return `arg = ${arg}`;
82+
| ^^^ Type: T
83+
4 | }
5484
---
5585

5686
[TestRestrictTemplateExpressionsRule/invalid-14 - 1]
5787
Diagnostic 1: invalidType (3:27 - 3:29)
58-
Message: Invalid type "any" of template literal expression.
88+
Message: Invalid type used in template literal expression.
5989
2 | function test(arg: any) {
6090
3 | return `arg = ${arg}`;
6191
| ~~~
6292
4 | }
93+
Label: Type: any (3:27 - 3:29)
94+
2 | function test(arg: any) {
95+
3 | return `arg = ${arg}`;
96+
| ^^^ Type: any
97+
4 | }
6398
---
6499

65100
[TestRestrictTemplateExpressionsRule/invalid-15 - 1]
66101
Diagnostic 1: invalidType (3:30 - 3:32)
67-
Message: Invalid type "RegExp" of template literal expression.
102+
Message: Invalid type used in template literal expression.
68103
2 | const arg = new RegExp('foo');
69104
3 | const msg = `arg = ${arg}`;
70105
| ~~~
71106
4 |
107+
Label: Type: RegExp (3:30 - 3:32)
108+
2 | const arg = new RegExp('foo');
109+
3 | const msg = `arg = ${arg}`;
110+
| ^^^ Type: RegExp
111+
4 |
72112
---
73113

74114
[TestRestrictTemplateExpressionsRule/invalid-16 - 1]
75115
Diagnostic 1: invalidType (3:30 - 3:32)
76-
Message: Invalid type "RegExp" of template literal expression.
116+
Message: Invalid type used in template literal expression.
77117
2 | const arg = /foo/;
78118
3 | const msg = `arg = ${arg}`;
79119
| ~~~
80120
4 |
121+
Label: Type: RegExp (3:30 - 3:32)
122+
2 | const arg = /foo/;
123+
3 | const msg = `arg = ${arg}`;
124+
| ^^^ Type: RegExp
125+
4 |
81126
---
82127

83128
[TestRestrictTemplateExpressionsRule/invalid-17 - 1]
84129
Diagnostic 1: invalidType (3:28 - 3:32)
85-
Message: Invalid type "never" of template literal expression.
130+
Message: Invalid type used in template literal expression.
86131
2 | declare const value: never;
87132
3 | const stringy = `${value}`;
88133
| ~~~~~
89134
4 |
135+
Label: Type: never (3:28 - 3:32)
136+
2 | declare const value: never;
137+
3 | const stringy = `${value}`;
138+
| ^^^^^ Type: never
139+
4 |
90140
---
91141

92142
[TestRestrictTemplateExpressionsRule/invalid-18 - 1]
93143
Diagnostic 1: invalidType (3:27 - 3:29)
94-
Message: Invalid type "unknown" of template literal expression.
144+
Message: Invalid type used in template literal expression.
95145
2 | function test<T extends any>(arg: T) {
96146
3 | return `arg = ${arg}`;
97147
| ~~~
98148
4 | }
149+
Label: Type: unknown (3:27 - 3:29)
150+
2 | function test<T extends any>(arg: T) {
151+
3 | return `arg = ${arg}`;
152+
| ^^^ Type: unknown
153+
4 | }
99154
---
100155

101156
[TestRestrictTemplateExpressionsRule/invalid-2 - 1]
102157
Diagnostic 1: invalidType (2:30 - 2:33)
103-
Message: Invalid type "null" of template literal expression.
158+
Message: Invalid type used in template literal expression.
104159
1 |
105160
2 | const msg = `arg = ${null}`;
106161
| ~~~~
107162
3 |
163+
Label: Type: null (2:30 - 2:33)
164+
1 |
165+
2 | const msg = `arg = ${null}`;
166+
| ^^^^ Type: null
167+
3 |
108168
---
109169

110170
[TestRestrictTemplateExpressionsRule/invalid-3 - 1]
111171
Diagnostic 1: invalidType (3:30 - 3:32)
112-
Message: Invalid type "number[]" of template literal expression.
172+
Message: Invalid type used in template literal expression.
113173
2 | declare const arg: number[];
114174
3 | const msg = `arg = ${arg}`;
115175
| ~~~
116176
4 |
177+
Label: Type: number[] (3:30 - 3:32)
178+
2 | declare const arg: number[];
179+
3 | const msg = `arg = ${arg}`;
180+
| ^^^ Type: number[]
181+
4 |
117182
---
118183

119184
[TestRestrictTemplateExpressionsRule/invalid-4 - 1]
120185
Diagnostic 1: invalidType (2:30 - 2:34)
121-
Message: Invalid type "(number | undefined)[]" of template literal expression.
186+
Message: Invalid type used in template literal expression.
122187
1 |
123188
2 | const msg = `arg = ${[, 2]}`;
124189
| ~~~~~
125190
3 |
191+
Label: Type: (number | undefined)[] (2:30 - 2:34)
192+
1 |
193+
2 | const msg = `arg = ${[, 2]}`;
194+
| ^^^^^ Type: (number | undefined)[]
195+
3 |
126196
---
127197

128198
[TestRestrictTemplateExpressionsRule/invalid-5 - 1]
129199
Diagnostic 1: invalidType (1:22 - 1:38)
130-
Message: Invalid type "Promise<void>" of template literal expression.
200+
Message: Invalid type used in template literal expression.
131201
1 | const msg = `arg = ${Promise.resolve()}`;
132202
| ~~~~~~~~~~~~~~~~~
203+
Label: Type: Promise<void> (1:22 - 1:38)
204+
1 | const msg = `arg = ${Promise.resolve()}`;
205+
| ^^^^^^^^^^^^^^^^^ Type: Promise<void>
133206
---
134207

135208
[TestRestrictTemplateExpressionsRule/invalid-6 - 1]
136209
Diagnostic 1: invalidType (1:22 - 1:32)
137-
Message: Invalid type "Error" of template literal expression.
210+
Message: Invalid type used in template literal expression.
138211
1 | const msg = `arg = ${new Error()}`;
139212
| ~~~~~~~~~~~
213+
Label: Type: Error (1:22 - 1:32)
214+
1 | const msg = `arg = ${new Error()}`;
215+
| ^^^^^^^^^^^ Type: Error
140216
---
141217

142218
[TestRestrictTemplateExpressionsRule/invalid-7 - 1]
143219
Diagnostic 1: invalidType (3:30 - 3:32)
144-
Message: Invalid type "[number | undefined, string]" of template literal expression.
220+
Message: Invalid type used in template literal expression.
145221
2 | declare const arg: [number | undefined, string];
146222
3 | const msg = `arg = ${arg}`;
147223
| ~~~
148224
4 |
225+
Label: Type: [number | undefined, string] (3:30 - 3:32)
226+
2 | declare const arg: [number | undefined, string];
227+
3 | const msg = `arg = ${arg}`;
228+
| ^^^ Type: [number | undefined, string]
229+
4 |
149230
---
150231

151232
[TestRestrictTemplateExpressionsRule/invalid-8 - 1]
152233
Diagnostic 1: invalidType (3:30 - 3:32)
153-
Message: Invalid type "number" of template literal expression.
234+
Message: Invalid type used in template literal expression.
154235
2 | declare const arg: number;
155236
3 | const msg = `arg = ${arg}`;
156237
| ~~~
157238
4 |
239+
Label: Type: number (3:30 - 3:32)
240+
2 | declare const arg: number;
241+
3 | const msg = `arg = ${arg}`;
242+
| ^^^ Type: number
243+
4 |
158244
---
159245

160246
[TestRestrictTemplateExpressionsRule/invalid-9 - 1]
161247
Diagnostic 1: invalidType (3:30 - 3:32)
162-
Message: Invalid type "boolean" of template literal expression.
248+
Message: Invalid type used in template literal expression.
163249
2 | declare const arg: boolean;
164250
3 | const msg = `arg = ${arg}`;
165251
| ~~~
166252
4 |
253+
Label: Type: boolean (3:30 - 3:32)
254+
2 | declare const arg: boolean;
255+
3 | const msg = `arg = ${arg}`;
256+
| ^^^ Type: boolean
257+
4 |
167258
---

0 commit comments

Comments
 (0)