2 * @fileoverview Rule to validate spacing before function paren.
3 * @author Mathias Schreck <https://github.com/lo1tuma>
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
11 const astUtils = require("./utils/ast-utils");
13 //------------------------------------------------------------------------------
15 //------------------------------------------------------------------------------
22 description: "enforce consistent spacing before `function` definition opening parenthesis",
23 category: "Stylistic Issues",
25 url: "https://eslint.org/docs/rules/space-before-function-paren"
28 fixable: "whitespace",
34 enum: ["always", "never"]
40 enum: ["always", "never", "ignore"]
43 enum: ["always", "never", "ignore"]
46 enum: ["always", "never", "ignore"]
49 additionalProperties: false
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] : {};
62 * Determines whether a function has a name.
63 * @param {ASTNode} node The function node.
64 * @returns {boolean} Whether the function has a name.
66 function isNamedFunction(node) {
71 const parent = node.parent;
73 return parent.type === "MethodDefinition" ||
74 (parent.type === "Property" &&
76 parent.kind === "get" ||
77 parent.kind === "set" ||
84 * Gets the config for a given function
85 * @param {ASTNode} node The function node
86 * @returns {string} "always", "never", or "ignore"
88 function getConfigForFunction(node) {
89 if (node.type === "ArrowFunctionExpression") {
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;
95 } else if (isNamedFunction(node)) {
96 return overrideConfig.named || baseConfig;
98 // `generator-star-spacing` should warn anonymous generators. E.g. `function* () {}`
99 } else if (!node.generator) {
100 return overrideConfig.anonymous || baseConfig;
107 * Checks the parens of a function node
108 * @param {ASTNode} node A function node
111 function checkFunction(node) {
112 const functionConfig = getConfigForFunction(node);
114 if (functionConfig === "ignore") {
118 const rightToken = sourceCode.getFirstToken(node, astUtils.isOpeningParenToken);
119 const leftToken = sourceCode.getTokenBefore(rightToken);
120 const hasSpacing = sourceCode.isSpaceBetweenTokens(leftToken, rightToken);
122 if (hasSpacing && functionConfig === "never") {
125 loc: leftToken.loc.end,
126 message: "Unexpected space before function parentheses.",
128 const comments = sourceCode.getCommentsBefore(rightToken);
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")) {
134 return fixer.replaceTextRange(
135 [leftToken.range[1], rightToken.range[0]],
136 comments.reduce((text, comment) => text + sourceCode.getText(comment), "")
140 } else if (!hasSpacing && functionConfig === "always") {
143 loc: leftToken.loc.end,
144 message: "Missing space before function parentheses.",
145 fix: fixer => fixer.insertTextAfter(leftToken, " ")
151 ArrowFunctionExpression: checkFunction,
152 FunctionDeclaration: checkFunction,
153 FunctionExpression: checkFunction