Skip to content

Commit

Permalink
fix: improve sort-imports fix function
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Jun 7, 2023
1 parent a4d30d1 commit e7a39f2
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions rules/sort-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,13 @@ export default createEslintRule<Options, MESSAGE_ID>({
return linesBetweenImports.filter(line => !line.trim().length).length
}

let fix = (fixer: TSESLint.RuleFixer): TSESLint.RuleFix[] => {
let fix = (
fixer: TSESLint.RuleFixer,
nodesToFix: SortingNodeWithGroup[],
): TSESLint.RuleFix[] => {
let fixes: TSESLint.RuleFix[] = []

let grouped = nodes.reduce(
let grouped = nodesToFix.reduce(
(
accumulator: {
[key: string]: SortingNodeWithGroup[]
Expand Down Expand Up @@ -329,7 +332,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
formatted.forEach((node, i) => {
fixes.push(
fixer.replaceTextRange(
getNodeRange(nodes.at(i)!.node, source),
getNodeRange(nodesToFix.at(i)!.node, source),
source.text.slice(...getNodeRange(node.node, source)),
),
)
Expand All @@ -339,8 +342,8 @@ export default createEslintRule<Options, MESSAGE_ID>({

if (nextNode) {
let linesBetweenImports = getLinesBetweenImports(
nodes.at(i)!,
nodes.at(i + 1)!,
nodesToFix.at(i)!,
nodesToFix.at(i + 1)!,
)

if (
Expand All @@ -352,8 +355,9 @@ export default createEslintRule<Options, MESSAGE_ID>({
) {
fixes.push(
fixer.removeRange([
getNodeRange(nodes.at(i)!.node, source).at(1)!,
getNodeRange(nodes.at(i + 1)!.node, source).at(0)! - 1,
getNodeRange(nodesToFix.at(i)!.node, source).at(1)!,
getNodeRange(nodesToFix.at(i + 1)!.node, source).at(0)! -
1,
]),
)
}
Expand All @@ -365,8 +369,10 @@ export default createEslintRule<Options, MESSAGE_ID>({
fixes.push(
fixer.replaceTextRange(
[
getNodeRange(nodes.at(i)!.node, source).at(1)!,
getNodeRange(nodes.at(i + 1)!.node, source).at(0)! - 1,
getNodeRange(nodesToFix.at(i)!.node, source).at(1)!,
getNodeRange(nodesToFix.at(i + 1)!.node, source).at(
0,
)! - 1,
],
'\n',
),
Expand All @@ -380,7 +386,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
) {
fixes.push(
fixer.insertTextAfterRange(
getNodeRange(nodes.at(i)!.node, source),
getNodeRange(nodesToFix.at(i)!.node, source),
'\n',
),
)
Expand Down Expand Up @@ -429,7 +435,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
right: right.name,
},
node: right.node,
fix,
fix: fixer => fix(fixer, nodeList),
})
}

Expand All @@ -444,7 +450,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
right: right.name,
},
node: right.node,
fix,
fix: fixer => fix(fixer, nodeList),
})
}

Expand All @@ -457,7 +463,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
right: right.name,
},
node: right.node,
fix,
fix: fixer => fix(fixer, nodeList),
})
} else if (
numberOfEmptyLinesBetween > 1 ||
Expand All @@ -470,7 +476,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
right: right.name,
},
node: right.node,
fix,
fix: fixer => fix(fixer, nodeList),
})
}
}
Expand Down

0 comments on commit e7a39f2

Please sign in to comment.