2 * @fileoverview Rule to flag non-quoted property names in object literals.
3 * @author Mathias Bynens <http://mathiasbynens.be/>
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
11 const espree = require("espree"),
12 keywords = require("./utils/keywords");
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
23 description: "require quotes around object literal property names",
24 category: "Stylistic Issues",
26 url: "https://eslint.org/docs/rules/quote-props"
35 enum: ["always", "as-needed", "consistent", "consistent-as-needed"]
45 enum: ["always", "as-needed", "consistent", "consistent-as-needed"]
60 additionalProperties: false
74 const MODE = context.options[0],
75 KEYWORDS = context.options[1] && context.options[1].keywords,
76 CHECK_UNNECESSARY = !context.options[1] || context.options[1].unnecessary !== false,
77 NUMBERS = context.options[1] && context.options[1].numbers,
79 MESSAGE_UNNECESSARY = "Unnecessarily quoted property '{{property}}' found.",
80 MESSAGE_UNQUOTED = "Unquoted property '{{property}}' found.",
81 MESSAGE_NUMERIC = "Unquoted number literal '{{property}}' used as key.",
82 MESSAGE_RESERVED = "Unquoted reserved word '{{property}}' used as key.",
83 sourceCode = context.getSourceCode();
87 * Checks whether a certain string constitutes an ES3 token
88 * @param {string} tokenStr The string to be checked.
89 * @returns {boolean} `true` if it is an ES3 token.
91 function isKeyword(tokenStr) {
92 return keywords.indexOf(tokenStr) >= 0;
96 * Checks if an espree-tokenized key has redundant quotes (i.e. whether quotes are unnecessary)
97 * @param {string} rawKey The raw key value from the source
98 * @param {espreeTokens} tokens The espree-tokenized node key
99 * @param {boolean} [skipNumberLiterals=false] Indicates whether number literals should be checked
100 * @returns {boolean} Whether or not a key has redundant quotes.
103 function areQuotesRedundant(rawKey, tokens, skipNumberLiterals) {
104 return tokens.length === 1 && tokens[0].start === 0 && tokens[0].end === rawKey.length &&
105 (["Identifier", "Keyword", "Null", "Boolean"].indexOf(tokens[0].type) >= 0 ||
106 (tokens[0].type === "Numeric" && !skipNumberLiterals && String(+tokens[0].value) === tokens[0].value));
110 * Returns a string representation of a property node with quotes removed
111 * @param {ASTNode} key Key AST Node, which may or may not be quoted
112 * @returns {string} A replacement string for this property
114 function getUnquotedKey(key) {
115 return key.type === "Identifier" ? key.name : key.value;
119 * Returns a string representation of a property node with quotes added
120 * @param {ASTNode} key Key AST Node, which may or may not be quoted
121 * @returns {string} A replacement string for this property
123 function getQuotedKey(key) {
124 if (key.type === "Literal" && typeof key.value === "string") {
126 // If the key is already a string literal, don't replace the quotes with double quotes.
127 return sourceCode.getText(key);
130 // Otherwise, the key is either an identifier or a number literal.
131 return `"${key.type === "Identifier" ? key.name : key.value}"`;
135 * Ensures that a property's key is quoted only when necessary
136 * @param {ASTNode} node Property AST node
139 function checkUnnecessaryQuotes(node) {
140 const key = node.key;
142 if (node.method || node.computed || node.shorthand) {
146 if (key.type === "Literal" && typeof key.value === "string") {
150 tokens = espree.tokenize(key.value);
155 if (tokens.length !== 1) {
159 const isKeywordToken = isKeyword(tokens[0].value);
161 if (isKeywordToken && KEYWORDS) {
165 if (CHECK_UNNECESSARY && areQuotesRedundant(key.value, tokens, NUMBERS)) {
168 message: MESSAGE_UNNECESSARY,
169 data: { property: key.value },
170 fix: fixer => fixer.replaceText(key, getUnquotedKey(key))
173 } else if (KEYWORDS && key.type === "Identifier" && isKeyword(key.name)) {
176 message: MESSAGE_RESERVED,
177 data: { property: key.name },
178 fix: fixer => fixer.replaceText(key, getQuotedKey(key))
180 } else if (NUMBERS && key.type === "Literal" && typeof key.value === "number") {
183 message: MESSAGE_NUMERIC,
184 data: { property: key.value },
185 fix: fixer => fixer.replaceText(key, getQuotedKey(key))
191 * Ensures that a property's key is quoted
192 * @param {ASTNode} node Property AST node
195 function checkOmittedQuotes(node) {
196 const key = node.key;
198 if (!node.method && !node.computed && !node.shorthand && !(key.type === "Literal" && typeof key.value === "string")) {
201 message: MESSAGE_UNQUOTED,
202 data: { property: key.name || key.value },
203 fix: fixer => fixer.replaceText(key, getQuotedKey(key))
209 * Ensures that an object's keys are consistently quoted, optionally checks for redundancy of quotes
210 * @param {ASTNode} node Property AST node
211 * @param {boolean} checkQuotesRedundancy Whether to check quotes' redundancy
214 function checkConsistency(node, checkQuotesRedundancy) {
215 const quotedProps = [],
217 let keywordKeyName = null,
218 necessaryQuotes = false;
220 node.properties.forEach(property => {
221 const key = property.key;
223 if (!key || property.method || property.computed || property.shorthand) {
227 if (key.type === "Literal" && typeof key.value === "string") {
229 quotedProps.push(property);
231 if (checkQuotesRedundancy) {
235 tokens = espree.tokenize(key.value);
237 necessaryQuotes = true;
241 necessaryQuotes = necessaryQuotes || !areQuotesRedundant(key.value, tokens) || KEYWORDS && isKeyword(tokens[0].value);
243 } else if (KEYWORDS && checkQuotesRedundancy && key.type === "Identifier" && isKeyword(key.name)) {
244 unquotedProps.push(property);
245 necessaryQuotes = true;
246 keywordKeyName = key.name;
248 unquotedProps.push(property);
252 if (checkQuotesRedundancy && quotedProps.length && !necessaryQuotes) {
253 quotedProps.forEach(property => {
256 message: "Properties shouldn't be quoted as all quotes are redundant.",
257 fix: fixer => fixer.replaceText(property.key, getUnquotedKey(property.key))
260 } else if (unquotedProps.length && keywordKeyName) {
261 unquotedProps.forEach(property => {
264 message: "Properties should be quoted as '{{property}}' is a reserved word.",
265 data: { property: keywordKeyName },
266 fix: fixer => fixer.replaceText(property.key, getQuotedKey(property.key))
269 } else if (quotedProps.length && unquotedProps.length) {
270 unquotedProps.forEach(property => {
273 message: "Inconsistently quoted property '{{key}}' found.",
274 data: { key: property.key.name || property.key.value },
275 fix: fixer => fixer.replaceText(property.key, getQuotedKey(property.key))
283 if (MODE === "always" || !MODE) {
284 checkOmittedQuotes(node);
286 if (MODE === "as-needed") {
287 checkUnnecessaryQuotes(node);
290 ObjectExpression(node) {
291 if (MODE === "consistent") {
292 checkConsistency(node, false);
294 if (MODE === "consistent-as-needed") {
295 checkConsistency(node, true);