Skip to content

Commit a847a94

Browse files
committed
Re-format eslint rules to easier-to-read form and add highlights
1 parent 78c287e commit a847a94

File tree

2 files changed

+132
-186
lines changed

2 files changed

+132
-186
lines changed

src/content/3/en/part3d.md

Lines changed: 65 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -254,27 +254,28 @@ We will answer all of the questions:
254254
The configuration will be saved in the generated _eslint.config.mjs_ file:
255255

256256
```js
257-
import globals from 'globals'
257+
import globals from "globals";
258+
258259

260+
/** @type {import('eslint').Linter.Config[]} */
259261
export default [
260-
{ files: ["**/*.js"], languageOptions: {sourceType: "commonjs"} },
261-
{ languageOptions: { globals: globals.browser } },
262-
]
262+
{files: ["**/*.js"], languageOptions: {sourceType: "commonjs"}},
263+
{languageOptions: { globals: globals.browser }},
264+
];
263265
```
264266

265267
We will reformat the configuration file from its current form to the following:
266268

267269
```js
268-
// ...
270+
import globals from 'globals'
271+
269272
export default [
270273
{
271-
files: ["**/*.js"],
274+
files: ['**/*.js'],
272275
languageOptions: {
273-
sourceType: "commonjs",
274-
globals: {
275-
...globals.node,
276-
},
277-
ecmaVersion: "latest",
276+
sourceType: 'commonjs',
277+
globals: { ...globals.node },
278+
ecmaVersion: 'latest',
278279
},
279280
},
280281
]
@@ -289,15 +290,15 @@ Finally, the _ecmaVersion_ property is set to "latest". This sets the ECMAScript
289290
We want to make use of [ESLint's recommended](https://eslint.org/docs/latest/use/configure/configuration-files#using-predefined-configurations) settings along with our own. The _@eslint/js_ package we installed earlier provides us with predefined configurations for ESLint. We'll import it and enable it in the configuration file:
290291

291292
```js
292-
// ...
293-
import js from '@eslint/js'
293+
import globals from 'globals'
294+
import js from '@eslint/js' // highlight-line
294295
// ...
295296

296297
export default [
297298
js.configs.recommended, // highlight-line
298299
{
299300
// ...
300-
}
301+
},
301302
]
302303
```
303304

@@ -312,33 +313,24 @@ npm install --save-dev @stylistic/eslint-plugin-js
312313
Import and enable the plugin, and add these four code style rules:
313314

314315
```js
315-
// ...
316-
import stylisticJs from '@stylistic/eslint-plugin-js'
316+
import globals from 'globals'
317+
import js from '@eslint/js'
318+
import stylisticJs from '@stylistic/eslint-plugin-js' // highlight-line
317319

318320
export default [
319321
{
320322
// ...
321-
plugins: {
322-
'@stylistic/js': stylisticJs
323-
},
324-
rules: {
325-
'@stylistic/js/indent': [
326-
'error',
327-
2
328-
],
329-
'@stylistic/js/linebreak-style': [
330-
'error',
331-
'unix'
332-
],
333-
'@stylistic/js/quotes': [
334-
'error',
335-
'single'
336-
],
337-
'@stylistic/js/semi': [
338-
'error',
339-
'never'
340-
],
323+
// highlight-start
324+
plugins: {
325+
'@stylistic/js': stylisticJs,
341326
},
327+
rules: {
328+
'@stylistic/js/indent': ['error', 2],
329+
'@stylistic/js/linebreak-style': ['error', 'unix'],
330+
'@stylistic/js/quotes': ['error', 'single'],
331+
'@stylistic/js/semi': ['error', 'never'],
332+
},
333+
// highlight-end
342334
},
343335
]
344336
```
@@ -359,8 +351,8 @@ It is recommended to create a separate _npm script_ for linting:
359351
"scripts": {
360352
"start": "node index.js",
361353
"dev": "node --watch index.js",
362-
// ...
363354
"lint": "eslint ." // highlight-line
355+
// ...
364356
},
365357
// ...
366358
}
@@ -373,11 +365,16 @@ Files in the <em>dist</em> directory also get checked when the command is run. W
373365
```js
374366
// ...
375367
export default [
376-
// ...
368+
js.configs.recommended,
369+
{
370+
files: ['**/*.js'],
371+
// ...
372+
},
373+
// highlight-start
377374
{
378-
ignores: ["dist/**"],
375+
ignores: ['dist/**'],
379376
},
380-
//...
377+
// highlight-end
381378
]
382379
```
383380

@@ -406,8 +403,9 @@ export default [
406403
// ...
407404
rules: {
408405
// ...
409-
'eqeqeq': 'error',
406+
eqeqeq: 'error', // highlight-line
410407
},
408+
// ...
411409
]
412410
```
413411

@@ -420,14 +418,12 @@ export default [
420418
// ...
421419
rules: {
422420
// ...
423-
'eqeqeq': 'error',
421+
eqeqeq: 'error',
422+
// highlight-start
424423
'no-trailing-spaces': 'error',
425-
'object-curly-spacing': [
426-
'error', 'always'
427-
],
428-
'arrow-spacing': [
429-
'error', { 'before': true, 'after': true },
430-
],
424+
'object-curly-spacing': ['error', 'always'],
425+
'arrow-spacing': ['error', { before: true, after: true }],
426+
// highlight-end
431427
},
432428
]
433429
```
@@ -443,23 +439,19 @@ export default [
443439
]
444440
```
445441

446-
This includes a rule that warns about <em>console.log</em> commands. Disabling a rule can be accomplished by defining its "value" as 0 or "off" in the configuration file. Let's do this for the _no-console_ rule in the meantime.
442+
This includes a rule that warns about <em>console.log</em> commands which we don't want to use. Disabling a rule can be accomplished by defining its "value" as 0 or _off_ in the configuration file. Let's do this for the _no-console_ rule in the meantime.
447443

448444
```js
449445
[
450446
{
451447
// ...
452448
rules: {
453449
// ...
454-
'eqeqeq': 'error',
450+
eqeqeq: 'error',
455451
'no-trailing-spaces': 'error',
456-
'object-curly-spacing': [
457-
'error', 'always'
458-
],
459-
'arrow-spacing': [
460-
'error', { 'before': true, 'after': true },
461-
],
462-
'no-console': 'off',
452+
'object-curly-spacing': ['error', 'always'],
453+
'arrow-spacing': ['error', { before: true, after: true }],
454+
'no-console': 'off', // highlight-line
463455
},
464456
},
465457
]
@@ -468,54 +460,36 @@ This includes a rule that warns about <em>console.log</em> commands. Disabling a
468460
Disabling the no-console rule will allow us to use console.log statements without ESLint flagging them as issues. This can be particularly useful during development when you need to debug your code. Here's the complete configuration file with all the changes we have made so far:
469461

470462
```js
471-
import globals from "globals";
472-
import stylisticJs from '@stylistic/eslint-plugin-js'
463+
import globals from 'globals'
473464
import js from '@eslint/js'
465+
import stylisticJs from '@stylistic/eslint-plugin-js'
474466

475467
export default [
476468
js.configs.recommended,
477469
{
478-
files: ["**/*.js"],
470+
files: ['**/*.js'],
479471
languageOptions: {
480-
sourceType: "commonjs",
481-
globals: {
482-
...globals.node,
483-
},
484-
ecmaVersion: "latest",
472+
sourceType: 'commonjs',
473+
globals: { ...globals.node },
474+
ecmaVersion: 'latest',
485475
},
486476
plugins: {
487-
'@stylistic/js': stylisticJs
477+
'@stylistic/js': stylisticJs,
488478
},
489479
rules: {
490-
'@stylistic/js/indent': [
491-
'error',
492-
2
493-
],
494-
'@stylistic/js/linebreak-style': [
495-
'error',
496-
'unix'
497-
],
498-
'@stylistic/js/quotes': [
499-
'error',
500-
'single'
501-
],
502-
'@stylistic/js/semi': [
503-
'error',
504-
'never'
505-
],
506-
'eqeqeq': 'error',
480+
'@stylistic/js/indent': ['error', 2],
481+
'@stylistic/js/linebreak-style': ['error', 'unix'],
482+
'@stylistic/js/quotes': ['error', 'single'],
483+
'@stylistic/js/semi': ['error', 'never'],
484+
eqeqeq: 'error',
507485
'no-trailing-spaces': 'error',
508-
'object-curly-spacing': [
509-
'error', 'always'
510-
],
511-
'arrow-spacing': [
512-
'error', { 'before': true, 'after': true },
513-
],
486+
'object-curly-spacing': ['error', 'always'],
487+
'arrow-spacing': ['error', { before: true, after: true }],
514488
'no-console': 'off',
515489
},
516490
},
517-
{
518-
ignores: ["dist/**", "build/**"],
491+
{
492+
ignores: ['dist/**'],
519493
},
520494
]
521495
```

0 commit comments

Comments
 (0)