0
# Function Formatting
1
2
Rules that enforce consistent formatting for function declarations, calls, and parameters, including parentheses and argument positioning.
3
4
## function-call-spacing
5
6
Requires or disallows spacing between function identifiers and their invocations.
7
8
```javascript { .api }
9
const functionCallSpacing: Rule.RuleModule;
10
11
// Rule options
12
type FunctionCallSpacingOptions =
13
| 'always'
14
| 'never'
15
| ['always' | 'never', {
16
allowNewlines?: boolean;
17
}];
18
```
19
20
**Usage Examples:**
21
22
```javascript
23
// ✓ Good with "never" (default)
24
const result = myFunction();
25
const result = myFunction(arg1, arg2);
26
27
// ✓ Good with "always"
28
const result = myFunction ();
29
const result = myFunction (arg1, arg2);
30
31
// ✓ Good with { allowNewlines: true }
32
const result = myFunction
33
();
34
```
35
36
**Configuration:**
37
38
```javascript
39
rules: {
40
'@stylistic/js/function-call-spacing': ['error', 'never']
41
}
42
```
43
44
## func-call-spacing
45
46
Deprecated alias for `function-call-spacing`. Use `function-call-spacing` instead.
47
48
```javascript { .api }
49
const funcCallSpacing: Rule.RuleModule; // @deprecated
50
```
51
52
## function-call-argument-newline
53
54
Enforces line breaks between arguments of a function call.
55
56
```javascript { .api }
57
const functionCallArgumentNewline: Rule.RuleModule;
58
59
// Rule options
60
type FunctionCallArgumentNewlineOptions =
61
| 'always'
62
| 'never'
63
| 'consistent';
64
```
65
66
**Usage Examples:**
67
68
```javascript
69
// ✓ Good with "always"
70
myFunction(
71
arg1,
72
arg2,
73
arg3
74
);
75
76
// ✓ Good with "never"
77
myFunction(arg1, arg2, arg3);
78
79
// ✓ Good with "consistent"
80
myFunction(arg1, arg2); // all same line
81
myFunction(
82
arg1,
83
arg2,
84
arg3
85
); // all separate lines
86
```
87
88
**Configuration:**
89
90
```javascript
91
rules: {
92
'@stylistic/js/function-call-argument-newline': ['error', 'consistent']
93
}
94
```
95
96
## function-paren-newline
97
98
Enforces consistent line breaks inside function parentheses.
99
100
```javascript { .api }
101
const functionParenNewline: Rule.RuleModule;
102
103
// Rule options
104
type FunctionParenNewlineOptions =
105
| 'always'
106
| 'never'
107
| 'consistent'
108
| 'multiline'
109
| 'multiline-arguments'
110
| {
111
minItems?: number;
112
};
113
```
114
115
**Usage Examples:**
116
117
```javascript
118
// ✓ Good with "multiline"
119
function short(a, b) {} // single line OK
120
121
function long(
122
parameterOne,
123
parameterTwo,
124
parameterThree
125
) {} // multiline parameters require newlines
126
127
// ✓ Good with { minItems: 3 }
128
function few(a, b) {} // under threshold
129
function many(
130
a,
131
b,
132
c
133
) {} // meets threshold
134
```
135
136
**Configuration:**
137
138
```javascript
139
rules: {
140
'@stylistic/js/function-paren-newline': ['error', 'multiline']
141
}
142
```
143
144
## space-before-function-paren
145
146
Enforces consistent spacing before function definition opening parentheses.
147
148
```javascript { .api }
149
const spaceBeforeFunctionParen: Rule.RuleModule;
150
151
// Rule options
152
type SpaceBeforeFunctionParenOptions =
153
| 'always'
154
| 'never'
155
| {
156
anonymous?: 'always' | 'never' | 'ignore';
157
named?: 'always' | 'never' | 'ignore';
158
asyncArrow?: 'always' | 'never' | 'ignore';
159
};
160
```
161
162
**Usage Examples:**
163
164
```javascript
165
// ✓ Good with "never" (default)
166
function foo() {}
167
const bar = function() {};
168
const baz = () => {};
169
170
// ✓ Good with "always"
171
function foo () {}
172
const bar = function () {};
173
const baz = () => {};
174
175
// ✓ Good with custom config
176
function foo() {} // named: 'never'
177
const bar = function () {}; // anonymous: 'always'
178
const baz = async () => {}; // asyncArrow: 'never'
179
```
180
181
**Configuration:**
182
183
```javascript
184
rules: {
185
'@stylistic/js/space-before-function-paren': ['error', {
186
anonymous: 'always',
187
named: 'never',
188
asyncArrow: 'always'
189
}]
190
}
191
```
192
193
## new-parens
194
195
Enforces or disallows parentheses when invoking a constructor with no arguments.
196
197
```javascript { .api }
198
const newParens: Rule.RuleModule;
199
200
// Rule options
201
type NewParensOptions =
202
| 'always'
203
| 'never';
204
```
205
206
**Usage Examples:**
207
208
```javascript
209
// ✓ Good with "always" (default)
210
const person = new Person();
211
const date = new Date();
212
213
// ✓ Good with "never"
214
const person = new Person;
215
const date = new Date;
216
217
// Constructor with arguments always requires parens
218
const person = new Person('John', 30); // always required
219
```
220
221
**Configuration:**
222
223
```javascript
224
rules: {
225
'@stylistic/js/new-parens': ['error', 'always']
226
}
227
```
228
229
## space-in-parens
230
231
Enforces consistent spacing inside parentheses.
232
233
```javascript { .api }
234
const spaceInParens: Rule.RuleModule;
235
236
// Rule options
237
type SpaceInParensOptions =
238
| 'always'
239
| 'never'
240
| ['always' | 'never', {
241
exceptions?: string[];
242
}];
243
```
244
245
**Usage Examples:**
246
247
```javascript
248
// ✓ Good with "never" (default)
249
function foo(arg1, arg2) {}
250
const result = foo(a, b);
251
252
// ✓ Good with "always"
253
function foo( arg1, arg2 ) {}
254
const result = foo( a, b );
255
256
// ✓ Good with exceptions
257
const result = foo(a, b); // normal call
258
const empty = foo(); // exception for empty
259
```
260
261
**Configuration:**
262
263
```javascript
264
rules: {
265
'@stylistic/js/space-in-parens': ['error', 'never']
266
}
267
```
268
269
## Common Function Formatting Combinations
270
271
### Compact Function Style
272
```javascript
273
rules: {
274
'@stylistic/js/function-call-spacing': ['error', 'never'],
275
'@stylistic/js/space-before-function-paren': ['error', 'never'],
276
'@stylistic/js/space-in-parens': ['error', 'never'],
277
'@stylistic/js/function-call-argument-newline': ['error', 'consistent']
278
}
279
280
// Result:
281
function myFunction(a, b) {
282
return myOtherFunction(a, b);
283
}
284
```
285
286
### Spaced Function Style
287
```javascript
288
rules: {
289
'@stylistic/js/function-call-spacing': ['error', 'never'],
290
'@stylistic/js/space-before-function-paren': ['error', 'always'],
291
'@stylistic/js/space-in-parens': ['error', 'always'],
292
'@stylistic/js/function-paren-newline': ['error', 'multiline']
293
}
294
295
// Result:
296
function myFunction ( a, b ) {
297
return myOtherFunction( a, b );
298
}
299
```
300
301
## Type Definitions
302
303
```typescript { .api }
304
interface FunctionFormattingRules {
305
'function-call-spacing': Rule.RuleModule;
306
'func-call-spacing': Rule.RuleModule; // deprecated
307
'function-call-argument-newline': Rule.RuleModule;
308
'function-paren-newline': Rule.RuleModule;
309
'space-before-function-paren': Rule.RuleModule;
310
'new-parens': Rule.RuleModule;
311
'space-in-parens': Rule.RuleModule;
312
}
313
314
interface FunctionSpacingConfig {
315
anonymous?: 'always' | 'never' | 'ignore';
316
named?: 'always' | 'never' | 'ignore';
317
asyncArrow?: 'always' | 'never' | 'ignore';
318
}
319
```