Skip to content

Commit 1def9db

Browse files
authored
fix(sort-variable-declarations): fix destructured assignments dependencies not detected
1 parent 5441bde commit 1def9db

File tree

2 files changed

+54
-22
lines changed

2 files changed

+54
-22
lines changed

rules/sort-variable-declarations.ts

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ export default createEslintRule<Options, MessageId>({
9696

9797
let selector: Selector
9898

99-
let dependencies: string[] = []
99+
let dependencies: string[] = extractDependencies(declaration.id)
100100
if (declaration.init) {
101-
dependencies = extractDependencies(declaration.init)
101+
dependencies.push(...extractDependencies(declaration.init))
102102
selector = 'initialized'
103103
} else {
104104
selector = 'uninitialized'
@@ -246,37 +246,37 @@ function extractDependencies(init: TSESTree.Expression): string[] {
246246
}
247247

248248
if (nodeValue.type === 'Property') {
249-
traverseNode(nodeValue.key)
250-
traverseNode(nodeValue.value)
249+
checkNode(nodeValue.key)
250+
checkNode(nodeValue.value)
251251
}
252252

253253
if (nodeValue.type === 'ConditionalExpression') {
254-
traverseNode(nodeValue.test)
255-
traverseNode(nodeValue.consequent)
256-
traverseNode(nodeValue.alternate)
254+
checkNode(nodeValue.test)
255+
checkNode(nodeValue.consequent)
256+
checkNode(nodeValue.alternate)
257257
}
258258

259259
if (
260260
'expression' in nodeValue &&
261261
typeof nodeValue.expression !== 'boolean'
262262
) {
263-
traverseNode(nodeValue.expression)
263+
checkNode(nodeValue.expression)
264264
}
265265

266266
if ('object' in nodeValue) {
267-
traverseNode(nodeValue.object)
267+
checkNode(nodeValue.object)
268268
}
269269

270270
if ('callee' in nodeValue) {
271-
traverseNode(nodeValue.callee)
271+
checkNode(nodeValue.callee)
272272
}
273273

274274
if ('left' in nodeValue) {
275-
traverseNode(nodeValue.left)
275+
checkNode(nodeValue.left)
276276
}
277277

278278
if ('right' in nodeValue) {
279-
traverseNode(nodeValue.right as TSESTree.Node)
279+
checkNode(nodeValue.right as TSESTree.Node)
280280
}
281281

282282
if ('elements' in nodeValue) {
@@ -285,37 +285,33 @@ function extractDependencies(init: TSESTree.Expression): string[] {
285285
)
286286

287287
for (let element of elements) {
288-
traverseNode(element)
288+
checkNode(element)
289289
}
290290
}
291291

292292
if ('argument' in nodeValue && nodeValue.argument) {
293-
traverseNode(nodeValue.argument)
293+
checkNode(nodeValue.argument)
294294
}
295295

296296
if ('arguments' in nodeValue) {
297297
for (let argument of nodeValue.arguments) {
298-
traverseNode(argument)
298+
checkNode(argument)
299299
}
300300
}
301301

302302
if ('properties' in nodeValue) {
303303
for (let property of nodeValue.properties) {
304-
traverseNode(property)
304+
checkNode(property)
305305
}
306306
}
307307

308308
if ('expressions' in nodeValue) {
309309
for (let nodeExpression of nodeValue.expressions) {
310-
traverseNode(nodeExpression)
310+
checkNode(nodeExpression)
311311
}
312312
}
313313
}
314314

315-
function traverseNode(nodeValue: TSESTree.Node): void {
316-
checkNode(nodeValue)
317-
}
318-
319-
traverseNode(init)
315+
checkNode(init)
320316
return dependencies
321317
}

test/rules/sort-variable-declarations.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,18 @@ describe('sort-variable-declarations', () => {
458458
})
459459
})
460460

461+
it('detects dependencies in destructured assignments', async () => {
462+
await valid({
463+
code: dedent`
464+
let a = "a",
465+
[{
466+
b = a,
467+
}] = {}
468+
`,
469+
options: [options],
470+
})
471+
})
472+
461473
it('ignores dependencies inside function bodies', async () => {
462474
await valid({
463475
code: dedent`
@@ -2304,6 +2316,18 @@ describe('sort-variable-declarations', () => {
23042316
})
23052317
})
23062318

2319+
it('detects dependencies in destructured assignments', async () => {
2320+
await valid({
2321+
code: dedent`
2322+
let a = "a",
2323+
[{
2324+
b = a,
2325+
}] = {}
2326+
`,
2327+
options: [options],
2328+
})
2329+
})
2330+
23072331
it('ignores dependencies inside function bodies', async () => {
23082332
await valid({
23092333
code: dedent`
@@ -4150,6 +4174,18 @@ describe('sort-variable-declarations', () => {
41504174
})
41514175
})
41524176

4177+
it('detects dependencies in destructured assignments', async () => {
4178+
await valid({
4179+
code: dedent`
4180+
let a = "a",
4181+
[{
4182+
b = a,
4183+
}] = {}
4184+
`,
4185+
options: [options],
4186+
})
4187+
})
4188+
41534189
it('ignores dependencies inside function bodies', async () => {
41544190
await valid({
41554191
code: dedent`

0 commit comments

Comments
 (0)