Skip to content

Commit

Permalink
fix: Improve ending commas and semicolon behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
hugop95 authored and azat-io committed Nov 19, 2024
1 parent e2954de commit a3804b8
Show file tree
Hide file tree
Showing 38 changed files with 1,296 additions and 102 deletions.
4 changes: 2 additions & 2 deletions rules/sort-array-includes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ export let sortArray = <MessageIds extends string>(
name:
element.type === 'Literal'
? `${element.value}`
: sourceCode.text.slice(...element.range),
size: rangeToDiff(element.range),
: sourceCode.getText(element),
size: rangeToDiff(element, sourceCode),
node: element,
group,
}
Expand Down
13 changes: 9 additions & 4 deletions rules/sort-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ export default createEslintRule<SortClassesOptions, MESSAGE_ID>({
} else if (member.key.type === 'Identifier') {
;({ name } = member.key)
} else {
name = sourceCode.text.slice(...member.key.range)
name = sourceCode.getText(member.key)
}

let isPrivateHash =
Expand All @@ -410,6 +410,7 @@ export default createEslintRule<SortClassesOptions, MESSAGE_ID>({
let memberValue: undefined | string
let modifiers: Modifier[] = []
let selectors: Selector[] = []
let addSafetySemicolonWhenInline: boolean = true
if (
member.type === 'MethodDefinition' ||
member.type === 'TSAbstractMethodDefinition'
Expand All @@ -424,6 +425,8 @@ export default createEslintRule<SortClassesOptions, MESSAGE_ID>({
}
if (member.type === 'TSAbstractMethodDefinition') {
modifiers.push('abstract')
} else {
addSafetySemicolonWhenInline = false
}

if (decorated) {
Expand Down Expand Up @@ -469,6 +472,8 @@ export default createEslintRule<SortClassesOptions, MESSAGE_ID>({

selectors.push('index-signature')
} else if (member.type === 'StaticBlock') {
addSafetySemicolonWhenInline = false

selectors.push('static-block')

dependencies = extractDependencies(member, true)
Expand Down Expand Up @@ -502,7 +507,6 @@ export default createEslintRule<SortClassesOptions, MESSAGE_ID>({
selectors.push('accessor-property')
} else {
// Member is necessarily a Property

// Similarly to above for methods, prioritize 'static', 'declare', 'decorated', 'abstract', 'override' and 'readonly'
// over accessibility modifiers
if (member.static) {
Expand Down Expand Up @@ -598,12 +602,13 @@ export default createEslintRule<SortClassesOptions, MESSAGE_ID>({

let sortingNode: SortingNodeWithDependencies = {
size: overloadSignatureGroupMember
? rangeToDiff(overloadSignatureGroupMember.range)
: rangeToDiff(member.range),
? rangeToDiff(overloadSignatureGroupMember, sourceCode)
: rangeToDiff(member, sourceCode),
group: getGroup(),
node: member,
dependencies,
name,
addSafetySemicolonWhenInline,
dependencyName: getDependencyName(
name,
modifiers.includes('static'),
Expand Down
2 changes: 1 addition & 1 deletion rules/sort-decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ let sortDecorators = (
setCustomGroups(options.customGroups, name)

let sortingNode: SortDecoratorsSortingNode = {
size: rangeToDiff(decorator.range),
size: rangeToDiff(decorator, sourceCode),
node: decorator,
group: getGroup(),
name,
Expand Down
4 changes: 2 additions & 2 deletions rules/sort-enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,13 @@ export default createEslintRule<Options, MESSAGE_ID>({
}
let lastSortingNode = accumulator.at(-1)?.at(-1)
let sortingNode: SortingNodeWithDependencies = {
size: rangeToDiff(member.range),
size: rangeToDiff(member, sourceCode),
node: member,
dependencies,
name:
member.id.type === 'Literal'
? `${member.id.value}`
: `${sourceCode.text.slice(...member.id.range)}`,
: `${sourceCode.getText(member.id)}`,
}

if (
Expand Down
3 changes: 2 additions & 1 deletion rules/sort-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,10 @@ export default createEslintRule<Options, MESSAGE_ID>({
| TSESTree.ExportAllDeclaration,
) => {
let sortingNode: SortExportsSortingNode = {
size: rangeToDiff(node.range),
size: rangeToDiff(node, sourceCode),
name: node.source.value,
node,
addSafetySemicolonWhenInline: true,
}
let lastNode = parts.at(-1)?.at(-1)
if (
Expand Down
2 changes: 1 addition & 1 deletion rules/sort-heritage-clauses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ const sortHeritageClauses = (
setCustomGroups(options.customGroups, name)

return {
size: rangeToDiff(heritageClause.range),
size: rangeToDiff(heritageClause, sourceCode),
node: heritageClause,
group: getGroup(),
name,
Expand Down
5 changes: 3 additions & 2 deletions rules/sort-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
if (node.moduleReference.type === 'TSExternalModuleReference') {
name = `${node.moduleReference.expression.value}`
} else {
name = sourceCode.text.slice(...node.moduleReference.range)
name = sourceCode.getText(node.moduleReference)
}
} else {
let decl = node.declarations[0].init as TSESTree.CallExpression
Expand Down Expand Up @@ -515,9 +515,10 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
}

nodes.push({
size: rangeToDiff(node.range),
size: rangeToDiff(node, sourceCode),
group: getGroup(),
node,
addSafetySemicolonWhenInline: true,
isIgnored:
!options.sortSideEffects &&
isSideEffect &&
Expand Down
3 changes: 2 additions & 1 deletion rules/sort-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,11 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
}

let elementSortingNode: SortingNode = {
size: rangeToDiff(element.range),
size: rangeToDiff(element, sourceCode),
node: element,
group: getGroup(),
name,
addSafetySemicolonWhenInline: true,
}

if (
Expand Down
4 changes: 2 additions & 2 deletions rules/sort-intersection-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,8 @@ export default createEslintRule<Options, MESSAGE_ID>({

let lastSortingNode = accumulator.at(-1)?.at(-1)
let sortingNode: SortingNode = {
name: sourceCode.text.slice(...type.range),
size: rangeToDiff(type.range),
name: sourceCode.getText(type),
size: rangeToDiff(type, sourceCode),
group: getGroup(),
node: type,
}
Expand Down
5 changes: 3 additions & 2 deletions rules/sort-jsx-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({

let shouldIgnore = false
if (options.ignorePattern.length) {
let tagName = sourceCode.text.slice(...node.openingElement.name.range)
let tagName = sourceCode.getText(node.openingElement.name)
shouldIgnore = options.ignorePattern.some(pattern =>
matches(tagName, pattern, options.matcher),
)
Expand Down Expand Up @@ -202,10 +202,11 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
}

let jsxNode = {
size: rangeToDiff(attribute.range),
size: rangeToDiff(attribute, sourceCode),
group: getGroup(),
node: attribute,
name,
requiresEndingSemicolonOrCommaWhenInline: true,
}

accumulator.at(-1)!.push(jsxNode)
Expand Down
6 changes: 3 additions & 3 deletions rules/sort-maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,15 @@ export default createEslintRule<Options, MESSAGE_ID>({
} else if (left.type === 'Literal') {
name = left.raw
} else {
name = sourceCode.text.slice(...left.range)
name = sourceCode.getText(left)
}
} else {
name = sourceCode.text.slice(...element.range)
name = sourceCode.getText(element)
}

let lastSortingNode = formattedMembers.at(-1)?.at(-1)
let sortingNode: SortingNode = {
size: rangeToDiff(element.range),
size: rangeToDiff(element, sourceCode),
node: element,
name,
}
Expand Down
2 changes: 1 addition & 1 deletion rules/sort-named-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export default createEslintRule<Options, MESSAGE_ID>({

let lastSortingNode = formattedMembers.at(-1)?.at(-1)
let sortingNode: SortingNode = {
size: rangeToDiff(specifier.range),
size: rangeToDiff(specifier, sourceCode),
node: specifier,
group,
name,
Expand Down
2 changes: 1 addition & 1 deletion rules/sort-named-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export default createEslintRule<Options, MESSAGE_ID>({

let lastSortingNode = formattedMembers.at(-1)?.at(-1)
let sortingNode: SortingNode = {
size: rangeToDiff(specifier.range),
size: rangeToDiff(specifier, sourceCode),
node: specifier,
group,
name,
Expand Down
10 changes: 2 additions & 8 deletions rules/sort-object-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,6 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
node.members.reduce(
(accumulator: SortObjectTypesSortingNode[][], member) => {
let name: string
let raw = sourceCode.text.slice(
member.range.at(0),
member.range.at(1),
)
let lastSortingNode = accumulator.at(-1)?.at(-1)

let { getGroup, defineGroup, setCustomGroups } =
Expand Down Expand Up @@ -268,14 +264,12 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
defineGroup('multiline')
}

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

let sortingNode: SortObjectTypesSortingNode = {
size: rangeToDiff(member.range) - endSize,
size: rangeToDiff(member, sourceCode),
group: getGroup(),
node: member,
name,
addSafetySemicolonWhenInline: true,
}

if (
Expand Down
4 changes: 2 additions & 2 deletions rules/sort-objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
} else if (prop.key.type === 'Literal') {
name = `${prop.key.value}`
} else {
name = sourceCode.text.slice(...prop.key.range)
name = sourceCode.getText(prop.key)
}

if (prop.value.type === 'AssignmentPattern') {
Expand All @@ -437,7 +437,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
}

let propSortingNode: SortingNodeWithDependencies = {
size: rangeToDiff(prop.range),
size: rangeToDiff(prop, sourceCode),
node: prop,
group: getGroup(),
dependencies,
Expand Down
5 changes: 3 additions & 2 deletions rules/sort-switch-case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,14 @@ export default createEslintRule<Options, MESSAGE_ID>({
name = 'default'
isDefaultClause = true
} else {
name = sourceCode.text.slice(...caseNode.test.range)
name = sourceCode.getText(caseNode.test)
}

return {
size: rangeToDiff(caseNode.test?.range ?? caseNode.range),
size: rangeToDiff(caseNode.test ?? caseNode, sourceCode),
node: caseNode,
isDefaultClause,
addSafetySemicolonWhenInline: true,
name,
}
},
Expand Down
4 changes: 2 additions & 2 deletions rules/sort-union-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,8 @@ export default createEslintRule<Options, MESSAGE_ID>({

let lastSortingNode = accumulator.at(-1)?.at(-1)
let sortingNode: SortingNode = {
name: sourceCode.text.slice(...type.range),
size: rangeToDiff(type.range),
name: sourceCode.getText(type),
size: rangeToDiff(type, sourceCode),
group: getGroup(),
node: type,
}
Expand Down
2 changes: 1 addition & 1 deletion rules/sort-variable-declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ export default createEslintRule<Options, MESSAGE_ID>({

let lastSortingNode = accumulator.at(-1)?.at(-1)
let sortingNode: SortingNodeWithDependencies = {
size: rangeToDiff(declaration.range),
size: rangeToDiff(declaration, sourceCode),
node: declaration,
dependencies,
name,
Expand Down
54 changes: 54 additions & 0 deletions test/sort-array-includes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,60 @@ describe(ruleName, () => {
invalid: [],
},
)

ruleTester.run(
`${ruleName}(${type}): sorts inline elements correctly`,
rule,
{
valid: [],
invalid: [
{
code: dedent`
[
b, a
].includes(value)
`,
output: dedent`
[
a, b
].includes(value)
`,
options: [options],
errors: [
{
messageId: 'unexpectedArrayIncludesOrder',
data: {
left: 'b',
right: 'a',
},
},
],
},
{
code: dedent`
[
b, a,
].includes(value)
`,
output: dedent`
[
a, b,
].includes(value)
`,
options: [options],
errors: [
{
messageId: 'unexpectedArrayIncludesOrder',
data: {
left: 'b',
right: 'a',
},
},
],
},
],
},
)
})

describe(`${ruleName}: sorting by natural order`, () => {
Expand Down
Loading

0 comments on commit a3804b8

Please sign in to comment.