2 title: space-before-function-paren
7 - space-return-throw-case
12 When formatting a function, whitespace is allowed between the function name or `function` keyword and the opening paren. Named functions also require a space between the `function` keyword and the function name, but anonymous functions require no whitespace. For example:
15 function withoutSpace(x) {
19 function withSpace (x) {
23 var anonymousWithoutSpace = function() {};
25 var anonymousWithSpace = function () {};
28 Style guides may require a space after the `function` keyword for anonymous functions, while others specify no whitespace. Similarly, the space after a function name may or may not be required.
32 This rule aims to enforce consistent spacing before function parentheses and as such, will warn whenever whitespace doesn't match the preferences specified.
36 This rule has a string option or an object option:
40 "space-before-function-paren": ["error", "always"],
42 "space-before-function-paren": ["error", {
43 "anonymous": "always",
45 "asyncArrow": "always"
50 * `always` (default) requires a space followed by the `(` of arguments.
51 * `never` disallows any space followed by the `(` of arguments.
53 The string option does not check async arrow function expressions for backward compatibility.
55 You can also use a separate option for each type of function.
56 Each of the following options can be set to `"always"`, `"never"`, or `"ignore"`. The default is `"always"`.
58 * `anonymous` is for anonymous function expressions (e.g. `function () {}`).
59 * `named` is for named function expressions (e.g. `function foo () {}`).
60 * `asyncArrow` is for async arrow function expressions (e.g. `async () => {}`).
64 Examples of **incorrect** code for this rule with the default `"always"` option:
69 /*eslint space-before-function-paren: "error"*/
76 var bar = function() {
80 var bar = function foo() {
96 var foo = async() => 1
101 Examples of **correct** code for this rule with the default `"always"` option:
106 /*eslint space-before-function-paren: "error"*/
113 var bar = function () {
117 var bar = function foo () {
133 var foo = async () => 1
140 Examples of **incorrect** code for this rule with the `"never"` option:
145 /*eslint space-before-function-paren: ["error", "never"]*/
152 var bar = function () {
156 var bar = function foo () {
172 var foo = async () => 1
177 Examples of **correct** code for this rule with the `"never"` option:
182 /*eslint space-before-function-paren: ["error", "never"]*/
189 var bar = function() {
193 var bar = function foo() {
209 var foo = async() => 1
214 ### `{"anonymous": "always", "named": "never", "asyncArrow": "always"}`
216 Examples of **incorrect** code for this rule with the `{"anonymous": "always", "named": "never", "asyncArrow": "always"}` option:
221 /*eslint space-before-function-paren: ["error", {"anonymous": "always", "named": "never", "asyncArrow": "always"}]*/
228 var bar = function() {
244 var foo = async(a) => await a
249 Examples of **correct** code for this rule with the `{"anonymous": "always", "named": "never", "asyncArrow": "always"}` option:
254 /*eslint space-before-function-paren: ["error", {"anonymous": "always", "named": "never", "asyncArrow": "always"}]*/
261 var bar = function () {
277 var foo = async (a) => await a
282 ### `{"anonymous": "never", "named": "always"}`
284 Examples of **incorrect** code for this rule with the `{"anonymous": "never", "named": "always"}` option:
289 /*eslint space-before-function-paren: ["error", { "anonymous": "never", "named": "always" }]*/
296 var bar = function () {
315 Examples of **correct** code for this rule with the `{"anonymous": "never", "named": "always"}` option:
320 /*eslint space-before-function-paren: ["error", { "anonymous": "never", "named": "always" }]*/
327 var bar = function() {
346 ### `{"anonymous": "ignore", "named": "always"}`
348 Examples of **incorrect** code for this rule with the `{"anonymous": "ignore", "named": "always"}` option:
353 /*eslint space-before-function-paren: ["error", { "anonymous": "ignore", "named": "always" }]*/
375 Examples of **correct** code for this rule with the `{"anonymous": "ignore", "named": "always"}` option:
380 /*eslint space-before-function-paren: ["error", { "anonymous": "ignore", "named": "always" }]*/
383 var bar = function() {
387 var bar = function () {
410 ## When Not To Use It
412 You can turn this rule off if you are not concerned with the consistency of spacing before function parenthesis.