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
56 unexpectedSpace: "Unexpected space before function parentheses.",
57 missingSpace: "Missing space before function parentheses."
62 const sourceCode = context.getSourceCode();
63 const baseConfig = typeof context.options[0] === "string" ? context.options[0] : "always";
64 const overrideConfig = typeof context.options[0] === "object" ? context.options[0] : {};
67 * Determines whether a function has a name.
68 * @param {ASTNode} node The function node.
69 * @returns {boolean} Whether the function has a name.
71 function isNamedFunction(node) {
76 const parent = node.parent;
78 return parent.type === "MethodDefinition" ||
79 (parent.type === "Property" &&
81 parent.kind === "get" ||
82 parent.kind === "set" ||
89 * Gets the config for a given function
90 * @param {ASTNode} node The function node
91 * @returns {string} "always", "never", or "ignore"
93 function getConfigForFunction(node) {
94 if (node.type === "ArrowFunctionExpression") {
96 // Always ignore non-async functions and arrow functions without parens, e.g. async foo => bar
97 if (node.async && astUtils.isOpeningParenToken(sourceCode.getFirstToken(node, { skip: 1 }))) {
98 return overrideConfig.asyncArrow || baseConfig;
100 } else if (isNamedFunction(node)) {
101 return overrideConfig.named || baseConfig;
103 // `generator-star-spacing` should warn anonymous generators. E.g. `function* () {}`
104 } else if (!node.generator) {
105 return overrideConfig.anonymous || baseConfig;
112 * Checks the parens of a function node
113 * @param {ASTNode} node A function node
116 function checkFunction(node) {
117 const functionConfig = getConfigForFunction(node);
119 if (functionConfig === "ignore") {
123 const rightToken = sourceCode.getFirstToken(node, astUtils.isOpeningParenToken);
124 const leftToken = sourceCode.getTokenBefore(rightToken);
125 const hasSpacing = sourceCode.isSpaceBetweenTokens(leftToken, rightToken);
127 if (hasSpacing && functionConfig === "never") {
131 start: leftToken.loc.end,
132 end: rightToken.loc.start
134 messageId: "unexpectedSpace",
136 const comments = sourceCode.getCommentsBefore(rightToken);
138 // Don't fix anything if there's a single line comment between the left and the right token
139 if (comments.some(comment => comment.type === "Line")) {
142 return fixer.replaceTextRange(
143 [leftToken.range[1], rightToken.range[0]],
144 comments.reduce((text, comment) => text + sourceCode.getText(comment), "")
148 } else if (!hasSpacing && functionConfig === "always") {
152 messageId: "missingSpace",
153 fix: fixer => fixer.insertTextAfter(leftToken, " ")
159 ArrowFunctionExpression: checkFunction,
160 FunctionDeclaration: checkFunction,
161 FunctionExpression: checkFunction