Skip to content

Commit

Permalink
fix: improve dependency detection in sort-variable-declarations rule
Browse files Browse the repository at this point in the history
  • Loading branch information
hugop95 authored Sep 7, 2024
1 parent ae02009 commit 6beb536
Show file tree
Hide file tree
Showing 3 changed files with 596 additions and 110 deletions.
52 changes: 47 additions & 5 deletions rules/sort-variable-declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,44 @@ export default createEslintRule<Options, MESSAGE_ID>({
let dependencies: string[] = []

let checkNode = (nodeValue: TSESTree.Node) => {
/**
* No need to check the body of functions and arrow functions
*/
if (
nodeValue.type === 'ArrowFunctionExpression' ||
nodeValue.type === 'FunctionExpression'
) {
return
}

if (nodeValue.type === 'Identifier') {
dependencies.push(nodeValue.name)
}

if (nodeValue.type === 'Property') {
traverseNode(nodeValue.key)
traverseNode(nodeValue.value)
}

if (nodeValue.type === 'ConditionalExpression') {
traverseNode(nodeValue.test)
traverseNode(nodeValue.consequent)
traverseNode(nodeValue.alternate)
}

if (
'body' in nodeValue &&
nodeValue.body &&
!Array.isArray(nodeValue.body)
'expression' in nodeValue &&
typeof nodeValue.expression !== 'boolean'
) {
traverseNode(nodeValue.body)
traverseNode(nodeValue.expression)
}

if ('object' in nodeValue) {
traverseNode(nodeValue.object)
}

if ('callee' in nodeValue) {
traverseNode(nodeValue.callee)
}

if ('left' in nodeValue) {
Expand All @@ -113,9 +141,23 @@ export default createEslintRule<Options, MESSAGE_ID>({
nodeValue.elements
.filter(currentNode => currentNode !== null)
.forEach(traverseNode)
} else if ('arguments' in nodeValue) {
}

if ('argument' in nodeValue && nodeValue.argument) {
traverseNode(nodeValue.argument)
}

if ('arguments' in nodeValue) {
nodeValue.arguments.forEach(traverseNode)
}

if ('properties' in nodeValue) {
nodeValue.properties.forEach(traverseNode)
}

if ('expressions' in nodeValue) {
nodeValue.expressions.forEach(traverseNode)
}
}

let traverseNode = (nodeValue: TSESTree.Node) => {
Expand Down
94 changes: 93 additions & 1 deletion test/sort-classes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2681,7 +2681,7 @@ describe(ruleName, () => {
)

ruleTester.run(
`${ruleName}(${type}) detects arrow function expression dependencies`,
`${ruleName}(${type}) detects function expression dependencies`,
rule,
{
valid: [
Expand All @@ -2705,6 +2705,26 @@ describe(ruleName, () => {
},
],
},
{
code: dedent`
class Class {
b = function() {
return 1
}
a = this.b()
static b = function() {
return 1
}
static a = this.b()
}
`,
options: [
{
...options,
groups: [['property', 'method']],
},
],
},
{
code: dedent`
class Class {
Expand All @@ -2725,6 +2745,26 @@ describe(ruleName, () => {
},
],
},
{
code: dedent`
class Class {
b = function() {
return 1
}
a = [1].map(this.b)
static b = function() {
return 1
}
static a = [1].map(this.b)
}
`,
options: [
{
...options,
groups: [['property', 'method']],
},
],
},
{
code: dedent`
class Class {
Expand All @@ -2745,6 +2785,26 @@ describe(ruleName, () => {
},
],
},
{
code: dedent`
class Class {
b = function() {
return 1
}
a = [1].map(this.b)
static b = function() {
return 1
}
static a = [1].map(Class.b)
}
`,
options: [
{
...options,
groups: [['property', 'method']],
},
],
},
],
invalid: [],
},
Expand Down Expand Up @@ -3253,6 +3313,38 @@ describe(ruleName, () => {
},
],
},
{
code: dedent`
class Class {
b = []
a = [...this.b]
static b = []
static a = [...this.b]
}
`,
options: [
{
...options,
groups: ['property'],
},
],
},
{
code: dedent`
class Class {
b = []
a = [...this.b]
static b = []
static a = [...Class.b]
}
`,
options: [
{
...options,
groups: ['property'],
},
],
},
],
invalid: [],
},
Expand Down
Loading

0 comments on commit 6beb536

Please sign in to comment.