1 |
|
2 |
|
3 |
|
4 |
|
5 | "use strict";
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | const espree = require("espree"),
|
12 | keywords = require("./utils/keywords");
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | module.exports = {
|
19 | meta: {
|
20 | type: "suggestion",
|
21 |
|
22 | docs: {
|
23 | description: "require quotes around object literal property names",
|
24 | category: "Stylistic Issues",
|
25 | recommended: false,
|
26 | url: "https://eslint.org/docs/rules/quote-props"
|
27 | },
|
28 |
|
29 | schema: {
|
30 | anyOf: [
|
31 | {
|
32 | type: "array",
|
33 | items: [
|
34 | {
|
35 | enum: ["always", "as-needed", "consistent", "consistent-as-needed"]
|
36 | }
|
37 | ],
|
38 | minItems: 0,
|
39 | maxItems: 1
|
40 | },
|
41 | {
|
42 | type: "array",
|
43 | items: [
|
44 | {
|
45 | enum: ["always", "as-needed", "consistent", "consistent-as-needed"]
|
46 | },
|
47 | {
|
48 | type: "object",
|
49 | properties: {
|
50 | keywords: {
|
51 | type: "boolean"
|
52 | },
|
53 | unnecessary: {
|
54 | type: "boolean"
|
55 | },
|
56 | numbers: {
|
57 | type: "boolean"
|
58 | }
|
59 | },
|
60 | additionalProperties: false
|
61 | }
|
62 | ],
|
63 | minItems: 0,
|
64 | maxItems: 2
|
65 | }
|
66 | ]
|
67 | },
|
68 |
|
69 | fixable: "code"
|
70 | },
|
71 |
|
72 | create(context) {
|
73 |
|
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,
|
78 |
|
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();
|
84 |
|
85 |
|
86 | |
87 |
|
88 |
|
89 |
|
90 |
|
91 | function isKeyword(tokenStr) {
|
92 | return keywords.indexOf(tokenStr) >= 0;
|
93 | }
|
94 |
|
95 | |
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
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));
|
107 | }
|
108 |
|
109 | |
110 |
|
111 |
|
112 |
|
113 |
|
114 | function getUnquotedKey(key) {
|
115 | return key.type === "Identifier" ? key.name : key.value;
|
116 | }
|
117 |
|
118 | |
119 |
|
120 |
|
121 |
|
122 |
|
123 | function getQuotedKey(key) {
|
124 | if (key.type === "Literal" && typeof key.value === "string") {
|
125 |
|
126 |
|
127 | return sourceCode.getText(key);
|
128 | }
|
129 |
|
130 |
|
131 | return `"${key.type === "Identifier" ? key.name : key.value}"`;
|
132 | }
|
133 |
|
134 | |
135 |
|
136 |
|
137 |
|
138 |
|
139 | function checkUnnecessaryQuotes(node) {
|
140 | const key = node.key;
|
141 |
|
142 | if (node.method || node.computed || node.shorthand) {
|
143 | return;
|
144 | }
|
145 |
|
146 | if (key.type === "Literal" && typeof key.value === "string") {
|
147 | let tokens;
|
148 |
|
149 | try {
|
150 | tokens = espree.tokenize(key.value);
|
151 | } catch (e) {
|
152 | return;
|
153 | }
|
154 |
|
155 | if (tokens.length !== 1) {
|
156 | return;
|
157 | }
|
158 |
|
159 | const isKeywordToken = isKeyword(tokens[0].value);
|
160 |
|
161 | if (isKeywordToken && KEYWORDS) {
|
162 | return;
|
163 | }
|
164 |
|
165 | if (CHECK_UNNECESSARY && areQuotesRedundant(key.value, tokens, NUMBERS)) {
|
166 | context.report({
|
167 | node,
|
168 | message: MESSAGE_UNNECESSARY,
|
169 | data: { property: key.value },
|
170 | fix: fixer => fixer.replaceText(key, getUnquotedKey(key))
|
171 | });
|
172 | }
|
173 | } else if (KEYWORDS && key.type === "Identifier" && isKeyword(key.name)) {
|
174 | context.report({
|
175 | node,
|
176 | message: MESSAGE_RESERVED,
|
177 | data: { property: key.name },
|
178 | fix: fixer => fixer.replaceText(key, getQuotedKey(key))
|
179 | });
|
180 | } else if (NUMBERS && key.type === "Literal" && typeof key.value === "number") {
|
181 | context.report({
|
182 | node,
|
183 | message: MESSAGE_NUMERIC,
|
184 | data: { property: key.value },
|
185 | fix: fixer => fixer.replaceText(key, getQuotedKey(key))
|
186 | });
|
187 | }
|
188 | }
|
189 |
|
190 | |
191 |
|
192 |
|
193 |
|
194 |
|
195 | function checkOmittedQuotes(node) {
|
196 | const key = node.key;
|
197 |
|
198 | if (!node.method && !node.computed && !node.shorthand && !(key.type === "Literal" && typeof key.value === "string")) {
|
199 | context.report({
|
200 | node,
|
201 | message: MESSAGE_UNQUOTED,
|
202 | data: { property: key.name || key.value },
|
203 | fix: fixer => fixer.replaceText(key, getQuotedKey(key))
|
204 | });
|
205 | }
|
206 | }
|
207 |
|
208 | |
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 | function checkConsistency(node, checkQuotesRedundancy) {
|
215 | const quotedProps = [],
|
216 | unquotedProps = [];
|
217 | let keywordKeyName = null,
|
218 | necessaryQuotes = false;
|
219 |
|
220 | node.properties.forEach(property => {
|
221 | const key = property.key;
|
222 |
|
223 | if (!key || property.method || property.computed || property.shorthand) {
|
224 | return;
|
225 | }
|
226 |
|
227 | if (key.type === "Literal" && typeof key.value === "string") {
|
228 |
|
229 | quotedProps.push(property);
|
230 |
|
231 | if (checkQuotesRedundancy) {
|
232 | let tokens;
|
233 |
|
234 | try {
|
235 | tokens = espree.tokenize(key.value);
|
236 | } catch (e) {
|
237 | necessaryQuotes = true;
|
238 | return;
|
239 | }
|
240 |
|
241 | necessaryQuotes = necessaryQuotes || !areQuotesRedundant(key.value, tokens) || KEYWORDS && isKeyword(tokens[0].value);
|
242 | }
|
243 | } else if (KEYWORDS && checkQuotesRedundancy && key.type === "Identifier" && isKeyword(key.name)) {
|
244 | unquotedProps.push(property);
|
245 | necessaryQuotes = true;
|
246 | keywordKeyName = key.name;
|
247 | } else {
|
248 | unquotedProps.push(property);
|
249 | }
|
250 | });
|
251 |
|
252 | if (checkQuotesRedundancy && quotedProps.length && !necessaryQuotes) {
|
253 | quotedProps.forEach(property => {
|
254 | context.report({
|
255 | node: property,
|
256 | message: "Properties shouldn't be quoted as all quotes are redundant.",
|
257 | fix: fixer => fixer.replaceText(property.key, getUnquotedKey(property.key))
|
258 | });
|
259 | });
|
260 | } else if (unquotedProps.length && keywordKeyName) {
|
261 | unquotedProps.forEach(property => {
|
262 | context.report({
|
263 | node: 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))
|
267 | });
|
268 | });
|
269 | } else if (quotedProps.length && unquotedProps.length) {
|
270 | unquotedProps.forEach(property => {
|
271 | context.report({
|
272 | node: 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))
|
276 | });
|
277 | });
|
278 | }
|
279 | }
|
280 |
|
281 | return {
|
282 | Property(node) {
|
283 | if (MODE === "always" || !MODE) {
|
284 | checkOmittedQuotes(node);
|
285 | }
|
286 | if (MODE === "as-needed") {
|
287 | checkUnnecessaryQuotes(node);
|
288 | }
|
289 | },
|
290 | ObjectExpression(node) {
|
291 | if (MODE === "consistent") {
|
292 | checkConsistency(node, false);
|
293 | }
|
294 | if (MODE === "consistent-as-needed") {
|
295 | checkConsistency(node, true);
|
296 | }
|
297 | }
|
298 | };
|
299 |
|
300 | }
|
301 | };
|