Blame view
node_modules/eslint/lib/rules/quote-props.js
7.64 KB
c39994410
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
/** * @fileoverview Rule to flag non-quoted property names in object literals. * @author Mathias Bynens <http://mathiasbynens.be/> * @copyright 2014 Brandon Mills. All rights reserved. * @copyright 2015 Tomasz Olędzki. All rights reserved. */ "use strict"; //------------------------------------------------------------------------------ // Requirements //------------------------------------------------------------------------------ var espree = require("espree"), keywords = require("../util/keywords"); //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ module.exports = function(context) { var MODE = context.options[0], KEYWORDS = context.options[1] && context.options[1].keywords, CHECK_UNNECESSARY = !context.options[1] || context.options[1].unnecessary !== false, NUMBERS = context.options[1] && context.options[1].numbers, MESSAGE_UNNECESSARY = "Unnecessarily quoted property `{{property}}` found.", MESSAGE_UNQUOTED = "Unquoted property `{{property}}` found.", MESSAGE_NUMERIC = "Unquoted number literal `{{property}}` used as key.", MESSAGE_RESERVED = "Unquoted reserved word `{{property}}` used as key."; /** * Checks whether a certain string constitutes an ES3 token * @param {string} tokenStr - The string to be checked. * @returns {boolean} `true` if it is an ES3 token. */ function isKeyword(tokenStr) { return keywords.indexOf(tokenStr) >= 0; } /** * Checks if an espree-tokenized key has redundant quotes (i.e. whether quotes are unnecessary) * @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 && (["Identifier", "Keyword", "Null", "Boolean"].indexOf(tokens[0].type) >= 0 || (tokens[0].type === "Numeric" && !skipNumberLiterals && "" + +tokens[0].value === tokens[0].value)); } /** * Ensures that a property's key is quoted only when necessary * @param {ASTNode} node Property AST node * @returns {void} */ function checkUnnecessaryQuotes(node) { var key = node.key, isKeywordToken, tokens; if (node.method || node.computed || node.shorthand) { return; } if (key.type === "Literal" && typeof key.value === "string") { try { tokens = espree.tokenize(key.value); } catch (e) { return; } if (tokens.length !== 1) { return; } isKeywordToken = isKeyword(tokens[0].value); if (isKeywordToken && KEYWORDS) { return; } if (CHECK_UNNECESSARY && areQuotesRedundant(tokens, NUMBERS)) { context.report(node, MESSAGE_UNNECESSARY, {property: key.value}); } } else if (KEYWORDS && key.type === "Identifier" && isKeyword(key.name)) { context.report(node, MESSAGE_RESERVED, {property: key.name}); } else if (NUMBERS && key.type === "Literal" && typeof key.value === "number") { context.report(node, MESSAGE_NUMERIC, {property: key.value}); } } /** * Ensures that a property's key is quoted * @param {ASTNode} node Property AST node * @returns {void} */ function checkOmittedQuotes(node) { var key = node.key; if (!node.method && !node.computed && !node.shorthand && !(key.type === "Literal" && typeof key.value === "string")) { context.report(node, MESSAGE_UNQUOTED, { property: key.name || key.value }); } } /** * Ensures that an object's keys are consistently quoted, optionally checks for redundancy of quotes * @param {ASTNode} node Property AST node * @param {boolean} checkQuotesRedundancy Whether to check quotes' redundancy * @returns {void} */ function checkConsistency(node, checkQuotesRedundancy) { var quotes = false, lackOfQuotes = false, necessaryQuotes = false; node.properties.forEach(function(property) { var key = property.key, tokens; if (!key || property.method || property.computed || property.shorthand) { return; } if (key.type === "Literal" && typeof key.value === "string") { quotes = true; if (checkQuotesRedundancy) { try { tokens = espree.tokenize(key.value); } catch (e) { necessaryQuotes = true; return; } necessaryQuotes = necessaryQuotes || !areQuotesRedundant(tokens) || KEYWORDS && isKeyword(tokens[0].value); } } else if (KEYWORDS && checkQuotesRedundancy && key.type === "Identifier" && isKeyword(key.name)) { necessaryQuotes = true; context.report(node, "Properties should be quoted as `{{property}}` is a reserved word.", {property: key.name}); } else { lackOfQuotes = true; } if (quotes && lackOfQuotes) { context.report(node, "Inconsistently quoted property `{{key}}` found.", { key: key.name || key.value }); } }); if (checkQuotesRedundancy && quotes && !necessaryQuotes) { context.report(node, "Properties shouldn't be quoted as all quotes are redundant."); } } return { "Property": function(node) { if (MODE === "always" || !MODE) { checkOmittedQuotes(node); } if (MODE === "as-needed") { checkUnnecessaryQuotes(node); } }, "ObjectExpression": function(node) { if (MODE === "consistent") { checkConsistency(node, false); } if (MODE === "consistent-as-needed") { checkConsistency(node, true); } } }; }; module.exports.schema = { "anyOf": [ { "type": "array", "items": [ { "enum": [0, 1, 2] }, { "enum": ["always", "as-needed", "consistent", "consistent-as-needed"] } ], "minItems": 1, "maxItems": 2 }, { "type": "array", "items": [ { "enum": [0, 1, 2] }, { "enum": ["always", "as-needed", "consistent", "consistent-as-needed"] }, { "type": "object", "properties": { "keywords": { "type": "boolean" }, "unnecessary": { "type": "boolean" }, "numbers": { "type": "boolean" } }, "additionalProperties": false } ], "minItems": 1, "maxItems": 3 } ] }; |