Skip to content

Commit

Permalink
Update: Check empty string property names in sort-keys (#12073)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic authored and platinumazure committed Aug 18, 2019
1 parent acce6de commit d642150
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/rules/sort-keys.js
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

Expand Down
57 changes: 57 additions & 0 deletions tests/lib/rules/sort-keys.js
Expand Up @@ -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: [] },
Expand All @@ -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 } },
Expand Down Expand Up @@ -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'."]
Expand Down Expand Up @@ -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.
{
Expand Down Expand Up @@ -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"],
Expand Down

0 comments on commit d642150

Please sign in to comment.