eslint-plugin-sort-destructure-keys
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -37,2 +37,49 @@ const naturalCompare = require('natural-compare-lite'); | ||
/** | ||
* Returns a function that will return the appropriate string | ||
* to sort the name by. | ||
*/ | ||
function createSortName(caseSensitive) { | ||
return a => caseSensitive ? a : a.toLowerCase(); | ||
} | ||
/** | ||
* Creates a "fixer" function to be used by `--fix`. | ||
*/ | ||
function createFix({context, fixer, node, caseSensitive}) { | ||
const sortName = createSortName(caseSensitive); | ||
const sourceCode = context.getSourceCode(); | ||
const sourceText = sourceCode.getText(); | ||
const sorted = node.properties | ||
.concat() | ||
.sort((a, b) => naturalCompare( | ||
sortName(a.key.name), | ||
sortName(b.key.name) | ||
)) | ||
const newText = sorted | ||
.map((child, i) => { | ||
const textAfter = i === sorted.length - 1 | ||
// If it's the last item, there's no text after to append. | ||
? '' | ||
// Otherwise, we need to grab the text after the original node. | ||
: sourceText.slice( | ||
node.properties[i].range[1], // End index of the current node . | ||
node.properties[i + 1].range[0] // Start index of the next node. | ||
); | ||
return sourceCode.getText(child) + textAfter; | ||
}) | ||
.join(''); | ||
return fixer.replaceTextRange( | ||
[ | ||
node.properties[0].range[0], // Start index of the first node. | ||
node.properties[node.properties.length -1].range[1] // End index of the last node. | ||
], | ||
newText | ||
) | ||
} | ||
module.exports = { | ||
@@ -45,2 +92,3 @@ meta: { | ||
}, | ||
fixable: 'code', | ||
messages: { | ||
@@ -63,5 +111,9 @@ sort: `Expected object keys to be in sorted order. Expected {{first}} to be before {{second}}.` | ||
create(context) { | ||
const options = context.options[0] || {}; | ||
const {caseSensitive = true} = options; | ||
const sortName = createSortName(caseSensitive); | ||
return { | ||
ObjectPattern(node) { | ||
/** | ||
/* | ||
* If the node is more complex than just basic destructuring | ||
@@ -75,4 +127,2 @@ * with literal defaults, we just skip it. If some values use | ||
const options = context.options[0] || {}; | ||
const caseSensitive = options.caseSensitive || false; | ||
let prevNode = null; | ||
@@ -84,4 +134,4 @@ | ||
const nextName = getNodeName(nextNode.key); | ||
const first = caseSensitive ? prevName : prevName.toLowerCase(); | ||
const second = caseSensitive ? nextName : nextName.toLowerCase(); | ||
const first = sortName(prevName); | ||
const second = sortName(nextName); | ||
@@ -95,3 +145,9 @@ if (naturalCompare(first, second) > 0) { | ||
second: prevName | ||
} | ||
}, | ||
fix: fixer => createFix({ | ||
context, | ||
caseSensitive, | ||
fixer, | ||
node | ||
}) | ||
}); | ||
@@ -98,0 +154,0 @@ |
{ | ||
"name": "eslint-plugin-sort-destructure-keys", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "require object destructure key to be sorted", | ||
@@ -5,0 +5,0 @@ "repository": "https://github.com/mthadley/eslint-plugin-sort-destructure-keys", |
@@ -79,1 +79,15 @@ # eslint-plugin-sort-destructure-keys | ||
``` | ||
## Changelog | ||
### `1.3.0` | ||
- Add support for `--fix` eslint cli flag | ||
### `1.2.0` | ||
- Add peer dependenct support for eslint `^5.0.0`. | ||
### `1.1.0` | ||
- Add `caseSensitive` option. |
@@ -23,2 +23,8 @@ const rule = require('../../../lib/rules/sort-destructure-keys'); | ||
valid: [ | ||
` | ||
const { | ||
a, | ||
b | ||
} = someObj; | ||
`, | ||
'const {owner, ...userRoleNames} = FaroConstants.userRoleNames;', | ||
@@ -50,32 +56,95 @@ 'const {a, b} = someObj;', | ||
{ | ||
code: ` | ||
const { | ||
b, | ||
a | ||
} = someObj; | ||
`, | ||
errors: just('b', 'a'), | ||
output: ` | ||
const { | ||
a, | ||
b | ||
} = someObj; | ||
` | ||
}, | ||
{ | ||
code: ` | ||
const { | ||
b, | ||
a = 3, | ||
c | ||
} = someObj; | ||
`, | ||
errors: just('b', 'a'), | ||
output: ` | ||
const { | ||
a = 3, | ||
b, | ||
c | ||
} = someObj; | ||
` | ||
}, | ||
{ | ||
code: ` | ||
const { | ||
a, | ||
b: { | ||
e, | ||
d | ||
}, | ||
c | ||
} = someObj; | ||
`, | ||
errors: just('e', 'd'), | ||
output: ` | ||
const { | ||
a, | ||
b: { | ||
d, | ||
e | ||
}, | ||
c | ||
} = someObj; | ||
` | ||
}, | ||
{ | ||
code: 'const {b, a} = someObj;', | ||
errors: just('b', 'a') | ||
errors: just('b', 'a'), | ||
output: 'const {a, b} = someObj;' | ||
}, | ||
{ | ||
code: 'const func = ({b, a}) => a + b;', | ||
errors: just('b', 'a') | ||
errors: just('b', 'a'), | ||
output: 'const func = ({a, b}) => a + b;', | ||
}, | ||
{ | ||
code: 'const {a, c, b} = someObj;', | ||
errors: just('c', 'b') | ||
errors: just('c', 'b'), | ||
output: 'const {a, b, c} = someObj;' | ||
}, | ||
{ | ||
code: 'const {a, c, b = 3} = someObj;', | ||
errors: just('c', 'b') | ||
errors: just('c', 'b'), | ||
output: 'const {a, b = 3, c} = someObj;' | ||
}, | ||
{ | ||
code: 'const {a, b, c: {e, d}} = someObj;', | ||
errors: just('e', 'd') | ||
errors: just('e', 'd'), | ||
output: 'const {a, b, c: {d, e}} = someObj;' | ||
}, | ||
{ | ||
code: 'const {a, c: {e, d}, b} = someObj;', | ||
errors: [msg('e', 'd'), msg('c', 'b')] | ||
errors: [msg('e', 'd'), msg('c', 'b')], | ||
output: 'const {a, b, c: {e, d}} = someObj;' | ||
}, | ||
{ | ||
code: 'const {a, c: {e, d = e}, b} = someObj;', | ||
errors: [msg('c', 'b')] | ||
errors: [msg('c', 'b')], | ||
output: 'const {a, b, c: {e, d = e}} = someObj;' | ||
}, | ||
{ | ||
code: 'const {a, c: {e, d}, b = c} = someObj;', | ||
errors: [msg('e', 'd')] | ||
errors: [msg('e', 'd')], | ||
output: 'const {a, c: {d, e}, b = c} = someObj;' | ||
}, | ||
@@ -85,2 +154,3 @@ { | ||
errors: just('b', 'a'), | ||
output: 'const {a, b} = someObj;', | ||
options: [{ caseSensitive: true }] | ||
@@ -91,2 +161,3 @@ }, | ||
errors: just('a', 'B'), | ||
output: 'const {B, a} = someObj;', | ||
options: [{ caseSensitive: true }] | ||
@@ -97,2 +168,3 @@ }, | ||
errors: just('abc', 'aBd'), | ||
output: 'const {aBd, abc} = someObj;', | ||
options: [{ caseSensitive: true }] | ||
@@ -99,0 +171,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13092
307
93