eslint-plugin-lube 0.6.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -43,7 +43,9 @@ export default {
43
43
  }
44
44
  )?.range[0]
45
45
  : node.range[1] - 1
46
- if (end == null || text[node.range[0]] != opener || text[end] != closer) return
46
+ if (end == null || text[node.range[0]] != opener || text[end] != closer) {
47
+ return
48
+ }
47
49
  return {
48
50
  end,
49
51
  items,
@@ -68,15 +70,17 @@ export default {
68
70
  let left = code_start
69
71
  for (;;) {
70
72
  left = source.skip_left(code_start, start)
71
- if (left <= start || text[left - 1] != "(") break
72
- const right = source.skip_right(code_end, end)
73
- if (right >= end || text[right] != ")") break
73
+ if (left <= start || text[left - 1] != "(") {
74
+ break
75
+ }
74
76
  code_start = left - 1
75
- code_end = right + 1
77
+ code_end = source.skip_right(code_end, end) + 1
76
78
  }
77
79
  const right = source.skip_right(code_end, end)
78
80
  let last = right
79
- while (last > code_end && source.is_space(text.charCodeAt(last - 1))) last--
81
+ while (last > code_end && source.is_space(text.charCodeAt(last - 1))) {
82
+ last--
83
+ }
80
84
  return {
81
85
  after_comma: "",
82
86
  after_comma_comments: 0,
@@ -117,7 +121,9 @@ export default {
117
121
  if (first && last) {
118
122
  open = source_code.getTokenBefore(first)
119
123
  close = source_code.getTokenAfter(last)
120
- if (close?.value == ",") close = source_code.getTokenAfter(close)
124
+ if (close?.value == ",") {
125
+ close = source_code.getTokenAfter(close)
126
+ }
121
127
  } else {
122
128
  const before = node.typeParameters ?? (node.type == "ArrowFunctionExpression" ? void 0 : node.id)
123
129
  open = before
@@ -135,7 +141,9 @@ export default {
135
141
  || close?.value != ")"
136
142
  || open.range[0] < node.range[0]
137
143
  || close.range[1] > node.range[1]
138
- ) return
144
+ ) {
145
+ return
146
+ }
139
147
  return {
140
148
  end: close.range[0],
141
149
  items: params,
@@ -161,7 +169,9 @@ export default {
161
169
  close?.value != ")"
162
170
  || open?.value != "("
163
171
  || open.range[0] >= close.range[0]
164
- ) return
172
+ ) {
173
+ return
174
+ }
165
175
  return {
166
176
  end: close.range[0],
167
177
  items: node.arguments,
@@ -183,10 +193,10 @@ export default {
183
193
  node.properties,
184
194
  object_curly_spacing
185
195
  )
186
- case "SequenceExpression":
196
+ default:
187
197
  return {
188
198
  end: node.range[1],
189
- items: node.expressions,
199
+ items: /** @type {import("../../private.js").AstNode & import("estree").SequenceExpression} */(node)/**/.expressions,
190
200
  line_indent: source.get_line_indent(
191
201
  node,
192
202
  source.get_anchor(node.range[0]),
@@ -196,14 +206,15 @@ export default {
196
206
  start: node.range[0]
197
207
  }
198
208
  }
199
- return undefined
200
209
  }
201
210
  /**
202
211
  * @param {import("../../private.js").AstNode} node
203
212
  * @returns {void}
204
213
  */
205
214
  function push(node) {
206
- if (!ignore_template_literal || node.range[0] >= template_end) nodes.push(node)
215
+ if (!ignore_template_literal || node.range[0] >= template_end) {
216
+ nodes.push(node)
217
+ }
207
218
  }
208
219
  /**
209
220
  * @param {number} start
@@ -224,7 +235,9 @@ export default {
224
235
  const texts = []
225
236
  for (let index = start; index < end; index++) {
226
237
  const comment = source.comment_by_start.get(index)
227
- if (!comment) continue
238
+ if (!comment) {
239
+ continue
240
+ }
228
241
  texts.push(
229
242
  render(
230
243
  comment.range[0],
@@ -244,12 +257,16 @@ export default {
244
257
  function take_same_line_comments(position, limit, holes) {
245
258
  let index = position
246
259
  for (;;) {
247
- while (index < limit && (text[index] == " " || text[index] == "\t" || text[index] == "," && holes-- > 0)) index++
260
+ while (index < limit && (text[index] == " " || text[index] == "\t" || text[index] == "," && holes-- > 0)) {
261
+ index++
262
+ }
248
263
  const comment = source.comment_by_start.get(index)
249
264
  if (!comment || comment.range[1] > limit || comment.loc?.start.line != comment.loc?.end.line) {
250
265
  return position
251
266
  }
252
- if (comment.type == "Line") return comment.range[1]
267
+ if (comment.type == "Line") {
268
+ return comment.range[1]
269
+ }
253
270
  index = comment.range[1]
254
271
  }
255
272
  }
@@ -266,7 +283,9 @@ export default {
266
283
  */
267
284
  function verify(node) {
268
285
  const sequence = get_sequence(node)
269
- if (!sequence) return
286
+ if (!sequence) {
287
+ return
288
+ }
270
289
  const { end, line_indent, start } = sequence
271
290
  const is_sequence_expression = node.type == "SequenceExpression"
272
291
  const items = sequence.items.map(
@@ -275,9 +294,13 @@ export default {
275
294
  const last_index = items.length - 1
276
295
  items.forEach(
277
296
  (before, i) => {
278
- if (!before || i == last_index || before.trailing_line_comment || text[before.right] != ",") return
297
+ if (!before || i == last_index || before.trailing_line_comment || text[before.right] != ",") {
298
+ return
299
+ }
279
300
  let next = i + 1
280
- while (next <= last_index && !items[next]) next++
301
+ while (next <= last_index && !items[next]) {
302
+ next++
303
+ }
281
304
  const item = items[i + 1]
282
305
  const comma_end = before.right + 1
283
306
  const taken = take_same_line_comments(
@@ -285,11 +308,15 @@ export default {
285
308
  items[next]?.code_start ?? end,
286
309
  next - i - 1
287
310
  )
288
- if (taken == comma_end) return
311
+ if (taken == comma_end) {
312
+ return
313
+ }
289
314
  before.after_comma = item ? render(comma_end, taken).trim() : render_comments(comma_end, taken)
290
315
  before.after_comma_comments = source.count_comments(comma_end, taken)
291
316
  before.after_comma_line_comment = true
292
- if (!item) return
317
+ if (!item) {
318
+ return
319
+ }
293
320
  item.left = source.skip_left(item.code_start, taken)
294
321
  item.text = render(item.left, item.right).trim()
295
322
  }
@@ -303,7 +330,9 @@ export default {
303
330
  let comment_end = end
304
331
  while (source.is_space(
305
332
  text.charCodeAt(comment_end - 1)
306
- )) comment_end--
333
+ )) {
334
+ comment_end--
335
+ }
307
336
  last.after_comma = after_comma.trimStart()
308
337
  last.after_comma_comments = comments
309
338
  last.after_comma_line_comment = source.comment_by_end.get(comment_end)?.type == "Line"
@@ -317,7 +346,9 @@ export default {
317
346
  let length = 0
318
347
  let multiline = false
319
348
  for (const item of items) {
320
- if (!item) continue
349
+ if (!item) {
350
+ continue
351
+ }
321
352
  covered_comments += source.count_comments(item.left, item.right) + item.after_comma_comments
322
353
  length += text_width(item.text) + item.after_comma.length
323
354
  if (
@@ -326,11 +357,17 @@ export default {
326
357
  || item.after_comma.includes("\n")
327
358
  || item.text.includes("\n")
328
359
  || item.trailing_line_comment
329
- ) multiline = true
360
+ ) {
361
+ multiline = true
362
+ }
363
+ }
364
+ if (covered_comments != source.count_comments(start, end)) {
365
+ return
330
366
  }
331
- if (covered_comments != source.count_comments(start, end)) return
332
367
  const too_long = length > max_length
333
- if (too_long) multiline = true
368
+ if (too_long) {
369
+ multiline = true
370
+ }
334
371
  let corrected_text = ""
335
372
  if (multiline) {
336
373
  const item_indent = is_sequence_expression ? line_indent : line_indent + indent
@@ -341,7 +378,9 @@ export default {
341
378
  continue
342
379
  }
343
380
  const add_indent = !is_sequence_expression && source.get_line_start(start) == source.get_line_start(item.node.range[0])
344
- if (add_indent && source.has_multiline_literal(item.left, item.right)) return
381
+ if (add_indent && source.has_multiline_literal(item.left, item.right)) {
382
+ return
383
+ }
345
384
  /**
346
385
  * @param {string} value
347
386
  * @returns {string}
@@ -364,12 +403,19 @@ export default {
364
403
  corrected_text += indented(item.text) + "," + comment + "\n" + item_indent
365
404
  }
366
405
  }
367
- if (!is_sequence_expression) corrected_text = "\n" + item_indent + corrected_text + "\n" + line_indent
406
+ if (!is_sequence_expression) {
407
+ corrected_text = "\n" + item_indent + corrected_text + "\n" + line_indent
408
+ }
368
409
  } else {
369
410
  corrected_text = items.map(item => item ? item.text : "").join(", ")
370
- if (last?.after_comma) corrected_text += " " + last.after_comma
371
- else if (items[last_index] === null) corrected_text += ","
372
- if (length && sequence.spacing) corrected_text = " " + corrected_text + " "
411
+ if (last?.after_comma) {
412
+ corrected_text += " " + last.after_comma
413
+ } else if (items[last_index] === null) {
414
+ corrected_text += ","
415
+ }
416
+ if (length && sequence.spacing) {
417
+ corrected_text = " " + corrected_text + " "
418
+ }
373
419
  }
374
420
  const current = render(start, end)
375
421
  const same_structure = to_structure(current) == to_structure(corrected_text)
@@ -411,31 +457,49 @@ export default {
411
457
  /** @type {import("../../private.js").RuleListener} */
412
458
  const listener = {
413
459
  ArrayExpression: node => {
414
- if (check_array) push(node)
460
+ if (check_array) {
461
+ push(node)
462
+ }
415
463
  },
416
464
  ArrayPattern: node => {
417
- if (check_array) push(node)
465
+ if (check_array) {
466
+ push(node)
467
+ }
418
468
  },
419
469
  ArrowFunctionExpression: node => {
420
- if (check_call) push(node)
470
+ if (check_call) {
471
+ push(node)
472
+ }
421
473
  },
422
474
  CallExpression: node => {
423
- if (check_call) push(node)
475
+ if (check_call) {
476
+ push(node)
477
+ }
424
478
  },
425
479
  FunctionDeclaration: node => {
426
- if (check_call) push(node)
480
+ if (check_call) {
481
+ push(node)
482
+ }
427
483
  },
428
484
  FunctionExpression: node => {
429
- if (check_call) push(node)
485
+ if (check_call) {
486
+ push(node)
487
+ }
430
488
  },
431
489
  NewExpression: node => {
432
- if (check_call) push(node)
490
+ if (check_call) {
491
+ push(node)
492
+ }
433
493
  },
434
494
  ObjectExpression: node => {
435
- if (check_object) push(node)
495
+ if (check_object) {
496
+ push(node)
497
+ }
436
498
  },
437
499
  ObjectPattern: node => {
438
- if (check_object) push(node)
500
+ if (check_object) {
501
+ push(node)
502
+ }
439
503
  },
440
504
  "Program:exit": () => {
441
505
  for (let i = nodes.length - 1; i >= 0; i--) {
@@ -445,10 +509,14 @@ export default {
445
509
  }
446
510
  },
447
511
  SequenceExpression: node => {
448
- if (check_sequence) push(node)
512
+ if (check_sequence) {
513
+ push(node)
514
+ }
449
515
  },
450
516
  TemplateLiteral: node => {
451
- if (node.range[1] > template_end) template_end = node.range[1]
517
+ if (node.range[1] > template_end) {
518
+ template_end = node.range[1]
519
+ }
452
520
  }
453
521
  }
454
522
  return /** @type {import("eslint").Rule.RuleListener} */(/** @type {unknown} */(listener))/**/
@@ -23,24 +23,18 @@ export default {
23
23
  */
24
24
  function collect(node, depth, gaps) {
25
25
  const parts = get_parts(node)
26
- const question = source_code.getTokenAfter(
26
+ const question = /** @type {import("eslint").AST.Token} */(source_code.getTokenAfter(
27
27
  /** @type {import("estree").Node} */(/** @type {unknown} */(parts.test))/**/,
28
28
  {
29
29
  filter: token => token.value == "?"
30
30
  }
31
- )
32
- const colon = question && source_code.getTokenAfter(
31
+ ))/**/
32
+ const colon = /** @type {import("eslint").AST.Token} */(source_code.getTokenAfter(
33
33
  /** @type {import("estree").Node} */(/** @type {unknown} */(parts.consequent))/**/,
34
34
  {
35
35
  filter: token => token.value == ":"
36
36
  }
37
- )
38
- if (
39
- !question
40
- || !colon
41
- || question.range[1] > parts.consequent.range[0]
42
- || colon.range[1] > parts.alternate.range[0]
43
- ) return false
37
+ ))/**/
44
38
  for (const operator of [ question, colon ]) {
45
39
  const before = /** @type {import("eslint").AST.Token | import("estree").Comment} */(source_code.getTokenBefore(
46
40
  operator,
@@ -50,7 +44,9 @@ export default {
50
44
  operator,
51
45
  { includeComments: true }
52
46
  ))/**/
53
- if (after.type == "Line") return false
47
+ if (after.type == "Line") {
48
+ return false
49
+ }
54
50
  gaps.push(
55
51
  {
56
52
  depth,
@@ -70,7 +66,9 @@ export default {
70
66
  parts.consequent,
71
67
  parts.alternate
72
68
  ]) {
73
- if (is_chained(branch) && !collect(branch, depth + 1, gaps)) return false
69
+ if (is_chained(branch) && !collect(branch, depth + 1, gaps)) {
70
+ return false
71
+ }
74
72
  }
75
73
  return true
76
74
  }
@@ -97,9 +95,13 @@ export default {
97
95
  */
98
96
  function is_chained(node) {
99
97
  const parent = node.parent
100
- if (!parent || !is_ternary(node) || parent.type != node.type) return false
98
+ if (!parent || !is_ternary(node) || parent.type != node.type) {
99
+ return false
100
+ }
101
101
  const parts = get_parts(parent)
102
- if (node != parts.consequent && node != parts.alternate) return false
102
+ if (node != parts.consequent && node != parts.alternate) {
103
+ return false
104
+ }
103
105
  const before = source_code.getTokenBefore(
104
106
  /** @type {import("estree").Node} */(/** @type {unknown} */(node))/**/
105
107
  )
@@ -117,19 +119,27 @@ export default {
117
119
  * @returns {void}
118
120
  */
119
121
  function push(node) {
120
- if (!ignore_template_literal || node.range[0] >= template_end) nodes.push(node)
122
+ if (!ignore_template_literal || node.range[0] >= template_end) {
123
+ nodes.push(node)
124
+ }
121
125
  }
122
126
  /**
123
127
  * @param {import("../../private.js").TernaryNode} node
124
128
  * @returns {void}
125
129
  */
126
130
  function verify(node) {
127
- if (is_chained(node)) return
131
+ if (is_chained(node)) {
132
+ return
133
+ }
128
134
  /** @type {import("../../private.js").TernaryGap[]} */
129
135
  const gaps = []
130
- if (!collect(node, 0, gaps)) return
136
+ if (!collect(node, 0, gaps)) {
137
+ return
138
+ }
131
139
  let length = node.range[1] - node.range[0]
132
- for (const gap of gaps) length -= gap.end - gap.start - 1
140
+ for (const gap of gaps) {
141
+ length -= gap.end - gap.start - 1
142
+ }
133
143
  const reason = gaps.length > 4
134
144
  ? "a branch is another ternary"
135
145
  : length > max_length
@@ -152,15 +162,21 @@ export default {
152
162
  for (const gap of gaps) {
153
163
  const current = text.slice(gap.start, gap.end)
154
164
  const breaks = multiline && gap.line_break
155
- if (breaks && line_break_regex.test(current)) continue
165
+ if (breaks && line_break_regex.test(current)) {
166
+ continue
167
+ }
156
168
  const value = breaks
157
169
  ? eol + line_indent + indent.repeat(gap.depth + 1)
158
170
  : " "
159
- if (current == value) continue
171
+ if (current == value) {
172
+ continue
173
+ }
160
174
  ranges.push([ gap.start, gap.end ])
161
175
  values.push(value)
162
176
  }
163
- if (!ranges.length) return
177
+ if (!ranges.length) {
178
+ return
179
+ }
164
180
  context.report(
165
181
  {
166
182
  data: { reason },
@@ -185,7 +201,9 @@ export default {
185
201
  )
186
202
  },
187
203
  "Program:exit": () => {
188
- for (const node of nodes) verify(node)
204
+ for (const node of nodes) {
205
+ verify(node)
206
+ }
189
207
  },
190
208
  /** @param {import("estree").Node} node */
191
209
  TSConditionalType(node) {
@@ -196,7 +214,9 @@ export default {
196
214
  /** @param {import("estree").TemplateLiteral} node */
197
215
  TemplateLiteral(node) {
198
216
  const end = /** @type {[number, number]} */(node.range)/**/[1]
199
- if (end > template_end) template_end = end
217
+ if (end > template_end) {
218
+ template_end = end
219
+ }
200
220
  }
201
221
  }
202
222
  },