Skip to content

Commit

Permalink
fix: do not sort enums with implicit values
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Jun 9, 2023
1 parent a5a0582 commit 166edac
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
5 changes: 4 additions & 1 deletion rules/sort-enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ export default createEslintRule<Options, MESSAGE_ID>({
],
create: context => ({
TSEnumDeclaration: node => {
if (node.members.length > 1) {
if (
node.members.length > 1 &&
node.members.some(({ initializer }) => initializer)
) {
let options = complete(context.options.at(0), {
type: SortType.alphabetical,
order: SortOrder.asc,
Expand Down
72 changes: 72 additions & 0 deletions test/sort-enums.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,30 @@ describe(RULE_NAME, () => {
],
})
})

it(`${RULE_NAME}(${type}): does not sort enums with implicit values`, () => {
ruleTester.run(RULE_NAME, rule, {
valid: [
{
code: dedent`
export enum KessokuBand {
HitoriGotou, // implicit value: 0
NijikaIjichi, // implicit value: 1
RyouYamada, // implicit value: 2
IkuyoKita, // implicit value: 3
}
`,
options: [
{
type: SortType.alphabetical,
order: SortOrder.asc,
},
],
},
],
invalid: [],
})
})
})

describe(`${RULE_NAME}: sorting by natural order`, () => {
Expand Down Expand Up @@ -560,6 +584,30 @@ describe(RULE_NAME, () => {
],
})
})

it(`${RULE_NAME}(${type}): does not sort enums with implicit values`, () => {
ruleTester.run(RULE_NAME, rule, {
valid: [
{
code: dedent`
export enum KessokuBand {
HitoriGotou, // implicit value: 0
NijikaIjichi, // implicit value: 1
RyouYamada, // implicit value: 2
IkuyoKita, // implicit value: 3
}
`,
options: [
{
type: SortType.natural,
order: SortOrder.asc,
},
],
},
],
invalid: [],
})
})
})

describe(`${RULE_NAME}: sorting by line length`, () => {
Expand Down Expand Up @@ -850,6 +898,30 @@ describe(RULE_NAME, () => {
],
})
})

it(`${RULE_NAME}(${type}): does not sort enums with implicit values`, () => {
ruleTester.run(RULE_NAME, rule, {
valid: [
{
code: dedent`
export enum KessokuBand {
HitoriGotou, // implicit value: 0
NijikaIjichi, // implicit value: 1
RyouYamada, // implicit value: 2
IkuyoKita, // implicit value: 3
}
`,
options: [
{
type: SortType['line-length'],
order: SortOrder.desc,
},
],
},
],
invalid: [],
})
})
})

describe(`${RULE_NAME}: misc`, () => {
Expand Down

0 comments on commit 166edac

Please sign in to comment.