diff --git a/rules/sort-enums.ts b/rules/sort-enums.ts index cd20c7e1..fc9944b4 100644 --- a/rules/sort-enums.ts +++ b/rules/sort-enums.ts @@ -164,6 +164,10 @@ export default createEslintRule({ if ('right' in nodeValue) { checkNode(nodeValue.right) } + + if ('expressions' in nodeValue) { + nodeValue.expressions.forEach(checkNode) + } } checkNode(expression) diff --git a/test/sort-enums.test.ts b/test/sort-enums.test.ts index cd47cf8f..2af9f171 100644 --- a/test/sort-enums.test.ts +++ b/test/sort-enums.test.ts @@ -1704,209 +1704,236 @@ describe(ruleName, () => { }, ) - ruleTester.run(`${ruleName}: works with dependencies`, rule, { - valid: [], - invalid: [ - { - code: dedent` - enum Enum { - C = 'C', - B = 0, - A = B, - } - `, - output: dedent` - enum Enum { - B = 0, - A = B, - C = 'C', - } - `, - options: [ - { - type: 'alphabetical', - }, - ], - errors: [ - { - messageId: 'unexpectedEnumsOrder', - data: { - left: 'C', - right: 'B', + describe('detects dependencies', () => { + ruleTester.run(`${ruleName}: works with dependencies`, rule, { + valid: [], + invalid: [ + { + code: dedent` + enum Enum { + C = 'C', + B = 0, + A = B, + } + `, + output: dedent` + enum Enum { + B = 0, + A = B, + C = 'C', + } + `, + options: [ + { + type: 'alphabetical', }, - }, - ], - }, - { - code: dedent` - enum Enum { - C = 'C', - B = 0, - A = Enum.B, - } - `, - output: dedent` - enum Enum { - B = 0, - A = Enum.B, - C = 'C', - } - `, - options: [ - { - type: 'alphabetical', - }, - ], - errors: [ - { - messageId: 'unexpectedEnumsOrder', - data: { - left: 'C', - right: 'B', + ], + errors: [ + { + messageId: 'unexpectedEnumsOrder', + data: { + left: 'C', + right: 'B', + }, }, - }, - ], - }, - { - code: dedent` - enum Enum { - C = 3, - B = 0, - A = 1 | 2 | B | Enum.B, - } - `, - output: dedent` - enum Enum { - B = 0, - A = 1 | 2 | B | Enum.B, - C = 3, - } - `, - options: [ - { - type: 'alphabetical', - }, - ], - errors: [ - { - messageId: 'unexpectedEnumsOrder', - data: { - left: 'C', - right: 'B', + ], + }, + { + code: dedent` + enum Enum { + C = 'C', + B = 0, + A = Enum.B, + } + `, + output: dedent` + enum Enum { + B = 0, + A = Enum.B, + C = 'C', + } + `, + options: [ + { + type: 'alphabetical', }, - }, - ], - }, - { - code: dedent` - enum Enum { - B = 'B', - A = AnotherEnum.B, - C = 'C', - } - `, - output: dedent` - enum Enum { - A = AnotherEnum.B, - B = 'B', - C = 'C', - } - `, - options: [ - { - type: 'alphabetical', - }, - ], - errors: [ - { - messageId: 'unexpectedEnumsOrder', - data: { - left: 'B', - right: 'A', + ], + errors: [ + { + messageId: 'unexpectedEnumsOrder', + data: { + left: 'C', + right: 'B', + }, }, - }, - ], - }, - { - code: dedent` - enum Enum { - A = Enum.C, - B = 10, - C = 10, - } - `, - output: dedent` - enum Enum { - C = 10, - A = Enum.C, - B = 10, - } - `, - options: [ - { - type: 'alphabetical', - }, - ], - errors: [ - { - messageId: 'unexpectedEnumsOrder', - data: { - left: 'B', - right: 'C', + ], + }, + { + code: dedent` + enum Enum { + C = 3, + B = 0, + A = 1 | 2 | B | Enum.B, + } + `, + output: dedent` + enum Enum { + B = 0, + A = 1 | 2 | B | Enum.B, + C = 3, + } + `, + options: [ + { + type: 'alphabetical', }, - }, - ], - }, - ], - }) + ], + errors: [ + { + messageId: 'unexpectedEnumsOrder', + data: { + left: 'C', + right: 'B', + }, + }, + ], + }, + { + code: dedent` + enum Enum { + B = 'B', + A = AnotherEnum.B, + C = 'C', + } + `, + output: dedent` + enum Enum { + A = AnotherEnum.B, + B = 'B', + C = 'C', + } + `, + options: [ + { + type: 'alphabetical', + }, + ], + errors: [ + { + messageId: 'unexpectedEnumsOrder', + data: { + left: 'B', + right: 'A', + }, + }, + ], + }, + { + code: dedent` + enum Enum { + A = Enum.C, + B = 10, + C = 10, + } + `, + output: dedent` + enum Enum { + C = 10, + A = Enum.C, + B = 10, + } + `, + options: [ + { + type: 'alphabetical', + }, + ], + errors: [ + { + messageId: 'unexpectedEnumsOrder', + data: { + left: 'B', + right: 'C', + }, + }, + ], + }, + ], + }) - ruleTester.run(`${ruleName}: detects circular dependencies`, rule, { - valid: [], - invalid: [ + ruleTester.run( + `${ruleName} detects dependencies in template literal expressions`, + rule, { - code: dedent` - enum Enum { - A = 'A', - B = F, - C = 'C', - D = B, - E = 'E', - F = D - } - `, - output: dedent` - enum Enum { - A = 'A', - D = B, - F = D, - B = F, - C = 'C', - E = 'E' - } - `, - options: [ + valid: [ { - type: 'alphabetical', + code: dedent` + enum Enum { + A = \`\${AnotherEnum.D}\`, + D = 'D', + B = \`\${Enum.D}\`, + C = \`\${D}\`, + } + `, + options: [ + { + type: 'alphabetical', + }, + ], }, ], - errors: [ - { - messageId: 'unexpectedEnumsOrder', - data: { - left: 'C', - right: 'D', + invalid: [], + }, + ) + + ruleTester.run(`${ruleName}: detects circular dependencies`, rule, { + valid: [], + invalid: [ + { + code: dedent` + enum Enum { + A = 'A', + B = F, + C = 'C', + D = B, + E = 'E', + F = D + } + `, + output: dedent` + enum Enum { + A = 'A', + D = B, + F = D, + B = F, + C = 'C', + E = 'E' + } + `, + options: [ + { + type: 'alphabetical', }, - }, - { - messageId: 'unexpectedEnumsOrder', - data: { - left: 'E', - right: 'F', + ], + errors: [ + { + messageId: 'unexpectedEnumsOrder', + data: { + left: 'C', + right: 'D', + }, }, - }, - ], - }, - ], + { + messageId: 'unexpectedEnumsOrder', + data: { + left: 'E', + right: 'F', + }, + }, + ], + }, + ], + }) }) }) })