eslint-plugin-lube 0.5.0 → 0.6.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 (49) hide show
  1. package/CHANGELOG.md +260 -0
  2. package/LICENSE +21 -0
  3. package/README.md +120 -9
  4. package/docs/ascii-order.md +81 -0
  5. package/docs/pretty-imports.md +70 -59
  6. package/docs/pretty-jsdoc-casting.md +31 -23
  7. package/docs/pretty-sequence.md +129 -117
  8. package/docs/pretty-ternary.md +55 -0
  9. package/docs/svelte-naming-convention.md +59 -46
  10. package/package.json +60 -36
  11. package/src/index.js +299 -0
  12. package/src/rules/ascii-order.js +940 -0
  13. package/src/rules/pretty-imports.js +176 -0
  14. package/src/rules/pretty-jsdoc-casting.js +278 -0
  15. package/src/rules/pretty-sequence.js +489 -0
  16. package/src/rules/pretty-ternary.js +227 -0
  17. package/src/rules/svelte-naming-convention.js +903 -0
  18. package/src/utils/fix_recorder.js +50 -0
  19. package/src/utils/keep_comments.js +99 -0
  20. package/src/utils/keep_line_breaks.js +56 -0
  21. package/src/utils/skip_hole_commas.js +32 -0
  22. package/src/utils/skip_optional_patterns.js +27 -0
  23. package/src/utils/skip_unchanged_fixes.js +35 -0
  24. package/src/utils/source_text.js +442 -0
  25. package/src/utils/split_declarations.js +75 -0
  26. package/src/utils/text_width.js +9 -0
  27. package/types/index.d.ts +29 -0
  28. package/types/index.d.ts.map +1 -0
  29. package/index.js +0 -125
  30. package/private.d.ts +0 -168
  31. package/rules/pretty-imports.js +0 -147
  32. package/rules/pretty-jsdoc-casting.js +0 -236
  33. package/rules/pretty-sequence.js +0 -441
  34. package/rules/svelte-naming-convention.js +0 -361
  35. package/test/pretty-imports/ExportNamedDeclaration.js +0 -123
  36. package/test/pretty-imports/ImportDeclaration.js +0 -125
  37. package/test/pretty-imports.js +0 -20
  38. package/test/pretty-jsdoc-casting/case_by_expression.js +0 -249
  39. package/test/pretty-jsdoc-casting/various_situations.js +0 -95
  40. package/test/pretty-jsdoc-casting.js +0 -22
  41. package/test/pretty-sequence/case_by_node_type.js +0 -340
  42. package/test/pretty-sequence/case_by_option.js +0 -142
  43. package/test/pretty-sequence/various_situations.js +0 -193
  44. package/test/pretty-sequence.js +0 -23
  45. package/test/svelte-naming-convention/basic_snake_case.js +0 -54
  46. package/test/svelte-naming-convention/case_by_identifier.js +0 -878
  47. package/test/svelte-naming-convention/snake_case_with_symbols.js +0 -46
  48. package/test/svelte-naming-convention.js +0 -25
  49. package/tsconfig.json +0 -16
@@ -0,0 +1,903 @@
1
+ /** @type {import("eslint").Rule.RuleModule} */
2
+ export default {
3
+ create(context) {
4
+ /** @type {import("../../private.js").RuleOptions["svelte-naming-convention"]} */
5
+ const option = context.options[0]
6
+ const fix_same_names = option?.fixSameNames ?? true
7
+ const source = context.sourceCode
8
+ const function_types = new Set(
9
+ [
10
+ "ArrowFunctionExpression",
11
+ "FunctionDeclaration",
12
+ "FunctionExpression",
13
+ "TSDeclareFunction",
14
+ "TSEmptyBodyFunctionExpression"
15
+ ]
16
+ )
17
+ const jsdoc_param_regex = /@(?:arg|argument|param)(?![\w$])/g
18
+ const lowercase_regex = /^[a-z]/
19
+ const space_regex = /\s/
20
+ const store_regex = /^\$[^$]/
21
+ const svelte_expression_parents = new Set(
22
+ [
23
+ "SvelteAwaitBlock",
24
+ "SvelteEachBlock",
25
+ "SvelteIfBlock",
26
+ "SvelteKeyBlock"
27
+ ]
28
+ )
29
+ const svelte_usage_parents = new Set(
30
+ [
31
+ "SvelteDebugTag",
32
+ "SvelteMustacheTag",
33
+ "SvelteSpecialDirective",
34
+ "SvelteSpreadAttribute",
35
+ "SvelteStyleDirective"
36
+ ]
37
+ )
38
+ const word_character_regex = /[\w$]/
39
+ const allow_regex = /^[_$]?[_$]?(?:[\da-z]+(?:_[\da-z]+)*\$?\$?)?$|^[A-Z](?:_?[\dA-Z]+)*$|^(?:[\dA-Z][\da-z]*)+$/
40
+ const camel_case_regex = /^[\da-z]+([A-Z][\da-z]*)*$/
41
+ const fix_regex = /([\da-z]?)([A-Z][\dA-Z]*)/g
42
+ const fixable_name_regex = /^[_$]?[_$]?(?:[\dA-Za-z]+(?:_[\dA-Za-z]+)*\$?\$?)?$/
43
+ /** @type {Set<string>} */
44
+ const blocked = new Set()
45
+ const is_svelte = !!(/** @type {{ isSvelte?: boolean } | undefined} */(source.parserServices)/**/)?.isSvelte
46
+ /** @type {Map<string, import("../../private.js").NamingIdentifier[]>} */
47
+ const store_usages = new Map()
48
+ /** @type {Set<import("../../private.js").NamingIdentifier>} */
49
+ const svelte_shorthands = new Set()
50
+ /** @type {Map<string, (import("../../private.js").AstNode & import("estree").Identifier)[]>} */
51
+ const declarations = new Map()
52
+ /** @type {Set<import("../../private.js").AstNode>} */
53
+ const default_exports = new Set()
54
+ /** @type {Map<string, (import("../../private.js").AstNode & import("estree").Identifier)[]>} */
55
+ const export_locals = new Map()
56
+ /** @type {Set<import("../../private.js").AstNode>} */
57
+ const export_shorthands = new Set()
58
+ /** @type {Set<import("../../private.js").AstNode>} */
59
+ const import_shorthands = new Set()
60
+ /** @type {Set<import("../../private.js").AstNode & import("estree").Identifier>} */
61
+ const jsx_objects = new Set()
62
+ /** @type {Set<import("../../private.js").AstNode>} */
63
+ const shorthand_properties = new Set()
64
+ /** @type {Map<string, (import("../../private.js").AstNode & import("estree").Identifier)[]>} */
65
+ const usages = new Map()
66
+ /** @type {Set<import("../../private.js").AstNode>} */
67
+ const visited = new Set()
68
+ /** @type {import("../../private.js").DisableDirective[] | undefined} */
69
+ 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) nodes.push(node)
78
+ else map.set(node.name, [ node ])
79
+ }
80
+ /**
81
+ * @param {import("../../private.js").Comment} comment
82
+ * @param {string} name
83
+ * @param {boolean} names_params
84
+ * @returns {import("../../private.js").RenameEdit[]}
85
+ */
86
+ function comment_edits(comment, name, names_params) {
87
+ if (comment.type != "Block" || !comment.value.startsWith("*")) return []
88
+ const text = comment.value
89
+ const start = comment.range[0] + 2
90
+ const fixed_name = to_snake_case(name)
91
+ const escaped = name.replace(/\$/g, "\\$")
92
+ /** @type {import("../../private.js").RenameEdit[]} */
93
+ const edits = []
94
+ for (const match of text.matchAll(
95
+ RegExp(
96
+ `(?<=(?:asserts|typeof)\\s+)${escaped}(?![\\w$])|(?<![\\w$.])${escaped}(?=\\s+is(?![\\w$]))`,
97
+ "g"
98
+ )
99
+ )) {
100
+ if (!in_braces(text, match.index)) continue
101
+ edits.push(
102
+ {
103
+ range: [
104
+ start + match.index,
105
+ start + match.index + name.length
106
+ ],
107
+ text: fixed_name
108
+ }
109
+ )
110
+ }
111
+ if (!names_params) return edits
112
+ for (const match of text.matchAll(jsdoc_param_regex)) {
113
+ let index = skip_space(
114
+ text,
115
+ match.index + match[0].length
116
+ )
117
+ if (text[index] == "{") {
118
+ for (let depth = 0; index < text.length; index++) {
119
+ if (text[index] == "{") depth++
120
+ else if (text[index] == "}" && !--depth) break
121
+ }
122
+ index = skip_space(text, index + 1)
123
+ }
124
+ if (text[index] == "[") index = skip_space(text, index + 1)
125
+ if (text.startsWith(name, index) && !word_character_regex.test(
126
+ text[index + name.length] ?? ""
127
+ )) {
128
+ edits.push(
129
+ {
130
+ range: [
131
+ start + index,
132
+ start + index + name.length
133
+ ],
134
+ text: fixed_name
135
+ }
136
+ )
137
+ }
138
+ }
139
+ return edits
140
+ }
141
+ /**
142
+ * @param {import("../../private.js").NamingIdentifier} node
143
+ * @param {Map<import("../../private.js").AstNode, import("eslint").Scope.Variable | null>} resolution
144
+ * @returns {import("../../private.js").RenameEdit[]}
145
+ */
146
+ function declaration_edits(node, resolution) {
147
+ /** @type {import("../../private.js").RenameEdit[]} */
148
+ const edits = [ rename_edit(node) ]
149
+ const block = /** @type {import("../../private.js").AstNode | undefined} */(resolution.get(node)?.scope.block)/**/
150
+ const func = function_of_param(node)
151
+ const leading = func && leading_comment(func)
152
+ /** @type {Set<import("../../private.js").Comment>} */
153
+ const comments = new Set(
154
+ block
155
+ ? /** @type {import("../../private.js").Comment[]} */(source.getAllComments())/**/.filter(
156
+ comment => block.type == "Program" || comment.range[0] >= block.range[0] && comment.range[1] <= block.range[1]
157
+ )
158
+ : []
159
+ )
160
+ if (leading) comments.add(leading)
161
+ for (const comment of comments) edits.push(
162
+ ...comment_edits(
163
+ comment,
164
+ node.name,
165
+ comment == leading
166
+ )
167
+ )
168
+ return edits
169
+ }
170
+ /**
171
+ * @param {import("../../private.js").AstNode & import("estree").Identifier} node
172
+ * @returns {void}
173
+ */
174
+ function declare(node) {
175
+ add(declarations, node)
176
+ }
177
+ /**
178
+ * @param {import("../../private.js").AstNode & import("estree").Identifier} node
179
+ * @returns {void}
180
+ */
181
+ function defer(node) {
182
+ add(usages, node)
183
+ }
184
+ /**
185
+ * @param {string} _
186
+ * @param {string} a
187
+ * @param {string} b
188
+ * @returns {string}
189
+ */
190
+ function fix_handler(_, a, b) {
191
+ return a + (a ? "_" : "") + b.toLowerCase()
192
+ }
193
+ /**
194
+ * @param {import("../../private.js").AstNode} node
195
+ * @returns {import("../../private.js").AstNode | undefined}
196
+ */
197
+ function function_of_param(node) {
198
+ const child = node.parent.type == "AssignmentPattern" && node.parent.left == node || node.parent.type == "RestElement"
199
+ ? node.parent
200
+ : node
201
+ const func = /** @type {import("../../private.js").AstNode & { params?: import("../../private.js").AstNode[] }} */(child.parent)/**/
202
+ return function_types.has(func.type) && func.params?.includes(child) ? func : void 0
203
+ }
204
+ /**
205
+ * @param {string} text
206
+ * @param {number} index
207
+ * @returns {boolean}
208
+ */
209
+ function in_braces(text, index) {
210
+ let depth = 0
211
+ for (let i = 0; i < index; i++) {
212
+ if (text[i] == "{") depth++
213
+ else if (text[i] == "}" && depth) depth--
214
+ }
215
+ return depth > 0
216
+ }
217
+ /**
218
+ * @param {import("../../private.js").AstNode} node
219
+ * @returns {boolean}
220
+ */
221
+ function is_declaration(node) {
222
+ for (let parent = node.parent; parent; parent = parent.parent) {
223
+ switch (parent.type) {
224
+ case "ArrayPattern":
225
+ case "AssignmentPattern":
226
+ case "ObjectPattern":
227
+ case "Property":
228
+ case "RestElement":
229
+ break
230
+ case "AssignmentExpression":
231
+ case "ForInStatement":
232
+ case "ForOfStatement":
233
+ return false
234
+ default:
235
+ return true
236
+ }
237
+ }
238
+ return true
239
+ }
240
+ /**
241
+ * @param {import("../../private.js").AstNode} node
242
+ * @returns {boolean}
243
+ */
244
+ function is_disabled(node) {
245
+ const { column, line } = /** @type {import("estree").SourceLocation} */(node.loc)/**/.start
246
+ let disabled = false
247
+ disable_directives ??= (/** @type {import("../../private.js").DirectiveSource} */(/** @type {unknown} */(source))/**/).getDisableDirectives?.().directives ?? []
248
+ for (const directive of disable_directives) {
249
+ const rule_ids = directive.value.split(",").map(rule_id => rule_id.trim())
250
+ .filter(Boolean)
251
+ if (rule_ids.length && !rule_ids.includes(context.id)) continue
252
+ const comment_loc = directive.node.loc
253
+ switch (directive.type) {
254
+ case "disable":
255
+ case "enable":
256
+ if (comment_loc.start.line < line || comment_loc.start.line == line && comment_loc.start.column < column) {
257
+ disabled = directive.type == "disable"
258
+ }
259
+ break
260
+ case "disable-line":
261
+ if (comment_loc.start.line == line) return true
262
+ break
263
+ case "disable-next-line":
264
+ if (comment_loc.end.line + 1 == line) return true
265
+ }
266
+ }
267
+ return disabled
268
+ }
269
+ /**
270
+ * @param {import("../../private.js").AstNode} func
271
+ * @returns {import("../../private.js").Comment | undefined}
272
+ */
273
+ function leading_comment(func) {
274
+ let anchor = func
275
+ const parent = func.parent
276
+ switch (parent.type) {
277
+ case "MethodDefinition":
278
+ case "Property":
279
+ case "PropertyDefinition":
280
+ anchor = parent
281
+ break
282
+ case "VariableDeclarator":
283
+ if (parent.init == func) anchor = parent.parent
284
+ }
285
+ if (anchor.parent.type == "ExportDefaultDeclaration" || anchor.parent.type == "ExportNamedDeclaration") anchor = anchor.parent
286
+ return /** @type {import("../../private.js").Comment[]} */(source.getCommentsBefore(
287
+ /** @type {import("estree").Node} */(anchor)/**/
288
+ ))/**/.at(-1)
289
+ }
290
+ /**
291
+ * @param {import("../../private.js").NamingIdentifier} target
292
+ * @returns {import("../../private.js").RenameEdit}
293
+ */
294
+ function rename_edit(target) {
295
+ const name = target.name
296
+ const fixed_name = to_snake_case(name)
297
+ const parent = target.parent
298
+ const decorators = target.decorators
299
+ const start = source.text.indexOf(
300
+ name,
301
+ decorators?.length
302
+ ? /** @type {import("../../private.js").AstNode} */(decorators[decorators.length - 1])/**/.range[1]
303
+ : target.range[0]
304
+ )
305
+ const range = /** @type {[number, number]} */([ start, start + name.length ])/**/
306
+ if (svelte_shorthands.has(target)) return {
307
+ range: /** @type {[number, number]} */(parent.range)/**/,
308
+ text: name + "={" + fixed_name + "}"
309
+ }
310
+ if (shorthand_properties.has(target)) return {
311
+ range,
312
+ text: name + ": " + fixed_name
313
+ }
314
+ if (import_shorthands.has(target)) return {
315
+ range,
316
+ text: name + " as " + fixed_name
317
+ }
318
+ if (export_shorthands.has(target)) return {
319
+ range,
320
+ text: fixed_name + " as " + name
321
+ }
322
+ if (parent.type == "ImportSpecifier" && parent.imported.type == "Identifier" && parent.imported.name == fixed_name) {
323
+ return {
324
+ range: [
325
+ /** @type {import("../../private.js").AstNode} */(/** @type {unknown} */(parent.imported))/**/.range[0],
326
+ range[1]
327
+ ],
328
+ text: fixed_name
329
+ }
330
+ }
331
+ if (parent.type == "ExportSpecifier" && parent.local == target && parent.exported.type == "Identifier" && parent.exported.name == fixed_name) {
332
+ return {
333
+ range: [
334
+ range[0],
335
+ /** @type {import("../../private.js").AstNode} */(/** @type {unknown} */(parent.exported))/**/.range[1]
336
+ ],
337
+ text: fixed_name
338
+ }
339
+ }
340
+ return { range, text: fixed_name }
341
+ }
342
+ /**
343
+ * @param {import("../../private.js").NamingIdentifier} node
344
+ * @param {import("../../private.js").RenameEdit[] | null} edits
345
+ * @returns {void}
346
+ */
347
+ function report(node, edits) {
348
+ context.report(
349
+ {
350
+ data: {
351
+ expected: to_snake_case(node.name),
352
+ name: node.name
353
+ },
354
+ fix(fixer) {
355
+ if (!edits) return null
356
+ /** @type {Set<number>} */
357
+ const starts = new Set()
358
+ return edits.filter(
359
+ edit => !starts.has(edit.range[0]) && !!starts.add(edit.range[0])
360
+ )
361
+ .map(
362
+ edit => fixer.replaceTextRange(edit.range, edit.text)
363
+ )
364
+ },
365
+ messageId: fixable_name_regex.test(node.name) && allow_regex.test(to_snake_case(node.name))
366
+ ? "rename"
367
+ : "case",
368
+ node: /** @type {import("estree").Node} */(node)/**/
369
+ }
370
+ )
371
+ }
372
+ /**
373
+ * @param {string} text
374
+ * @param {number} index
375
+ * @returns {number}
376
+ */
377
+ function skip_space(text, index) {
378
+ while (index < text.length && space_regex.test(
379
+ /** @type {string} */(text[index])/**/
380
+ )) index++
381
+ return index
382
+ }
383
+ /**
384
+ * @param {string} name
385
+ * @returns {string}
386
+ */
387
+ function to_snake_case(name) {
388
+ return name.replace(fix_regex, fix_handler)
389
+ }
390
+ /** @type {import("../../private.js").RuleListener} */
391
+ const listener = {
392
+ /** @param {import("../../private.js").AstNode & import("estree").Identifier} node */
393
+ Identifier(node) {
394
+ const name = node.name
395
+ if (store_regex.test(name)) {
396
+ const store = name.slice(1)
397
+ if (is_svelte) {
398
+ const nodes = store_usages.get(store)
399
+ if (nodes) nodes.push(node)
400
+ else store_usages.set(store, [ node ])
401
+ } else blocked.add(store)
402
+ }
403
+ if (allow_regex.test(name) || visited.has(node)) return
404
+ visited.add(node)
405
+ const parent = /** @type {import("../../private.js").NamingParent} */(node.parent)/**/
406
+ switch (parent.type) {
407
+ case "AccessorProperty":
408
+ case "TSAbstractAccessorProperty":
409
+ case "TSAbstractMethodDefinition":
410
+ case "TSAbstractPropertyDefinition":
411
+ case "TSMethodSignature":
412
+ case "TSPropertySignature":
413
+ if (parent.computed || parent.value == node) defer(node)
414
+ break
415
+ case "ArrayExpression":
416
+ defer(node)
417
+ break
418
+ case "ArrayPattern":
419
+ if (is_declaration(node)) declare(node)
420
+ else defer(node)
421
+ break
422
+ case "ArrowFunctionExpression":
423
+ if (parent.body == node) defer(node)
424
+ else declare(node)
425
+ break
426
+ case "AssignmentExpression":
427
+ defer(node)
428
+ break
429
+ case "AssignmentPattern":
430
+ if (parent.left != node) defer(node)
431
+ else if (parent.parent?.type == "Property" && parent.parent.shorthand) {
432
+ shorthand_properties.add(node)
433
+ defer(node)
434
+ } else if (is_declaration(node)) declare(node)
435
+ else defer(node)
436
+ break
437
+ case "AwaitExpression":
438
+ defer(node)
439
+ break
440
+ case "BinaryExpression":
441
+ case "Decorator":
442
+ case "JSXExpressionContainer":
443
+ case "JSXSpreadAttribute":
444
+ case "JSXSpreadChild":
445
+ case "TSAsExpression":
446
+ case "TSExportAssignment":
447
+ case "TSInstantiationExpression":
448
+ case "TSNonNullExpression":
449
+ case "TSSatisfiesExpression":
450
+ case "TSTypeAssertion":
451
+ case "TSTypePredicate":
452
+ case "TSTypeQuery":
453
+ case "WithStatement":
454
+ defer(node)
455
+ break
456
+ case "BreakStatement":
457
+ case "ContinueStatement":
458
+ case "ImportAttribute":
459
+ case "LabeledStatement":
460
+ case "MetaProperty":
461
+ case "TSCallSignatureDeclaration":
462
+ case "TSConstructSignatureDeclaration":
463
+ case "TSConstructorType":
464
+ case "TSEmptyBodyFunctionExpression":
465
+ case "TSEnumDeclaration":
466
+ case "TSFunctionType":
467
+ case "TSIndexSignature":
468
+ case "TSInterfaceDeclaration":
469
+ case "TSMappedType":
470
+ case "TSModuleDeclaration":
471
+ case "TSNamespaceExportDeclaration":
472
+ case "TSTypeAliasDeclaration":
473
+ case "TSTypeParameter":
474
+ break
475
+ case "CallExpression":
476
+ defer(node)
477
+ break
478
+ case "CatchClause":
479
+ declare(node)
480
+ break
481
+ case "ClassDeclaration":
482
+ case "ClassExpression":
483
+ if (parent.id == node) declare(node)
484
+ else defer(node)
485
+ break
486
+ case "ConditionalExpression":
487
+ defer(node)
488
+ break
489
+ case "DoWhileStatement":
490
+ defer(node)
491
+ break
492
+ case "ExportAllDeclaration":
493
+ break
494
+ case "ExportDefaultDeclaration":
495
+ default_exports.add(node)
496
+ defer(node)
497
+ break
498
+ case "ExportSpecifier":
499
+ if (parent.local == node && !(/** @type {import("estree").ExportNamedDeclaration} */(parent.parent)/**/).source) {
500
+ const is_shorthand = parent.exported == node || parent.exported.range?.[0] == node.range[0]
501
+ if (is_shorthand) export_shorthands.add(node)
502
+ add(export_locals, node)
503
+ }
504
+ break
505
+ case "ExpressionStatement":
506
+ defer(node)
507
+ break
508
+ case "ForInStatement":
509
+ defer(node)
510
+ break
511
+ case "ForOfStatement":
512
+ defer(node)
513
+ break
514
+ case "ForStatement":
515
+ defer(node)
516
+ break
517
+ case "FunctionDeclaration":
518
+ case "TSDeclareFunction":
519
+ if (parent.id != node) {
520
+ if (parent.type == "FunctionDeclaration") declare(node)
521
+ } else if (parent.parent?.type != "ExportNamedDeclaration" || !camel_case_regex.test(name)) declare(node)
522
+ break
523
+ case "FunctionExpression":
524
+ declare(node)
525
+ break
526
+ case "IfStatement":
527
+ defer(node)
528
+ break
529
+ case "ImportDefaultSpecifier":
530
+ declare(node)
531
+ break
532
+ case "ImportExpression":
533
+ defer(node)
534
+ break
535
+ case "ImportNamespaceSpecifier":
536
+ declare(node)
537
+ break
538
+ case "ImportSpecifier":
539
+ if (parent.local == node) {
540
+ if (parent.imported.type == "Identifier" && parent.imported.name == name) {
541
+ import_shorthands.add(node)
542
+ defer(node)
543
+ } else declare(node)
544
+ }
545
+ break
546
+ case "LogicalExpression":
547
+ defer(node)
548
+ break
549
+ case "MemberExpression":
550
+ if (parent.object == node || parent.computed) defer(node)
551
+ break
552
+ case "MethodDefinition":
553
+ if (parent.computed) defer(node)
554
+ break
555
+ case "NewExpression":
556
+ defer(node)
557
+ break
558
+ case "Property":
559
+ if (parent.value == node) {
560
+ const is_shorthand = parent.shorthand
561
+ if (is_shorthand) shorthand_properties.add(node)
562
+ const declares = !is_shorthand && parent.parent?.type == "ObjectPattern" && is_declaration(node)
563
+ if (declares) declare(node)
564
+ else defer(node)
565
+ } else if (parent.computed) defer(node)
566
+ break
567
+ case "PropertyDefinition":
568
+ if (parent.value == node || parent.computed) defer(node)
569
+ break
570
+ case "RestElement":
571
+ if (is_declaration(node)) declare(node)
572
+ else defer(node)
573
+ break
574
+ case "ReturnStatement":
575
+ defer(node)
576
+ break
577
+ case "SequenceExpression":
578
+ defer(node)
579
+ break
580
+ case "SpreadElement":
581
+ defer(node)
582
+ break
583
+ case "SwitchCase":
584
+ defer(node)
585
+ break
586
+ case "SwitchStatement":
587
+ defer(node)
588
+ break
589
+ case "TSEnumMember":
590
+ if (parent.initializer == node) defer(node)
591
+ break
592
+ case "TSImportEqualsDeclaration":
593
+ if (parent.id == node) declare(node)
594
+ else defer(node)
595
+ break
596
+ case "TSQualifiedName":
597
+ if (parent.left == node) defer(node)
598
+ break
599
+ case "TaggedTemplateExpression":
600
+ defer(node)
601
+ break
602
+ case "TemplateLiteral":
603
+ defer(node)
604
+ break
605
+ case "ThrowStatement":
606
+ defer(node)
607
+ break
608
+ case "UnaryExpression":
609
+ defer(node)
610
+ break
611
+ case "UpdateExpression":
612
+ defer(node)
613
+ break
614
+ case "VariableDeclarator":
615
+ if (parent.id == node) {
616
+ if (
617
+ parent.parent?.parent?.type != "ExportNamedDeclaration"
618
+ || !camel_case_regex.test(name)
619
+ ) {
620
+ declare(node)
621
+ }
622
+ } else defer(node)
623
+ break
624
+ case "WhileStatement":
625
+ defer(node)
626
+ break
627
+ case "YieldExpression":
628
+ defer(node)
629
+ break
630
+ default: {
631
+ const type = /** @type {string} */(parent.type)/**/
632
+ const svelte_parent = /** @type {{ expression?: unknown, kind?: string, value?: unknown }} */(/** @type {unknown} */(parent))/**/
633
+ if (type == "SvelteShorthandAttribute") {
634
+ if (svelte_parent.value == node) {
635
+ svelte_shorthands.add(node)
636
+ defer(node)
637
+ }
638
+ } else if (
639
+ svelte_usage_parents.has(type)
640
+ || type == "SvelteDirective" && svelte_parent.kind != "Let"
641
+ || svelte_expression_parents.has(type) && svelte_parent.expression == node
642
+ ) defer(node)
643
+ else blocked.add(name)
644
+ }
645
+ }
646
+ },
647
+ /** @param {import("../../private.js").AstNode & import("estree").Identifier} node */
648
+ JSXIdentifier(node) {
649
+ const name = node.name
650
+ const parent = /** @type {{ object?: unknown, type: string }} */(/** @type {unknown} */(node.parent))/**/
651
+ if (parent.type == "JSXMemberExpression" && parent.object == node) {
652
+ if (allow_regex.test(name)) return
653
+ jsx_objects.add(node)
654
+ defer(node)
655
+ } else if ((parent.type == "JSXOpeningElement" || parent.type == "JSXClosingElement") && !lowercase_regex.test(name)) {
656
+ blocked.add(name)
657
+ }
658
+ },
659
+ "Program:exit": () => {
660
+ if (!declarations.size) return
661
+ /** @type {Map<import("../../private.js").AstNode, import("eslint").Scope.Variable | null>} */
662
+ const resolution = new Map()
663
+ /** @type {Map<string, import("eslint").Scope.Variable[]>} */
664
+ const variables_by_name = new Map()
665
+ const scopes = source.scopeManager?.scopes ?? []
666
+ for (const scope of scopes) {
667
+ for (const variable of scope.variables) {
668
+ const same_names = variables_by_name.get(variable.name)
669
+ if (same_names) same_names.push(variable)
670
+ else variables_by_name.set(variable.name, [ variable ])
671
+ for (const identifier of variable.identifiers) resolution.set(
672
+ /** @type {import("../../private.js").AstNode} */(/** @type {unknown} */(identifier))/**/,
673
+ variable
674
+ )
675
+ }
676
+ }
677
+ for (const scope of scopes) {
678
+ for (const reference of scope.references) {
679
+ const identifier = /** @type {import("../../private.js").AstNode} */(/** @type {unknown} */(reference.identifier))/**/
680
+ if (!resolution.has(identifier)) resolution.set(identifier, reference.resolved)
681
+ }
682
+ }
683
+ for (const node of jsx_objects) {
684
+ if (!resolution.has(node)) blocked.add(node.name)
685
+ }
686
+ /** @type {Set<import("eslint").Scope.Variable>} */
687
+ const renamable = new Set()
688
+ /** @type {Set<import("../../private.js").AstNode>} */
689
+ const declared = new Set()
690
+ for (const nodes of declarations.values()) {
691
+ for (const node of nodes) {
692
+ declared.add(node)
693
+ const variable = resolution.get(node)
694
+ if (variable) renamable.add(variable)
695
+ }
696
+ }
697
+ /** @type {Map<string, Set<import("eslint").Scope.Scope>>} */
698
+ const scopes_by_target = new Map()
699
+ for (const variable of renamable) {
700
+ const target = to_snake_case(variable.name)
701
+ const target_scopes = scopes_by_target.get(target)
702
+ if (target_scopes) target_scopes.add(variable.scope)
703
+ else scopes_by_target.set(
704
+ target,
705
+ new Set([ variable.scope ])
706
+ )
707
+ }
708
+ /** @type {Set<string>} */
709
+ const nested_targets = new Set()
710
+ for (const variable of renamable) {
711
+ const target = to_snake_case(variable.name)
712
+ const target_scopes = /** @type {Set<import("eslint").Scope.Scope>} */(scopes_by_target.get(target))/**/
713
+ for (let scope = variable.scope.upper; scope; scope = scope.upper) {
714
+ if (target_scopes.has(scope)) nested_targets.add(target)
715
+ }
716
+ }
717
+ /**
718
+ * @param {import("../../private.js").AstNode} node
719
+ * @returns {boolean}
720
+ */
721
+ function belongs(node) {
722
+ if (!resolution.has(node)) return true
723
+ const variable = resolution.get(node)
724
+ return !!variable && renamable.has(variable)
725
+ }
726
+ /**
727
+ * @param {string} name
728
+ * @returns {boolean}
729
+ */
730
+ function collides(name) {
731
+ const others = variables_by_name.get(to_snake_case(name)) ?? []
732
+ if (!others.length) return false
733
+ for (const variable of variables_by_name.get(name) ?? []) {
734
+ if (!renamable.has(variable)) continue
735
+ for (const scope of [
736
+ variable.scope,
737
+ ...variable.references.map(reference => reference.from)
738
+ ]) {
739
+ for (let current = /** @type {import("eslint").Scope.Scope | null} */(scope)/**/; current; current = current.upper) {
740
+ if (others.some(
741
+ other => other.scope == current
742
+ )) return true
743
+ if (current == variable.scope) break
744
+ }
745
+ }
746
+ for (const other of others) {
747
+ for (const reference of other.references) {
748
+ for (let current = /** @type {import("eslint").Scope.Scope | null} */(reference.from)/**/; current && current != other.scope; current = current.upper) {
749
+ if (current == variable.scope) return true
750
+ }
751
+ }
752
+ }
753
+ }
754
+ return false
755
+ }
756
+ /**
757
+ * @param {string} name
758
+ * @returns {boolean}
759
+ */
760
+ function has_foreign_declaration(name) {
761
+ return (variables_by_name.get(name) ?? []).some(
762
+ variable => renamable.has(variable) && variable.identifiers.some(
763
+ identifier => !declared.has(
764
+ /** @type {import("../../private.js").AstNode} */(/** @type {unknown} */(identifier))/**/
765
+ )
766
+ )
767
+ )
768
+ }
769
+ /** @type {Set<import("../../private.js").AstNode>} */
770
+ const held = new Set(default_exports)
771
+ for (const variable of renamable) {
772
+ for (const reference of variable.references) {
773
+ if (reference.isWrite() && !reference.init) held.add(
774
+ /** @type {import("../../private.js").AstNode} */(/** @type {unknown} */(reference.identifier))/**/
775
+ )
776
+ }
777
+ }
778
+ /** @type {Map<string, import("../../private.js").RenamePlan>} */
779
+ const plans = new Map()
780
+ for (const [ name, nodes ] of declarations) {
781
+ const target = to_snake_case(name)
782
+ const used = (usages.get(name) ?? []).filter(belongs)
783
+ plans.set(
784
+ name,
785
+ {
786
+ atomic: !fix_same_names || variables_by_name.has(target) || nested_targets.has(target) || store_usages.has(name),
787
+ exported: (export_locals.get(name) ?? []).filter(belongs),
788
+ fixable: !blocked.has(name)
789
+ && fixable_name_regex.test(name)
790
+ && !collides(name)
791
+ && !has_foreign_declaration(name)
792
+ && !nodes.some(is_disabled),
793
+ held: used.filter(node => held.has(node)),
794
+ used: used.filter(node => !held.has(node))
795
+ }
796
+ )
797
+ }
798
+ /**
799
+ * @param {import("../../private.js").NamingIdentifier} node
800
+ * @param {import("../../private.js").NamingIdentifier[]} nodes
801
+ * @param {import("../../private.js").NamingIdentifier[]} usages_held
802
+ * @returns {import("../../private.js").RenameEdit[]}
803
+ */
804
+ function declaration_fix(node, nodes, usages_held) {
805
+ const variable = resolution.get(node)
806
+ return [
807
+ ...declaration_edits(node, resolution),
808
+ ...nodes.find(
809
+ other => resolution.get(other) == variable
810
+ ) == node
811
+ ? usages_held.filter(
812
+ usage => resolution.get(usage) == variable
813
+ ).map(rename_edit)
814
+ : []
815
+ ]
816
+ }
817
+ /** @type {import("../../private.js").RenameEdit[]} */
818
+ const atomic = []
819
+ /** @type {import("../../private.js").NamingIdentifier | undefined} */
820
+ let atomic_node
821
+ for (const [ name, nodes ] of declarations) {
822
+ const plan = /** @type {import("../../private.js").RenamePlan} */(plans.get(name))/**/
823
+ if (!plan.exported.length || !plan.fixable || !plan.atomic && plan.used.length) continue
824
+ atomic_node ??= nodes[0]
825
+ for (const node of nodes) atomic.push(
826
+ ...declaration_edits(node, resolution)
827
+ )
828
+ for (const node of [
829
+ ...plan.exported,
830
+ ...plan.used,
831
+ ...plan.held,
832
+ ...store_usages.get(name) ?? []
833
+ ]) atomic.push(rename_edit(node))
834
+ }
835
+ for (const [ name, nodes ] of declarations) {
836
+ const {
837
+ atomic: renames_at_once,
838
+ exported,
839
+ fixable,
840
+ held: usages_held,
841
+ used
842
+ } = /** @type {import("../../private.js").RenamePlan} */(plans.get(name))/**/
843
+ if (exported.length) {
844
+ for (const node of nodes) report(
845
+ node,
846
+ node == atomic_node ? atomic : null
847
+ )
848
+ } else if (!renames_at_once) {
849
+ for (const node of nodes) report(
850
+ node,
851
+ fixable && !used.length ? declaration_fix(node, nodes, usages_held) : null
852
+ )
853
+ } else {
854
+ report(
855
+ /** @type {import("../../private.js").NamingIdentifier} */(nodes[0])/**/,
856
+ fixable
857
+ ? [
858
+ ...nodes.flatMap(
859
+ node => declaration_edits(node, resolution)
860
+ ),
861
+ ...used.map(rename_edit),
862
+ ...usages_held.map(rename_edit),
863
+ ...(store_usages.get(name) ?? []).map(rename_edit)
864
+ ]
865
+ : null
866
+ )
867
+ for (const node of nodes.slice(1)) report(node, null)
868
+ }
869
+ if (!fix_same_names) continue
870
+ for (const node of exported) report(node, null)
871
+ for (const node of usages_held) report(node, null)
872
+ for (const node of used) report(
873
+ node,
874
+ fixable && !renames_at_once ? [ rename_edit(node) ] : null
875
+ )
876
+ }
877
+ }
878
+ }
879
+ return /** @type {import("eslint").Rule.RuleListener} */(/** @type {unknown} */(listener))/**/
880
+ },
881
+ meta: {
882
+ docs: {
883
+ description: "Enforces snake_case with some symbols for variable declaration.",
884
+ recommended: false,
885
+ url: "https://github.com/artxe/lube-series/blob/master/packages/eslint-plugin-lube/docs/svelte-naming-convention.md"
886
+ },
887
+ fixable: "code",
888
+ messages: {
889
+ case: "Rename '{{name}}' to snake_case, UPPER_SNAKE_CASE or PascalCase, following the Svelte code conventions",
890
+ rename: "Rename '{{name}}' to '{{expected}}': declarations are snake_case, following the Svelte code conventions"
891
+ },
892
+ schema: [
893
+ {
894
+ additionalProperties: false,
895
+ properties: {
896
+ fixSameNames: { default: true, type: "boolean" }
897
+ },
898
+ type: "object"
899
+ }
900
+ ],
901
+ type: "suggestion"
902
+ }
903
+ }