UNPKG

5.13 kBJavaScriptView Raw
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
11const astUtils = require("./utils/ast-utils");
12
13//------------------------------------------------------------------------------
14// Rule Definition
15//------------------------------------------------------------------------------
16
17module.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 => 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};