Skip to content

Commit

Permalink
Fix: quote-props false positive on certain keys (fixes #5532)
Browse files Browse the repository at this point in the history
This patch ensures the "tokenized" version of the key is an exact match
to avoid any false positives when the key includes spaces around of it
or has "comment blocks" in it such as `//..` or `/*..*/`
  • Loading branch information
BYK committed Mar 13, 2016
1 parent fce1917 commit 689cb7d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/rules/quote-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ module.exports = function(context) {

/**
* Checks if an espree-tokenized key has redundant quotes (i.e. whether quotes are unnecessary)
* @param {string} rawKey The raw key value from the source
* @param {espreeTokens} tokens The espree-tokenized node key
* @param {boolean} [skipNumberLiterals=false] Indicates whether number literals should be checked
* @returns {boolean} Whether or not a key has redundant quotes.
* @private
*/
function areQuotesRedundant(tokens, skipNumberLiterals) {
return tokens.length === 1 &&
function areQuotesRedundant(rawKey, tokens, skipNumberLiterals) {
return tokens.length === 1 && tokens[0].start === 0 && tokens[0].end === rawKey.length &&
(["Identifier", "Keyword", "Null", "Boolean"].indexOf(tokens[0].type) >= 0 ||
(tokens[0].type === "Numeric" && !skipNumberLiterals && "" + +tokens[0].value === tokens[0].value));
}
Expand Down Expand Up @@ -83,7 +84,7 @@ module.exports = function(context) {
return;
}

if (CHECK_UNNECESSARY && areQuotesRedundant(tokens, NUMBERS)) {
if (CHECK_UNNECESSARY && areQuotesRedundant(key.value, tokens, NUMBERS)) {
context.report(node, MESSAGE_UNNECESSARY, {property: key.value});
}
} else if (KEYWORDS && key.type === "Identifier" && isKeyword(key.name)) {
Expand Down Expand Up @@ -139,7 +140,7 @@ module.exports = function(context) {
return;
}

necessaryQuotes = necessaryQuotes || !areQuotesRedundant(tokens) || KEYWORDS && isKeyword(tokens[0].value);
necessaryQuotes = necessaryQuotes || !areQuotesRedundant(key.value, tokens) || KEYWORDS && isKeyword(tokens[0].value);
}
} else if (KEYWORDS && checkQuotesRedundancy && key.type === "Identifier" && isKeyword(key.name)) {
necessaryQuotes = true;
Expand Down
5 changes: 5 additions & 0 deletions tests/lib/rules/quote-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ ruleTester.run("quote-props", rule, {
{ code: "({ a: 0, '@': 0 })", options: ["as-needed"] },
{ code: "({ a: 0, 0: 0 })", options: ["as-needed"] },
{ code: "({ a: 0, '0x0': 0 })", options: ["as-needed"] },
{ code: "({ ' 0': 0, '0x0': 0 })", options: ["as-needed"] },
{ code: "({ '0 ': 0 })", options: ["as-needed"] },
{ code: "({ 'hey//meh': 0 })", options: ["as-needed"] },
{ code: "({ 'hey/*meh': 0 })", options: ["as-needed"] },
{ code: "({ 'hey/*meh*/': 0 })", options: ["as-needed"] },
{ code: "({ 'a': 0, '-b': 0 })", options: ["consistent"] },
{ code: "({ 'true': 0, 'b': 0 })", options: ["consistent"] },
{ code: "({ null: 0, a: 0 })", options: ["consistent"] },
Expand Down

0 comments on commit 689cb7d

Please sign in to comment.