eslint-config-dicodingacademy 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +608 -0
  2. package/index.js +26 -0
  3. package/package.json +27 -0
package/README.md ADDED
@@ -0,0 +1,608 @@
1
+ # Dicoding Academy JavaScript - Style Guide
2
+
3
+ ## Introduction
4
+
5
+ > Ada banyak sekali opsi yang bisa diikuti perihal gaya penulisan kode JavaScript. Oleh sebab itu, untuk mengurangi kebingungan, kami standarkan penulisan kode JavaScript yang digunakan dalam lingkup Dicoding Academy.
6
+
7
+ *Style guide* ini dibuat untuk menstandarkan penulisan kode JavaScript yang berada di dalam platform Dicoding Academy, seperti potongan kode yang kami ajarkan di dalam sebuah kelas. Selain itu, style guide ini bisa menjadi panduan bagi siswa untuk menstandarkan gaya penulisan kode JavaScript dalam berbagai aktivitas, salah satunya ketika pengerjaan proyek submission.
8
+
9
+
10
+
11
+ ## Meta Rules
12
+
13
+ ### Encoding menggunakan UTF-8
14
+
15
+ Untuk menampilkan dan mengubah teks dengan benar, teks editor harus mengetahui *text encoding* yang digunakan. Pastikan *text encoding* yang digunakan adalah **UTF-8**. Pada teks editor yang umum (seperti VSCode atau WebStorm), informasi encoding yang digunakan dapat ditemukan di *status bar* yang berada di bawah kode editor.
16
+
17
+ Informasi tambahan:
18
+
19
+ - https://www.jetbrains.com/help/idea/encoding.html
20
+
21
+
22
+
23
+ ### Comments
24
+
25
+ Gunakan komentar untuk menjelaskan kode yang ditulis, seperti baris kode yang sulit dimengerti, informasi argumen pada sebuah fungsi, dan lain sebagainya. Jangan menambahkan komentar pada kode yang "mudah" untuk dibaca dan dimengerti.
26
+
27
+ #### Multiline Comment
28
+
29
+ Gunakan `/** ... */` untuk komentar lebih dari satu baris.
30
+
31
+ Contoh **SALAH** ⛔.
32
+
33
+ ```javascript
34
+ // make() returns a new element
35
+ // based on the passed in tag name
36
+ function make(tag) {
37
+
38
+ // ...
39
+
40
+ return element;
41
+ }
42
+ ```
43
+
44
+ Contoh **BENAR** ✅.
45
+
46
+ ```javascript
47
+ /**
48
+ * make() returns a new element
49
+ * based on the passed in tag name
50
+ */
51
+ function make(tag) {
52
+
53
+ // ...
54
+
55
+ return element;
56
+ }
57
+ ```
58
+
59
+
60
+
61
+ #### Singleline Comment
62
+
63
+ Gunakan `//` untuk komentar satu baris. Sebisa mungkin komentar ditulis di baris baru.
64
+
65
+ Contoh **SALAH** ⛔.
66
+
67
+ ```javascript
68
+ const interval = 60_000; // 1 hour in millis
69
+
70
+ /**
71
+ * 1 hour in milis
72
+ */
73
+ const interval = 60_000;
74
+ ```
75
+
76
+ Contoh **BENAR** ✅.
77
+
78
+ ```javascript
79
+ // 1 hour in millis
80
+ const interval = 60_000;
81
+ ```
82
+
83
+
84
+
85
+ #### Type Information
86
+
87
+ Anda dapat (secara opsional) mendokumentasikan kode yang ditulis (termasuk menjelaskan informasi tipe data) menggunakan komentar yang ditulis dengan mengikuti format standar JSDoc. Format yang standar dapat dikenali oleh Code Editor sehingga dapat membantu memudahkan pengembangan.
88
+
89
+ Contoh **SALAH** ⛔ karena tidak menggunakan standar format apa pun.
90
+
91
+ ```javascript
92
+ /**
93
+ * function that sum two of numbers
94
+ * Potentially throwing an TypeError
95
+ * a is number
96
+ * b is number
97
+ */
98
+ function sum(a, b) {
99
+ if (typeof a !== 'number' || typeof b !== 'number') {
100
+ throw new TypeError('all given argument should be a number');
101
+ }
102
+
103
+ return a + b;
104
+ }
105
+ ```
106
+
107
+ Contoh **BENAR** ✅ dengan mengikuti format JSDoc.
108
+
109
+ ```javascript
110
+ /**
111
+ * function that sum two of numbers
112
+ *
113
+ * @throws {TypeError} will throw if the arguments is not number
114
+ * @params {number} a
115
+ * @params {number} b
116
+ */
117
+ function sum(a, b) {
118
+ if (typeof a !== 'number' || typeof b !== 'number') {
119
+ throw new TypeError('all given argument should be a number');
120
+ }
121
+
122
+ return a + b;
123
+ }
124
+ ```
125
+
126
+ Informasi tambahan:
127
+
128
+ - https://jsdoc.app/
129
+
130
+
131
+
132
+ ### Action Items
133
+
134
+ Tandai sebuah *placeholder* (hal yang perlu dilakukan) dengan teks `TODO:`. Anda bisa gunakan `//` jika to-do dijelaskan dalam satu baris atau `/** */` jika lebih dari satu baris. Jangan tambahkan karakter lain seperti `@` atau `@@` ketika menulis `TODO:` karena tidak perlu.
135
+
136
+ Berikut adalah contoh yang **SALAH** ⛔.
137
+
138
+ ```javascript
139
+ function sum(a, b) {
140
+ // @TODO: throws an TypeError when both argument is not number
141
+
142
+ return a + b;
143
+ }
144
+ ```
145
+
146
+ Berikut adalah contoh yang **BENAR** ✅.
147
+
148
+ ```javascript
149
+ function sum(a, b) {
150
+ // TODO: throws an TypeError when both argument is not number
151
+
152
+ return a + b;
153
+ }
154
+
155
+
156
+ /**
157
+ * TODO:
158
+ * Create a function that:
159
+ * - called average
160
+ * - receives an array of numbers as argument
161
+ * - ... etc
162
+ */
163
+ ```
164
+
165
+
166
+
167
+ ### Line Break (Line Endings)
168
+
169
+ Anda harus menggunakan format baris baru yang konsisten pada setiap berkas. Di panduan ini kami fokuskan hanya pada penggunaan **unix** (LF atau \n). Pada teks editor yang umum (seperti VSCode atau WebStorm), informasi *line endings* yang digunakan dapat ditemukan di *status bar* yang berada di bawah kode editor.
170
+
171
+ Informasi tambahan:
172
+
173
+ - https://www.jetbrains.com/help/webstorm/configuring-line-endings-and-line-separators.html
174
+ - https://www.cs.toronto.edu/~krueger/csc209h/tut/line-endings.html
175
+
176
+
177
+
178
+ ## Code Styles
179
+
180
+ ### Trailing Whitespace
181
+
182
+ Terkadang kita tidak sadar menambahkan spasi (*whitespace*) yang tidak perlu di akhir baris kode. Bila whitespace tersebut terbawa ke VCS seperti git, tidak jarang developer menjadi bingung dan frustrasi mencari "perbedaan" dari sebuah baris kode karena sulit untuk terlihat. Maka dari itu, pastikan setiap akhir baris kode bersih dari *whitespace*.
183
+
184
+ Contoh yang **SALAH** ⛔ karena baris kode (pertama) mengandung *whitespace*.
185
+
186
+ ```javascript
187
+ const foo = 0;
188
+ const bar = 5;
189
+ ```
190
+
191
+ Contoh yang **BENAR** ✅. Tidak ada *whitespace* di setiap akhir baris kode.
192
+
193
+ ```javascript
194
+ const foo = 0;
195
+ const bar = 5;
196
+ ```
197
+
198
+
199
+
200
+ ### Indentation
201
+
202
+ Penggunaan indentasi pada kode harus konsisten. Untuk menjaga konsistensi, kami menggunakan **2** buah **spasi** sebagai indentasi.
203
+
204
+ Berikut contoh yang **SALAH** ⛔.
205
+
206
+ ```javascript
207
+ function adder(by) {
208
+ return (value) => {
209
+ return by + value;
210
+ }
211
+ }
212
+ ```
213
+
214
+ Berikut contoh yang **BENAR** ✅.
215
+
216
+ ```javascript
217
+ function adder(by) {
218
+ return (value) => {
219
+ return by + value;
220
+ }
221
+ }
222
+ ```
223
+
224
+
225
+
226
+ ### Naming
227
+
228
+ Secara umum penamaan variabel atau fungsi harus menggunakan format `camelCase`.
229
+
230
+ Berikut contoh yang **SALAH** ⛔.
231
+
232
+ ```javascript
233
+ const my_favorite_color = '#112C85';
234
+
235
+ function do_something() {
236
+ // ...
237
+ }
238
+
239
+ const { from_json } = JSON.parse('{}');
240
+ ```
241
+
242
+ Berikut contoh yang **BENAR** ✅.
243
+
244
+ ```javascript
245
+ const myFavoriteColor = '#112C85';
246
+
247
+ function doSomething() {
248
+ // ...
249
+ }
250
+
251
+ const { from_json: fromJson } = JSON.parse('{}');
252
+ ```
253
+
254
+
255
+
256
+ ### Arrow Function Parentheses
257
+
258
+ Agar menjaga konsistensi penulisan, selalu gunakan tanda kurung (parentheses) setiap kali membuat arrow function baik tanpa argumen, 1 argumen, atau lebih.
259
+
260
+ Berikut contoh yang **SALAH** ⛔.
261
+
262
+ ```javascript
263
+ document.addEventListener('click', event => {
264
+ // ...
265
+ });
266
+
267
+ store.subscribe(() => {
268
+ // ...
269
+ })
270
+
271
+ server.handler((request, response) => {
272
+ // ...
273
+ });
274
+ ```
275
+
276
+ Berikut contoh yang **BENAR** ✅.
277
+
278
+ ```javascript
279
+ document.addEventListener('click', (event) => {
280
+ // ...
281
+ });
282
+
283
+ store.subscribe(() => {
284
+ // ...
285
+ })
286
+
287
+ server.handler((request, response) => {
288
+ // ...
289
+ });
290
+ ```
291
+
292
+
293
+
294
+ ### Formatting
295
+
296
+ #### Comma
297
+
298
+ Selalu tambahkan spasi di antara nilai yang dipisahkan oleh tanda koma.
299
+
300
+ Berikut contoh yang **SALAH** ⛔.
301
+
302
+ ```javascript
303
+ function sum(a,b,c) {
304
+ // ...
305
+ }
306
+
307
+ const numbers = [1,2,3,4];
308
+ const person = { name: 'Fulan',age: 30 };
309
+ ```
310
+
311
+ Berikut contoh yang **BENAR** ✅.
312
+
313
+ ```javascript
314
+ function sum(a, b, c) {
315
+ // ...
316
+ }
317
+
318
+ const numbers = [1, 2, 3, 4];
319
+ const person = { name: 'Fulan', age: 30 };
320
+ ```
321
+
322
+
323
+
324
+ #### Object Literals
325
+
326
+ Selalu tambahkan spasi di antara tanda `{ }` dan nama properti yang didefinsikan dalam satu baris.
327
+
328
+ Berikut contoh yang **SALAH** ⛔.
329
+
330
+ ```javascript
331
+ const person = {name: 'Fulan', age: 30};
332
+ const {name, age} = person;
333
+
334
+ function printPerson({name, age}) {
335
+ // ...
336
+ }
337
+ ```
338
+
339
+ Berikut contoh yang **BENAR** ✅.
340
+
341
+ ```javascript
342
+ const person = { name: 'Fulan', age: 30 };
343
+ const { name, age } = person;
344
+
345
+
346
+ function printPerson({ name, age }) {
347
+ // ...
348
+ }
349
+ ```
350
+
351
+
352
+
353
+ #### Array Literals
354
+
355
+ Jangan tambahkan spasi di antara tanda `[ ]` dan elemen di dalam array.
356
+
357
+ Berikut contoh yang **SALAH** ⛔.
358
+
359
+ ```javascript
360
+ const numbers = [ 1, 2, 3 ];
361
+ const [ first, second ] = numbers;
362
+ ```
363
+
364
+ Berikut contoh yang **BENAR** ✅.
365
+
366
+ ```javascript
367
+ const numbers = [1, 2, 3];
368
+ const [first, second] = numbers;
369
+ ```
370
+
371
+
372
+
373
+ #### Parentheses
374
+
375
+ Jangan tambahkan spasi di antara tanda `( )` dan expression yang ada di dalamnya.
376
+
377
+ Berikut contoh yang **SALAH** ⛔.
378
+
379
+ ```javascript
380
+ function sum( a, b ) {
381
+ // ...
382
+ }
383
+
384
+ const value = 1 + ( 2 * 1 );
385
+
386
+ if ( value === 1 ) {
387
+ // ...
388
+ }
389
+ ```
390
+
391
+ Berikut contoh yang **BENAR** ✅.
392
+
393
+ ```javascript
394
+ function sum(a, b) {
395
+ // ...
396
+ }
397
+
398
+ const value = 1 + (2 * 1);
399
+
400
+ if (value === 1) {
401
+ // ...
402
+ }
403
+ ```
404
+
405
+
406
+
407
+ #### Control Statement
408
+
409
+ Beri 1 spasi sebelum menggunakan tanda `()` pada *control statement* (contohnya `if`, `while`, `for`, dan lainnya). Namun, jangan berikan 1 spasi antara daftar argumen dan nama fungsi ketika mendeklarasikan regular function ataupun ketika memanggil sebuah fungsi.
410
+
411
+ Berikut contoh yang **SALAH** ⛔.
412
+
413
+ ```javascript
414
+ function fight () {
415
+ console.log('Swooosh!');
416
+ }
417
+
418
+ if(isJedi) {
419
+ fight ();
420
+ }
421
+ ```
422
+
423
+ Berikut contoh yang **BENAR** ✅.
424
+
425
+ ```javascript
426
+ function figth() {
427
+ console.log('Swooosh!');
428
+ }
429
+
430
+ if (isJedi) {
431
+ fight();
432
+ }
433
+ ```
434
+
435
+
436
+
437
+ ## Language Rules
438
+
439
+ ### Variable Declaration
440
+
441
+ #### Prefer using `const`
442
+
443
+ Gunakan `const` ketika membuat sebuah variabel yang tidak ditetapkan ulang nilainya.
444
+
445
+ Berikut contoh yang **SALAH** ⛔.
446
+
447
+ ```javascript
448
+ let a = 1;
449
+ var b = 2;
450
+ ```
451
+
452
+ Berikut contoh yang **BENAR** ✅.
453
+
454
+ ```javascript
455
+ const a = 1;
456
+ const b = 2;
457
+ ```
458
+
459
+
460
+
461
+ #### Using `let`, avoid `var`
462
+
463
+ Jika sebuah variabel perlu ditetapkan ulang nilainya, gunakan `let` alih-alih `var`.
464
+
465
+ Berikut contoh yang **SALAH** ⛔.
466
+
467
+ ```javascript
468
+ var count = 1;
469
+
470
+ if (true) {
471
+ count += 1;
472
+ }
473
+ ```
474
+
475
+ Berikut contoh yang **BENAR** ✅.
476
+
477
+ ```javascript
478
+ let count = 1;
479
+
480
+ if (true) {
481
+ count += 1;
482
+ }
483
+ ```
484
+
485
+ Informasi tambahan:
486
+
487
+ - https://dev.to/mindninjax/stop-using-var-for-declaring-variables-2p3a
488
+
489
+ ### Semicolon
490
+
491
+ Di balik layar, JavaScript akan menambahkan **titik koma**--*sesuai aturan yang berlaku*--sebagai tanda berakhirnya statement pada baris kode yang tidak diberikan semicolon. Aturan ini disebut sebagai *[Automatic Semicolon Insertion (ASI)](https://tc39.es/ecma262/#sec-automatic-semicolon-insertion)*. ASI mengandung beberapa perilaku yang esentrik dan membuat kode yang kita tulis rusak jika ia salah menginterpretasikan baris kode tersebut. ASI juga akan semakin rumit dipahami seiring bertambahnya fitur atau sintaks pada bahasa pemrograman JavaScript. Dengan menuliskan titik koma (semicolon) secara eksplisit tentu akan membantu untuk mencegah masalah-masalah yang ditimbulkan karena ASI.
492
+
493
+ Berikut contoh yang **SALAH** ⛔ karena kode akan membangkitkan error.
494
+
495
+ ```javascript
496
+ // will raise exception
497
+ const luke = {}
498
+ const leia = {}
499
+ [luke, leia].forEach((jedi) => jedi.father = 'vader')
500
+
501
+
502
+ // will raise exception
503
+ const reaction = "No! That’s impossible!"
504
+ (async function meanwhileOnTheFalcon() {
505
+ // handle `leia`, `lando`, `chewie`, `r2`, `c3p0`
506
+ // ...
507
+ }())
508
+
509
+
510
+ // return undefined because ASI insert semicolon after `return`
511
+ function foo() {
512
+ return
513
+ 'search your feelings, you know it to be foo'
514
+ }
515
+ ```
516
+
517
+ Berikut contoh yang **BENAR** ✅.
518
+
519
+ ``` javascript
520
+ // good
521
+ const luke = {};
522
+ const leia = {};
523
+ [luke, leia].forEach((jedi) => jedi.father = 'vader');
524
+
525
+ // good
526
+ const reaction = 'No! That’s impossible!';
527
+ (async function meanwhileOnTheFalcon() {
528
+ // handle `leia`, `lando`, `chewie`, `r2`, `c3p0`
529
+ // ...
530
+ }());
531
+
532
+ // good
533
+ function foo() {
534
+ return 'search your feelings, you know it to be foo';
535
+ }
536
+ ```
537
+
538
+ Informasi tambahan:
539
+
540
+ - https://stackoverflow.com/questions/7365172/semicolon-before-self-invoking-function/7365214#7365214
541
+
542
+ ### Strings
543
+
544
+ Agar konsisten, selalu gunakan **kutip satu** `' '` untuk membuat nilai string.
545
+
546
+ Berikut contoh yang **SALAH** ⛔.
547
+
548
+ ```javascript
549
+ // Bad
550
+ const name = "Fulan";
551
+
552
+ // Bad
553
+ const address = `Bandung`;
554
+ ```
555
+
556
+ Berikut contoh yang **BENAR** ✅.
557
+
558
+ ```javascript
559
+ // Good
560
+ const name = 'Fulan';
561
+ const address = 'Bandung';
562
+ ```
563
+
564
+ Anda boleh gunakan tanda lain untuk kasus-kasus tertentu, seperti:
565
+
566
+ Gunakan `" "` jika nilai string mengandung karakter '.
567
+
568
+ ```javascript
569
+ // Bad
570
+ const movie = 'Howl\'s Moving Castle';
571
+
572
+ // Good
573
+ const movie = "Howl's Moving Castle";
574
+ ```
575
+
576
+ Gunakan ` `` ` jika membutuhkan JavaScript expression dalam membangun nilai string.
577
+
578
+ ```javascript
579
+ // Bad
580
+ function sayHi(name) {
581
+ return 'How are you, ' + name + '?';
582
+ }
583
+
584
+ // Good
585
+ function sayHi(name) {
586
+ return `How are you, ${name}?`;
587
+ }
588
+ ```
589
+
590
+
591
+
592
+ ### Arrow Function for Function Expressions
593
+
594
+ Function di JavaScript merupakan first-class citizen. Sama seperti nilai primitif, function dapat berupa expression dan disimpan menjadi nilai variabel, argumen fungsi, atau nilai yang dikembalikan oleh fungsi. Ketika Anda membuat function expression (contohnya menetapkan fungsi callback), gunakanlah arrow function.
595
+
596
+ Berikut contoh yang **SALAH** ⛔.
597
+
598
+ ```javascript
599
+ [1, 2, 3].map(function(x) {
600
+ return x * 2;
601
+ });
602
+ ```
603
+
604
+ Berikut contoh yang **BENAR** ✅.
605
+
606
+ ```javascript
607
+ [1, 2, 3].map((x) => x * 2);
608
+ ```
package/index.js ADDED
@@ -0,0 +1,26 @@
1
+ const config = [
2
+ {
3
+ rules: {
4
+ 'linebreak-style': ['error', 'unix'],
5
+ 'no-trailing-spaces': 'error',
6
+ 'indent': ['error', 2],
7
+ 'camelcase': 'error',
8
+ 'arrow-parens': ['error', 'always'],
9
+ 'comma-spacing': ['error', { 'before': false, 'after': true }],
10
+ 'object-curly-spacing': ['error', 'always'],
11
+ 'array-bracket-spacing': ['error', 'never'],
12
+ 'space-in-parent': ['error', 'never'],
13
+ 'space-before-function-paren': ['error', 'never'],
14
+ 'func-call-spacing': ['error', 'never'],
15
+ 'keyword-spacing': ['error', { 'before': true, 'after': true }],
16
+ 'prefer-const': 'error',
17
+ 'no-var': 'error',
18
+ 'semi': ['error', 'always'],
19
+ 'quotes': ['error', 'single', { 'avoidEscape': true }],
20
+ 'prefer-template': 'error',
21
+ 'prefer-arrow-callback': 'error'
22
+ }
23
+ }
24
+ ]
25
+
26
+ export default config;
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "eslint-config-dicodingacademy",
3
+ "version": "0.9.0",
4
+ "description": "Standard style and guide for writing JavaScript in Dicoding Academy ecosystem",
5
+ "main": "index.js",
6
+ "scripts": {},
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/dicodingacademy/javascript-style-guide.git"
10
+ },
11
+ "keywords": [
12
+ "javascript",
13
+ "style",
14
+ "guide",
15
+ "eslint",
16
+ "eslintconfig"
17
+ ],
18
+ "author": "dicodingacademy",
19
+ "license": "ISC",
20
+ "bugs": {
21
+ "url": "https://github.com/dicodingacademy/javascript-style-guide/issues"
22
+ },
23
+ "homepage": "https://github.com/dicodingacademy/javascript-style-guide#readme",
24
+ "peerDependencies": {
25
+ "eslint": ">= 9"
26
+ }
27
+ }