Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Aug 18, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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