Skip to content

Commit 126552f

Browse files
feat: update error location in for-direction and no-dupe-args (#20258)
feat: upadate error location to header of statements
1 parent 927272d commit 126552f

5 files changed

Lines changed: 322 additions & 35 deletions

File tree

‎lib/rules/for-direction.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ module.exports = {
4646
*/
4747
function report(node) {
4848
context.report({
49-
node,
49+
loc: {
50+
start: node.loc.start,
51+
end: sourceCode.getTokenBefore(node.body).loc.end,
52+
},
5053
messageId: "incorrectDirection",
5154
});
5255
}

‎lib/rules/no-dupe-args.js‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66
"use strict";
77

8+
//------------------------------------------------------------------------------
9+
// Requirements
10+
//------------------------------------------------------------------------------
11+
12+
const astUtils = require("./utils/ast-utils");
13+
814
//------------------------------------------------------------------------------
915
// Rule Definition
1016
//------------------------------------------------------------------------------
@@ -58,10 +64,15 @@ module.exports = {
5864

5965
// Checks and reports duplications.
6066
const defs = variable.defs.filter(isParameter);
67+
const loc = {
68+
start: astUtils.getOpeningParenOfParams(node, sourceCode)
69+
.loc.start,
70+
end: sourceCode.getTokenBefore(node.body).loc.end,
71+
};
6172

6273
if (defs.length >= 2) {
6374
context.report({
64-
node,
75+
loc,
6576
messageId: "unexpected",
6677
data: { name: variable.name },
6778
});

‎lib/rules/utils/ast-utils.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2729,4 +2729,5 @@ module.exports = {
27292729
isStartOfExpressionStatement,
27302730
needsPrecedingSemicolon,
27312731
isImportAttributeKey,
2732+
getOpeningParenOfParams,
27322733
};

‎tests/lib/rules/for-direction.js‎

Lines changed: 213 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -84,97 +84,287 @@ ruleTester.run("for-direction", rule, {
8484
],
8585
invalid: [
8686
// test if '++', '--'
87-
{ code: "for(var i = 0; i < 10; i--){}", errors: [incorrectDirection] },
87+
{
88+
code: "for(var i = 0; i < 10; i--){}",
89+
errors: [
90+
{
91+
...incorrectDirection,
92+
line: 1,
93+
column: 1,
94+
endLine: 1,
95+
endColumn: 28,
96+
},
97+
],
98+
},
8899
{
89100
code: "for(var i = 0; i <= 10; i--){}",
90-
errors: [incorrectDirection],
101+
errors: [
102+
{
103+
...incorrectDirection,
104+
line: 1,
105+
column: 1,
106+
endLine: 1,
107+
endColumn: 29,
108+
},
109+
],
91110
},
92111
{
93112
code: "for(var i = 10; i > 10; i++){}",
94-
errors: [incorrectDirection],
113+
errors: [
114+
{
115+
...incorrectDirection,
116+
line: 1,
117+
column: 1,
118+
endLine: 1,
119+
endColumn: 29,
120+
},
121+
],
95122
},
96123
{
97124
code: "for(var i = 10; i >= 0; i++){}",
98-
errors: [incorrectDirection],
125+
errors: [
126+
{
127+
...incorrectDirection,
128+
line: 1,
129+
column: 1,
130+
endLine: 1,
131+
endColumn: 29,
132+
},
133+
],
99134
},
100135

101136
// test if '++', '--' with counter 'i' on the right side of test condition
102-
{ code: "for(var i = 0; 10 > i; i--){}", errors: [incorrectDirection] },
137+
{
138+
code: "for(var i = 0; 10 > i; i--){}",
139+
errors: [
140+
{
141+
...incorrectDirection,
142+
line: 1,
143+
column: 1,
144+
endLine: 1,
145+
endColumn: 28,
146+
},
147+
],
148+
},
103149
{
104150
code: "for(var i = 0; 10 >= i; i--){}",
105-
errors: [incorrectDirection],
151+
errors: [
152+
{
153+
...incorrectDirection,
154+
line: 1,
155+
column: 1,
156+
endLine: 1,
157+
endColumn: 29,
158+
},
159+
],
106160
},
107161
{
108162
code: "for(var i = 10; 10 < i; i++){}",
109-
errors: [incorrectDirection],
163+
errors: [
164+
{
165+
...incorrectDirection,
166+
line: 1,
167+
column: 1,
168+
endLine: 1,
169+
endColumn: 29,
170+
},
171+
],
110172
},
111173
{
112174
code: "for(var i = 10; 0 <= i; i++){}",
113-
errors: [incorrectDirection],
175+
errors: [
176+
{
177+
...incorrectDirection,
178+
line: 1,
179+
column: 1,
180+
endLine: 1,
181+
endColumn: 29,
182+
},
183+
],
114184
},
115185

116186
// test if '+=', '-='
117187
{
118188
code: "for(var i = 0; i < 10; i-=1){}",
119-
errors: [incorrectDirection],
189+
errors: [
190+
{
191+
...incorrectDirection,
192+
line: 1,
193+
column: 1,
194+
endLine: 1,
195+
endColumn: 29,
196+
},
197+
],
120198
},
121199
{
122200
code: "for(var i = 0; i <= 10; i-=1){}",
123-
errors: [incorrectDirection],
201+
errors: [
202+
{
203+
...incorrectDirection,
204+
line: 1,
205+
column: 1,
206+
endLine: 1,
207+
endColumn: 30,
208+
},
209+
],
124210
},
125211
{
126212
code: "for(var i = 10; i > 10; i+=1){}",
127-
errors: [incorrectDirection],
213+
errors: [
214+
{
215+
...incorrectDirection,
216+
line: 1,
217+
column: 1,
218+
endLine: 1,
219+
endColumn: 30,
220+
},
221+
],
128222
},
129223
{
130224
code: "for(var i = 10; i >= 0; i+=1){}",
131-
errors: [incorrectDirection],
225+
errors: [
226+
{
227+
...incorrectDirection,
228+
line: 1,
229+
column: 1,
230+
endLine: 1,
231+
endColumn: 30,
232+
},
233+
],
132234
},
133235
{
134236
code: "for(var i = 0; i < 10; i+=-1){}",
135-
errors: [incorrectDirection],
237+
errors: [
238+
{
239+
...incorrectDirection,
240+
line: 1,
241+
column: 1,
242+
endLine: 1,
243+
endColumn: 30,
244+
},
245+
],
136246
},
137247
{
138248
code: "for(var i = 0; i <= 10; i+=-1){}",
139-
errors: [incorrectDirection],
249+
errors: [
250+
{
251+
...incorrectDirection,
252+
line: 1,
253+
column: 1,
254+
endLine: 1,
255+
endColumn: 31,
256+
},
257+
],
140258
},
141259
{
142260
code: "for(var i = 10; i > 10; i-=-1){}",
143-
errors: [incorrectDirection],
261+
errors: [
262+
{
263+
...incorrectDirection,
264+
line: 1,
265+
column: 1,
266+
endLine: 1,
267+
endColumn: 31,
268+
},
269+
],
144270
},
145271
{
146272
code: "for(var i = 10; i >= 0; i-=-1){}",
147-
errors: [incorrectDirection],
273+
errors: [
274+
{
275+
...incorrectDirection,
276+
line: 1,
277+
column: 1,
278+
endLine: 1,
279+
endColumn: 31,
280+
},
281+
],
148282
},
149283
{
150284
code: "for(var i = 0n; i > l; i+=1n){}",
151-
errors: [incorrectDirection],
285+
errors: [
286+
{
287+
...incorrectDirection,
288+
line: 1,
289+
column: 1,
290+
endLine: 1,
291+
endColumn: 30,
292+
},
293+
],
152294
},
153295
{
154296
code: "for(var i = 0n; i < l; i+=-1n){}",
155-
errors: [incorrectDirection],
297+
errors: [
298+
{
299+
...incorrectDirection,
300+
line: 1,
301+
column: 1,
302+
endLine: 1,
303+
endColumn: 31,
304+
},
305+
],
156306
},
157307
{
158308
code: "for(var i = MIN; i <= MAX; i-=true){}",
159-
errors: [incorrectDirection],
309+
errors: [
310+
{
311+
...incorrectDirection,
312+
line: 1,
313+
column: 1,
314+
endLine: 1,
315+
endColumn: 36,
316+
},
317+
],
160318
},
161319
{
162320
code: "for(var i = 0; i < 10; i-=+5e-7){}",
163-
errors: [incorrectDirection],
321+
errors: [
322+
{
323+
...incorrectDirection,
324+
line: 1,
325+
column: 1,
326+
endLine: 1,
327+
endColumn: 33,
328+
},
329+
],
164330
},
165331
{
166332
code: "for(var i = 0; i < MAX; i += (2 - 3));",
167-
errors: [incorrectDirection],
333+
errors: [
334+
{
335+
...incorrectDirection,
336+
line: 1,
337+
column: 1,
338+
endLine: 1,
339+
endColumn: 38,
340+
},
341+
],
168342
},
169343
{
170344
code: "var n = -2; for(var i = 0; i < 10; i += n);",
171-
errors: [incorrectDirection],
345+
errors: [
346+
{
347+
...incorrectDirection,
348+
line: 1,
349+
column: 13,
350+
endLine: 1,
351+
endColumn: 43,
352+
},
353+
],
172354
},
173355

174356
// test if '+=', '-=' with counter 'i' on the right side of test condition
175357
{
176358
code: "for(var i = 0; 10 > i; i-=1){}",
177-
errors: [incorrectDirection],
359+
errors: [
360+
{
361+
...incorrectDirection,
362+
line: 1,
363+
column: 1,
364+
endLine: 1,
365+
endColumn: 29,
366+
},
367+
],
178368
},
179369
],
180370
});

0 commit comments

Comments
 (0)