Skip to content
Permalink
Browse files

Update: Check empty string property names in sort-keys (#12073)

  • Loading branch information
mdjermanovic authored and platinumazure committed Aug 18, 2019
1 parent acce6de commit d642150fe016608e71a1df2a72960e915b3cfbad
Showing with 68 additions and 3 deletions.
  1. +11 −3 lib/rules/sort-keys.js
  2. +57 −0 tests/lib/rules/sort-keys.js
@@ -29,7 +29,13 @@ const astUtils = require("./utils/ast-utils"),
* @private
*/
function getPropertyName(node) {
return astUtils.getStaticPropertyName(node) || node.key.name || null;
const staticName = astUtils.getStaticPropertyName(node);

if (staticName !== null) {
return staticName;
}

return node.key.name || null;
}

/**
@@ -151,9 +157,11 @@ module.exports = {
const numKeys = stack.numKeys;
const thisName = getPropertyName(node);

stack.prevName = thisName || prevName;
if (thisName !== null) {
stack.prevName = thisName;
}

if (!prevName || !thisName || numKeys < minKeys) {
if (prevName === null || thisName === null || numKeys < minKeys) {
return;
}

@@ -22,6 +22,10 @@ ruleTester.run("sort-keys", rule, {
valid: [

// default (asc)
{ code: "var obj = {'':1, [``]:2}", options: [], parserOptions: { ecmaVersion: 6 } },
{ code: "var obj = {[``]:1, '':2}", options: [], parserOptions: { ecmaVersion: 6 } },
{ code: "var obj = {'':1, a:2}", options: [] },
{ code: "var obj = {[``]:1, a:2}", options: [], parserOptions: { ecmaVersion: 6 } },
{ code: "var obj = {_:2, a:1, b:3} // default", options: [] },
{ code: "var obj = {a:1, b:3, c:2}", options: [] },
{ code: "var obj = {a:2, b:3, b_:1}", options: [] },
@@ -32,13 +36,17 @@ ruleTester.run("sort-keys", rule, {

// ignore non-simple computed properties.
{ code: "var obj = {a:1, b:3, [a + b]: -1, c:2}", options: [], parserOptions: { ecmaVersion: 6 } },
{ code: "var obj = {'':1, [f()]:2, a:3}", options: [], parserOptions: { ecmaVersion: 6 } },
{ code: "var obj = {a:1, [b++]:2, '':3}", options: ["desc"], parserOptions: { ecmaVersion: 6 } },

// ignore properties separated by spread properties
{ code: "var obj = {a:1, ...z, b:1}", options: [], parserOptions: { ecmaVersion: 2018 } },
{ code: "var obj = {b:1, ...z, a:1}", options: [], parserOptions: { ecmaVersion: 2018 } },
{ code: "var obj = {...a, b:1, ...c, d:1}", options: [], parserOptions: { ecmaVersion: 2018 } },
{ code: "var obj = {...a, b:1, ...d, ...c, e:2, z:5}", options: [], parserOptions: { ecmaVersion: 2018 } },
{ code: "var obj = {b:1, ...c, ...d, e:2}", options: [], parserOptions: { ecmaVersion: 2018 } },
{ code: "var obj = {a:1, ...z, '':2}", options: [], parserOptions: { ecmaVersion: 2018 } },
{ code: "var obj = {'':1, ...z, 'a':2}", options: ["desc"], parserOptions: { ecmaVersion: 2018 } },

// not ignore properties not separated by spread properties
{ code: "var obj = {...z, a:1, b:1}", options: [], parserOptions: { ecmaVersion: 2018 } },
@@ -159,6 +167,15 @@ ruleTester.run("sort-keys", rule, {
invalid: [

// default (asc)
{
code: "var obj = {a:1, '':2} // default",
errors: ["Expected object keys to be in ascending order. '' should be before 'a'."]
},
{
code: "var obj = {a:1, [``]:2} // default",
parserOptions: { ecmaVersion: 6 },
errors: ["Expected object keys to be in ascending order. '' should be before 'a'."]
},
{
code: "var obj = {a:1, _:2, b:3} // default",
errors: ["Expected object keys to be in ascending order. '_' should be before 'a'."]
@@ -234,6 +251,31 @@ ruleTester.run("sort-keys", rule, {
parserOptions: { ecmaVersion: 2018 },
errors: ["Expected object keys to be in descending order. 'b' should be before 'a'."]
},
{
code: "var obj = {...z, '':1, a:2}",
options: ["desc"],
parserOptions: { ecmaVersion: 2018 },
errors: ["Expected object keys to be in descending order. 'a' should be before ''."]
},

// ignore non-simple computed properties, but their position shouldn't affect other comparisons.
{
code: "var obj = {a:1, [b+c]:2, '':3}",
parserOptions: { ecmaVersion: 6 },
errors: ["Expected object keys to be in ascending order. '' should be before 'a'."]
},
{
code: "var obj = {'':1, [b+c]:2, a:3}",
options: ["desc"],
parserOptions: { ecmaVersion: 6 },
errors: ["Expected object keys to be in descending order. 'a' should be before ''."]
},
{
code: "var obj = {b:1, [f()]:2, '':3, a:4}",
options: ["desc"],
parserOptions: { ecmaVersion: 6 },
errors: ["Expected object keys to be in descending order. 'a' should be before ''."]
},

// not ignore simple computed properties.
{
@@ -478,6 +520,21 @@ ruleTester.run("sort-keys", rule, {
},

// desc
{
code: "var obj = {'':1, a:'2'} // desc",
options: ["desc"],
errors: [
"Expected object keys to be in descending order. 'a' should be before ''."
]
},
{
code: "var obj = {[``]:1, a:'2'} // desc",
options: ["desc"],
parserOptions: { ecmaVersion: 6 },
errors: [
"Expected object keys to be in descending order. 'a' should be before ''."
]
},
{
code: "var obj = {a:1, _:2, b:3} // desc",
options: ["desc"],

0 comments on commit d642150

Please sign in to comment.
You can’t perform that action at this time.