Skip to content

Commit

Permalink
fix: fix single line type objects sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Jun 4, 2023
1 parent 11744b1 commit aaa446a
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 6 deletions.
6 changes: 5 additions & 1 deletion rules/sort-object-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export default createEslintRule<Options, MESSAGE_ID>({

let nodes: SortingNode[] = node.members.map(member => {
let name: string
let raw = source.text.slice(member.range.at(0), member.range.at(1))

if (member.type === AST_NODE_TYPES.TSPropertySignature) {
if (member.key.type === AST_NODE_TYPES.Identifier) {
Expand All @@ -102,8 +103,11 @@ export default createEslintRule<Options, MESSAGE_ID>({
name = source.text.slice(member.range.at(0), member.range.at(1))
}

let endsWithComma = raw.endsWith(';') || raw.endsWith(',')
let endSize = endsWithComma ? 1 : 0

return {
size: rangeToDiff(member.range),
size: rangeToDiff(member.range) - endSize,
node: member,
name,
}
Expand Down
10 changes: 5 additions & 5 deletions test/sort-interfaces.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ describe(RULE_NAME, () => {
interface DossierByTwilight {
name: string
age: string
country: 'Westalis' | 'Ostania',
country: 'Westalis' | 'Ostania'
}
`,
output: dedent`
interface DossierByTwilight {
age: string
country: 'Westalis' | 'Ostania',
country: 'Westalis' | 'Ostania'
name: string
}
`,
Expand Down Expand Up @@ -462,7 +462,7 @@ describe(RULE_NAME, () => {
code: dedent`
interface DossierByTwilight {
age: string
country: 'Westalis' | 'Ostania',
country: 'Westalis' | 'Ostania'
name: string
}
`,
Expand All @@ -480,13 +480,13 @@ describe(RULE_NAME, () => {
interface DossierByTwilight {
name: string
age: string
country: 'Westalis' | 'Ostania',
country: 'Westalis' | 'Ostania'
}
`,
output: dedent`
interface DossierByTwilight {
age: string
country: 'Westalis' | 'Ostania',
country: 'Westalis' | 'Ostania'
name: string
}
`,
Expand Down
129 changes: 129 additions & 0 deletions test/sort-object-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,49 @@ describe(RULE_NAME, () => {
],
})
})

it(`${RULE_NAME}(${type}): sorts inline type members`, () => {
ruleTester.run(RULE_NAME, rule, {
valid: [
{
code: dedent`
addToDeathNote<{ name: string; reasonOfDeath: string }>(/* ... */)
`,
options: [
{
type: SortType.alphabetical,
order: SortOrder.asc,
},
],
},
],
invalid: [
{
code: dedent`
addToDeathNote<{ reasonOfDeath: string; name: string }>(/* ... */)
`,
output: dedent`
addToDeathNote<{ name: string; reasonOfDeath: string }>(/* ... */)
`,
options: [
{
type: SortType.alphabetical,
order: SortOrder.asc,
},
],
errors: [
{
messageId: 'unexpectedObjectTypesOrder',
data: {
first: 'reasonOfDeath',
second: 'name',
},
},
],
},
],
})
})
})

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

it(`${RULE_NAME}(${type}): sorts inline type members`, () => {
ruleTester.run(RULE_NAME, rule, {
valid: [
{
code: dedent`
addToDeathNote<{ name: string; reasonOfDeath: string }>(/* ... */)
`,
options: [
{
type: SortType.natural,
order: SortOrder.asc,
},
],
},
],
invalid: [
{
code: dedent`
addToDeathNote<{ reasonOfDeath: string; name: string }>(/* ... */)
`,
output: dedent`
addToDeathNote<{ name: string; reasonOfDeath: string }>(/* ... */)
`,
options: [
{
type: SortType.natural,
order: SortOrder.asc,
},
],
errors: [
{
messageId: 'unexpectedObjectTypesOrder',
data: {
first: 'reasonOfDeath',
second: 'name',
},
},
],
},
],
})
})
})

describe(`${RULE_NAME}: sorting by line length`, () => {
Expand Down Expand Up @@ -808,5 +894,48 @@ describe(RULE_NAME, () => {
],
})
})

it(`${RULE_NAME}(${type}): sorts inline type members`, () => {
ruleTester.run(RULE_NAME, rule, {
valid: [
{
code: dedent`
addToDeathNote<{ reasonOfDeath: string; name: string }>(/* ... */)
`,
options: [
{
type: SortType['line-length'],
order: SortOrder.desc,
},
],
},
],
invalid: [
{
code: dedent`
addToDeathNote<{ name: string; reasonOfDeath: string }>(/* ... */)
`,
output: dedent`
addToDeathNote<{ reasonOfDeath: string; name: string }>(/* ... */)
`,
options: [
{
type: SortType['line-length'],
order: SortOrder.desc,
},
],
errors: [
{
messageId: 'unexpectedObjectTypesOrder',
data: {
first: 'name',
second: 'reasonOfDeath',
},
},
],
},
],
})
})
})
})
6 changes: 6 additions & 0 deletions utils/get-node-range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export let getNodeRange = (
let start = node.range.at(0)!
let end = node.range.at(1)!

let raw = sourceCode.text.slice(start, end)

if (ASTUtils.isParenthesized(node, sourceCode)) {
let bodyOpeningParen = sourceCode.getTokenBefore(
node,
Expand All @@ -29,6 +31,10 @@ export let getNodeRange = (

let comment = getComment(node, sourceCode)

if (raw.endsWith(';') || raw.endsWith(',')) {
end -= 1
}

if (comment) {
start = comment.range.at(0)!
}
Expand Down

0 comments on commit aaa446a

Please sign in to comment.