eslint-plugin-lube 0.8.0 → 0.9.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.
Files changed (39) hide show
  1. package/CHANGELOG.md +95 -0
  2. package/README.md +17 -4
  3. package/docs/closures-last.md +34 -0
  4. package/docs/indent.md +102 -0
  5. package/docs/no-ambiguous-start.md +23 -0
  6. package/docs/no-loose-types.md +25 -0
  7. package/docs/no-typedef.md +24 -0
  8. package/docs/pretty-jsdoc-lines.md +30 -0
  9. package/docs/pretty-parens.md +59 -0
  10. package/docs/require-returns.md +21 -0
  11. package/docs/svelte-naming-convention.md +5 -6
  12. package/package.json +2 -7
  13. package/src/index.js +78 -19
  14. package/src/rules/ascii-order.js +113 -127
  15. package/src/rules/closures-last.js +323 -0
  16. package/src/rules/indent.js +84 -0
  17. package/src/rules/no-ambiguous-start.js +42 -0
  18. package/src/rules/no-loose-types.js +127 -0
  19. package/src/rules/no-typedef.js +44 -0
  20. package/src/rules/prefer-param-tag.js +35 -35
  21. package/src/rules/prefer-type-tag.js +29 -29
  22. package/src/rules/pretty-imports.js +23 -23
  23. package/src/rules/pretty-jsdoc-casting.js +39 -39
  24. package/src/rules/pretty-jsdoc-lines.js +104 -0
  25. package/src/rules/pretty-parens.js +280 -0
  26. package/src/rules/pretty-sequence.js +79 -95
  27. package/src/rules/pretty-ternary.js +27 -31
  28. package/src/rules/require-returns.js +190 -0
  29. package/src/rules/svelte-naming-convention.js +457 -546
  30. package/src/utils/accept_cr_eol.js +36 -0
  31. package/src/utils/indent_model.js +1596 -0
  32. package/src/utils/jsdoc_tags.js +30 -0
  33. package/src/utils/order_compare.js +17 -0
  34. package/src/utils/rule_options.js +30 -0
  35. package/src/utils/skip_unchanged_fixes.js +3 -3
  36. package/src/utils/source_text.js +80 -255
  37. package/src/utils/split_declarations.js +35 -14
  38. package/src/utils/trim_fixes.js +108 -0
  39. package/types/index.d.ts.map +1 -1
@@ -49,8 +49,6 @@ export default {
49
49
  const svelte_shorthands = new Set()
50
50
  /** @type {Map<string, (import("../../private.js").AstNode & import("estree").Identifier)[]>} */
51
51
  const declarations = new Map()
52
- /** @type {Set<import("../../private.js").AstNode>} */
53
- const default_exports = new Set()
54
52
  /** @type {Map<string, (import("../../private.js").AstNode & import("estree").Identifier)[]>} */
55
53
  const export_locals = new Map()
56
54
  /** @type {Set<import("../../private.js").AstNode>} */
@@ -67,415 +65,55 @@ export default {
67
65
  const visited = new Set()
68
66
  /** @type {import("../../private.js").DisableDirective[] | undefined} */
69
67
  let disable_directives
70
- /**
71
- * @param {Map<string, (import("../../private.js").AstNode & import("estree").Identifier)[]>} map
72
- * @param {import("../../private.js").AstNode & import("estree").Identifier} node
73
- * @returns {void}
74
- */
75
- function add(map, node) {
76
- const nodes = map.get(node.name)
77
- if (nodes) {
78
- nodes.push(node)
79
- } else {
80
- map.set(node.name, [ node ])
81
- }
82
- }
83
- /**
84
- * @param {import("../../private.js").Comment} comment
85
- * @param {string} name
86
- * @param {boolean} names_params
87
- * @returns {import("../../private.js").RenameEdit[]}
88
- */
89
- function comment_edits(comment, name, names_params) {
90
- if (comment.type != "Block" || !comment.value.startsWith("*")) {
91
- return []
92
- }
93
- const text = comment.value
94
- const start = comment.range[0] + 2
95
- const fixed_name = to_snake_case(name)
96
- const escaped = name.replace(/\$/g, "\\$")
97
- /** @type {import("../../private.js").RenameEdit[]} */
98
- const edits = []
99
- for (const match of text.matchAll(
100
- RegExp(
101
- `(?<=(?:asserts|typeof)\\s+)${escaped}(?![\\w$])|(?<![\\w$.])${escaped}(?=\\s+is(?![\\w$]))`,
102
- "g"
103
- )
104
- )) {
105
- if (!in_braces(text, match.index)) {
106
- continue
107
- }
108
- edits.push(
109
- {
110
- range: [
111
- start + match.index,
112
- start + match.index + name.length
113
- ],
114
- text: fixed_name
115
- }
116
- )
117
- }
118
- if (!names_params) {
119
- return edits
120
- }
121
- for (const match of text.matchAll(jsdoc_param_regex)) {
122
- let index = skip_space(
123
- text,
124
- match.index + match[0].length
125
- )
126
- if (text[index] == "{") {
127
- for (let depth = 0; index < text.length; index++) {
128
- if (text[index] == "{") {
129
- depth++
130
- } else if (text[index] == "}" && !--depth) {
131
- break
68
+ /** @type {import("../../private.js").RuleListener} */
69
+ const listener = {
70
+ /** @param {import("../../private.js").AstNode & import("estree").Identifier} node */
71
+ Identifier(node) {
72
+ const name = node.name
73
+ if (store_regex.test(name)) {
74
+ const store = name.slice(1)
75
+ if (is_svelte) {
76
+ const nodes = store_usages.get(store)
77
+ if (nodes) {
78
+ nodes.push(node)
79
+ } else {
80
+ store_usages.set(store, [ node ])
132
81
  }
82
+ } else {
83
+ blocked.add(store)
133
84
  }
134
- index = skip_space(text, index + 1)
135
- }
136
- if (text[index] == "[") {
137
- index = skip_space(text, index + 1)
138
- }
139
- if (text.startsWith(name, index) && !word_character_regex.test(
140
- text[index + name.length] ?? ""
141
- )) {
142
- edits.push(
143
- {
144
- range: [
145
- start + index,
146
- start + index + name.length
147
- ],
148
- text: fixed_name
149
- }
150
- )
151
85
  }
152
- }
153
- return edits
154
- }
155
- /**
156
- * @param {import("../../private.js").NamingIdentifier} node
157
- * @param {Map<import("../../private.js").AstNode, import("eslint").Scope.Variable | null>} resolution
158
- * @returns {import("../../private.js").RenameEdit[]}
159
- */
160
- function declaration_edits(node, resolution) {
161
- /** @type {import("../../private.js").RenameEdit[]} */
162
- const edits = [ rename_edit(node) ]
163
- const block = /** @type {import("../../private.js").AstNode} */(/** @type {import("eslint").Scope.Variable} */(resolution.get(node))/**/.scope.block)/**/
164
- const func = function_of_param(node)
165
- const leading = func && leading_comment(func)
166
- /** @type {Set<import("../../private.js").Comment>} */
167
- const comments = new Set(
168
- /** @type {import("../../private.js").Comment[]} */(source.getAllComments())/**/.filter(
169
- comment => block.type == "Program" || comment.range[0] >= block.range[0] && comment.range[1] <= block.range[1]
170
- )
171
- )
172
- if (leading) {
173
- comments.add(leading)
174
- }
175
- for (const comment of comments) {
176
- edits.push(
177
- ...comment_edits(
178
- comment,
179
- node.name,
180
- comment == leading
181
- )
182
- )
183
- }
184
- return edits
185
- }
186
- /**
187
- * @param {import("../../private.js").AstNode & import("estree").Identifier} node
188
- * @returns {void}
189
- */
190
- function declare(node) {
191
- add(declarations, node)
192
- }
193
- /**
194
- * @param {import("../../private.js").AstNode & import("estree").Identifier} node
195
- * @returns {void}
196
- */
197
- function defer(node) {
198
- add(usages, node)
199
- }
200
- /**
201
- * @param {string} _
202
- * @param {string} a
203
- * @param {string} b
204
- * @returns {string}
205
- */
206
- function fix_handler(_, a, b) {
207
- return a + (a ? "_" : "") + b.toLowerCase()
208
- }
209
- /**
210
- * @param {import("../../private.js").AstNode} node
211
- * @returns {import("../../private.js").AstNode | undefined}
212
- */
213
- function function_of_param(node) {
214
- const child = node.parent.type == "AssignmentPattern" && node.parent.left == node || node.parent.type == "RestElement"
215
- ? node.parent
216
- : node
217
- /** @type {import("../../private.js").AstNode & { params?: import("../../private.js").AstNode[] }} */
218
- const func = child.parent
219
- return function_types.has(func.type) && func.params?.includes(child) ? func : void 0
220
- }
221
- /**
222
- * @param {string} text
223
- * @param {number} index
224
- * @returns {boolean}
225
- */
226
- function in_braces(text, index) {
227
- let depth = 0
228
- for (let i = 0; i < index; i++) {
229
- if (text[i] == "{") {
230
- depth++
231
- } else if (text[i] == "}" && depth) {
232
- depth--
86
+ if (allow_regex.test(name) || visited.has(node)) {
87
+ return
233
88
  }
234
- }
235
- return depth > 0
236
- }
237
- /**
238
- * @param {import("../../private.js").AstNode} node
239
- * @returns {boolean}
240
- */
241
- function is_declaration(node) {
242
- for (let parent = node.parent; ; parent = parent.parent) {
89
+ visited.add(node)
90
+ const parent = /** @type {import("../../private.js").NamingParent} */(node.parent)/**/
243
91
  switch (parent.type) {
244
- case "ArrayPattern":
245
- case "AssignmentPattern":
246
- case "ObjectPattern":
247
- case "Property":
248
- case "RestElement":
249
- break
250
- case "AssignmentExpression":
251
- case "ForInStatement":
252
- case "ForOfStatement":
253
- return false
254
- default:
255
- return true
256
- }
257
- }
258
- }
259
- /**
260
- * @param {import("../../private.js").AstNode} node
261
- * @returns {boolean}
262
- */
263
- function is_disabled(node) {
264
- const { column, line } = /** @type {import("estree").SourceLocation} */(node.loc)/**/.start
265
- let disabled = false
266
- disable_directives ??= (/** @type {import("../../private.js").DirectiveSource} */(/** @type {unknown} */(source))/**/).getDisableDirectives?.().directives ?? []
267
- for (const directive of disable_directives) {
268
- const rule_ids = directive.value.split(",").map(rule_id => rule_id.trim())
269
- .filter(Boolean)
270
- if (rule_ids.length && !rule_ids.includes(context.id)) {
271
- continue
272
- }
273
- const comment_loc = directive.node.loc
274
- switch (directive.type) {
275
- case "disable":
276
- case "enable":
277
- if (comment_loc.start.line < line || comment_loc.start.line == line && comment_loc.start.column < column) {
278
- disabled = directive.type == "disable"
92
+ case "AccessorProperty":
93
+ case "TSAbstractAccessorProperty":
94
+ case "TSAbstractMethodDefinition":
95
+ case "TSAbstractPropertyDefinition":
96
+ case "TSMethodSignature":
97
+ case "TSPropertySignature":
98
+ if (parent.computed || parent.value == node) {
99
+ defer(node)
279
100
  }
280
101
  break
281
- case "disable-line":
282
- if (comment_loc.start.line == line) {
283
- return true
102
+ case "ArrayExpression":
103
+ defer(node)
104
+ break
105
+ case "ArrayPattern":
106
+ if (is_declaration(node)) {
107
+ declare(node)
108
+ } else {
109
+ defer(node)
284
110
  }
285
111
  break
286
- case "disable-next-line":
287
- if (comment_loc.end.line + 1 == line) {
288
- return true
289
- }
290
- }
291
- }
292
- return disabled
293
- }
294
- /**
295
- * @param {import("../../private.js").AstNode} func
296
- * @returns {import("../../private.js").Comment | undefined}
297
- */
298
- function leading_comment(func) {
299
- let anchor = func
300
- const parent = func.parent
301
- switch (parent.type) {
302
- case "MethodDefinition":
303
- case "Property":
304
- case "PropertyDefinition":
305
- anchor = parent
306
- break
307
- case "VariableDeclarator":
308
- anchor = parent.parent
309
- }
310
- if (anchor.parent.type == "ExportDefaultDeclaration" || anchor.parent.type == "ExportNamedDeclaration") {
311
- anchor = anchor.parent
312
- }
313
- return /** @type {import("../../private.js").Comment[]} */(source.getCommentsBefore(
314
- /** @type {import("estree").Node} */(anchor)/**/
315
- ))/**/.at(-1)
316
- }
317
- /**
318
- * @param {import("../../private.js").NamingIdentifier} target
319
- * @returns {import("../../private.js").RenameEdit}
320
- */
321
- function rename_edit(target) {
322
- const name = target.name
323
- const fixed_name = to_snake_case(name)
324
- const parent = target.parent
325
- const decorators = target.decorators
326
- const start = source.text.indexOf(
327
- name,
328
- decorators?.length
329
- ? /** @type {import("../../private.js").AstNode} */(decorators[decorators.length - 1])/**/.range[1]
330
- : target.range[0]
331
- )
332
- /** @type {[number, number]} */
333
- const range = [ start, start + name.length ]
334
- if (svelte_shorthands.has(target)) {
335
- return {
336
- range: /** @type {[number, number]} */(parent.range)/**/,
337
- text: name + "={" + fixed_name + "}"
338
- }
339
- }
340
- if (shorthand_properties.has(target)) {
341
- return {
342
- range,
343
- text: name + ": " + fixed_name
344
- }
345
- }
346
- if (import_shorthands.has(target)) {
347
- return {
348
- range,
349
- text: name + " as " + fixed_name
350
- }
351
- }
352
- if (export_shorthands.has(target)) {
353
- return {
354
- range,
355
- text: fixed_name + " as " + name
356
- }
357
- }
358
- if (parent.type == "ImportSpecifier" && parent.imported.type == "Identifier" && parent.imported.name == fixed_name) {
359
- return {
360
- range: [
361
- /** @type {import("../../private.js").AstNode} */(/** @type {unknown} */(parent.imported))/**/.range[0],
362
- range[1]
363
- ],
364
- text: fixed_name
365
- }
366
- }
367
- if (parent.type == "ExportSpecifier" && parent.local == target && parent.exported.type == "Identifier" && parent.exported.name == fixed_name) {
368
- return {
369
- range: [
370
- range[0],
371
- /** @type {import("../../private.js").AstNode} */(/** @type {unknown} */(parent.exported))/**/.range[1]
372
- ],
373
- text: fixed_name
374
- }
375
- }
376
- return { range, text: fixed_name }
377
- }
378
- /**
379
- * @param {import("../../private.js").NamingIdentifier} node
380
- * @param {import("../../private.js").RenameEdit[] | null} edits
381
- * @returns {void}
382
- */
383
- function report(node, edits) {
384
- context.report(
385
- {
386
- data: {
387
- expected: to_snake_case(node.name),
388
- name: node.name
389
- },
390
- fix(fixer) {
391
- if (!edits) {
392
- return null
393
- }
394
- /** @type {Set<number>} */
395
- const starts = new Set()
396
- return edits.filter(
397
- edit => !starts.has(edit.range[0]) && !!starts.add(edit.range[0])
398
- )
399
- .map(
400
- edit => fixer.replaceTextRange(edit.range, edit.text)
401
- )
402
- },
403
- messageId: fixable_name_regex.test(node.name) && allow_regex.test(to_snake_case(node.name))
404
- ? "rename"
405
- : "case",
406
- node: /** @type {import("estree").Node} */(node)/**/
407
- }
408
- )
409
- }
410
- /**
411
- * @param {string} text
412
- * @param {number} index
413
- * @returns {number}
414
- */
415
- function skip_space(text, index) {
416
- while (index < text.length && space_regex.test(
417
- /** @type {string} */(text[index])/**/
418
- )) {
419
- index++
420
- }
421
- return index
422
- }
423
- /**
424
- * @param {string} name
425
- * @returns {string}
426
- */
427
- function to_snake_case(name) {
428
- return name.replace(fix_regex, fix_handler)
429
- }
430
- /** @type {import("../../private.js").RuleListener} */
431
- const listener = {
432
- /** @param {import("../../private.js").AstNode & import("estree").Identifier} node */
433
- Identifier(node) {
434
- const name = node.name
435
- if (store_regex.test(name)) {
436
- const store = name.slice(1)
437
- if (is_svelte) {
438
- const nodes = store_usages.get(store)
439
- if (nodes) {
440
- nodes.push(node)
441
- } else {
442
- store_usages.set(store, [ node ])
443
- }
444
- } else {
445
- blocked.add(store)
446
- }
447
- }
448
- if (allow_regex.test(name) || visited.has(node)) {
449
- return
450
- }
451
- visited.add(node)
452
- const parent = /** @type {import("../../private.js").NamingParent} */(node.parent)/**/
453
- switch (parent.type) {
454
- case "AccessorProperty":
455
- case "TSAbstractAccessorProperty":
456
- case "TSAbstractMethodDefinition":
457
- case "TSAbstractPropertyDefinition":
458
- case "TSMethodSignature":
459
- case "TSPropertySignature":
460
- if (parent.computed || parent.value == node) {
461
- defer(node)
462
- }
463
- break
464
- case "ArrayExpression":
465
- defer(node)
466
- break
467
- case "ArrayPattern":
468
- if (is_declaration(node)) {
469
- declare(node)
470
- } else {
471
- defer(node)
472
- }
473
- break
474
- case "ArrowFunctionExpression":
475
- if (parent.body == node) {
476
- defer(node)
477
- } else {
478
- declare(node)
112
+ case "ArrowFunctionExpression":
113
+ if (parent.body == node) {
114
+ defer(node)
115
+ } else {
116
+ declare(node)
479
117
  }
480
118
  break
481
119
  case "AssignmentExpression":
@@ -554,7 +192,6 @@ export default {
554
192
  case "ExportAllDeclaration":
555
193
  break
556
194
  case "ExportDefaultDeclaration":
557
- default_exports.add(node)
558
195
  defer(node)
559
196
  break
560
197
  case "ExportSpecifier":
@@ -708,7 +345,7 @@ export default {
708
345
  if (parent.id == node) {
709
346
  if (
710
347
  parent.parent?.parent?.type != "ExportNamedDeclaration"
711
- || !camel_case_regex.test(name)
348
+ || !camel_case_regex.test(name)
712
349
  ) {
713
350
  declare(node)
714
351
  }
@@ -733,8 +370,8 @@ export default {
733
370
  }
734
371
  } else if (
735
372
  svelte_usage_parents.has(type)
736
- || type == "SvelteDirective" && svelte_parent.kind != "Let"
737
- || svelte_expression_parents.has(type) && svelte_parent.expression == node
373
+ || type == "SvelteDirective" && svelte_parent.kind != "Let"
374
+ || svelte_expression_parents.has(type) && svelte_parent.expression == node
738
375
  ) {
739
376
  defer(node)
740
377
  } else {
@@ -777,7 +414,7 @@ export default {
777
414
  }
778
415
  for (const identifier of variable.identifiers) {
779
416
  resolution.set(
780
- /** @type {import("../../private.js").AstNode} */(/** @type {unknown} */(identifier))/**/,
417
+ /** @type {import("../../private.js").AstNode} */(/** @type {unknown} */(identifier))/**/,
781
418
  variable
782
419
  )
783
420
  }
@@ -808,29 +445,55 @@ export default {
808
445
  )
809
446
  }
810
447
  }
811
- /** @type {Map<string, Set<import("eslint").Scope.Scope>>} */
812
- const scopes_by_target = new Map()
813
- for (const variable of renamable) {
814
- const target = to_snake_case(variable.name)
815
- const target_scopes = scopes_by_target.get(target)
816
- if (target_scopes) {
817
- target_scopes.add(variable.scope)
818
- } else {
819
- scopes_by_target.set(
820
- target,
821
- new Set([ variable.scope ])
822
- )
448
+ /** @type {import("../../private.js").RenameEdit[]} */
449
+ const edits = []
450
+ /** @type {import("../../private.js").NamingIdentifier | undefined} */
451
+ let fix_node
452
+ /** @type {Set<string>} */
453
+ const targets = new Set()
454
+ /** @type {Map<string, import("../../private.js").NamingIdentifier[]>} */
455
+ const reported_usages = new Map()
456
+ for (const [ name, nodes ] of declarations) {
457
+ const exported = (export_locals.get(name) ?? []).filter(belongs)
458
+ const used = (usages.get(name) ?? []).filter(belongs)
459
+ reported_usages.set(name, [ ...exported, ...used ])
460
+ const renamed = [
461
+ ...exported,
462
+ ...used,
463
+ ...store_usages.get(name) ?? []
464
+ ]
465
+ const target = to_snake_case(name)
466
+ if (
467
+ targets.has(target)
468
+ || blocked.has(name)
469
+ || !fixable_name_regex.test(name)
470
+ || collides(name)
471
+ || has_foreign_declaration(name)
472
+ || [ ...nodes, ...renamed ].some(is_disabled)
473
+ ) {
474
+ continue
823
475
  }
476
+ targets.add(target)
477
+ fix_node ??= nodes[0]
478
+ edits.push(
479
+ ...nodes.flatMap(
480
+ node => declaration_edits(node, resolution)
481
+ ),
482
+ ...renamed.map(rename_edit)
483
+ )
824
484
  }
825
- /** @type {Set<string>} */
826
- const nested_targets = new Set()
827
- for (const variable of renamable) {
828
- const target = to_snake_case(variable.name)
829
- const target_scopes = /** @type {Set<import("eslint").Scope.Scope>} */(scopes_by_target.get(target))/**/
830
- for (let scope = variable.scope.upper; scope; scope = scope.upper) {
831
- if (target_scopes.has(scope)) {
832
- nested_targets.add(target)
833
- }
485
+ for (const [ name, nodes ] of declarations) {
486
+ for (const node of nodes) {
487
+ report(
488
+ node,
489
+ node == fix_node ? edits : null
490
+ )
491
+ }
492
+ if (!fix_same_names) {
493
+ continue
494
+ }
495
+ for (const node of /** @type {import("../../private.js").NamingIdentifier[]} */(reported_usages.get(name))/**/) {
496
+ report(node, null)
834
497
  }
835
498
  }
836
499
  /**
@@ -897,139 +560,387 @@ export default {
897
560
  )
898
561
  )
899
562
  }
900
- /** @type {Set<import("../../private.js").AstNode>} */
901
- const held = new Set(default_exports)
902
- for (const variable of renamable) {
903
- for (const reference of variable.references) {
904
- if (reference.isWrite() && !reference.init) {
905
- held.add(
906
- /** @type {import("../../private.js").AstNode} */(/** @type {unknown} */(reference.identifier))/**/
907
- )
908
- }
909
- }
563
+ }
564
+ }
565
+ return /** @type {import("eslint").Rule.RuleListener} */(/** @type {unknown} */(listener))/**/
566
+ /**
567
+ * @param {Map<string, (import("../../private.js").AstNode & import("estree").Identifier)[]>} map
568
+ * @param {import("../../private.js").AstNode & import("estree").Identifier} node
569
+ * @returns {void}
570
+ */
571
+ function add(map, node) {
572
+ const nodes = map.get(node.name)
573
+ if (nodes) {
574
+ nodes.push(node)
575
+ } else {
576
+ map.set(node.name, [ node ])
577
+ }
578
+ }
579
+ /**
580
+ * @param {import("../../private.js").Comment} comment
581
+ * @param {string} name
582
+ * @param {boolean} names_params
583
+ * @returns {import("../../private.js").RenameEdit[]}
584
+ */
585
+ function comment_edits(comment, name, names_params) {
586
+ if (comment.type != "Block" || !comment.value.startsWith("*")) {
587
+ return []
588
+ }
589
+ const text = comment.value
590
+ const start = comment.range[0] + 2
591
+ const fixed_name = to_snake_case(name)
592
+ const escaped = name.replace(/\$/g, "\\$")
593
+ /** @type {import("../../private.js").RenameEdit[]} */
594
+ const edits = []
595
+ for (const match of text.matchAll(
596
+ RegExp(
597
+ `(?<=(?:asserts|typeof)\\s+)${escaped}(?![\\w$])|(?<![\\w$.])${escaped}(?=\\s+is(?![\\w$]))`,
598
+ "g"
599
+ )
600
+ )) {
601
+ if (!in_braces(text, match.index)) {
602
+ continue
910
603
  }
911
- /** @type {Map<string, import("../../private.js").RenamePlan>} */
912
- const plans = new Map()
913
- for (const [ name, nodes ] of declarations) {
914
- const target = to_snake_case(name)
915
- const used = (usages.get(name) ?? []).filter(belongs)
916
- plans.set(
917
- name,
604
+ edits.push(
605
+ {
606
+ range: [
607
+ start + match.index,
608
+ start + match.index + name.length
609
+ ],
610
+ text: fixed_name
611
+ }
612
+ )
613
+ }
614
+ if (!names_params) {
615
+ return edits
616
+ }
617
+ for (const match of text.matchAll(jsdoc_param_regex)) {
618
+ let index = skip_space(
619
+ text,
620
+ match.index + match[0].length
621
+ )
622
+ if (text[index] == "{") {
623
+ for (let depth = 0; index < text.length; index++) {
624
+ if (text[index] == "{") {
625
+ depth++
626
+ } else if (text[index] == "}" && !--depth) {
627
+ break
628
+ }
629
+ }
630
+ index = skip_space(text, index + 1)
631
+ }
632
+ if (text[index] == "[") {
633
+ index = skip_space(text, index + 1)
634
+ }
635
+ if (text.startsWith(name, index) && !word_character_regex.test(
636
+ text[index + name.length] ?? ""
637
+ )) {
638
+ edits.push(
918
639
  {
919
- atomic: !fix_same_names || variables_by_name.has(target) || nested_targets.has(target) || store_usages.has(name),
920
- exported: (export_locals.get(name) ?? []).filter(belongs),
921
- fixable: !blocked.has(name)
922
- && fixable_name_regex.test(name)
923
- && !collides(name)
924
- && !has_foreign_declaration(name)
925
- && !nodes.some(is_disabled),
926
- held: used.filter(node => held.has(node)),
927
- used: used.filter(node => !held.has(node))
640
+ range: [
641
+ start + index,
642
+ start + index + name.length
643
+ ],
644
+ text: fixed_name
928
645
  }
929
646
  )
930
647
  }
931
- /**
932
- * @param {import("../../private.js").NamingIdentifier} node
933
- * @param {import("../../private.js").NamingIdentifier[]} nodes
934
- * @param {import("../../private.js").NamingIdentifier[]} usages_held
935
- * @returns {import("../../private.js").RenameEdit[]}
936
- */
937
- function declaration_fix(node, nodes, usages_held) {
938
- const variable = resolution.get(node)
939
- return [
940
- ...declaration_edits(node, resolution),
941
- ...nodes.find(
942
- other => resolution.get(other) == variable
943
- ) == node
944
- ? usages_held.filter(
945
- usage => resolution.get(usage) == variable
946
- ).map(rename_edit)
947
- : []
948
- ]
648
+ }
649
+ return edits
650
+ }
651
+ /**
652
+ * @param {import("../../private.js").NamingIdentifier} node
653
+ * @param {Map<import("../../private.js").AstNode, import("eslint").Scope.Variable | null>} resolution
654
+ * @returns {import("../../private.js").RenameEdit[]}
655
+ */
656
+ function declaration_edits(node, resolution) {
657
+ /** @type {import("../../private.js").RenameEdit[]} */
658
+ const edits = [ rename_edit(node) ]
659
+ const block = /** @type {import("../../private.js").AstNode} */(/** @type {import("eslint").Scope.Variable} */(resolution.get(node))/**/.scope.block)/**/
660
+ const func = function_of_param(node)
661
+ const leading = func && leading_comment(func)
662
+ /** @type {Set<import("../../private.js").Comment>} */
663
+ const comments = new Set(
664
+ /** @type {import("../../private.js").Comment[]} */(source.getAllComments())/**/.filter(
665
+ comment => block.type == "Program" || comment.range[0] >= block.range[0] && comment.range[1] <= block.range[1]
666
+ )
667
+ )
668
+ if (leading) {
669
+ comments.add(leading)
670
+ }
671
+ for (const comment of comments) {
672
+ edits.push(
673
+ ...comment_edits(
674
+ comment,
675
+ node.name,
676
+ comment == leading
677
+ )
678
+ )
679
+ }
680
+ return edits
681
+ }
682
+ /**
683
+ * @param {import("../../private.js").AstNode & import("estree").Identifier} node
684
+ * @returns {void}
685
+ */
686
+ function declare(node) {
687
+ if (!is_ambient(node)) {
688
+ add(declarations, node)
689
+ }
690
+ }
691
+ /**
692
+ * @param {import("../../private.js").AstNode & import("estree").Identifier} node
693
+ * @returns {void}
694
+ */
695
+ function defer(node) {
696
+ add(usages, node)
697
+ }
698
+ /**
699
+ * @param {string} _
700
+ * @param {string} a
701
+ * @param {string} b
702
+ * @returns {string}
703
+ */
704
+ function fix_handler(_, a, b) {
705
+ return a + (a ? "_" : "") + b.toLowerCase()
706
+ }
707
+ /**
708
+ * @param {import("../../private.js").AstNode} node
709
+ * @returns {import("../../private.js").AstNode | undefined}
710
+ */
711
+ function function_of_param(node) {
712
+ const child = node.parent.type == "AssignmentPattern" && node.parent.left == node || node.parent.type == "RestElement"
713
+ ? node.parent
714
+ : node
715
+ /** @type {import("../../private.js").AstNode & { params?: import("../../private.js").AstNode[] }} */
716
+ const func = child.parent
717
+ return function_types.has(func.type) && func.params?.includes(child) ? func : void 0
718
+ }
719
+ /**
720
+ * @param {string} text
721
+ * @param {number} index
722
+ * @returns {boolean}
723
+ */
724
+ function in_braces(text, index) {
725
+ let depth = 0
726
+ for (let i = 0; i < index; i++) {
727
+ if (text[i] == "{") {
728
+ depth++
729
+ } else if (text[i] == "}" && depth) {
730
+ depth--
949
731
  }
950
- /** @type {import("../../private.js").RenameEdit[]} */
951
- const atomic = []
952
- /** @type {import("../../private.js").NamingIdentifier | undefined} */
953
- let atomic_node
954
- for (const [ name, nodes ] of declarations) {
955
- const plan = /** @type {import("../../private.js").RenamePlan} */(plans.get(name))/**/
956
- if (!plan.exported.length || !plan.fixable || !plan.atomic && plan.used.length) {
957
- continue
732
+ }
733
+ return depth > 0
734
+ }
735
+ /**
736
+ * @param {import("../../private.js").AstNode} node
737
+ * @returns {boolean}
738
+ */
739
+ function is_ambient(node) {
740
+ for (
741
+ let current = /** @type {import("../../private.js").AstNode | undefined} */(node)/**/;
742
+ current;
743
+ current = current.parent
744
+ ) {
745
+ if ("declare" in current && current.declare === true) {
746
+ return true
747
+ }
748
+ }
749
+ return false
750
+ }
751
+ /**
752
+ * @param {import("../../private.js").AstNode} node
753
+ * @returns {boolean}
754
+ */
755
+ function is_declaration(node) {
756
+ for (let parent = node.parent; ; parent = parent.parent) {
757
+ switch (parent.type) {
758
+ case "ArrayPattern":
759
+ case "AssignmentPattern":
760
+ case "ObjectPattern":
761
+ case "Property":
762
+ case "RestElement":
763
+ break
764
+ case "AssignmentExpression":
765
+ case "ForInStatement":
766
+ case "ForOfStatement":
767
+ return false
768
+ default:
769
+ return true
770
+ }
771
+ }
772
+ }
773
+ /**
774
+ * @param {import("../../private.js").AstNode} node
775
+ * @returns {boolean}
776
+ */
777
+ function is_disabled(node) {
778
+ const { column, line } = /** @type {import("estree").SourceLocation} */(node.loc)/**/.start
779
+ let disabled = false
780
+ disable_directives ??= (/** @type {import("../../private.js").DirectiveSource} */(/** @type {unknown} */(source))/**/).getDisableDirectives?.().directives ?? []
781
+ for (const directive of disable_directives) {
782
+ const rule_ids = directive.value.split(",").map(rule_id => rule_id.trim())
783
+ .filter(Boolean)
784
+ if (rule_ids.length && !rule_ids.includes(context.id)) {
785
+ continue
786
+ }
787
+ const comment_loc = directive.node.loc
788
+ switch (directive.type) {
789
+ case "disable":
790
+ case "enable":
791
+ if (comment_loc.start.line < line || comment_loc.start.line == line && comment_loc.start.column < column) {
792
+ disabled = directive.type == "disable"
958
793
  }
959
- atomic_node ??= nodes[0]
960
- for (const node of nodes) {
961
- atomic.push(
962
- ...declaration_edits(node, resolution)
963
- )
794
+ break
795
+ case "disable-line":
796
+ if (comment_loc.start.line == line) {
797
+ return true
964
798
  }
965
- for (const node of [
966
- ...plan.exported,
967
- ...plan.used,
968
- ...plan.held,
969
- ...store_usages.get(name) ?? []
970
- ]) {
971
- atomic.push(rename_edit(node))
799
+ break
800
+ case "disable-next-line":
801
+ if (comment_loc.end.line + 1 == line) {
802
+ return true
972
803
  }
973
804
  }
974
- for (const [ name, nodes ] of declarations) {
975
- const {
976
- atomic: renames_at_once,
977
- exported,
978
- fixable,
979
- held: usages_held,
980
- used
981
- } = /** @type {import("../../private.js").RenamePlan} */(plans.get(name))/**/
982
- if (exported.length) {
983
- for (const node of nodes) {
984
- report(
985
- node,
986
- node == atomic_node ? atomic : null
987
- )
988
- }
989
- } else if (!renames_at_once) {
990
- for (const node of nodes) {
991
- report(
992
- node,
993
- fixable && !used.length ? declaration_fix(node, nodes, usages_held) : null
994
- )
995
- }
996
- } else {
997
- report(
998
- /** @type {import("../../private.js").NamingIdentifier} */(nodes[0])/**/,
999
- fixable
1000
- ? [
1001
- ...nodes.flatMap(
1002
- node => declaration_edits(node, resolution)
1003
- ),
1004
- ...used.map(rename_edit),
1005
- ...usages_held.map(rename_edit),
1006
- ...(store_usages.get(name) ?? []).map(rename_edit)
1007
- ]
1008
- : null
1009
- )
1010
- for (const node of nodes.slice(1)) {
1011
- report(node, null)
805
+ }
806
+ return disabled
807
+ }
808
+ /**
809
+ * @param {import("../../private.js").AstNode} func
810
+ * @returns {import("../../private.js").Comment | undefined}
811
+ */
812
+ function leading_comment(func) {
813
+ let anchor = func
814
+ const parent = func.parent
815
+ switch (parent.type) {
816
+ case "MethodDefinition":
817
+ case "Property":
818
+ case "PropertyDefinition":
819
+ anchor = parent
820
+ break
821
+ case "VariableDeclarator":
822
+ anchor = parent.parent
823
+ }
824
+ if (anchor.parent.type == "ExportDefaultDeclaration" || anchor.parent.type == "ExportNamedDeclaration") {
825
+ anchor = anchor.parent
826
+ }
827
+ return /** @type {import("../../private.js").Comment[]} */(source.getCommentsBefore(
828
+ /** @type {import("estree").Node} */(anchor)/**/
829
+ ))/**/.at(-1)
830
+ }
831
+ /**
832
+ * @param {import("../../private.js").NamingIdentifier} target
833
+ * @returns {import("../../private.js").RenameEdit}
834
+ */
835
+ function rename_edit(target) {
836
+ const name = target.name
837
+ const fixed_name = to_snake_case(name)
838
+ const parent = target.parent
839
+ const decorators = target.decorators
840
+ const start = source.text.indexOf(
841
+ name,
842
+ decorators?.length
843
+ ? /** @type {import("../../private.js").AstNode} */(decorators[decorators.length - 1])/**/.range[1]
844
+ : target.range[0]
845
+ )
846
+ /** @type {[number, number]} */
847
+ const range = [ start, start + name.length ]
848
+ if (svelte_shorthands.has(target)) {
849
+ return {
850
+ range: /** @type {[number, number]} */(parent.range)/**/,
851
+ text: name + "={" + fixed_name + "}"
852
+ }
853
+ }
854
+ if (shorthand_properties.has(target)) {
855
+ return {
856
+ range,
857
+ text: name + ": " + fixed_name
858
+ }
859
+ }
860
+ if (import_shorthands.has(target)) {
861
+ return {
862
+ range,
863
+ text: name + " as " + fixed_name
864
+ }
865
+ }
866
+ if (export_shorthands.has(target)) {
867
+ return {
868
+ range,
869
+ text: fixed_name + " as " + name
870
+ }
871
+ }
872
+ if (parent.type == "ImportSpecifier" && parent.imported.type == "Identifier" && parent.imported.name == fixed_name) {
873
+ return {
874
+ range: [
875
+ /** @type {import("../../private.js").AstNode} */(/** @type {unknown} */(parent.imported))/**/.range[0],
876
+ range[1]
877
+ ],
878
+ text: fixed_name
879
+ }
880
+ }
881
+ if (parent.type == "ExportSpecifier" && parent.local == target && parent.exported.type == "Identifier" && parent.exported.name == fixed_name) {
882
+ return {
883
+ range: [
884
+ range[0],
885
+ /** @type {import("../../private.js").AstNode} */(/** @type {unknown} */(parent.exported))/**/.range[1]
886
+ ],
887
+ text: fixed_name
888
+ }
889
+ }
890
+ return { range, text: fixed_name }
891
+ }
892
+ /**
893
+ * @param {import("../../private.js").NamingIdentifier} node
894
+ * @param {import("../../private.js").RenameEdit[] | null} edits
895
+ * @returns {void}
896
+ */
897
+ function report(node, edits) {
898
+ context.report(
899
+ {
900
+ data: {
901
+ expected: to_snake_case(node.name),
902
+ name: node.name
903
+ },
904
+ fix(fixer) {
905
+ if (!edits) {
906
+ return null
1012
907
  }
1013
- }
1014
- if (!fix_same_names) {
1015
- continue
1016
- }
1017
- for (const node of exported) {
1018
- report(node, null)
1019
- }
1020
- for (const node of usages_held) {
1021
- report(node, null)
1022
- }
1023
- for (const node of used) {
1024
- report(
1025
- node,
1026
- fixable && !renames_at_once ? [ rename_edit(node) ] : null
908
+ /** @type {Set<number>} */
909
+ const starts = new Set()
910
+ return edits.filter(
911
+ edit => !starts.has(edit.range[0]) && !!starts.add(edit.range[0])
1027
912
  )
1028
- }
913
+ .map(
914
+ edit => fixer.replaceTextRange(edit.range, edit.text)
915
+ )
916
+ },
917
+ messageId: fixable_name_regex.test(node.name) && allow_regex.test(to_snake_case(node.name))
918
+ ? "rename"
919
+ : "case",
920
+ node: /** @type {import("estree").Node} */(node)/**/
1029
921
  }
922
+ )
923
+ }
924
+ /**
925
+ * @param {string} text
926
+ * @param {number} index
927
+ * @returns {number}
928
+ */
929
+ function skip_space(text, index) {
930
+ while (index < text.length && space_regex.test(
931
+ /** @type {string} */(text[index])/**/
932
+ )) {
933
+ index++
1030
934
  }
935
+ return index
936
+ }
937
+ /**
938
+ * @param {string} name
939
+ * @returns {string}
940
+ */
941
+ function to_snake_case(name) {
942
+ return name.replace(fix_regex, fix_handler)
1031
943
  }
1032
- return /** @type {import("eslint").Rule.RuleListener} */(/** @type {unknown} */(listener))/**/
1033
944
  },
1034
945
  meta: {
1035
946
  docs: {