minor adjustment to readme
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / eslint / lib / rules / space-before-function-paren.js
1 /**
2  * @fileoverview Rule to validate spacing before function paren.
3  * @author Mathias Schreck <https://github.com/lo1tuma>
4  */
5 "use strict";
6
7 //------------------------------------------------------------------------------
8 // Requirements
9 //------------------------------------------------------------------------------
10
11 const astUtils = require("./utils/ast-utils");
12
13 //------------------------------------------------------------------------------
14 // Rule Definition
15 //------------------------------------------------------------------------------
16
17 module.exports = {
18     meta: {
19         type: "layout",
20
21         docs: {
22             description: "enforce consistent spacing before `function` definition opening parenthesis",
23             category: "Stylistic Issues",
24             recommended: false,
25             url: "https://eslint.org/docs/rules/space-before-function-paren"
26         },
27
28         fixable: "whitespace",
29
30         schema: [
31             {
32                 oneOf: [
33                     {
34                         enum: ["always", "never"]
35                     },
36                     {
37                         type: "object",
38                         properties: {
39                             anonymous: {
40                                 enum: ["always", "never", "ignore"]
41                             },
42                             named: {
43                                 enum: ["always", "never", "ignore"]
44                             },
45                             asyncArrow: {
46                                 enum: ["always", "never", "ignore"]
47                             }
48                         },
49                         additionalProperties: false
50                     }
51                 ]
52             }
53         ]
54     },
55
56     create(context) {
57         const sourceCode = context.getSourceCode();
58         const baseConfig = typeof context.options[0] === "string" ? context.options[0] : "always";
59         const overrideConfig = typeof context.options[0] === "object" ? context.options[0] : {};
60
61         /**
62          * Determines whether a function has a name.
63          * @param {ASTNode} node The function node.
64          * @returns {boolean} Whether the function has a name.
65          */
66         function isNamedFunction(node) {
67             if (node.id) {
68                 return true;
69             }
70
71             const parent = node.parent;
72
73             return parent.type === "MethodDefinition" ||
74                 (parent.type === "Property" &&
75                     (
76                         parent.kind === "get" ||
77                         parent.kind === "set" ||
78                         parent.method
79                     )
80                 );
81         }
82
83         /**
84          * Gets the config for a given function
85          * @param {ASTNode} node The function node
86          * @returns {string} "always", "never", or "ignore"
87          */
88         function getConfigForFunction(node) {
89             if (node.type === "ArrowFunctionExpression") {
90
91                 // Always ignore non-async functions and arrow functions without parens, e.g. async foo => bar
92                 if (node.async && astUtils.isOpeningParenToken(sourceCode.getFirstToken(node, { skip: 1 }))) {
93                     return overrideConfig.asyncArrow || baseConfig;
94                 }
95             } else if (isNamedFunction(node)) {
96                 return overrideConfig.named || baseConfig;
97
98             // `generator-star-spacing` should warn anonymous generators. E.g. `function* () {}`
99             } else if (!node.generator) {
100                 return overrideConfig.anonymous || baseConfig;
101             }
102
103             return "ignore";
104         }
105
106         /**
107          * Checks the parens of a function node
108          * @param {ASTNode} node A function node
109          * @returns {void}
110          */
111         function checkFunction(node) {
112             const functionConfig = getConfigForFunction(node);
113
114             if (functionConfig === "ignore") {
115                 return;
116             }
117
118             const rightToken = sourceCode.getFirstToken(node, astUtils.isOpeningParenToken);
119             const leftToken = sourceCode.getTokenBefore(rightToken);
120             const hasSpacing = sourceCode.isSpaceBetweenTokens(leftToken, rightToken);
121
122             if (hasSpacing && functionConfig === "never") {
123                 context.report({
124                     node,
125                     loc: leftToken.loc.end,
126                     message: "Unexpected space before function parentheses.",
127                     fix(fixer) {
128                         const comments = sourceCode.getCommentsBefore(rightToken);
129
130                         // Don't fix anything if there's a single line comment between the left and the right token
131                         if (comments.some(comment => comment.type === "Line")) {
132                             return null;
133                         }
134                         return fixer.replaceTextRange(
135                             [leftToken.range[1], rightToken.range[0]],
136                             comments.reduce((text, comment) => text + sourceCode.getText(comment), "")
137                         );
138                     }
139                 });
140             } else if (!hasSpacing && functionConfig === "always") {
141                 context.report({
142                     node,
143                     loc: leftToken.loc.end,
144                     message: "Missing space before function parentheses.",
145                     fix: fixer => fixer.insertTextAfter(leftToken, " ")
146                 });
147             }
148         }
149
150         return {
151             ArrowFunctionExpression: checkFunction,
152             FunctionDeclaration: checkFunction,
153             FunctionExpression: checkFunction
154         };
155     }
156 };