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.
- package/CHANGELOG.md +39 -0
- package/README.md +6 -1
- package/docs/prefer-param-tag.md +25 -0
- package/docs/prefer-type-tag.md +45 -0
- package/package.json +3 -3
- package/src/index.js +14 -2
- package/src/rules/ascii-order.js +165 -78
- package/src/rules/prefer-param-tag.js +252 -0
- package/src/rules/prefer-type-tag.js +165 -0
- package/src/rules/pretty-imports.js +21 -12
- package/src/rules/pretty-jsdoc-casting.js +76 -37
- package/src/rules/pretty-sequence.js +110 -42
- package/src/rules/pretty-ternary.js +43 -23
- package/src/rules/svelte-naming-convention.js +287 -134
- package/src/utils/keep_body_comments.js +53 -0
- package/src/utils/keep_comments.js +6 -2
- package/src/utils/keep_line_breaks.js +3 -1
- package/src/utils/skip_hole_commas.js +4 -4
- package/src/utils/skip_optional_patterns.js +3 -1
- package/src/utils/skip_unchanged_fixes.js +3 -1
- package/src/utils/source_text.js +57 -23
- package/types/index.d.ts.map +1 -1
|
@@ -42,7 +42,7 @@ export default {
|
|
|
42
42
|
const fixable_name_regex = /^[_$]?[_$]?(?:[\dA-Za-z]+(?:_[\dA-Za-z]+)*\$?\$?)?$/
|
|
43
43
|
/** @type {Set<string>} */
|
|
44
44
|
const blocked = new Set()
|
|
45
|
-
const is_svelte = !!
|
|
45
|
+
const is_svelte = !!source.parserServices?.isSvelte
|
|
46
46
|
/** @type {Map<string, import("../../private.js").NamingIdentifier[]>} */
|
|
47
47
|
const store_usages = new Map()
|
|
48
48
|
/** @type {Set<import("../../private.js").NamingIdentifier>} */
|
|
@@ -74,8 +74,11 @@ export default {
|
|
|
74
74
|
*/
|
|
75
75
|
function add(map, node) {
|
|
76
76
|
const nodes = map.get(node.name)
|
|
77
|
-
if (nodes)
|
|
78
|
-
|
|
77
|
+
if (nodes) {
|
|
78
|
+
nodes.push(node)
|
|
79
|
+
} else {
|
|
80
|
+
map.set(node.name, [ node ])
|
|
81
|
+
}
|
|
79
82
|
}
|
|
80
83
|
/**
|
|
81
84
|
* @param {import("../../private.js").Comment} comment
|
|
@@ -84,7 +87,9 @@ export default {
|
|
|
84
87
|
* @returns {import("../../private.js").RenameEdit[]}
|
|
85
88
|
*/
|
|
86
89
|
function comment_edits(comment, name, names_params) {
|
|
87
|
-
if (comment.type != "Block" || !comment.value.startsWith("*"))
|
|
90
|
+
if (comment.type != "Block" || !comment.value.startsWith("*")) {
|
|
91
|
+
return []
|
|
92
|
+
}
|
|
88
93
|
const text = comment.value
|
|
89
94
|
const start = comment.range[0] + 2
|
|
90
95
|
const fixed_name = to_snake_case(name)
|
|
@@ -97,7 +102,9 @@ export default {
|
|
|
97
102
|
"g"
|
|
98
103
|
)
|
|
99
104
|
)) {
|
|
100
|
-
if (!in_braces(text, match.index))
|
|
105
|
+
if (!in_braces(text, match.index)) {
|
|
106
|
+
continue
|
|
107
|
+
}
|
|
101
108
|
edits.push(
|
|
102
109
|
{
|
|
103
110
|
range: [
|
|
@@ -108,7 +115,9 @@ export default {
|
|
|
108
115
|
}
|
|
109
116
|
)
|
|
110
117
|
}
|
|
111
|
-
if (!names_params)
|
|
118
|
+
if (!names_params) {
|
|
119
|
+
return edits
|
|
120
|
+
}
|
|
112
121
|
for (const match of text.matchAll(jsdoc_param_regex)) {
|
|
113
122
|
let index = skip_space(
|
|
114
123
|
text,
|
|
@@ -116,12 +125,17 @@ export default {
|
|
|
116
125
|
)
|
|
117
126
|
if (text[index] == "{") {
|
|
118
127
|
for (let depth = 0; index < text.length; index++) {
|
|
119
|
-
if (text[index] == "{")
|
|
120
|
-
|
|
128
|
+
if (text[index] == "{") {
|
|
129
|
+
depth++
|
|
130
|
+
} else if (text[index] == "}" && !--depth) {
|
|
131
|
+
break
|
|
132
|
+
}
|
|
121
133
|
}
|
|
122
134
|
index = skip_space(text, index + 1)
|
|
123
135
|
}
|
|
124
|
-
if (text[index] == "[")
|
|
136
|
+
if (text[index] == "[") {
|
|
137
|
+
index = skip_space(text, index + 1)
|
|
138
|
+
}
|
|
125
139
|
if (text.startsWith(name, index) && !word_character_regex.test(
|
|
126
140
|
text[index + name.length] ?? ""
|
|
127
141
|
)) {
|
|
@@ -146,25 +160,27 @@ export default {
|
|
|
146
160
|
function declaration_edits(node, resolution) {
|
|
147
161
|
/** @type {import("../../private.js").RenameEdit[]} */
|
|
148
162
|
const edits = [ rename_edit(node) ]
|
|
149
|
-
const block = /** @type {import("../../private.js").AstNode
|
|
163
|
+
const block = /** @type {import("../../private.js").AstNode} */(/** @type {import("eslint").Scope.Variable} */(resolution.get(node))/**/.scope.block)/**/
|
|
150
164
|
const func = function_of_param(node)
|
|
151
165
|
const leading = func && leading_comment(func)
|
|
152
166
|
/** @type {Set<import("../../private.js").Comment>} */
|
|
153
167
|
const comments = new Set(
|
|
154
|
-
|
|
155
|
-
|
|
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
|
|
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]
|
|
166
170
|
)
|
|
167
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
|
+
}
|
|
168
184
|
return edits
|
|
169
185
|
}
|
|
170
186
|
/**
|
|
@@ -198,7 +214,8 @@ export default {
|
|
|
198
214
|
const child = node.parent.type == "AssignmentPattern" && node.parent.left == node || node.parent.type == "RestElement"
|
|
199
215
|
? node.parent
|
|
200
216
|
: node
|
|
201
|
-
|
|
217
|
+
/** @type {import("../../private.js").AstNode & { params?: import("../../private.js").AstNode[] }} */
|
|
218
|
+
const func = child.parent
|
|
202
219
|
return function_types.has(func.type) && func.params?.includes(child) ? func : void 0
|
|
203
220
|
}
|
|
204
221
|
/**
|
|
@@ -209,8 +226,11 @@ export default {
|
|
|
209
226
|
function in_braces(text, index) {
|
|
210
227
|
let depth = 0
|
|
211
228
|
for (let i = 0; i < index; i++) {
|
|
212
|
-
if (text[i] == "{")
|
|
213
|
-
|
|
229
|
+
if (text[i] == "{") {
|
|
230
|
+
depth++
|
|
231
|
+
} else if (text[i] == "}" && depth) {
|
|
232
|
+
depth--
|
|
233
|
+
}
|
|
214
234
|
}
|
|
215
235
|
return depth > 0
|
|
216
236
|
}
|
|
@@ -219,7 +239,7 @@ export default {
|
|
|
219
239
|
* @returns {boolean}
|
|
220
240
|
*/
|
|
221
241
|
function is_declaration(node) {
|
|
222
|
-
for (let parent = node.parent;
|
|
242
|
+
for (let parent = node.parent; ; parent = parent.parent) {
|
|
223
243
|
switch (parent.type) {
|
|
224
244
|
case "ArrayPattern":
|
|
225
245
|
case "AssignmentPattern":
|
|
@@ -235,7 +255,6 @@ export default {
|
|
|
235
255
|
return true
|
|
236
256
|
}
|
|
237
257
|
}
|
|
238
|
-
return true
|
|
239
258
|
}
|
|
240
259
|
/**
|
|
241
260
|
* @param {import("../../private.js").AstNode} node
|
|
@@ -248,7 +267,9 @@ export default {
|
|
|
248
267
|
for (const directive of disable_directives) {
|
|
249
268
|
const rule_ids = directive.value.split(",").map(rule_id => rule_id.trim())
|
|
250
269
|
.filter(Boolean)
|
|
251
|
-
if (rule_ids.length && !rule_ids.includes(context.id))
|
|
270
|
+
if (rule_ids.length && !rule_ids.includes(context.id)) {
|
|
271
|
+
continue
|
|
272
|
+
}
|
|
252
273
|
const comment_loc = directive.node.loc
|
|
253
274
|
switch (directive.type) {
|
|
254
275
|
case "disable":
|
|
@@ -258,10 +279,14 @@ export default {
|
|
|
258
279
|
}
|
|
259
280
|
break
|
|
260
281
|
case "disable-line":
|
|
261
|
-
if (comment_loc.start.line == line)
|
|
282
|
+
if (comment_loc.start.line == line) {
|
|
283
|
+
return true
|
|
284
|
+
}
|
|
262
285
|
break
|
|
263
286
|
case "disable-next-line":
|
|
264
|
-
if (comment_loc.end.line + 1 == line)
|
|
287
|
+
if (comment_loc.end.line + 1 == line) {
|
|
288
|
+
return true
|
|
289
|
+
}
|
|
265
290
|
}
|
|
266
291
|
}
|
|
267
292
|
return disabled
|
|
@@ -280,9 +305,11 @@ export default {
|
|
|
280
305
|
anchor = parent
|
|
281
306
|
break
|
|
282
307
|
case "VariableDeclarator":
|
|
283
|
-
|
|
308
|
+
anchor = parent.parent
|
|
309
|
+
}
|
|
310
|
+
if (anchor.parent.type == "ExportDefaultDeclaration" || anchor.parent.type == "ExportNamedDeclaration") {
|
|
311
|
+
anchor = anchor.parent
|
|
284
312
|
}
|
|
285
|
-
if (anchor.parent.type == "ExportDefaultDeclaration" || anchor.parent.type == "ExportNamedDeclaration") anchor = anchor.parent
|
|
286
313
|
return /** @type {import("../../private.js").Comment[]} */(source.getCommentsBefore(
|
|
287
314
|
/** @type {import("estree").Node} */(anchor)/**/
|
|
288
315
|
))/**/.at(-1)
|
|
@@ -302,22 +329,31 @@ export default {
|
|
|
302
329
|
? /** @type {import("../../private.js").AstNode} */(decorators[decorators.length - 1])/**/.range[1]
|
|
303
330
|
: target.range[0]
|
|
304
331
|
)
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
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
|
+
}
|
|
309
339
|
}
|
|
310
|
-
if (shorthand_properties.has(target))
|
|
311
|
-
|
|
312
|
-
|
|
340
|
+
if (shorthand_properties.has(target)) {
|
|
341
|
+
return {
|
|
342
|
+
range,
|
|
343
|
+
text: name + ": " + fixed_name
|
|
344
|
+
}
|
|
313
345
|
}
|
|
314
|
-
if (import_shorthands.has(target))
|
|
315
|
-
|
|
316
|
-
|
|
346
|
+
if (import_shorthands.has(target)) {
|
|
347
|
+
return {
|
|
348
|
+
range,
|
|
349
|
+
text: name + " as " + fixed_name
|
|
350
|
+
}
|
|
317
351
|
}
|
|
318
|
-
if (export_shorthands.has(target))
|
|
319
|
-
|
|
320
|
-
|
|
352
|
+
if (export_shorthands.has(target)) {
|
|
353
|
+
return {
|
|
354
|
+
range,
|
|
355
|
+
text: fixed_name + " as " + name
|
|
356
|
+
}
|
|
321
357
|
}
|
|
322
358
|
if (parent.type == "ImportSpecifier" && parent.imported.type == "Identifier" && parent.imported.name == fixed_name) {
|
|
323
359
|
return {
|
|
@@ -352,7 +388,9 @@ export default {
|
|
|
352
388
|
name: node.name
|
|
353
389
|
},
|
|
354
390
|
fix(fixer) {
|
|
355
|
-
if (!edits)
|
|
391
|
+
if (!edits) {
|
|
392
|
+
return null
|
|
393
|
+
}
|
|
356
394
|
/** @type {Set<number>} */
|
|
357
395
|
const starts = new Set()
|
|
358
396
|
return edits.filter(
|
|
@@ -377,7 +415,9 @@ export default {
|
|
|
377
415
|
function skip_space(text, index) {
|
|
378
416
|
while (index < text.length && space_regex.test(
|
|
379
417
|
/** @type {string} */(text[index])/**/
|
|
380
|
-
))
|
|
418
|
+
)) {
|
|
419
|
+
index++
|
|
420
|
+
}
|
|
381
421
|
return index
|
|
382
422
|
}
|
|
383
423
|
/**
|
|
@@ -396,11 +436,18 @@ export default {
|
|
|
396
436
|
const store = name.slice(1)
|
|
397
437
|
if (is_svelte) {
|
|
398
438
|
const nodes = store_usages.get(store)
|
|
399
|
-
if (nodes)
|
|
400
|
-
|
|
401
|
-
|
|
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
|
|
402
450
|
}
|
|
403
|
-
if (allow_regex.test(name) || visited.has(node)) return
|
|
404
451
|
visited.add(node)
|
|
405
452
|
const parent = /** @type {import("../../private.js").NamingParent} */(node.parent)/**/
|
|
406
453
|
switch (parent.type) {
|
|
@@ -410,29 +457,41 @@ export default {
|
|
|
410
457
|
case "TSAbstractPropertyDefinition":
|
|
411
458
|
case "TSMethodSignature":
|
|
412
459
|
case "TSPropertySignature":
|
|
413
|
-
if (parent.computed || parent.value == node)
|
|
460
|
+
if (parent.computed || parent.value == node) {
|
|
461
|
+
defer(node)
|
|
462
|
+
}
|
|
414
463
|
break
|
|
415
464
|
case "ArrayExpression":
|
|
416
465
|
defer(node)
|
|
417
466
|
break
|
|
418
467
|
case "ArrayPattern":
|
|
419
|
-
if (is_declaration(node))
|
|
420
|
-
|
|
468
|
+
if (is_declaration(node)) {
|
|
469
|
+
declare(node)
|
|
470
|
+
} else {
|
|
471
|
+
defer(node)
|
|
472
|
+
}
|
|
421
473
|
break
|
|
422
474
|
case "ArrowFunctionExpression":
|
|
423
|
-
if (parent.body == node)
|
|
424
|
-
|
|
475
|
+
if (parent.body == node) {
|
|
476
|
+
defer(node)
|
|
477
|
+
} else {
|
|
478
|
+
declare(node)
|
|
479
|
+
}
|
|
425
480
|
break
|
|
426
481
|
case "AssignmentExpression":
|
|
427
482
|
defer(node)
|
|
428
483
|
break
|
|
429
484
|
case "AssignmentPattern":
|
|
430
|
-
if (parent.left != node)
|
|
431
|
-
|
|
485
|
+
if (parent.left != node) {
|
|
486
|
+
defer(node)
|
|
487
|
+
} else if (parent.parent?.type == "Property" && parent.parent.shorthand) {
|
|
432
488
|
shorthand_properties.add(node)
|
|
433
489
|
defer(node)
|
|
434
|
-
} else if (is_declaration(node))
|
|
435
|
-
|
|
490
|
+
} else if (is_declaration(node)) {
|
|
491
|
+
declare(node)
|
|
492
|
+
} else {
|
|
493
|
+
defer(node)
|
|
494
|
+
}
|
|
436
495
|
break
|
|
437
496
|
case "AwaitExpression":
|
|
438
497
|
defer(node)
|
|
@@ -480,8 +539,11 @@ export default {
|
|
|
480
539
|
break
|
|
481
540
|
case "ClassDeclaration":
|
|
482
541
|
case "ClassExpression":
|
|
483
|
-
if (parent.id == node)
|
|
484
|
-
|
|
542
|
+
if (parent.id == node) {
|
|
543
|
+
declare(node)
|
|
544
|
+
} else {
|
|
545
|
+
defer(node)
|
|
546
|
+
}
|
|
485
547
|
break
|
|
486
548
|
case "ConditionalExpression":
|
|
487
549
|
defer(node)
|
|
@@ -498,7 +560,9 @@ export default {
|
|
|
498
560
|
case "ExportSpecifier":
|
|
499
561
|
if (parent.local == node && !(/** @type {import("estree").ExportNamedDeclaration} */(parent.parent)/**/).source) {
|
|
500
562
|
const is_shorthand = parent.exported == node || parent.exported.range?.[0] == node.range[0]
|
|
501
|
-
if (is_shorthand)
|
|
563
|
+
if (is_shorthand) {
|
|
564
|
+
export_shorthands.add(node)
|
|
565
|
+
}
|
|
502
566
|
add(export_locals, node)
|
|
503
567
|
}
|
|
504
568
|
break
|
|
@@ -517,8 +581,12 @@ export default {
|
|
|
517
581
|
case "FunctionDeclaration":
|
|
518
582
|
case "TSDeclareFunction":
|
|
519
583
|
if (parent.id != node) {
|
|
520
|
-
if (parent.type == "FunctionDeclaration")
|
|
521
|
-
|
|
584
|
+
if (parent.type == "FunctionDeclaration") {
|
|
585
|
+
declare(node)
|
|
586
|
+
}
|
|
587
|
+
} else if (parent.parent?.type != "ExportNamedDeclaration" || !camel_case_regex.test(name)) {
|
|
588
|
+
declare(node)
|
|
589
|
+
}
|
|
522
590
|
break
|
|
523
591
|
case "FunctionExpression":
|
|
524
592
|
declare(node)
|
|
@@ -540,17 +608,23 @@ export default {
|
|
|
540
608
|
if (parent.imported.type == "Identifier" && parent.imported.name == name) {
|
|
541
609
|
import_shorthands.add(node)
|
|
542
610
|
defer(node)
|
|
543
|
-
} else
|
|
611
|
+
} else {
|
|
612
|
+
declare(node)
|
|
613
|
+
}
|
|
544
614
|
}
|
|
545
615
|
break
|
|
546
616
|
case "LogicalExpression":
|
|
547
617
|
defer(node)
|
|
548
618
|
break
|
|
549
619
|
case "MemberExpression":
|
|
550
|
-
if (parent.object == node || parent.computed)
|
|
620
|
+
if (parent.object == node || parent.computed) {
|
|
621
|
+
defer(node)
|
|
622
|
+
}
|
|
551
623
|
break
|
|
552
624
|
case "MethodDefinition":
|
|
553
|
-
if (parent.computed)
|
|
625
|
+
if (parent.computed) {
|
|
626
|
+
defer(node)
|
|
627
|
+
}
|
|
554
628
|
break
|
|
555
629
|
case "NewExpression":
|
|
556
630
|
defer(node)
|
|
@@ -558,18 +632,30 @@ export default {
|
|
|
558
632
|
case "Property":
|
|
559
633
|
if (parent.value == node) {
|
|
560
634
|
const is_shorthand = parent.shorthand
|
|
561
|
-
if (is_shorthand)
|
|
635
|
+
if (is_shorthand) {
|
|
636
|
+
shorthand_properties.add(node)
|
|
637
|
+
}
|
|
562
638
|
const declares = !is_shorthand && parent.parent?.type == "ObjectPattern" && is_declaration(node)
|
|
563
|
-
if (declares)
|
|
564
|
-
|
|
565
|
-
|
|
639
|
+
if (declares) {
|
|
640
|
+
declare(node)
|
|
641
|
+
} else {
|
|
642
|
+
defer(node)
|
|
643
|
+
}
|
|
644
|
+
} else if (parent.computed) {
|
|
645
|
+
defer(node)
|
|
646
|
+
}
|
|
566
647
|
break
|
|
567
648
|
case "PropertyDefinition":
|
|
568
|
-
if (parent.value == node || parent.computed)
|
|
649
|
+
if (parent.value == node || parent.computed) {
|
|
650
|
+
defer(node)
|
|
651
|
+
}
|
|
569
652
|
break
|
|
570
653
|
case "RestElement":
|
|
571
|
-
if (is_declaration(node))
|
|
572
|
-
|
|
654
|
+
if (is_declaration(node)) {
|
|
655
|
+
declare(node)
|
|
656
|
+
} else {
|
|
657
|
+
defer(node)
|
|
658
|
+
}
|
|
573
659
|
break
|
|
574
660
|
case "ReturnStatement":
|
|
575
661
|
defer(node)
|
|
@@ -587,14 +673,21 @@ export default {
|
|
|
587
673
|
defer(node)
|
|
588
674
|
break
|
|
589
675
|
case "TSEnumMember":
|
|
590
|
-
if (parent.initializer == node)
|
|
676
|
+
if (parent.initializer == node) {
|
|
677
|
+
defer(node)
|
|
678
|
+
}
|
|
591
679
|
break
|
|
592
680
|
case "TSImportEqualsDeclaration":
|
|
593
|
-
if (parent.id == node)
|
|
594
|
-
|
|
681
|
+
if (parent.id == node) {
|
|
682
|
+
declare(node)
|
|
683
|
+
} else {
|
|
684
|
+
defer(node)
|
|
685
|
+
}
|
|
595
686
|
break
|
|
596
687
|
case "TSQualifiedName":
|
|
597
|
-
if (parent.left == node)
|
|
688
|
+
if (parent.left == node) {
|
|
689
|
+
defer(node)
|
|
690
|
+
}
|
|
598
691
|
break
|
|
599
692
|
case "TaggedTemplateExpression":
|
|
600
693
|
defer(node)
|
|
@@ -619,7 +712,9 @@ export default {
|
|
|
619
712
|
) {
|
|
620
713
|
declare(node)
|
|
621
714
|
}
|
|
622
|
-
} else
|
|
715
|
+
} else {
|
|
716
|
+
defer(node)
|
|
717
|
+
}
|
|
623
718
|
break
|
|
624
719
|
case "WhileStatement":
|
|
625
720
|
defer(node)
|
|
@@ -628,7 +723,8 @@ export default {
|
|
|
628
723
|
defer(node)
|
|
629
724
|
break
|
|
630
725
|
default: {
|
|
631
|
-
|
|
726
|
+
/** @type {string} */
|
|
727
|
+
const type = parent.type
|
|
632
728
|
const svelte_parent = /** @type {{ expression?: unknown, kind?: string, value?: unknown }} */(/** @type {unknown} */(parent))/**/
|
|
633
729
|
if (type == "SvelteShorthandAttribute") {
|
|
634
730
|
if (svelte_parent.value == node) {
|
|
@@ -639,17 +735,23 @@ export default {
|
|
|
639
735
|
svelte_usage_parents.has(type)
|
|
640
736
|
|| type == "SvelteDirective" && svelte_parent.kind != "Let"
|
|
641
737
|
|| svelte_expression_parents.has(type) && svelte_parent.expression == node
|
|
642
|
-
)
|
|
643
|
-
|
|
738
|
+
) {
|
|
739
|
+
defer(node)
|
|
740
|
+
} else {
|
|
741
|
+
blocked.add(name)
|
|
742
|
+
}
|
|
644
743
|
}
|
|
645
744
|
}
|
|
646
745
|
},
|
|
647
746
|
/** @param {import("../../private.js").AstNode & import("estree").Identifier} node */
|
|
648
747
|
JSXIdentifier(node) {
|
|
649
748
|
const name = node.name
|
|
650
|
-
|
|
749
|
+
/** @type {{ object?: unknown, type: string }} */
|
|
750
|
+
const parent = node.parent
|
|
651
751
|
if (parent.type == "JSXMemberExpression" && parent.object == node) {
|
|
652
|
-
if (allow_regex.test(name))
|
|
752
|
+
if (allow_regex.test(name)) {
|
|
753
|
+
return
|
|
754
|
+
}
|
|
653
755
|
jsx_objects.add(node)
|
|
654
756
|
defer(node)
|
|
655
757
|
} else if ((parent.type == "JSXOpeningElement" || parent.type == "JSXClosingElement") && !lowercase_regex.test(name)) {
|
|
@@ -657,31 +759,42 @@ export default {
|
|
|
657
759
|
}
|
|
658
760
|
},
|
|
659
761
|
"Program:exit": () => {
|
|
660
|
-
if (!declarations.size)
|
|
762
|
+
if (!declarations.size) {
|
|
763
|
+
return
|
|
764
|
+
}
|
|
661
765
|
/** @type {Map<import("../../private.js").AstNode, import("eslint").Scope.Variable | null>} */
|
|
662
766
|
const resolution = new Map()
|
|
663
767
|
/** @type {Map<string, import("eslint").Scope.Variable[]>} */
|
|
664
768
|
const variables_by_name = new Map()
|
|
665
|
-
const scopes = source.scopeManager
|
|
769
|
+
const scopes = /** @type {import("eslint").Scope.ScopeManager} */(source.scopeManager)/**/.scopes
|
|
666
770
|
for (const scope of scopes) {
|
|
667
771
|
for (const variable of scope.variables) {
|
|
668
772
|
const same_names = variables_by_name.get(variable.name)
|
|
669
|
-
if (same_names)
|
|
670
|
-
|
|
671
|
-
|
|
773
|
+
if (same_names) {
|
|
774
|
+
same_names.push(variable)
|
|
775
|
+
} else {
|
|
776
|
+
variables_by_name.set(variable.name, [ variable ])
|
|
777
|
+
}
|
|
778
|
+
for (const identifier of variable.identifiers) {
|
|
779
|
+
resolution.set(
|
|
672
780
|
/** @type {import("../../private.js").AstNode} */(/** @type {unknown} */(identifier))/**/,
|
|
673
|
-
|
|
674
|
-
|
|
781
|
+
variable
|
|
782
|
+
)
|
|
783
|
+
}
|
|
675
784
|
}
|
|
676
785
|
}
|
|
677
786
|
for (const scope of scopes) {
|
|
678
787
|
for (const reference of scope.references) {
|
|
679
788
|
const identifier = /** @type {import("../../private.js").AstNode} */(/** @type {unknown} */(reference.identifier))/**/
|
|
680
|
-
if (!resolution.has(identifier))
|
|
789
|
+
if (!resolution.has(identifier)) {
|
|
790
|
+
resolution.set(identifier, reference.resolved)
|
|
791
|
+
}
|
|
681
792
|
}
|
|
682
793
|
}
|
|
683
794
|
for (const node of jsx_objects) {
|
|
684
|
-
if (!resolution.has(node))
|
|
795
|
+
if (!resolution.has(node)) {
|
|
796
|
+
blocked.add(node.name)
|
|
797
|
+
}
|
|
685
798
|
}
|
|
686
799
|
/** @type {Set<import("eslint").Scope.Variable>} */
|
|
687
800
|
const renamable = new Set()
|
|
@@ -690,8 +803,9 @@ export default {
|
|
|
690
803
|
for (const nodes of declarations.values()) {
|
|
691
804
|
for (const node of nodes) {
|
|
692
805
|
declared.add(node)
|
|
693
|
-
|
|
694
|
-
|
|
806
|
+
renamable.add(
|
|
807
|
+
/** @type {import("eslint").Scope.Variable} */(resolution.get(node))/**/
|
|
808
|
+
)
|
|
695
809
|
}
|
|
696
810
|
}
|
|
697
811
|
/** @type {Map<string, Set<import("eslint").Scope.Scope>>} */
|
|
@@ -699,11 +813,14 @@ export default {
|
|
|
699
813
|
for (const variable of renamable) {
|
|
700
814
|
const target = to_snake_case(variable.name)
|
|
701
815
|
const target_scopes = scopes_by_target.get(target)
|
|
702
|
-
if (target_scopes)
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
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
|
+
)
|
|
823
|
+
}
|
|
707
824
|
}
|
|
708
825
|
/** @type {Set<string>} */
|
|
709
826
|
const nested_targets = new Set()
|
|
@@ -711,7 +828,9 @@ export default {
|
|
|
711
828
|
const target = to_snake_case(variable.name)
|
|
712
829
|
const target_scopes = /** @type {Set<import("eslint").Scope.Scope>} */(scopes_by_target.get(target))/**/
|
|
713
830
|
for (let scope = variable.scope.upper; scope; scope = scope.upper) {
|
|
714
|
-
if (target_scopes.has(scope))
|
|
831
|
+
if (target_scopes.has(scope)) {
|
|
832
|
+
nested_targets.add(target)
|
|
833
|
+
}
|
|
715
834
|
}
|
|
716
835
|
}
|
|
717
836
|
/**
|
|
@@ -719,7 +838,9 @@ export default {
|
|
|
719
838
|
* @returns {boolean}
|
|
720
839
|
*/
|
|
721
840
|
function belongs(node) {
|
|
722
|
-
if (!resolution.has(node))
|
|
841
|
+
if (!resolution.has(node)) {
|
|
842
|
+
return true
|
|
843
|
+
}
|
|
723
844
|
const variable = resolution.get(node)
|
|
724
845
|
return !!variable && renamable.has(variable)
|
|
725
846
|
}
|
|
@@ -729,9 +850,13 @@ export default {
|
|
|
729
850
|
*/
|
|
730
851
|
function collides(name) {
|
|
731
852
|
const others = variables_by_name.get(to_snake_case(name)) ?? []
|
|
732
|
-
if (!others.length)
|
|
733
|
-
|
|
734
|
-
|
|
853
|
+
if (!others.length) {
|
|
854
|
+
return false
|
|
855
|
+
}
|
|
856
|
+
for (const variable of /** @type {import("eslint").Scope.Variable[]} */(variables_by_name.get(name))/**/) {
|
|
857
|
+
if (!renamable.has(variable)) {
|
|
858
|
+
continue
|
|
859
|
+
}
|
|
735
860
|
for (const scope of [
|
|
736
861
|
variable.scope,
|
|
737
862
|
...variable.references.map(reference => reference.from)
|
|
@@ -739,14 +864,20 @@ export default {
|
|
|
739
864
|
for (let current = /** @type {import("eslint").Scope.Scope | null} */(scope)/**/; current; current = current.upper) {
|
|
740
865
|
if (others.some(
|
|
741
866
|
other => other.scope == current
|
|
742
|
-
))
|
|
743
|
-
|
|
867
|
+
)) {
|
|
868
|
+
return true
|
|
869
|
+
}
|
|
870
|
+
if (current == variable.scope) {
|
|
871
|
+
break
|
|
872
|
+
}
|
|
744
873
|
}
|
|
745
874
|
}
|
|
746
875
|
for (const other of others) {
|
|
747
876
|
for (const reference of other.references) {
|
|
748
877
|
for (let current = /** @type {import("eslint").Scope.Scope | null} */(reference.from)/**/; current && current != other.scope; current = current.upper) {
|
|
749
|
-
if (current == variable.scope)
|
|
878
|
+
if (current == variable.scope) {
|
|
879
|
+
return true
|
|
880
|
+
}
|
|
750
881
|
}
|
|
751
882
|
}
|
|
752
883
|
}
|
|
@@ -758,7 +889,7 @@ export default {
|
|
|
758
889
|
* @returns {boolean}
|
|
759
890
|
*/
|
|
760
891
|
function has_foreign_declaration(name) {
|
|
761
|
-
return (variables_by_name.get(name)
|
|
892
|
+
return /** @type {import("eslint").Scope.Variable[]} */(variables_by_name.get(name))/**/.some(
|
|
762
893
|
variable => renamable.has(variable) && variable.identifiers.some(
|
|
763
894
|
identifier => !declared.has(
|
|
764
895
|
/** @type {import("../../private.js").AstNode} */(/** @type {unknown} */(identifier))/**/
|
|
@@ -770,9 +901,11 @@ export default {
|
|
|
770
901
|
const held = new Set(default_exports)
|
|
771
902
|
for (const variable of renamable) {
|
|
772
903
|
for (const reference of variable.references) {
|
|
773
|
-
if (reference.isWrite() && !reference.init)
|
|
904
|
+
if (reference.isWrite() && !reference.init) {
|
|
905
|
+
held.add(
|
|
774
906
|
/** @type {import("../../private.js").AstNode} */(/** @type {unknown} */(reference.identifier))/**/
|
|
775
|
-
|
|
907
|
+
)
|
|
908
|
+
}
|
|
776
909
|
}
|
|
777
910
|
}
|
|
778
911
|
/** @type {Map<string, import("../../private.js").RenamePlan>} */
|
|
@@ -820,17 +953,23 @@ export default {
|
|
|
820
953
|
let atomic_node
|
|
821
954
|
for (const [ name, nodes ] of declarations) {
|
|
822
955
|
const plan = /** @type {import("../../private.js").RenamePlan} */(plans.get(name))/**/
|
|
823
|
-
if (!plan.exported.length || !plan.fixable || !plan.atomic && plan.used.length)
|
|
956
|
+
if (!plan.exported.length || !plan.fixable || !plan.atomic && plan.used.length) {
|
|
957
|
+
continue
|
|
958
|
+
}
|
|
824
959
|
atomic_node ??= nodes[0]
|
|
825
|
-
for (const node of nodes)
|
|
826
|
-
|
|
827
|
-
|
|
960
|
+
for (const node of nodes) {
|
|
961
|
+
atomic.push(
|
|
962
|
+
...declaration_edits(node, resolution)
|
|
963
|
+
)
|
|
964
|
+
}
|
|
828
965
|
for (const node of [
|
|
829
966
|
...plan.exported,
|
|
830
967
|
...plan.used,
|
|
831
968
|
...plan.held,
|
|
832
969
|
...store_usages.get(name) ?? []
|
|
833
|
-
])
|
|
970
|
+
]) {
|
|
971
|
+
atomic.push(rename_edit(node))
|
|
972
|
+
}
|
|
834
973
|
}
|
|
835
974
|
for (const [ name, nodes ] of declarations) {
|
|
836
975
|
const {
|
|
@@ -841,15 +980,19 @@ export default {
|
|
|
841
980
|
used
|
|
842
981
|
} = /** @type {import("../../private.js").RenamePlan} */(plans.get(name))/**/
|
|
843
982
|
if (exported.length) {
|
|
844
|
-
for (const node of nodes)
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
983
|
+
for (const node of nodes) {
|
|
984
|
+
report(
|
|
985
|
+
node,
|
|
986
|
+
node == atomic_node ? atomic : null
|
|
987
|
+
)
|
|
988
|
+
}
|
|
848
989
|
} else if (!renames_at_once) {
|
|
849
|
-
for (const node of nodes)
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
990
|
+
for (const node of nodes) {
|
|
991
|
+
report(
|
|
992
|
+
node,
|
|
993
|
+
fixable && !used.length ? declaration_fix(node, nodes, usages_held) : null
|
|
994
|
+
)
|
|
995
|
+
}
|
|
853
996
|
} else {
|
|
854
997
|
report(
|
|
855
998
|
/** @type {import("../../private.js").NamingIdentifier} */(nodes[0])/**/,
|
|
@@ -864,15 +1007,25 @@ export default {
|
|
|
864
1007
|
]
|
|
865
1008
|
: null
|
|
866
1009
|
)
|
|
867
|
-
for (const node of nodes.slice(1))
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
1010
|
+
for (const node of nodes.slice(1)) {
|
|
1011
|
+
report(node, null)
|
|
1012
|
+
}
|
|
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
|
|
1027
|
+
)
|
|
1028
|
+
}
|
|
876
1029
|
}
|
|
877
1030
|
}
|
|
878
1031
|
}
|