Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1 #32

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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