Skip to content

Commit 86f1626

Browse files
authored
fix(sort-modules): fix decorator dependencies not detected
1 parent 529916b commit 86f1626

File tree

2 files changed

+69
-14
lines changed

2 files changed

+69
-14
lines changed

rules/sort-modules.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -378,18 +378,6 @@ let extractDependencies = (
378378
): string[] => {
379379
let dependencies: string[] = []
380380

381-
let isPropertyOrAccessor = (
382-
node: TSESTree.Node,
383-
): node is TSESTree.PropertyDefinition | TSESTree.AccessorProperty =>
384-
node.type === 'PropertyDefinition' || node.type === 'AccessorProperty'
385-
386-
let isArrowFunction = (
387-
node: TSESTree.Node,
388-
): node is TSESTree.PropertyDefinition | TSESTree.AccessorProperty =>
389-
isPropertyOrAccessor(node) &&
390-
node.value !== null &&
391-
node.value.type === 'ArrowFunctionExpression'
392-
393381
/**
394382
* Search static methods only if there is a static block or a static property
395383
* that is not an arrow function
@@ -412,6 +400,10 @@ let extractDependencies = (
412400
return
413401
}
414402

403+
if ('decorators' in nodeValue) {
404+
traverseNode(nodeValue.decorators)
405+
}
406+
415407
if (
416408
nodeValue.type === 'NewExpression' &&
417409
nodeValue.callee.type === 'Identifier'
@@ -525,3 +517,15 @@ let extractDependencies = (
525517
checkNode(expression)
526518
return dependencies
527519
}
520+
521+
let isPropertyOrAccessor = (
522+
node: TSESTree.Node,
523+
): node is TSESTree.PropertyDefinition | TSESTree.AccessorProperty =>
524+
node.type === 'PropertyDefinition' || node.type === 'AccessorProperty'
525+
526+
let isArrowFunction = (
527+
node: TSESTree.Node,
528+
): node is TSESTree.PropertyDefinition | TSESTree.AccessorProperty =>
529+
isPropertyOrAccessor(node) &&
530+
node.value !== null &&
531+
node.value.type === 'ArrowFunctionExpression'

test/rules/sort-modules.test.ts

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,7 +1811,7 @@ describe(ruleName, () => {
18111811
options: [
18121812
{
18131813
...options,
1814-
groups: [],
1814+
groups: ['unknown'],
18151815
},
18161816
],
18171817
},
@@ -1829,7 +1829,7 @@ describe(ruleName, () => {
18291829
options: [
18301830
{
18311831
...options,
1832-
groups: [],
1832+
groups: ['unknown'],
18331833
},
18341834
],
18351835
code: dedent`
@@ -1843,6 +1843,57 @@ describe(ruleName, () => {
18431843
},
18441844
)
18451845

1846+
ruleTester.run(
1847+
`${ruleName}(${type}) detects dependencies in decorators`,
1848+
rule,
1849+
{
1850+
invalid: [
1851+
{
1852+
output: dedent`
1853+
enum B {}
1854+
1855+
class A {
1856+
@SomeDecorator({
1857+
a: {
1858+
b: c.concat([B])
1859+
}
1860+
})
1861+
property
1862+
}
1863+
`,
1864+
code: dedent`
1865+
class A {
1866+
@SomeDecorator({
1867+
a: {
1868+
b: c.concat([B])
1869+
}
1870+
})
1871+
property
1872+
}
1873+
1874+
enum B {}
1875+
`,
1876+
errors: [
1877+
{
1878+
data: {
1879+
nodeDependentOnRight: 'A',
1880+
right: 'B',
1881+
},
1882+
messageId: 'unexpectedModulesDependencyOrder',
1883+
},
1884+
],
1885+
options: [
1886+
{
1887+
...options,
1888+
groups: ['unknown'],
1889+
},
1890+
],
1891+
},
1892+
],
1893+
valid: [],
1894+
},
1895+
)
1896+
18461897
ruleTester.run(
18471898
`${ruleName}(${type}) detects and ignores circular dependencies`,
18481899
rule,

0 commit comments

Comments
 (0)