debian-mirror-gitlab/doc/development/fe_guide/tooling.md

258 lines
7.9 KiB
Markdown
Raw Normal View History

2021-01-29 00:20:46 +05:30
---
stage: none
group: unassigned
2021-02-22 17:27:13 +05:30
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2021-01-29 00:20:46 +05:30
---
2020-01-01 13:55:28 +05:30
# Tooling
## ESLint
We use ESLint to encapsulate and enforce frontend code standards. Our configuration may be found in the [`gitlab-eslint-config`](https://gitlab.com/gitlab-org/gitlab-eslint-config) project.
2020-07-28 23:09:34 +05:30
### Yarn Script
This section describes yarn scripts that are available to validate and apply automatic fixes to files using ESLint.
2021-02-22 17:27:13 +05:30
To check all staged files (based on `git diff`) with ESLint, run the following script:
2020-07-28 23:09:34 +05:30
```shell
2021-04-17 20:07:23 +05:30
yarn run lint:eslint:staged
2020-07-28 23:09:34 +05:30
```
2021-02-22 17:27:13 +05:30
A list of problems found are logged to the console.
2020-07-28 23:09:34 +05:30
2021-02-22 17:27:13 +05:30
To apply automatic ESLint fixes to all staged files (based on `git diff`), run the following script:
2020-07-28 23:09:34 +05:30
```shell
2021-04-17 20:07:23 +05:30
yarn run lint:eslint:staged:fix
2020-07-28 23:09:34 +05:30
```
2021-02-22 17:27:13 +05:30
If manual changes are required, a list of changes are sent to the console.
2020-07-28 23:09:34 +05:30
2021-04-17 20:07:23 +05:30
To check a specific file in the repository with ESLINT, run the following script (replacing $PATH_TO_FILE):
```shell
yarn run lint:eslint $PATH_TO_FILE
```
2020-07-28 23:09:34 +05:30
To check **all** files in the repository with ESLint, run the following script:
```shell
2021-04-17 20:07:23 +05:30
yarn run lint:eslint:all
2020-07-28 23:09:34 +05:30
```
2021-02-22 17:27:13 +05:30
A list of problems found are logged to the console.
2020-07-28 23:09:34 +05:30
To apply automatic ESLint fixes to **all** files in the repository, run the following script:
```shell
2021-04-17 20:07:23 +05:30
yarn run lint:eslint:all:fix
2020-07-28 23:09:34 +05:30
```
2021-02-22 17:27:13 +05:30
If manual changes are required, a list of changes are sent to the console.
2020-07-28 23:09:34 +05:30
2021-02-22 17:27:13 +05:30
WARNING:
2020-10-24 23:57:45 +05:30
Limit use to global rule updates. Otherwise, the changes can lead to huge Merge Requests.
2020-07-28 23:09:34 +05:30
2020-01-01 13:55:28 +05:30
### Disabling ESLint in new files
Do not disable ESLint when creating new files. Existing files may have existing rules
disabled due to legacy compatibility reasons but they are in the process of being refactored.
Do not disable specific ESLint rules. To avoid introducing technical debt, you may disable the following
rules only if you are invoking/instantiating existing code modules.
- [`no-new`](https://eslint.org/docs/rules/no-new)
- [`class-method-use-this`](https://eslint.org/docs/rules/class-methods-use-this)
2021-01-29 00:20:46 +05:30
Disable these rules on a per-line basis. This makes it easier to refactor in the
future. For example, use `eslint-disable-next-line` or `eslint-disable-line`.
2020-01-01 13:55:28 +05:30
### Disabling ESLint for a single violation
If you do need to disable a rule for a single violation, disable it for the smallest amount of code necessary:
```javascript
// bad
/* eslint-disable no-new */
import Foo from 'foo';
new Foo();
// better
import Foo from 'foo';
// eslint-disable-next-line no-new
new Foo();
```
### The `no-undef` rule and declaring globals
**Never** disable the `no-undef` rule. Declare globals with `/* global Foo */` instead.
When declaring multiple globals, always use one `/* global [name] */` line per variable.
```javascript
// bad
/* globals Flash, Cookies, jQuery */
// good
/* global Flash */
/* global Cookies */
/* global jQuery */
```
2021-04-17 20:07:23 +05:30
### Deprecating functions with `import/no-deprecated`
Our `@gitlab/eslint-plugin` Node module contains the [`eslint-plugin-import`](https://gitlab.com/gitlab-org/frontend/eslint-plugin) package.
We can use the [`import/no-deprecated`](https://github.com/benmosher/eslint-plugin-import/blob/HEAD/docs/rules/no-deprecated.md) rule to deprecate functions using a JSDoc block with a `@deprecated` tag:
```javascript
/**
* Convert search query into an object
*
* @param {String} query from "document.location.search"
* @param {Object} options
* @param {Boolean} options.gatherArrays - gather array values into an Array
* @returns {Object}
*
* ex: "?one=1&two=2" into {one: 1, two: 2}
* @deprecated Please use `queryToObject` instead. See https://gitlab.com/gitlab-org/gitlab/-/issues/283982 for more information
*/
export function queryToObject(query, options = {}) {
...
}
```
It is strongly encouraged that you:
- Put in an **alternative path for developers** looking to use this function.
- **Provide a link to the issue** that tracks the migration process.
NOTE:
Uses are detected if you import the deprecated function into another file. They are not detected when the function is used in the same file.
Running `$ yarn eslint` after this will give us the list of deprecated usages:
```shell
$ yarn eslint
./app/assets/javascripts/issuable_form.js
9:10 error Deprecated: Please use `queryToObject` instead. See https://gitlab.com/gitlab-org/gitlab/-/issues/283982 for more information import/no-deprecated
33:23 error Deprecated: Please use `queryToObject` instead. See https://gitlab.com/gitlab-org/gitlab/-/issues/283982 for more information import/no-deprecated
...
```
Grep for disabled cases of this rule to generate a working list to create issues from, so you can track the effort of removing deprecated uses:
```shell
$ grep "eslint-disable.*import/no-deprecated" -r .
./app/assets/javascripts/issuable_form.js:import { queryToObject, objectToQuery } from './lib/utils/url_utility'; // eslint-disable-line import/no-deprecate
./app/assets/javascripts/issuable_form.js: // eslint-disable-next-line import/no-deprecated
```
2022-07-16 23:28:13 +05:30
### GraphQL schema and operations validation
We use [`@graphql-eslint/eslint-plugin`](https://www.npmjs.com/package/@graphql-eslint/eslint-plugin)
to lint GraphQL schema and operations. This plugin requires the entire schema to function properly.
It is thus recommended to generate an up-to-date dump of the schema when running ESLint locally.
You can do this by running the `./scripts/dump_graphql_schema` script.
2020-01-01 13:55:28 +05:30
## Formatting with Prettier
2020-07-28 23:09:34 +05:30
> Support for `.graphql` [introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/227280) in GitLab 13.2.
Our code is automatically formatted with [Prettier](https://prettier.io) to follow our style guides. Prettier is taking care of formatting `.js`, `.vue`, `.graphql`, and `.scss` files based on the standard prettier rules. You can find all settings for Prettier in `.prettierrc`.
2020-01-01 13:55:28 +05:30
### Editor
2021-01-03 14:25:43 +05:30
The recommended method to include Prettier in your workflow is to set up your
preferred editor (all major editors are supported) accordingly. We suggest
setting up Prettier to run when each file is saved. For instructions about using
Prettier in your preferred editor, see the [Prettier documentation](https://prettier.io/docs/en/editors.html).
2020-01-01 13:55:28 +05:30
2022-08-27 11:52:29 +05:30
Take care that you only let Prettier format the same file types as the global Yarn script does (`.js`, `.vue`, `.graphql`, and `.scss`). For example, you can exclude file formats in your Visual Studio Code settings file:
2020-01-01 13:55:28 +05:30
2020-04-22 19:07:51 +05:30
```json
2020-01-01 13:55:28 +05:30
"prettier.disableLanguages": [
"json",
"markdown"
2020-04-22 19:07:51 +05:30
]
2020-01-01 13:55:28 +05:30
```
### Yarn Script
The following yarn scripts are available to do global formatting:
2020-04-22 19:07:51 +05:30
```shell
2021-04-17 20:07:23 +05:30
yarn run lint:prettier:staged:fix
2020-01-01 13:55:28 +05:30
```
2021-03-11 19:13:27 +05:30
Updates all staged files (based on `git diff`) with Prettier and saves the needed changes.
2020-01-01 13:55:28 +05:30
2020-04-22 19:07:51 +05:30
```shell
2021-04-17 20:07:23 +05:30
yarn run lint:prettier:staged
2020-01-01 13:55:28 +05:30
```
2021-03-11 19:13:27 +05:30
Checks all staged files (based on `git diff`) with Prettier and log which files would need manual updating to the console.
2020-01-01 13:55:28 +05:30
2020-04-22 19:07:51 +05:30
```shell
2021-04-17 20:07:23 +05:30
yarn run lint:prettier
2020-01-01 13:55:28 +05:30
```
Checks all files with Prettier and logs which files need manual updating to the console.
2020-04-22 19:07:51 +05:30
```shell
2021-04-17 20:07:23 +05:30
yarn run lint:prettier:fix
2020-01-01 13:55:28 +05:30
```
2021-04-17 20:07:23 +05:30
Formats all files in the repository with Prettier.
2020-01-01 13:55:28 +05:30
### VSCode Settings
#### Select Prettier as default formatter
To select Prettier as a formatter, add the following properties to your User or Workspace Settings:
```javascript
{
"[html]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[vue]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
2020-07-28 23:09:34 +05:30
},
"[graphql]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
2020-01-01 13:55:28 +05:30
}
}
```
#### Format on Save
To automatically format your files with Prettier, add the following properties to your User or Workspace Settings:
```javascript
{
"[html]": {
"editor.formatOnSave": true
},
"[javascript]": {
"editor.formatOnSave": true
},
"[vue]": {
"editor.formatOnSave": true
},
2020-07-28 23:09:34 +05:30
"[graphql]": {
"editor.formatOnSave": true
},
2020-01-01 13:55:28 +05:30
}
```