1 |
|
2 |
|
3 |
|
4 |
|
5 | "use strict";
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | const astUtils = require("./utils/ast-utils");
|
12 |
|
13 |
|
14 |
|
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 |
|
63 |
|
64 |
|
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 |
|
85 |
|
86 |
|
87 |
|
88 | function getConfigForFunction(node) {
|
89 | if (node.type === "ArrowFunctionExpression") {
|
90 |
|
91 |
|
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 |
|
99 | } else if (!node.generator) {
|
100 | return overrideConfig.anonymous || baseConfig;
|
101 | }
|
102 |
|
103 | return "ignore";
|
104 | }
|
105 |
|
106 | |
107 |
|
108 |
|
109 |
|
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 => fixer.removeRange([leftToken.range[1], rightToken.range[0]])
|
128 | });
|
129 | } else if (!hasSpacing && functionConfig === "always") {
|
130 | context.report({
|
131 | node,
|
132 | loc: leftToken.loc.end,
|
133 | message: "Missing space before function parentheses.",
|
134 | fix: fixer => fixer.insertTextAfter(leftToken, " ")
|
135 | });
|
136 | }
|
137 | }
|
138 |
|
139 | return {
|
140 | ArrowFunctionExpression: checkFunction,
|
141 | FunctionDeclaration: checkFunction,
|
142 | FunctionExpression: checkFunction
|
143 | };
|
144 | }
|
145 | };
|