Skip to content

Commit

Permalink
Fix #1
Browse files Browse the repository at this point in the history
  • Loading branch information
namnm committed Aug 10, 2021
1 parent 76033d7 commit d09b1dc
Show file tree
Hide file tree
Showing 3 changed files with 1,041 additions and 158 deletions.
37 changes: 35 additions & 2 deletions lib/rules/sort-keys-fix.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,36 @@ module.exports = {
natual: natual ? 'natural ' : '',
},
fix(fixer) {
// Check if already sorted
if (node.parent.__alreadySorted || node.parent.properties.__alreadySorted) {
return []
}
node.parent.__alreadySorted = true
node.parent.properties.__alreadySorted = true
// Split into parts on each spread operator (empty key)
const parts = []
let part = []
node.parent.properties.forEach(p => {
if (!p.key) {
parts.push(part)
part = []
} else {
part.push(p)
}
})
parts.push(part)
// Sort all parts
parts.forEach(part => {
part.sort((p1, p2) =>
(isValidOrder(getPropertyName(p1), getPropertyName(p2)) ? -1 : 1))
})
// Perform fixes
const fixes = []
const sourceCode = context.getSourceCode()
const moveProperty = (fromNode, toNode) => {
if (fromNode === toNode) {
return
}
const prevText = sourceCode.getText(fromNode)
const thisComments = sourceCode.getCommentsBefore(fromNode)
for (const thisComment of thisComments) {
Expand All @@ -185,8 +212,14 @@ module.exports = {
}
fixes.push(fixer.replaceText(toNode, prevText))
}
moveProperty(node, prevNode)
moveProperty(prevNode, node)
let newIndex = 0
parts.forEach(part => {
part.forEach(p => {
moveProperty(p, node.parent.properties[newIndex])
newIndex++
})
newIndex++
})
return fixes
},
})
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"eslint-plugin-prettier": "^3.1.1",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"eslint-plugin-tyrecheck": "https://tycgitlab.tyrecheck.com/leonid.buneev/eslint-plugin-tyrecheck.git",
"eslint-plugin-tyrecheck": "^2.24.0",
"jest": "^25.4.0",
"prettier": "^1.19.1"
},
Expand Down
Loading

0 comments on commit d09b1dc

Please sign in to comment.