|
| 1 | +--- |
| 2 | +sidebar_label: ESLint |
| 3 | +--- |
| 4 | + |
| 5 | +# ESLint - JavaScript Linter |
| 6 | + |
| 7 | +- https://eslint.org/ |
| 8 | +- https://github.com/eslint/eslint |
| 9 | + |
| 10 | +> ESLint is a tool for identifying and reporting on patterns found in ECMAScript/JavaScript code, with the goal of making code more consistent and avoiding bugs. |
| 11 | +
|
| 12 | +**Vortex** comes with [pre-configured ESLint ruleset](https://github.com/drevops/vortex/blob/develop/.eslintrc.json) for Drupal projects, along with [Prettier](https://prettier.io/) integration for automatic code formatting. |
| 13 | + |
| 14 | +:::info |
| 15 | +ESLint in Vortex is configured to lint **custom modules only** (`web/modules/custom`). Custom themes should maintain their own ESLint configuration within the theme directory. |
| 16 | +::: |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +### Check for violations |
| 21 | + |
| 22 | +import Tabs from '@theme/Tabs'; |
| 23 | +import TabItem from '@theme/TabItem'; |
| 24 | + |
| 25 | +<Tabs> |
| 26 | + <TabItem value="ahoy" label="Ahoy" default> |
| 27 | + ```shell |
| 28 | + ahoy lint-fe |
| 29 | + ``` |
| 30 | + </TabItem> |
| 31 | + <TabItem value="docker-compose" label="Docker Compose"> |
| 32 | + ```shell |
| 33 | + docker compose exec cli yarn run lint-js |
| 34 | + ``` |
| 35 | + </TabItem> |
| 36 | +</Tabs> |
| 37 | + |
| 38 | +### Fix violations |
| 39 | + |
| 40 | +ESLint supports automatic fixing of many violations using the `--fix` flag. Prettier integration provides additional auto-formatting capabilities. |
| 41 | + |
| 42 | +<Tabs> |
| 43 | + <TabItem value="ahoy" label="Ahoy" default> |
| 44 | + ```shell |
| 45 | + ahoy lint-fe-fix |
| 46 | + ``` |
| 47 | + </TabItem> |
| 48 | + <TabItem value="docker-compose" label="Docker Compose"> |
| 49 | + ```shell |
| 50 | + docker compose exec cli yarn run lint-fix-js |
| 51 | + ``` |
| 52 | + </TabItem> |
| 53 | +</Tabs> |
| 54 | + |
| 55 | +## Configuration |
| 56 | + |
| 57 | +See [configuration reference](https://eslint.org/docs/latest/use/configure/). |
| 58 | + |
| 59 | +All global configuration takes place in the [`.eslintrc.json`](https://github.com/drevops/vortex/blob/develop/.eslintrc.json) file. |
| 60 | + |
| 61 | +By default, ESLint will check against the following rules: |
| 62 | + |
| 63 | +- `airbnb-base` - Airbnb's base JavaScript style guide |
| 64 | +- `plugin:prettier/recommended` - Prettier integration for code formatting |
| 65 | +- `plugin:yml/recommended` - YAML file linting |
| 66 | + |
| 67 | +The configuration includes Drupal-specific globals: |
| 68 | + |
| 69 | +- `Drupal`, `drupalSettings`, `drupalTranslations` |
| 70 | +- `jQuery`, `once` |
| 71 | +- `CKEditor5` |
| 72 | +- And other common Drupal frontend libraries |
| 73 | + |
| 74 | +Targets include custom modules only: |
| 75 | + |
| 76 | +```json |
| 77 | +{ |
| 78 | + "scripts": { |
| 79 | + "lint-js": "eslint web/modules/custom --ext .js" |
| 80 | + } |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +Adding or removing targets in `package.json`: |
| 85 | + |
| 86 | +```json |
| 87 | +{ |
| 88 | + "scripts": { |
| 89 | + "lint-js": "eslint web/modules/custom web/sites/default --ext .js" |
| 90 | + } |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +### Prettier Integration |
| 95 | + |
| 96 | +Prettier is integrated via `eslint-plugin-prettier` and provides automatic code formatting. The rule `"prettier/prettier": "error"` ensures that all code formatting issues are caught by ESLint. |
| 97 | + |
| 98 | +When you run `ahoy lint-fe-fix`, both ESLint's `--fix` and Prettier's formatting are applied automatically. |
| 99 | + |
| 100 | +## Ignoring |
| 101 | + |
| 102 | +### Global Ignoring |
| 103 | + |
| 104 | +Ignoring paths **globally** takes place in the [`.eslintignore`](https://github.com/drevops/vortex/blob/develop/.eslintignore) file: |
| 105 | + |
| 106 | +```text |
| 107 | +node_modules/ |
| 108 | +vendor/ |
| 109 | +web/core/ |
| 110 | +web/libraries/ |
| 111 | +web/modules/contrib/ |
| 112 | +web/themes/contrib/ |
| 113 | +*.min.js |
| 114 | +``` |
| 115 | + |
| 116 | +### Inline Ignoring |
| 117 | + |
| 118 | +To ignore **all ESLint rules** within a file, place in the file header: |
| 119 | + |
| 120 | +```javascript |
| 121 | +/* eslint-disable */ |
| 122 | +``` |
| 123 | + |
| 124 | +To ignore **a specific rule** within a file, place in the file header: |
| 125 | + |
| 126 | +```javascript |
| 127 | +/* eslint-disable no-console, no-unused-vars */ |
| 128 | +``` |
| 129 | + |
| 130 | +To ignore rules for a **code block**: |
| 131 | + |
| 132 | +```javascript |
| 133 | +/* eslint-disable no-console */ |
| 134 | +console.log('Debug information'); |
| 135 | +/* eslint-enable no-console */ |
| 136 | +``` |
| 137 | + |
| 138 | +To ignore only the **next line**: |
| 139 | + |
| 140 | +```javascript |
| 141 | +// eslint-disable-next-line no-console |
| 142 | +console.log('Single debug statement'); |
| 143 | +``` |
| 144 | + |
| 145 | +To ignore rules for the **current line** (inline): |
| 146 | + |
| 147 | +```javascript |
| 148 | +console.log('Debug'); // eslint-disable-line no-console |
| 149 | +``` |
| 150 | + |
| 151 | +## Ignoring fail in continuous integration pipeline |
| 152 | + |
| 153 | +This tool runs in continuous integration pipeline by default and fails the build |
| 154 | +if there are any violations. |
| 155 | + |
| 156 | +Set `VORTEX_CI_NODEJS_LINT_IGNORE_FAILURE` environment variable to `1` to ignore |
| 157 | +failures. The tool will still run and report violations, if any. |
0 commit comments