Skip to content

Commit

Permalink
Fix: sort-keys throws Error at SpreadElement (fixes #11402) (#11403)
Browse files Browse the repository at this point in the history
* Fix: sort-keys throws Error at SpreadElement (fixes #11402)

* Fix: Add unit test

* Fix: address reviewer's comment
  • Loading branch information
kristw authored and btmills committed Feb 18, 2019
1 parent af9688b commit 1d6e639
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/rules/sort-keys.js
Expand Up @@ -129,8 +129,10 @@ module.exports = {
stack = stack.upper;
},

SpreadElement() {
stack.prevName = null;
SpreadElement(node) {
if (node.parent.type === "ObjectExpression") {
stack.prevName = null;
}
},

Property(node) {
Expand Down
4 changes: 4 additions & 0 deletions tests/lib/rules/sort-keys.js
Expand Up @@ -46,6 +46,10 @@ ruleTester.run("sort-keys", rule, {
{ code: "var obj = {a:1, b:1, ...z}", options: [], parserOptions: { ecmaVersion: 2018 } },
{ code: "var obj = {...z, ...x, a:1, ...c, ...d, f:5, e:4}", options: ["desc"], parserOptions: { ecmaVersion: 2018 } },

// works when spread occurs somewhere other than an object literal
{ code: "function fn(...args) { return [...args].length; }", options: [], parserOptions: { ecmaVersion: 2018 } },
{ code: "function g() {}; function f(...args) { return g(...args); }", options: [], parserOptions: { ecmaVersion: 2018 } },

// ignore destructuring patterns.
{ code: "let {a, b} = {}", options: [], parserOptions: { ecmaVersion: 6 } },

Expand Down

0 comments on commit 1d6e639

Please sign in to comment.