Skip to content

Commit

Permalink
Fix: quote-props rule should ignore computed and shorthand properti…
Browse files Browse the repository at this point in the history
…es (fixes #3557) (fixes #3544)
  • Loading branch information
BYK committed Aug 28, 2015
1 parent ab74b77 commit e683bc4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/rules/quote-props.js
Expand Up @@ -59,7 +59,7 @@ module.exports = function(context) {
isKeywordToken,
tokens;

if (node.method || node.computed) {
if (node.method || node.computed || node.shorthand) {
return;
}

Expand Down Expand Up @@ -96,7 +96,7 @@ module.exports = function(context) {
function checkOmittedQuotes(node) {
var key = node.key;

if (!node.method && !node.computed && !(key.type === "Literal" && typeof key.value === "string")) {
if (!node.method && !node.computed && !node.shorthand && !(key.type === "Literal" && typeof key.value === "string")) {
context.report(node, MESSAGE_UNQUOTED, {
property: key.name || key.value
});
Expand All @@ -118,7 +118,7 @@ module.exports = function(context) {
var key = property.key,
tokens;

if (property.method || property.computed) {
if (property.method || property.computed || property.shorthand) {
return;
}

Expand Down
13 changes: 13 additions & 0 deletions tests/lib/rules/quote-props.js
Expand Up @@ -27,9 +27,12 @@ ruleTester.run("quote-props", rule, {

{ code: "({ 'a': 0, b(){} })", ecmaFeatures: { objectLiteralShorthandMethods: true }},
{ code: "({ [x]: 0 });", env: {es6: true}},
{ code: "({ x });", env: {es6: true}},
{ code: "({ a: 0, b(){} })", options: ["as-needed"], ecmaFeatures: { objectLiteralShorthandMethods: true } },
{ code: "({ a: 0, [x]: 1 })", options: ["as-needed"], env: {es6: true} },
{ code: "({ a: 0, x })", options: ["as-needed"], env: {es6: true} },
{ code: "({ '@': 0, [x]: 1 })", options: ["as-needed"], env: {es6: true} },
{ code: "({ '@': 0, x })", options: ["as-needed"], env: {es6: true} },
{ code: "({ a: 0, b: 0 })", options: ["as-needed"] },
{ code: "({ a: 0, 0: 0 })", options: ["as-needed"] },
{ code: "({ a: 0, true: 0 })", options: ["as-needed"] },
Expand All @@ -46,12 +49,15 @@ ruleTester.run("quote-props", rule, {
{ code: "({ null: 0, a: 0 })", options: ["consistent"] },
{ code: "({ a: 0, b: 0 })", options: ["consistent"] },
{ code: "({ 'a': 1, [x]: 0 });", options: ["consistent"], env: {es6: true}},
{ code: "({ 'a': 1, x });", options: ["consistent"], env: {es6: true}},
{ code: "({ a: 0, b: 0 })", options: ["consistent-as-needed"] },
{ code: "({ a: 0, null: 0 })", options: ["consistent-as-needed"] },
{ code: "({ 'a': 0, '-b': 0 })", options: ["consistent-as-needed"] },
{ code: "({ '@': 0, 'B': 0 })", options: ["consistent-as-needed"] },
{ code: "({ '@': 1, [x]: 0 });", env: {"es6": true}, options: ["consistent-as-needed"]},
{ code: "({ '@': 1, x });", env: {"es6": true}, options: ["consistent-as-needed"]},
{ code: "({ a: 1, [x]: 0 });", env: {"es6": true}, options: ["consistent-as-needed"]},
{ code: "({ a: 1, x });", env: {"es6": true}, options: ["consistent-as-needed"]},
{ code: "({ a: 0, 'if': 0 })", options: ["as-needed", {keywords: true}] },
{ code: "({ a: 0, 'while': 0 })", options: ["as-needed", {keywords: true}] },
{ code: "({ a: 0, 'volatile': 0 })", options: ["as-needed", {keywords: true}] },
Expand Down Expand Up @@ -122,6 +128,13 @@ ruleTester.run("quote-props", rule, {
errors: [{
message: "Properties shouldn't be quoted as all quotes are redundant.", type: "ObjectExpression"
}]
}, {
code: "({ 'a': 0, x })",
env: {"es6": true},
options: ["consistent-as-needed"],
errors: [{
message: "Properties shouldn't be quoted as all quotes are redundant.", type: "ObjectExpression"
}]
}, {
code: "({ 'true': 0, 'null': 0 })",
options: ["consistent-as-needed"],
Expand Down

0 comments on commit e683bc4

Please sign in to comment.