minor adjustment to readme
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / eslint / lib / rules / object-shorthand.js
1 /**
2  * @fileoverview Rule to enforce concise object methods and properties.
3  * @author Jamund Ferguson
4  */
5
6 "use strict";
7
8 const OPTIONS = {
9     always: "always",
10     never: "never",
11     methods: "methods",
12     properties: "properties",
13     consistent: "consistent",
14     consistentAsNeeded: "consistent-as-needed"
15 };
16
17 //------------------------------------------------------------------------------
18 // Requirements
19 //------------------------------------------------------------------------------
20 const astUtils = require("./utils/ast-utils");
21
22 //------------------------------------------------------------------------------
23 // Rule Definition
24 //------------------------------------------------------------------------------
25 module.exports = {
26     meta: {
27         type: "suggestion",
28
29         docs: {
30             description: "require or disallow method and property shorthand syntax for object literals",
31             category: "ECMAScript 6",
32             recommended: false,
33             url: "https://eslint.org/docs/rules/object-shorthand"
34         },
35
36         fixable: "code",
37
38         schema: {
39             anyOf: [
40                 {
41                     type: "array",
42                     items: [
43                         {
44                             enum: ["always", "methods", "properties", "never", "consistent", "consistent-as-needed"]
45                         }
46                     ],
47                     minItems: 0,
48                     maxItems: 1
49                 },
50                 {
51                     type: "array",
52                     items: [
53                         {
54                             enum: ["always", "methods", "properties"]
55                         },
56                         {
57                             type: "object",
58                             properties: {
59                                 avoidQuotes: {
60                                     type: "boolean"
61                                 }
62                             },
63                             additionalProperties: false
64                         }
65                     ],
66                     minItems: 0,
67                     maxItems: 2
68                 },
69                 {
70                     type: "array",
71                     items: [
72                         {
73                             enum: ["always", "methods"]
74                         },
75                         {
76                             type: "object",
77                             properties: {
78                                 ignoreConstructors: {
79                                     type: "boolean"
80                                 },
81                                 avoidQuotes: {
82                                     type: "boolean"
83                                 },
84                                 avoidExplicitReturnArrows: {
85                                     type: "boolean"
86                                 }
87                             },
88                             additionalProperties: false
89                         }
90                     ],
91                     minItems: 0,
92                     maxItems: 2
93                 }
94             ]
95         }
96     },
97
98     create(context) {
99         const APPLY = context.options[0] || OPTIONS.always;
100         const APPLY_TO_METHODS = APPLY === OPTIONS.methods || APPLY === OPTIONS.always;
101         const APPLY_TO_PROPS = APPLY === OPTIONS.properties || APPLY === OPTIONS.always;
102         const APPLY_NEVER = APPLY === OPTIONS.never;
103         const APPLY_CONSISTENT = APPLY === OPTIONS.consistent;
104         const APPLY_CONSISTENT_AS_NEEDED = APPLY === OPTIONS.consistentAsNeeded;
105
106         const PARAMS = context.options[1] || {};
107         const IGNORE_CONSTRUCTORS = PARAMS.ignoreConstructors;
108         const AVOID_QUOTES = PARAMS.avoidQuotes;
109         const AVOID_EXPLICIT_RETURN_ARROWS = !!PARAMS.avoidExplicitReturnArrows;
110         const sourceCode = context.getSourceCode();
111
112         //--------------------------------------------------------------------------
113         // Helpers
114         //--------------------------------------------------------------------------
115
116         const CTOR_PREFIX_REGEX = /[^_$0-9]/u;
117
118         /**
119          * Determines if the first character of the name is a capital letter.
120          * @param {string} name The name of the node to evaluate.
121          * @returns {boolean} True if the first character of the property name is a capital letter, false if not.
122          * @private
123          */
124         function isConstructor(name) {
125             const match = CTOR_PREFIX_REGEX.exec(name);
126
127             // Not a constructor if name has no characters apart from '_', '$' and digits e.g. '_', '$$', '_8'
128             if (!match) {
129                 return false;
130             }
131
132             const firstChar = name.charAt(match.index);
133
134             return firstChar === firstChar.toUpperCase();
135         }
136
137         /**
138          * Determines if the property can have a shorthand form.
139          * @param {ASTNode} property Property AST node
140          * @returns {boolean} True if the property can have a shorthand form
141          * @private
142          *
143          */
144         function canHaveShorthand(property) {
145             return (property.kind !== "set" && property.kind !== "get" && property.type !== "SpreadElement" && property.type !== "SpreadProperty" && property.type !== "ExperimentalSpreadProperty");
146         }
147
148         /**
149          * Checks whether a node is a string literal.
150          * @param   {ASTNode} node Any AST node.
151          * @returns {boolean} `true` if it is a string literal.
152          */
153         function isStringLiteral(node) {
154             return node.type === "Literal" && typeof node.value === "string";
155         }
156
157         /**
158          * Determines if the property is a shorthand or not.
159          * @param {ASTNode} property Property AST node
160          * @returns {boolean} True if the property is considered shorthand, false if not.
161          * @private
162          *
163          */
164         function isShorthand(property) {
165
166             // property.method is true when `{a(){}}`.
167             return (property.shorthand || property.method);
168         }
169
170         /**
171          * Determines if the property's key and method or value are named equally.
172          * @param {ASTNode} property Property AST node
173          * @returns {boolean} True if the key and value are named equally, false if not.
174          * @private
175          *
176          */
177         function isRedundant(property) {
178             const value = property.value;
179
180             if (value.type === "FunctionExpression") {
181                 return !value.id; // Only anonymous should be shorthand method.
182             }
183             if (value.type === "Identifier") {
184                 return astUtils.getStaticPropertyName(property) === value.name;
185             }
186
187             return false;
188         }
189
190         /**
191          * Ensures that an object's properties are consistently shorthand, or not shorthand at all.
192          * @param   {ASTNode} node Property AST node
193          * @param   {boolean} checkRedundancy Whether to check longform redundancy
194          * @returns {void}
195          *
196          */
197         function checkConsistency(node, checkRedundancy) {
198
199             // We are excluding getters/setters and spread properties as they are considered neither longform nor shorthand.
200             const properties = node.properties.filter(canHaveShorthand);
201
202             // Do we still have properties left after filtering the getters and setters?
203             if (properties.length > 0) {
204                 const shorthandProperties = properties.filter(isShorthand);
205
206                 /*
207                  * If we do not have an equal number of longform properties as
208                  * shorthand properties, we are using the annotations inconsistently
209                  */
210                 if (shorthandProperties.length !== properties.length) {
211
212                     // We have at least 1 shorthand property
213                     if (shorthandProperties.length > 0) {
214                         context.report({ node, message: "Unexpected mix of shorthand and non-shorthand properties." });
215                     } else if (checkRedundancy) {
216
217                         /*
218                          * If all properties of the object contain a method or value with a name matching it's key,
219                          * all the keys are redundant.
220                          */
221                         const canAlwaysUseShorthand = properties.every(isRedundant);
222
223                         if (canAlwaysUseShorthand) {
224                             context.report({ node, message: "Expected shorthand for all properties." });
225                         }
226                     }
227                 }
228             }
229         }
230
231         /**
232          * Fixes a FunctionExpression node by making it into a shorthand property.
233          * @param {SourceCodeFixer} fixer The fixer object
234          * @param {ASTNode} node A `Property` node that has a `FunctionExpression` or `ArrowFunctionExpression` as its value
235          * @returns {Object} A fix for this node
236          */
237         function makeFunctionShorthand(fixer, node) {
238             const firstKeyToken = node.computed
239                 ? sourceCode.getFirstToken(node, astUtils.isOpeningBracketToken)
240                 : sourceCode.getFirstToken(node.key);
241             const lastKeyToken = node.computed
242                 ? sourceCode.getFirstTokenBetween(node.key, node.value, astUtils.isClosingBracketToken)
243                 : sourceCode.getLastToken(node.key);
244             const keyText = sourceCode.text.slice(firstKeyToken.range[0], lastKeyToken.range[1]);
245             let keyPrefix = "";
246
247             // key: /* */ () => {}
248             if (sourceCode.commentsExistBetween(lastKeyToken, node.value)) {
249                 return null;
250             }
251
252             if (node.value.async) {
253                 keyPrefix += "async ";
254             }
255             if (node.value.generator) {
256                 keyPrefix += "*";
257             }
258
259             const fixRange = [firstKeyToken.range[0], node.range[1]];
260             const methodPrefix = keyPrefix + keyText;
261
262             if (node.value.type === "FunctionExpression") {
263                 const functionToken = sourceCode.getTokens(node.value).find(token => token.type === "Keyword" && token.value === "function");
264                 const tokenBeforeParams = node.value.generator ? sourceCode.getTokenAfter(functionToken) : functionToken;
265
266                 return fixer.replaceTextRange(
267                     fixRange,
268                     methodPrefix + sourceCode.text.slice(tokenBeforeParams.range[1], node.value.range[1])
269                 );
270             }
271
272             const arrowToken = sourceCode.getTokenBefore(node.value.body, astUtils.isArrowToken);
273             const fnBody = sourceCode.text.slice(arrowToken.range[1], node.value.range[1]);
274
275             let shouldAddParensAroundParameters = false;
276             let tokenBeforeParams;
277
278             if (node.value.params.length === 0) {
279                 tokenBeforeParams = sourceCode.getFirstToken(node.value, astUtils.isOpeningParenToken);
280             } else {
281                 tokenBeforeParams = sourceCode.getTokenBefore(node.value.params[0]);
282             }
283
284             if (node.value.params.length === 1) {
285                 const hasParen = astUtils.isOpeningParenToken(tokenBeforeParams);
286                 const isTokenOutsideNode = tokenBeforeParams.range[0] < node.range[0];
287
288                 shouldAddParensAroundParameters = !hasParen || isTokenOutsideNode;
289             }
290
291             const sliceStart = shouldAddParensAroundParameters
292                 ? node.value.params[0].range[0]
293                 : tokenBeforeParams.range[0];
294             const sliceEnd = sourceCode.getTokenBefore(arrowToken).range[1];
295
296             const oldParamText = sourceCode.text.slice(sliceStart, sliceEnd);
297             const newParamText = shouldAddParensAroundParameters ? `(${oldParamText})` : oldParamText;
298
299             return fixer.replaceTextRange(
300                 fixRange,
301                 methodPrefix + newParamText + fnBody
302             );
303
304         }
305
306         /**
307          * Fixes a FunctionExpression node by making it into a longform property.
308          * @param {SourceCodeFixer} fixer The fixer object
309          * @param {ASTNode} node A `Property` node that has a `FunctionExpression` as its value
310          * @returns {Object} A fix for this node
311          */
312         function makeFunctionLongform(fixer, node) {
313             const firstKeyToken = node.computed ? sourceCode.getTokens(node).find(token => token.value === "[") : sourceCode.getFirstToken(node.key);
314             const lastKeyToken = node.computed ? sourceCode.getTokensBetween(node.key, node.value).find(token => token.value === "]") : sourceCode.getLastToken(node.key);
315             const keyText = sourceCode.text.slice(firstKeyToken.range[0], lastKeyToken.range[1]);
316             let functionHeader = "function";
317
318             if (node.value.async) {
319                 functionHeader = `async ${functionHeader}`;
320             }
321             if (node.value.generator) {
322                 functionHeader = `${functionHeader}*`;
323             }
324
325             return fixer.replaceTextRange([node.range[0], lastKeyToken.range[1]], `${keyText}: ${functionHeader}`);
326         }
327
328         /*
329          * To determine whether a given arrow function has a lexical identifier (`this`, `arguments`, `super`, or `new.target`),
330          * create a stack of functions that define these identifiers (i.e. all functions except arrow functions) as the AST is
331          * traversed. Whenever a new function is encountered, create a new entry on the stack (corresponding to a different lexical
332          * scope of `this`), and whenever a function is exited, pop that entry off the stack. When an arrow function is entered,
333          * keep a reference to it on the current stack entry, and remove that reference when the arrow function is exited.
334          * When a lexical identifier is encountered, mark all the arrow functions on the current stack entry by adding them
335          * to an `arrowsWithLexicalIdentifiers` set. Any arrow function in that set will not be reported by this rule,
336          * because converting it into a method would change the value of one of the lexical identifiers.
337          */
338         const lexicalScopeStack = [];
339         const arrowsWithLexicalIdentifiers = new WeakSet();
340         const argumentsIdentifiers = new WeakSet();
341
342         /**
343          * Enters a function. This creates a new lexical identifier scope, so a new Set of arrow functions is pushed onto the stack.
344          * Also, this marks all `arguments` identifiers so that they can be detected later.
345          * @returns {void}
346          */
347         function enterFunction() {
348             lexicalScopeStack.unshift(new Set());
349             context.getScope().variables.filter(variable => variable.name === "arguments").forEach(variable => {
350                 variable.references.map(ref => ref.identifier).forEach(identifier => argumentsIdentifiers.add(identifier));
351             });
352         }
353
354         /**
355          * Exits a function. This pops the current set of arrow functions off the lexical scope stack.
356          * @returns {void}
357          */
358         function exitFunction() {
359             lexicalScopeStack.shift();
360         }
361
362         /**
363          * Marks the current function as having a lexical keyword. This implies that all arrow functions
364          * in the current lexical scope contain a reference to this lexical keyword.
365          * @returns {void}
366          */
367         function reportLexicalIdentifier() {
368             lexicalScopeStack[0].forEach(arrowFunction => arrowsWithLexicalIdentifiers.add(arrowFunction));
369         }
370
371         //--------------------------------------------------------------------------
372         // Public
373         //--------------------------------------------------------------------------
374
375         return {
376             Program: enterFunction,
377             FunctionDeclaration: enterFunction,
378             FunctionExpression: enterFunction,
379             "Program:exit": exitFunction,
380             "FunctionDeclaration:exit": exitFunction,
381             "FunctionExpression:exit": exitFunction,
382
383             ArrowFunctionExpression(node) {
384                 lexicalScopeStack[0].add(node);
385             },
386             "ArrowFunctionExpression:exit"(node) {
387                 lexicalScopeStack[0].delete(node);
388             },
389
390             ThisExpression: reportLexicalIdentifier,
391             Super: reportLexicalIdentifier,
392             MetaProperty(node) {
393                 if (node.meta.name === "new" && node.property.name === "target") {
394                     reportLexicalIdentifier();
395                 }
396             },
397             Identifier(node) {
398                 if (argumentsIdentifiers.has(node)) {
399                     reportLexicalIdentifier();
400                 }
401             },
402
403             ObjectExpression(node) {
404                 if (APPLY_CONSISTENT) {
405                     checkConsistency(node, false);
406                 } else if (APPLY_CONSISTENT_AS_NEEDED) {
407                     checkConsistency(node, true);
408                 }
409             },
410
411             "Property:exit"(node) {
412                 const isConciseProperty = node.method || node.shorthand;
413
414                 // Ignore destructuring assignment
415                 if (node.parent.type === "ObjectPattern") {
416                     return;
417                 }
418
419                 // getters and setters are ignored
420                 if (node.kind === "get" || node.kind === "set") {
421                     return;
422                 }
423
424                 // only computed methods can fail the following checks
425                 if (node.computed && node.value.type !== "FunctionExpression" && node.value.type !== "ArrowFunctionExpression") {
426                     return;
427                 }
428
429                 //--------------------------------------------------------------
430                 // Checks for property/method shorthand.
431                 if (isConciseProperty) {
432                     if (node.method && (APPLY_NEVER || AVOID_QUOTES && isStringLiteral(node.key))) {
433                         const message = APPLY_NEVER ? "Expected longform method syntax." : "Expected longform method syntax for string literal keys.";
434
435                         // { x() {} } should be written as { x: function() {} }
436                         context.report({
437                             node,
438                             message,
439                             fix: fixer => makeFunctionLongform(fixer, node)
440                         });
441                     } else if (APPLY_NEVER) {
442
443                         // { x } should be written as { x: x }
444                         context.report({
445                             node,
446                             message: "Expected longform property syntax.",
447                             fix: fixer => fixer.insertTextAfter(node.key, `: ${node.key.name}`)
448                         });
449                     }
450                 } else if (APPLY_TO_METHODS && !node.value.id && (node.value.type === "FunctionExpression" || node.value.type === "ArrowFunctionExpression")) {
451                     if (IGNORE_CONSTRUCTORS && node.key.type === "Identifier" && isConstructor(node.key.name)) {
452                         return;
453                     }
454                     if (AVOID_QUOTES && isStringLiteral(node.key)) {
455                         return;
456                     }
457
458                     // {[x]: function(){}} should be written as {[x]() {}}
459                     if (node.value.type === "FunctionExpression" ||
460                         node.value.type === "ArrowFunctionExpression" &&
461                         node.value.body.type === "BlockStatement" &&
462                         AVOID_EXPLICIT_RETURN_ARROWS &&
463                         !arrowsWithLexicalIdentifiers.has(node.value)
464                     ) {
465                         context.report({
466                             node,
467                             message: "Expected method shorthand.",
468                             fix: fixer => makeFunctionShorthand(fixer, node)
469                         });
470                     }
471                 } else if (node.value.type === "Identifier" && node.key.name === node.value.name && APPLY_TO_PROPS) {
472
473                     // {x: x} should be written as {x}
474                     context.report({
475                         node,
476                         message: "Expected property shorthand.",
477                         fix(fixer) {
478                             return fixer.replaceText(node, node.value.name);
479                         }
480                     });
481                 } else if (node.value.type === "Identifier" && node.key.type === "Literal" && node.key.value === node.value.name && APPLY_TO_PROPS) {
482                     if (AVOID_QUOTES) {
483                         return;
484                     }
485
486                     // {"x": x} should be written as {x}
487                     context.report({
488                         node,
489                         message: "Expected property shorthand.",
490                         fix(fixer) {
491                             return fixer.replaceText(node, node.value.name);
492                         }
493                     });
494                 }
495             }
496         };
497     }
498 };