Skip to content

Commit f9c138a

Browse files
authored
feat: report max-depth violations on keywords (#20943)
1 parent 8ae1b5b commit f9c138a

2 files changed

Lines changed: 85 additions & 2 deletions

File tree

lib/rules/max-depth.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ module.exports = {
5454
},
5555

5656
create(context) {
57+
const sourceCode = context.sourceCode;
58+
5759
//--------------------------------------------------------------------------
5860
// Helpers
5961
//--------------------------------------------------------------------------
@@ -102,6 +104,7 @@ module.exports = {
102104
if (len > maxDepth) {
103105
context.report({
104106
node,
107+
loc: sourceCode.getFirstToken(node).loc,
105108
messageId: "tooDeeply",
106109
data: { depth: len, maxDepth },
107110
});

tests/lib/rules/max-depth.js

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ ruleTester.run("max-depth", rule, {
8080
{
8181
messageId: "tooDeeply",
8282
data: { depth: 3, maxDepth: 2 },
83+
line: 1,
84+
column: 43,
85+
endLine: 1,
86+
endColumn: 45,
8387
},
8488
],
8589
},
@@ -91,6 +95,10 @@ ruleTester.run("max-depth", rule, {
9195
{
9296
messageId: "tooDeeply",
9397
data: { depth: 3, maxDepth: 2 },
98+
line: 1,
99+
column: 44,
100+
endLine: 1,
101+
endColumn: 46,
94102
},
95103
],
96104
},
@@ -101,6 +109,10 @@ ruleTester.run("max-depth", rule, {
101109
{
102110
messageId: "tooDeeply",
103111
data: { depth: 2, maxDepth: 1 },
112+
line: 1,
113+
column: 38,
114+
endLine: 1,
115+
endColumn: 41,
104116
},
105117
],
106118
},
@@ -111,6 +123,10 @@ ruleTester.run("max-depth", rule, {
111123
{
112124
messageId: "tooDeeply",
113125
data: { depth: 2, maxDepth: 1 },
126+
line: 1,
127+
column: 52,
128+
endLine: 1,
129+
endColumn: 54,
114130
},
115131
],
116132
},
@@ -121,6 +137,10 @@ ruleTester.run("max-depth", rule, {
121137
{
122138
messageId: "tooDeeply",
123139
data: { depth: 2, maxDepth: 1 },
140+
line: 1,
141+
column: 8,
142+
endLine: 1,
143+
endColumn: 10,
124144
},
125145
],
126146
},
@@ -131,6 +151,24 @@ ruleTester.run("max-depth", rule, {
131151
{
132152
messageId: "tooDeeply",
133153
data: { depth: 2, maxDepth: 1 },
154+
line: 1,
155+
column: 33,
156+
endLine: 1,
157+
endColumn: 35,
158+
},
159+
],
160+
},
161+
{
162+
code: "function foo() { if (a) { switch (b) { case 1: foo(); } } }",
163+
options: [1],
164+
errors: [
165+
{
166+
messageId: "tooDeeply",
167+
data: { depth: 2, maxDepth: 1 },
168+
line: 1,
169+
column: 27,
170+
endLine: 1,
171+
endColumn: 33,
134172
},
135173
],
136174
},
@@ -142,6 +180,10 @@ ruleTester.run("max-depth", rule, {
142180
{
143181
messageId: "tooDeeply",
144182
data: { depth: 2, maxDepth: 1 },
183+
line: 1,
184+
column: 39,
185+
endLine: 1,
186+
endColumn: 41,
145187
},
146188
],
147189
},
@@ -152,10 +194,18 @@ ruleTester.run("max-depth", rule, {
152194
{
153195
messageId: "tooDeeply",
154196
data: { depth: 2, maxDepth: 1 },
197+
line: 1,
198+
column: 33,
199+
endLine: 1,
200+
endColumn: 35,
155201
},
156202
{
157203
messageId: "tooDeeply",
158204
data: { depth: 3, maxDepth: 1 },
205+
line: 1,
206+
column: 45,
207+
endLine: 1,
208+
endColumn: 47,
159209
},
160210
],
161211
},
@@ -165,6 +215,10 @@ ruleTester.run("max-depth", rule, {
165215
{
166216
messageId: "tooDeeply",
167217
data: { depth: 5, maxDepth: 4 },
218+
line: 1,
219+
column: 68,
220+
endLine: 1,
221+
endColumn: 70,
168222
},
169223
],
170224
},
@@ -177,6 +231,10 @@ ruleTester.run("max-depth", rule, {
177231
{
178232
messageId: "tooDeeply",
179233
data: { depth: 3, maxDepth: 2 },
234+
line: 1,
235+
column: 43,
236+
endLine: 1,
237+
endColumn: 45,
180238
},
181239
],
182240
},
@@ -185,14 +243,28 @@ ruleTester.run("max-depth", rule, {
185243
code: "function foo() { if (a) { if (b) { if (c) { if (d) { if (e) {} } } } } }",
186244
options: [{}],
187245
errors: [
188-
{ messageId: "tooDeeply", data: { depth: 5, maxDepth: 4 } },
246+
{
247+
messageId: "tooDeeply",
248+
data: { depth: 5, maxDepth: 4 },
249+
line: 1,
250+
column: 54,
251+
endLine: 1,
252+
endColumn: 56,
253+
},
189254
],
190255
},
191256
{
192257
code: "function foo() { if (true) {} }",
193258
options: [{ max: 0 }],
194259
errors: [
195-
{ messageId: "tooDeeply", data: { depth: 1, maxDepth: 0 } },
260+
{
261+
messageId: "tooDeeply",
262+
data: { depth: 1, maxDepth: 0 },
263+
line: 1,
264+
column: 18,
265+
endLine: 1,
266+
endColumn: 20,
267+
},
196268
],
197269
},
198270

@@ -206,6 +278,8 @@ ruleTester.run("max-depth", rule, {
206278
data: { depth: 3, maxDepth: 2 },
207279
line: 1,
208280
column: 38,
281+
endLine: 1,
282+
endColumn: 40,
209283
},
210284
],
211285
},
@@ -219,6 +293,8 @@ ruleTester.run("max-depth", rule, {
219293
data: { depth: 3, maxDepth: 2 },
220294
line: 1,
221295
column: 47,
296+
endLine: 1,
297+
endColumn: 49,
222298
},
223299
],
224300
},
@@ -232,6 +308,8 @@ ruleTester.run("max-depth", rule, {
232308
data: { depth: 3, maxDepth: 2 },
233309
line: 1,
234310
column: 64,
311+
endLine: 1,
312+
endColumn: 66,
235313
},
236314
],
237315
},
@@ -245,6 +323,8 @@ ruleTester.run("max-depth", rule, {
245323
data: { depth: 3, maxDepth: 2 },
246324
line: 1,
247325
column: 80,
326+
endLine: 1,
327+
endColumn: 82,
248328
},
249329
],
250330
},

0 commit comments

Comments
 (0)