]>
Commit | Line | Data |
---|---|---|
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 | ||
5 | 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: | |
6 | ||
7 | ```js | |
8 | function withoutSpace(x) { | |
9 | // ... | |
10 | } | |
11 | ||
12 | function withSpace (x) { | |
13 | // ... | |
14 | } | |
15 | ||
16 | var anonymousWithoutSpace = function() {}; | |
17 | ||
18 | var anonymousWithSpace = function () {}; | |
19 | ``` | |
20 | ||
21 | 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. | |
22 | ||
23 | ## Rule Details | |
24 | ||
25 | This rule aims to enforce consistent spacing before function parentheses and as such, will warn whenever whitespace doesn't match the preferences specified. | |
26 | ||
27 | This 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 | ||
29 | Examples of **incorrect** code for this rule with the default `"always"` option: | |
30 | ||
31 | ```js | |
32 | /*eslint-env es6*/ | |
33 | ||
34 | function foo() { | |
35 | // ... | |
36 | } | |
37 | ||
38 | var bar = function() { | |
39 | // ... | |
40 | }; | |
41 | ||
42 | var bar = function foo() { | |
43 | // ... | |
44 | }; | |
45 | ||
46 | class Foo { | |
47 | constructor() { | |
48 | // ... | |
49 | } | |
50 | } | |
51 | ||
52 | var foo = { | |
53 | bar() { | |
54 | // ... | |
55 | } | |
56 | }; | |
57 | ``` | |
58 | ||
59 | Examples of **correct** code for this rule with the default `"always"` option: | |
60 | ||
61 | ```js | |
62 | /*eslint-env es6*/ | |
63 | ||
64 | function foo () { | |
65 | // ... | |
66 | } | |
67 | ||
68 | var bar = function () { | |
69 | // ... | |
70 | }; | |
71 | ||
72 | var bar = function foo () { | |
73 | // ... | |
74 | }; | |
75 | ||
76 | class Foo { | |
77 | constructor () { | |
78 | // ... | |
79 | } | |
80 | } | |
81 | ||
82 | var foo = { | |
83 | bar () { | |
84 | // ... | |
85 | } | |
86 | }; | |
87 | ``` | |
88 | ||
89 | Examples of **incorrect** code for this rule with the `"never"` option: | |
90 | ||
91 | ```js | |
92 | /*eslint-env es6*/ | |
93 | ||
94 | function foo () { | |
95 | // ... | |
96 | } | |
97 | ||
98 | var bar = function () { | |
99 | // ... | |
100 | }; | |
101 | ||
102 | var bar = function foo () { | |
103 | // ... | |
104 | }; | |
105 | ||
106 | class Foo { | |
107 | constructor () { | |
108 | // ... | |
109 | } | |
110 | } | |
111 | ||
112 | var foo = { | |
113 | bar () { | |
114 | // ... | |
115 | } | |
116 | }; | |
117 | ``` | |
118 | ||
119 | Examples of **correct** code for this rule with the `"never"` option: | |
120 | ||
121 | ```js | |
122 | /*eslint-env es6*/ | |
123 | ||
124 | function foo() { | |
125 | // ... | |
126 | } | |
127 | ||
128 | var bar = function() { | |
129 | // ... | |
130 | }; | |
131 | ||
132 | var bar = function foo() { | |
133 | // ... | |
134 | }; | |
135 | ||
136 | class Foo { | |
137 | constructor() { | |
138 | // ... | |
139 | } | |
140 | } | |
141 | ||
142 | var foo = { | |
143 | bar() { | |
144 | // ... | |
145 | } | |
146 | }; | |
147 | ``` | |
148 | ||
149 | Examples of **incorrect** code for this rule with the `{"anonymous": "always", "named": "never"}` option: | |
150 | ||
151 | ```js | |
152 | /*eslint-env es6*/ | |
153 | ||
154 | function foo () { | |
155 | // ... | |
156 | } | |
157 | ||
158 | var bar = function() { | |
159 | // ... | |
160 | }; | |
161 | ||
162 | class Foo { | |
163 | constructor () { | |
164 | // ... | |
165 | } | |
166 | } | |
167 | ||
168 | var foo = { | |
169 | bar () { | |
170 | // ... | |
171 | } | |
172 | }; | |
173 | ``` | |
174 | ||
175 | Examples of **correct** code for this rule with the `{"anonymous": "always", "named": "never"}` option: | |
176 | ||
177 | ```js | |
178 | /*eslint-env es6*/ | |
179 | ||
180 | function foo() { | |
181 | // ... | |
182 | } | |
183 | ||
184 | var bar = function () { | |
185 | // ... | |
186 | }; | |
187 | ||
188 | class Foo { | |
189 | constructor() { | |
190 | // ... | |
191 | } | |
192 | } | |
193 | ||
194 | var foo = { | |
195 | bar() { | |
196 | // ... | |
197 | } | |
198 | }; | |
199 | ``` | |
200 | ||
201 | Examples of **incorrect** code for this rule with the `{"anonymous": "never", "named": "always"}` option: | |
202 | ||
203 | ```js | |
204 | /*eslint-env es6*/ | |
205 | ||
206 | function foo() { | |
207 | // ... | |
208 | } | |
209 | ||
210 | var bar = function () { | |
211 | // ... | |
212 | }; | |
213 | ||
214 | class Foo { | |
215 | constructor() { | |
216 | // ... | |
217 | } | |
218 | } | |
219 | ||
220 | var foo = { | |
221 | bar() { | |
222 | // ... | |
223 | } | |
224 | }; | |
225 | ``` | |
226 | ||
227 | Examples of **correct** code for this rule with the `{"anonymous": "never", "named": "always"}` option: | |
228 | ||
229 | ```js | |
230 | /*eslint-env es6*/ | |
231 | ||
232 | function foo () { | |
233 | // ... | |
234 | } | |
235 | ||
236 | var bar = function() { | |
237 | // ... | |
238 | }; | |
239 | ||
240 | class Foo { | |
241 | constructor () { | |
242 | // ... | |
243 | } | |
244 | } | |
245 | ||
246 | var foo = { | |
247 | bar () { | |
248 | // ... | |
249 | } | |
250 | }; | |
251 | ``` | |
252 | ||
253 | ## When Not To Use It | |
254 | ||
255 | You 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) |