Skip to content

Commit

Permalink
refactor: replace first and second with left and right
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Jun 6, 2023
1 parent 16887ea commit 0abffbe
Show file tree
Hide file tree
Showing 25 changed files with 798 additions and 802 deletions.
20 changes: 10 additions & 10 deletions rules/sort-array-includes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
],
messages: {
unexpectedArrayIncludesOrder:
'Expected "{{second}}" to come before "{{first}}"',
'Expected "{{right}}" to come before "{{left}}"',
},
},
defaultOptions: [
Expand Down Expand Up @@ -121,33 +121,33 @@ export default createEslintRule<Options, MESSAGE_ID>({
)
.flat()

pairwise(nodes, (first, second) => {
pairwise(nodes, (left, right) => {
let compareValue: boolean

if (
options['spread-last'] &&
first.node.type === AST_NODE_TYPES.Literal &&
second.node.type === AST_NODE_TYPES.SpreadElement
left.node.type === AST_NODE_TYPES.Literal &&
right.node.type === AST_NODE_TYPES.SpreadElement
) {
compareValue = false
} else if (
options['spread-last'] &&
first.node.type === AST_NODE_TYPES.SpreadElement &&
second.node.type === AST_NODE_TYPES.Literal
left.node.type === AST_NODE_TYPES.SpreadElement &&
right.node.type === AST_NODE_TYPES.Literal
) {
compareValue = true
} else {
compareValue = compare(first, second, options)
compareValue = compare(left, right, options)
}

if (compareValue) {
context.report({
messageId: 'unexpectedArrayIncludesOrder',
data: {
first: toSingleLine(first.name),
second: toSingleLine(second.name),
left: toSingleLine(left.name),
right: toSingleLine(right.name),
},
node: second.node,
node: right.node,
fix: fixer => {
let sortedNodes = sortNodes(nodes, options)

Expand Down
19 changes: 9 additions & 10 deletions rules/sort-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
},
],
messages: {
unexpectedClassesOrder:
'Expected "{{second}}" to come before "{{first}}"',
unexpectedClassesOrder: 'Expected "{{right}}" to come before "{{left}}"',
},
},
defaultOptions: [
Expand Down Expand Up @@ -172,21 +171,21 @@ export default createEslintRule<Options, MESSAGE_ID>({
return options.groups.length
}

pairwise(nodes, (first, second) => {
let firstNum = getGroupNumber(first)
let secondNum = getGroupNumber(second)
pairwise(nodes, (left, right) => {
let leftNum = getGroupNumber(left)
let rightNum = getGroupNumber(right)

if (
firstNum > secondNum ||
(firstNum === secondNum && compare(first, second, options))
leftNum > rightNum ||
(leftNum === rightNum && compare(left, right, options))
) {
context.report({
messageId: 'unexpectedClassesOrder',
data: {
first: toSingleLine(first.name),
second: toSingleLine(second.name),
left: toSingleLine(left.name),
right: toSingleLine(right.name),
},
node: second.node,
node: right.node,
fix: (fixer: TSESLint.RuleFixer) => {
let fixes: TSESLint.RuleFix[] = []

Expand Down
12 changes: 6 additions & 6 deletions rules/sort-enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
},
],
messages: {
unexpectedEnumsOrder: 'Expected "{{second}}" to come before "{{first}}"',
unexpectedEnumsOrder: 'Expected "{{right}}" to come before "{{left}}"',
},
},
defaultOptions: [
Expand Down Expand Up @@ -87,15 +87,15 @@ export default createEslintRule<Options, MESSAGE_ID>({
node: member,
}))

pairwise(nodes, (first, second) => {
if (compare(first, second, options)) {
pairwise(nodes, (left, right) => {
if (compare(left, right, options)) {
context.report({
messageId: 'unexpectedEnumsOrder',
data: {
first: toSingleLine(first.name),
second: toSingleLine(second.name),
left: toSingleLine(left.name),
right: toSingleLine(right.name),
},
node: second.node,
node: right.node,
fix: fixer =>
makeFixes(fixer, nodes, sortNodes(nodes, options), source),
})
Expand Down
49 changes: 24 additions & 25 deletions rules/sort-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,11 @@ export default createEslintRule<Options, MESSAGE_ID>({
},
],
messages: {
unexpectedImportsOrder:
'Expected "{{second}}" to come before "{{first}}"',
unexpectedImportsOrder: 'Expected "{{right}}" to come before "{{left}}"',
missedSpacingBetweenImports:
'Missed spacing between "{{first}}" and "{{second}}" imports',
'Missed spacing between "{{left}}" and "{{right}}" imports',
extraSpacingBetweenImports:
'Extra spacing between "{{first}}" and "{{second}}" imports',
'Extra spacing between "{{left}}" and "{{right}}" imports',
},
},
defaultOptions: [
Expand Down Expand Up @@ -397,24 +396,24 @@ export default createEslintRule<Options, MESSAGE_ID>({
return fixes
}

pairwise(nodes, (first, second) => {
let firstNum = getGroupNumber(first)
let secondNum = getGroupNumber(second)
pairwise(nodes, (left, right) => {
let leftNum = getGroupNumber(left)
let rightNum = getGroupNumber(right)

let numberOfEmptyLinesBetween = getLinesBetweenImports(first, second)
let numberOfEmptyLinesBetween = getLinesBetweenImports(left, right)

if (
!hasContentBetweenNodes(first, second) &&
(firstNum > secondNum ||
(firstNum === secondNum && compare(first, second, options)))
!hasContentBetweenNodes(left, right) &&
(leftNum > rightNum ||
(leftNum === rightNum && compare(left, right, options)))
) {
context.report({
messageId: 'unexpectedImportsOrder',
data: {
first: first.name,
second: second.name,
left: left.name,
right: right.name,
},
node: second.node,
node: right.node,
fix,
})
}
Expand All @@ -426,36 +425,36 @@ export default createEslintRule<Options, MESSAGE_ID>({
context.report({
messageId: 'extraSpacingBetweenImports',
data: {
first: first.name,
second: second.name,
left: left.name,
right: right.name,
},
node: second.node,
node: right.node,
fix,
})
}

if (options['newlines-between'] === 'always') {
if (firstNum < secondNum && numberOfEmptyLinesBetween === 0) {
if (leftNum < rightNum && numberOfEmptyLinesBetween === 0) {
context.report({
messageId: 'missedSpacingBetweenImports',
data: {
first: first.name,
second: second.name,
left: left.name,
right: right.name,
},
node: second.node,
node: right.node,
fix,
})
} else if (
numberOfEmptyLinesBetween > 1 ||
(firstNum === secondNum && numberOfEmptyLinesBetween > 0)
(leftNum === rightNum && numberOfEmptyLinesBetween > 0)
) {
context.report({
messageId: 'extraSpacingBetweenImports',
data: {
first: first.name,
second: second.name,
left: left.name,
right: right.name,
},
node: second.node,
node: right.node,
fix,
})
}
Expand Down
12 changes: 6 additions & 6 deletions rules/sort-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
],
messages: {
unexpectedInterfacePropertiesOrder:
'Expected "{{second}}" to come before "{{first}}"',
'Expected "{{right}}" to come before "{{left}}"',
},
},
defaultOptions: [
Expand Down Expand Up @@ -125,15 +125,15 @@ export default createEslintRule<Options, MESSAGE_ID>({
}
})

pairwise(nodes, (first, second) => {
if (compare(first, second, options)) {
pairwise(nodes, (left, right) => {
if (compare(left, right, options)) {
context.report({
messageId: 'unexpectedInterfacePropertiesOrder',
data: {
first: toSingleLine(first.name),
second: toSingleLine(second.name),
left: toSingleLine(left.name),
right: toSingleLine(right.name),
},
node: second.node,
node: right.node,
fix: fixer =>
makeFixes(fixer, nodes, sortNodes(nodes, options), source),
})
Expand Down
25 changes: 12 additions & 13 deletions rules/sort-jsx-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
},
],
messages: {
unexpectedJSXPropsOrder:
'Expected "{{second}}" to come before "{{first}}"',
unexpectedJSXPropsOrder: 'Expected "{{right}}" to come before "{{left}}"',
},
},
defaultOptions: [
Expand Down Expand Up @@ -169,18 +168,18 @@ export default createEslintRule<Options, MESSAGE_ID>({
)

parts.forEach(nodes => {
pairwise(nodes, (first, second) => {
pairwise(nodes, (left, right) => {
let comparison: boolean

if (
first.position === Position.exception &&
second.position === Position.exception
left.position === Position.exception &&
right.position === Position.exception
) {
comparison =
options['always-on-top'].indexOf(first.name) >
options['always-on-top'].indexOf(second.name)
} else if (first.position === second.position) {
comparison = compare(first, second, options)
options['always-on-top'].indexOf(left.name) >
options['always-on-top'].indexOf(right.name)
} else if (left.position === right.position) {
comparison = compare(left, right, options)
} else {
let positionPower = {
[Position.exception]: 2,
Expand All @@ -190,17 +189,17 @@ export default createEslintRule<Options, MESSAGE_ID>({
}

comparison =
positionPower[first.position] < positionPower[second.position]
positionPower[left.position] < positionPower[right.position]
}

if (comparison) {
context.report({
messageId: 'unexpectedJSXPropsOrder',
data: {
first: first.name,
second: second.name,
left: left.name,
right: right.name,
},
node: second.node,
node: right.node,
fix: fixer => {
let groups = groupBy(nodes, ({ position }) => position)

Expand Down
24 changes: 12 additions & 12 deletions rules/sort-map-elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
],
messages: {
unexpectedMapElementsOrder:
'Expected "{{second}}" to come before "{{first}}"',
'Expected "{{right}}" to come before "{{left}}"',
},
},
defaultOptions: [
Expand Down Expand Up @@ -111,14 +111,14 @@ export default createEslintRule<Options, MESSAGE_ID>({
let name: string

if (element.type === AST_NODE_TYPES.ArrayExpression) {
let [first] = element.elements
let [left] = element.elements

if (!first) {
name = `${first}`
} else if (first.type === AST_NODE_TYPES.Literal) {
name = first.raw
if (!left) {
name = `${left}`
} else if (left.type === AST_NODE_TYPES.Literal) {
name = left.raw
} else {
name = source.text.slice(...first.range)
name = source.text.slice(...left.range)
}
} else {
name = source.text.slice(...element.range)
Expand All @@ -131,15 +131,15 @@ export default createEslintRule<Options, MESSAGE_ID>({
}
})

pairwise(nodes, (first, second) => {
if (compare(first, second, options)) {
pairwise(nodes, (left, right) => {
if (compare(left, right, options)) {
context.report({
messageId: 'unexpectedMapElementsOrder',
data: {
first: toSingleLine(first.name),
second: toSingleLine(second.name),
left: toSingleLine(left.name),
right: toSingleLine(right.name),
},
node: second.node,
node: right.node,
fix: fixer =>
makeFixes(fixer, nodes, sortNodes(nodes, options), source),
})
Expand Down
12 changes: 6 additions & 6 deletions rules/sort-named-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
],
messages: {
unexpectedNamedExportsOrder:
'Expected "{{second}}" to come before "{{first}}"',
'Expected "{{right}}" to come before "{{left}}"',
},
},
defaultOptions: [
Expand All @@ -82,15 +82,15 @@ export default createEslintRule<Options, MESSAGE_ID>({
node: specifier,
}))

pairwise(nodes, (first, second) => {
if (compare(first, second, options)) {
pairwise(nodes, (left, right) => {
if (compare(left, right, options)) {
context.report({
messageId: 'unexpectedNamedExportsOrder',
data: {
first: first.name,
second: second.name,
left: left.name,
right: right.name,
},
node: second.node,
node: right.node,
fix: fixer =>
makeFixes(fixer, nodes, sortNodes(nodes, options), source),
})
Expand Down
Loading

0 comments on commit 0abffbe

Please sign in to comment.