]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/rules/space-before-function-parentheses.md
first commit
[pve-eslint.git] / eslint / docs / rules / space-before-function-parentheses.md
CommitLineData
eb39fafa
DC
1# space-before-function-parentheses: enforce consistent spacing before opening parenthesis in function definitions
2
3(removed) This rule was **removed** in ESLint v1.0 and **replaced** by the [space-before-function-paren](space-before-function-paren.md) rule. The name of the rule changed from "parentheses" to "paren" for consistency with the names of other rules.
4
5When 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:
6
7```js
8function withoutSpace(x) {
9 // ...
10}
11
12function withSpace (x) {
13 // ...
14}
15
16var anonymousWithoutSpace = function() {};
17
18var anonymousWithSpace = function () {};
19```
20
21Style 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.
22
23## Rule Details
24
25This rule aims to enforce consistent spacing before function parentheses and as such, will warn whenever whitespace doesn't match the preferences specified.
26
27This rule takes one argument. If it is `"always"`, which is the default option, all named functions and anonymous functions must have space before function parentheses. If `"never"` then all named functions and anonymous functions must not have space before function parentheses. If you want different spacing for named and anonymous functions you can pass a configuration object as the rule argument to configure those separately (e. g. `{"anonymous": "always", "named": "never"}`).
28
29Examples of **incorrect** code for this rule with the default `"always"` option:
30
31```js
32/*eslint-env es6*/
33
34function foo() {
35 // ...
36}
37
38var bar = function() {
39 // ...
40};
41
42var bar = function foo() {
43 // ...
44};
45
46class Foo {
47 constructor() {
48 // ...
49 }
50}
51
52var foo = {
53 bar() {
54 // ...
55 }
56};
57```
58
59Examples of **correct** code for this rule with the default `"always"` option:
60
61```js
62/*eslint-env es6*/
63
64function foo () {
65 // ...
66}
67
68var bar = function () {
69 // ...
70};
71
72var bar = function foo () {
73 // ...
74};
75
76class Foo {
77 constructor () {
78 // ...
79 }
80}
81
82var foo = {
83 bar () {
84 // ...
85 }
86};
87```
88
89Examples of **incorrect** code for this rule with the `"never"` option:
90
91```js
92/*eslint-env es6*/
93
94function foo () {
95 // ...
96}
97
98var bar = function () {
99 // ...
100};
101
102var bar = function foo () {
103 // ...
104};
105
106class Foo {
107 constructor () {
108 // ...
109 }
110}
111
112var foo = {
113 bar () {
114 // ...
115 }
116};
117```
118
119Examples of **correct** code for this rule with the `"never"` option:
120
121```js
122/*eslint-env es6*/
123
124function foo() {
125 // ...
126}
127
128var bar = function() {
129 // ...
130};
131
132var bar = function foo() {
133 // ...
134};
135
136class Foo {
137 constructor() {
138 // ...
139 }
140}
141
142var foo = {
143 bar() {
144 // ...
145 }
146};
147```
148
149Examples of **incorrect** code for this rule with the `{"anonymous": "always", "named": "never"}` option:
150
151```js
152/*eslint-env es6*/
153
154function foo () {
155 // ...
156}
157
158var bar = function() {
159 // ...
160};
161
162class Foo {
163 constructor () {
164 // ...
165 }
166}
167
168var foo = {
169 bar () {
170 // ...
171 }
172};
173```
174
175Examples of **correct** code for this rule with the `{"anonymous": "always", "named": "never"}` option:
176
177```js
178/*eslint-env es6*/
179
180function foo() {
181 // ...
182}
183
184var bar = function () {
185 // ...
186};
187
188class Foo {
189 constructor() {
190 // ...
191 }
192}
193
194var foo = {
195 bar() {
196 // ...
197 }
198};
199```
200
201Examples of **incorrect** code for this rule with the `{"anonymous": "never", "named": "always"}` option:
202
203```js
204/*eslint-env es6*/
205
206function foo() {
207 // ...
208}
209
210var bar = function () {
211 // ...
212};
213
214class Foo {
215 constructor() {
216 // ...
217 }
218}
219
220var foo = {
221 bar() {
222 // ...
223 }
224};
225```
226
227Examples of **correct** code for this rule with the `{"anonymous": "never", "named": "always"}` option:
228
229```js
230/*eslint-env es6*/
231
232function foo () {
233 // ...
234}
235
236var bar = function() {
237 // ...
238};
239
240class Foo {
241 constructor () {
242 // ...
243 }
244}
245
246var foo = {
247 bar () {
248 // ...
249 }
250};
251```
252
253## When Not To Use It
254
255You can turn this rule off if you are not concerned with the consistency of spacing before function parenthesis.
256
257## Related Rules
258
259* [space-after-keywords](space-after-keywords.md)
260* [space-return-throw-case](space-return-throw-case.md)