1
1
# Style guides and linting
2
+
2
3
See the relevant style guides for our guidelines and for information on linting:
3
4
4
5
## JavaScript
6
+
5
7
We defer to [ Airbnb] [ airbnb-js-style-guide ] on most style-related
6
8
conventions and enforce them with eslint.
7
9
8
- See [ our current .eslintrc] [ eslintrc ] for specific rules and patterns.
10
+ See [ our current .eslintrc] ( https://gitlab.com/gitlab-org/gitlab-ce/blob/master/. eslintrc.yml ) for specific rules and patterns.
9
11
10
12
### Common
11
13
@@ -21,10 +23,10 @@ refactor an existing one, you should abide by the eslint rules.
21
23
``` javascript
22
24
// bad
23
25
/* eslint-disable */
24
-
26
+
25
27
// better
26
28
/* eslint-disable some-rule, some-other-rule */
27
-
29
+
28
30
// best
29
31
// nothing :)
30
32
```
@@ -34,14 +36,14 @@ refactor an existing one, you should abide by the eslint rules.
34
36
` ` ` javascript
35
37
// bad
36
38
/* eslint-disable no-new */
37
-
39
+
38
40
import Foo from 'foo';
39
-
41
+
40
42
new Foo();
41
-
43
+
42
44
// better
43
45
import Foo from 'foo';
44
-
46
+
45
47
// eslint-disable-next-line no-new
46
48
new Foo();
47
49
` ` `
@@ -58,11 +60,11 @@ followed by any global declarations, then a blank newline prior to any imports o
58
60
/* global Foo */
59
61
/* eslint-disable no-new */
60
62
import Bar from './bar';
61
-
63
+
62
64
// good
63
65
/* eslint-disable no-new */
64
66
/* global Foo */
65
-
67
+
66
68
import Bar from './bar';
67
69
` ` `
68
70
@@ -73,7 +75,7 @@ followed by any global declarations, then a blank newline prior to any imports o
73
75
` ` ` javascript
74
76
// bad
75
77
/* globals Flash, Cookies, jQuery */
76
-
78
+
77
79
// good
78
80
/* global Flash */
79
81
/* global Cookies */
@@ -85,7 +87,7 @@ followed by any global declarations, then a blank newline prior to any imports o
85
87
```javascript
86
88
// bad
87
89
fn (p1 , p2 , p3 , p4 ) {}
88
-
90
+
89
91
// good
90
92
fn (options ) {}
91
93
```
@@ -191,28 +193,28 @@ Do not use them anymore and feel free to remove them when refactoring legacy cod
191
193
` ` ` javascript
192
194
// bad
193
195
const values = {foo: 1 };
194
-
196
+
195
197
function impureFunction (items ) {
196
198
const bar = 1 ;
197
-
199
+
198
200
items .foo = items .a * bar + 2 ;
199
-
201
+
200
202
return items .a ;
201
203
}
202
-
204
+
203
205
const c = impureFunction (values);
204
-
206
+
205
207
// good
206
208
var values = {foo: 1 };
207
-
209
+
208
210
function pureFunction (foo ) {
209
211
var bar = 1 ;
210
-
212
+
211
213
foo = foo * bar + 2 ;
212
-
214
+
213
215
return foo;
214
216
}
215
-
217
+
216
218
var c = pureFunction (values .foo );
217
219
` ` `
218
220
@@ -231,10 +233,10 @@ Do not use them anymore and feel free to remove them when refactoring legacy cod
231
233
document .addEventListener (' click' , this .handleCallback )
232
234
},
233
235
handleCallback () {
234
-
236
+
235
237
}
236
238
}
237
-
239
+
238
240
// Good
239
241
export class Foo {
240
242
constructor () {
@@ -253,12 +255,12 @@ Do not use them anymore and feel free to remove them when refactoring legacy cod
253
255
254
256
` ` ` javascript
255
257
const users = [ { name: ' Foo' }, { name: ' Bar' } ];
256
-
258
+
257
259
// bad
258
260
users .forEach ((user , index ) => {
259
261
user .id = index;
260
262
});
261
-
263
+
262
264
// good
263
265
const usersWithId = users .map ((user , index ) => {
264
266
return Object .assign ({}, user, { id: index });
@@ -272,10 +274,10 @@ Do not use them anymore and feel free to remove them when refactoring legacy cod
272
274
` ` ` javascript
273
275
// bad
274
276
+ ' 10' // 10
275
-
277
+
276
278
// good
277
279
Number (' 10' ) // 10
278
-
280
+
279
281
// better
280
282
parseInt (' 10' , 10 );
281
283
` ` `
@@ -289,7 +291,7 @@ Do not use them anymore and feel free to remove them when refactoring legacy cod
289
291
< button class = " add-user" >
290
292
Add User
291
293
< / button>
292
-
294
+
293
295
// good
294
296
< button class = " js-add-user" >
295
297
Add User
@@ -299,10 +301,12 @@ Do not use them anymore and feel free to remove them when refactoring legacy cod
299
301
### Vue.js
300
302
301
303
#### ` eslint- vue- plugin`
304
+
302
305
We default to [eslint-vue-plugin][eslint-plugin-vue], with the ` plugin: vue/ recommended` .
303
306
Please check this [rules][eslint-plugin-vue-rules] for more documentation.
304
307
305
308
#### Basic Rules
309
+
306
310
1. The service has it's own file
307
311
1. The store has it's own file
308
312
1. Use a function in the bundle file to instantiate the Vue component:
@@ -314,7 +318,7 @@ Please check this [rules][eslint-plugin-vue-rules] for more documentation.
314
318
new Component ({})
315
319
}
316
320
}
317
-
321
+
318
322
// good
319
323
document .addEventListener (' DOMContentLoaded' , () => new Vue ({
320
324
el: ' #element' ,
@@ -336,7 +340,7 @@ Please check this [rules][eslint-plugin-vue-rules] for more documentation.
336
340
}
337
341
}
338
342
}
339
-
343
+
340
344
// good
341
345
class Store {
342
346
constructor () {
@@ -354,14 +358,14 @@ Please check this [rules][eslint-plugin-vue-rules] for more documentation.
354
358
` ` ` javascript
355
359
// bad
356
360
import cardBoard from ' cardBoard.vue'
357
-
361
+
358
362
components: {
359
363
cardBoard,
360
364
};
361
-
365
+
362
366
// good
363
367
import CardBoard from ' cardBoard.vue'
364
-
368
+
365
369
components: {
366
370
CardBoard,
367
371
};
@@ -373,20 +377,21 @@ Please check this [rules][eslint-plugin-vue-rules] for more documentation.
373
377
` ` ` javascript
374
378
// bad
375
379
< component class = " btn" >
376
-
380
+
377
381
// good
378
382
< component css- class = " btn" >
379
-
383
+
380
384
// bad
381
385
< component myProp= " prop" / >
382
-
386
+
383
387
// good
384
388
< component my- prop= " prop" / >
385
389
` ` `
386
390
387
391
[#34371]: https://gitlab.com/gitlab-org/gitlab-ce/issues/34371
388
392
389
393
#### Alignment
394
+
390
395
1. Follow these alignment styles for the template method:
391
396
392
397
1. With more than one attribute, all attributes should be on a new line:
@@ -395,31 +400,31 @@ Please check this [rules][eslint-plugin-vue-rules] for more documentation.
395
400
// bad
396
401
< component v- if = " bar"
397
402
param= " baz" / >
398
-
403
+
399
404
< button class = " btn" > Click me< / button>
400
-
405
+
401
406
// good
402
407
< component
403
408
v- if = " bar"
404
409
param= " baz"
405
410
/ >
406
-
411
+
407
412
< button class = " btn" >
408
413
Click me
409
414
< / button>
410
415
` ` `
411
-
416
+
412
417
1. The tag can be inline if there is only one attribute:
413
418
414
419
` ` ` javascript
415
420
// good
416
421
< component bar= " bar" / >
417
-
422
+
418
423
// good
419
424
< component
420
425
bar= " bar"
421
426
/ >
422
-
427
+
423
428
// bad
424
429
< component
425
430
bar= " bar" / >
@@ -434,7 +439,7 @@ Please check this [rules][eslint-plugin-vue-rules] for more documentation.
434
439
template: `
435
440
<button :class='style'>Button</button>
436
441
`
437
-
442
+
438
443
// good
439
444
template: `
440
445
<button :class=" style" >Button</button>
@@ -447,7 +452,7 @@ Please check this [rules][eslint-plugin-vue-rules] for more documentation.
447
452
```javascript
448
453
// bad
449
454
props: ['foo']
450
-
455
+
451
456
// good
452
457
props: {
453
458
foo: {
@@ -467,7 +472,7 @@ Please check this [rules][eslint-plugin-vue-rules] for more documentation.
467
472
type: String,
468
473
}
469
474
}
470
-
475
+
471
476
// good
472
477
props: {
473
478
foo: {
@@ -490,7 +495,7 @@ On those a default key should not be provided.
490
495
required: false,
491
496
}
492
497
}
493
-
498
+
494
499
// good
495
500
props: {
496
501
foo: {
@@ -499,7 +504,7 @@ On those a default key should not be provided.
499
504
default: 'bar'
500
505
}
501
506
}
502
-
507
+
503
508
// good
504
509
props: {
505
510
foo: {
@@ -534,7 +539,7 @@ On those a default key should not be provided.
534
539
```javascript
535
540
// bad
536
541
<component v-on:click=" eventHandler" />
537
-
542
+
538
543
// good
539
544
<component @click=" eventHandler" />
540
545
```
@@ -544,7 +549,7 @@ On those a default key should not be provided.
544
549
```javascript
545
550
// bad
546
551
<component v-bind:class=" btn" />
547
-
552
+
548
553
// good
549
554
<component :class=" btsn" />
550
555
```
@@ -556,7 +561,7 @@ On those a default key should not be provided.
556
561
```javascript
557
562
// bad
558
563
<component></component>
559
-
564
+
560
565
// good
561
566
<component />
562
567
```
@@ -650,7 +655,7 @@ Useful links:
650
655
title=" Some tooltip text" >
651
656
Text
652
657
</span>
653
-
658
+
654
659
// good
655
660
<span
656
661
v-tooltip
@@ -666,10 +671,10 @@ Useful links:
666
671
```javascript
667
672
// bad
668
673
<span data-original-title=" tooltip text" >Foo</span>
669
-
674
+
670
675
// good
671
676
<span title=" tooltip text" >Foo</span>
672
-
677
+
673
678
$('span').tooltip('_fixTitle');
674
679
```
675
680
0 commit comments