Skip to content

Commit

Permalink
fix: fix sorting interfaces and types with comment on same line
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Jun 10, 2023
1 parent bbdbbdd commit 03e5508
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 3 deletions.
111 changes: 111 additions & 0 deletions test/sort-interfaces.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,43 @@ describe(RULE_NAME, () => {
],
})
})

it(`${RULE_NAME}(${type}): sorts interfaces with semi and comments on the same line`, () => {
ruleTester.run(RULE_NAME, rule, {
valid: [],
invalid: [
{
code: dedent`
interface Researcher {
rescuer: 'Tokita Kōsaku'; // Character
occupation: string; // Professional direction
}
`,
output: dedent`
interface Researcher {
occupation: string; // Professional direction
rescuer: 'Tokita Kōsaku'; // Character
}
`,
options: [
{
type: SortType.alphabetical,
order: SortOrder.asc,
},
],
errors: [
{
messageId: 'unexpectedInterfacePropertiesOrder',
data: {
left: 'rescuer',
right: 'occupation',
},
},
],
},
],
})
})
})

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

it(`${RULE_NAME}(${type}): sorts interfaces with semi and comments on the same line`, () => {
ruleTester.run(RULE_NAME, rule, {
valid: [],
invalid: [
{
code: dedent`
interface Researcher {
rescuer: 'Tokita Kōsaku'; // Character
occupation: string; // Professional direction
}
`,
output: dedent`
interface Researcher {
occupation: string; // Professional direction
rescuer: 'Tokita Kōsaku'; // Character
}
`,
options: [
{
type: SortType.natural,
order: SortOrder.asc,
},
],
errors: [
{
messageId: 'unexpectedInterfacePropertiesOrder',
data: {
left: 'rescuer',
right: 'occupation',
},
},
],
},
],
})
})
})

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

it(`${RULE_NAME}(${type}): sorts interfaces with semi and comments on the same line`, () => {
ruleTester.run(RULE_NAME, rule, {
valid: [],
invalid: [
{
code: dedent`
interface Researcher {
occupation: string; // Professional direction
rescuer: 'Tokita Kōsaku'; // Character
}
`,
output: dedent`
interface Researcher {
rescuer: 'Tokita Kōsaku'; // Character
occupation: string; // Professional direction
}
`,
options: [
{
type: SortType['line-length'],
order: SortOrder.desc,
},
],
errors: [
{
messageId: 'unexpectedInterfacePropertiesOrder',
data: {
left: 'occupation',
right: 'rescuer',
},
},
],
},
],
})
})
})

describe(`${RULE_NAME}: misc`, () => {
Expand Down
4 changes: 1 addition & 3 deletions utils/get-node-range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ export let getNodeRange = (
count: 2,
})

if (node.loc.start.line !== tokensAfter.at(1)?.loc.start.line) {
end += 1
} else {
if (node.loc.start.line === tokensAfter.at(1)?.loc.start.line) {
end -= 1
}
}
Expand Down
1 change: 1 addition & 0 deletions utils/make-fixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ export let makeFixes = (
)
}
})

return fixes
}

0 comments on commit 03e5508

Please sign in to comment.