Skip to content
Merged
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
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,39 @@ Then configure the rule under the rules section.
}
}
```

## Rule Options

```json
{
"sort-destructure-keys/sort-destructure-keys": [2, {"caseSensitive": false}]
}
```

### `caseSensitive`

When `true` the rule will enforce properties to be in case-sensitive order. Default is `false`.

Example of **incorrect** code for the `{"caseSensitive": false}` option:

```js
let {B, a, c} = obj;
```

Example of **correct** code for the `{"caseSensitive": false}` option:

```js
let {a, B, c} = obj;
```

Example of **incorrect** code for the `{"caseSensitive": true}` option:

```js
let {a, B, c} = obj;
```

Example of **correct** code for the `{"caseSensitive": true}` option:

```js
let {B, a, c} = obj;
```
18 changes: 16 additions & 2 deletions lib/rules/sort-destructure-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,17 @@ module.exports = {
messages: {
sort: `Expected object keys to be in sorted order. Expected {{first}} to be before {{second}}.`
},
schema: []
schema: [
{
type: 'object',
properties: {
caseSensitive: {
type: 'boolean',
}
},
additionalProperties: false
}
]
},

create(context) {
Expand All @@ -59,14 +69,18 @@ module.exports = {
return;
}

const options = context.options[0] || {};
const caseSensitive = options.caseSensitive || false;
let prevNode = null;

for (const nextNode of node.properties) {
if (prevNode && !isRestProperty(nextNode)) {
const prevName = getNodeName(prevNode.key);
const nextName = getNodeName(nextNode.key);
const first = caseSensitive ? prevName : prevName.toLowerCase();
const second = caseSensitive ? nextName : nextName.toLowerCase();

if (naturalCompare(prevName.toLowerCase(), nextName.toLowerCase()) > 0) {
if (naturalCompare(first, second) > 0) {
context.report({
node: nextNode,
messageId: 'sort',
Expand Down
27 changes: 27 additions & 0 deletions tests/lib/rules/sort-destructure-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ ruleTester.run('sort-destructure-keys', rule, {
'const {...other} = someObj;',
'const func = ({a, b}) => a + b;',
'const {a: {b, c}, d: {e, f: {g}}} = someObj;',
{
code: 'const {a, b} = someObj;',
options: [{ caseSensitive: true }]
},
{
code: 'const {B, a} = someObj;',
options: [{ caseSensitive: true }]
},
{
code: 'const {aCc, abb} = someObj;',
options: [{ caseSensitive: true }]
}
],
invalid: [
{
Expand Down Expand Up @@ -65,5 +77,20 @@ ruleTester.run('sort-destructure-keys', rule, {
code: 'const {a, c: {e, d}, b = c} = someObj;',
errors: [msg('e', 'd')]
},
{
code: 'const {b, a} = someObj;',
errors: just('b', 'a'),
options: [{ caseSensitive: true }]
},
{
code: 'const {a, B} = someObj;',
errors: just('a', 'B'),
options: [{ caseSensitive: true }]
},
{
code: 'const {abc, aBd} = someObj;',
errors: just('abc', 'aBd'),
options: [{ caseSensitive: true }]
},
]
});