602.md 6.5 KB
Newer Older
Lab机器人's avatar
readme  
Lab机器人 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
# Tooling

> 原文:[https://docs.gitlab.com/ee/development/fe_guide/tooling.html](https://docs.gitlab.com/ee/development/fe_guide/tooling.html)

*   [ESLint](#eslint)
    *   [Yarn Script](#yarn-script)
    *   [Disabling ESLint in new files](#disabling-eslint-in-new-files)
    *   [Disabling ESLint for a single violation](#disabling-eslint-for-a-single-violation)
    *   [The `no-undef` rule and declaring globals](#the-no-undef-rule-and-declaring-globals)
*   [Formatting with Prettier](#formatting-with-prettier)
    *   [Editor](#editor)
    *   [Yarn Script](#yarn-script-1)
        *   [Scripts during Conversion period](#scripts-during-conversion-period)
    *   [VSCode Settings](#vscode-settings)
        *   [Select Prettier as default formatter](#select-prettier-as-default-formatter)
        *   [Format on Save](#format-on-save)

# Tooling[](#tooling "Permalink")

## ESLint[](#eslint "Permalink")

我们使用 ESLint 封装和执行前端代码标准. 我们的配置可以在[`gitlab-eslint-config`](https://gitlab.com/gitlab-org/gitlab-eslint-config)项目中找到.

### Yarn Script[](#yarn-script "Permalink")

本节介绍可用于验证的脚本,并使用 ESLint 将自动修复应用于文件.

要使用 ESLint 检查所有当前暂存的文件(基于`git diff` ),请运行以下脚本:

```
yarn eslint-staged 
```

*找到的问题列表将记录到控制台.*

要将自动 ESLint 修复应用于所有当前暂存的文件(基于`git diff` ),请运行以下脚本:

```
yarn eslint-staged-fix 
```

*If manual changes are required, a list of changes will be sent to the console.*

要使用 ESLint 检查存储库中的**所有**文件,请运行以下脚本:

```
yarn eslint 
```

*找到的问题列表将记录到控制台.*

要将自动 ESLint 修复应用于存储库中的**所有**文件,请运行以下脚本:

```
yarn eslint-fix 
```

*如果需要手动更改,则会将更改列表发送到控制台.*

**警告:**限制使用全局规则更新. 否则,更改可能导致巨大的合并请求.

### Disabling ESLint in new files[](#disabling-eslint-in-new-files "Permalink")

创建新文件时,请不要禁用 ESLint. 由于遗留兼容性的原因,现有文件可能已禁用现有规则,但是它们正在重构中.

不要禁用特定的 ESLint 规则. 为避免引入技术债务,只有在调用/实例化现有代码模块时,才可以禁用以下规则.

*   [`no-new`](https://eslint.org/docs/rules/no-new)
*   [`class-method-use-this`](https://eslint.org/docs/rules/class-methods-use-this)

**注意:**逐行禁用这些规则. 这使得将来更容易重构. 例如,使用`eslint-disable-next-line``eslint-disable-line` .

### Disabling ESLint for a single violation[](#disabling-eslint-for-a-single-violation "Permalink")

如果确实需要为单个违规禁用规则,请以所需的最少代码量禁用它:

```
// 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[](#the-no-undef-rule-and-declaring-globals "Permalink")

**切勿**禁用`no-undef`规则. 用`/* global Foo */`代替声明 globals.

声明多个全局变量时,请始终为每个变量使用一个`/* global [name] */`行.

```
// bad
/* globals Flash, Cookies, jQuery */

// good
/* global Flash */
/* global Cookies */
/* global jQuery */ 
```

## Formatting with Prettier[](#formatting-with-prettier "Permalink")

对 GitLab 13.2 中[引入](https://gitlab.com/gitlab-org/gitlab/-/issues/227280)`.graphql`支持.

我们的代码会自动使用[Prettier](https://prettier.io)格式化,以遵循我们的样式指南. 漂亮正在格式化的护理`.js``.vue``.graphql``.scss`基于标准的漂亮规则文件. 您可以在`.prettierrc`找到`.prettierrc`所有设置.

### Editor[](#editor "Permalink")

在工作流程中包括更漂亮的最简单方法是相应地设置您的首选编辑器(支持所有主要编辑器). 我们建议将漂亮的文件设置为在保存每个文件时自动运行. [在此处](https://prettier.io/docs/en/editors.html)找到[](https://prettier.io/docs/en/editors.html)首选编辑器中进行设置的最佳方法.

请小心,你只能让更漂亮的格式相同的文件类型,全球纱线脚本执行( `.js``.vue``.graphql``.scss` ). 以 VSCode 为例,您可以轻松地在设置文件中排除文件格式:

```
 "prettier.disableLanguages":  [  "json",  "markdown"  ] 
```

### Yarn Script[](#yarn-script-1 "Permalink")

以下纱线脚本可用于进行全局格式化:

```
yarn prettier-staged-save 
```

使用 Prettier 更新所有当前暂存的文件(基于`git diff` )并保存所需的更改.

```
yarn prettier-staged 
```

使用 Prettier 检查所有当前暂存的文件(基于`git diff` ),并记录哪些文件需要手动更新到控制台.

```
yarn prettier-all 
```

使用 Prettier 检查所有文件,并记录哪些文件需要手动更新到控制台.

```
yarn prettier-all-save 
```

使用 Prettier 格式化存储库中的所有文件. (这仅应用于测试全局规则更新,否则您将获得巨大的 MR).

这些 Yarn 脚本的来源可以在`/scripts/frontend/prettier.js`找到.

#### Scripts during Conversion period[](#scripts-during-conversion-period "Permalink")

```
node ./scripts/frontend/prettier.js check-all ./vendor/ 
```

这将遍历特定文件夹中的所有文件并进行检查.

```
node ./scripts/frontend/prettier.js save-all ./vendor/ 
```

这将遍历特定文件夹中的所有文件并将其保存.

### VSCode Settings[](#vscode-settings "Permalink")

#### Select Prettier as default formatter[](#select-prettier-as-default-formatter "Permalink")

要将" Prettier"选择为格式器,请在"用户或工作区设置"中添加以下属性:

```
{
  "[html]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "[javascript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "[vue]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "[graphql]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  }
} 
```

#### Format on Save[](#format-on-save "Permalink")

要使用 Prettier 自动设置文件格式,请在"用户或工作区设置"中添加以下属性:

```
{
  "[html]": {
    "editor.formatOnSave": true
  },
  "[javascript]": {
    "editor.formatOnSave": true
  },
  "[vue]": {
    "editor.formatOnSave": true
  },
  "[graphql]": {
    "editor.formatOnSave": true
  },
} 
```