eslint-plugin-sort-destructure-keys
Advanced tools
Comparing version 1.0.0 to 1.0.1
module.exports.rules = { | ||
'sort-destructure-keys': require('rules/sort-destructure-keys') | ||
'sort-destructure-keys': require('./rules/sort-destructure-keys') | ||
}; |
const naturalCompare = require('natural-compare-lite'); | ||
function isRestProperty({type}) { | ||
return type === 'RestElement' || | ||
type === 'RestProperty' || | ||
type === 'ExperimentalRestProperty'; | ||
} | ||
/** | ||
* Returns whether or not a `Property` is safe | ||
* Returns whether or not a node is safe | ||
* to be sorted. | ||
*/ | ||
function shouldCheck({value}) { | ||
return value.type === 'Identifier' || | ||
function shouldCheck(node) { | ||
const {value} = node; | ||
return isRestProperty(node) || | ||
value.type === 'Identifier' || | ||
value.type === 'ObjectPattern' || | ||
@@ -21,2 +30,5 @@ value.type === 'AssignmentPattern' && | ||
}, | ||
messages: { | ||
sort: `Expected object keys to be in sorted order. Expected {{first}} to be before {{second}}.` | ||
}, | ||
schema: [] | ||
@@ -41,2 +53,3 @@ }, | ||
if (prevNode && | ||
!isRestProperty(nextNode) && | ||
naturalCompare(prevNode.key.name, nextNode.key.name) > 0 | ||
@@ -46,3 +59,7 @@ ) { | ||
node: nextNode, | ||
message: `Expected object keys to be in sorted order. Expected ${nextNode.key.name} to be before ${prevNode.key.name}.` | ||
messageId: 'sort', | ||
data: { | ||
first: nextNode.key.name, | ||
second: prevNode.key.name | ||
} | ||
}); | ||
@@ -49,0 +66,0 @@ |
{ | ||
"name": "eslint-plugin-sort-destructure-keys", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "require object destructure key to be sorted", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
const rule = require('../../../lib/rules/sort-destructure-keys'); | ||
const RuleTester = require('eslint').RuleTester; | ||
function msg(before, after) { | ||
return `Expected object keys to be in sorted order. Expected ${after} to be before ${before}.`; | ||
function msg(second, first) { | ||
return { | ||
messageId: 'sort', | ||
data: {first, second} | ||
}; | ||
} | ||
@@ -14,3 +17,3 @@ | ||
parserOptions: { | ||
ecmaVersion: 2015 | ||
ecmaVersion: 2018 | ||
} | ||
@@ -21,4 +24,10 @@ }); | ||
valid: [ | ||
'const {owner, ...userRoleNames} = FaroConstants.userRoleNames;', | ||
'const {a, b} = someObj;', | ||
'const {b, a = b} = someObj;' | ||
'const {a: foo, b} = someObj;', | ||
'const {b, a = b} = someObj;', | ||
'const {a = {}, b = {}} = someObj;', | ||
'const {a, b, ...other} = someObj;', | ||
'const {...other} = someObj;', | ||
'const func = ({a, b}) => a + b;' | ||
], | ||
@@ -31,2 +40,6 @@ invalid: [ | ||
{ | ||
code: 'const func = ({b, a}) => a + b;', | ||
errors: just('b', 'a') | ||
}, | ||
{ | ||
code: 'const {a, c, b} = someObj;', | ||
@@ -33,0 +46,0 @@ errors: just('c', 'b') |
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
6556
128