查看“︁ESLint”︁的源代码
来自康健生活
←
ESLint
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
== 作者 == [https://eslint.org/ ESLint] 是由 Nicholas C. Zakas 在2013年开始开发的,它的初衷就是为了能让开发者能自定义自己的 linting rules,而且它提供了一套相当完善的插件机制,可以自由的扩展,动态加载配置规则,同时可以方便的根据报错定位到具体的规则配置。 == .editorconfig == <syntaxhighlight lang="javascript"> # http://editorconfig.org root = true [*] #缩进风格:空格 indent_style = space #缩进大小4 indent_size = 4 #换行符lf end_of_line = lf #字符集utf-8 charset = utf-8 #是否删除行尾的空格 trim_trailing_whitespace = true #是否在文件的最后插入一个空行 insert_final_newline = true [*.md] trim_trailing_whitespace = false [Makefile] indent_style = tab </syntaxhighlight> == 特征 == * 可扩展、规则独立、可配置。 * lint工具 * QA工具 用来作为静态代码检查,避免低级错误和统一代码的风格。 == 安装 == <syntaxhighlight lang="bash"> npm install - g eslint npm install eslint-config-standard eslint-plugin-standard eslint-plugin-promise eslint --init</syntaxhighlight> == 配置文件 == ESLint 支持多种格式配置文件,同一目录下多个配置文件的优先级顺序如下: # JavaScript - <code>.eslintrc.js</code>。 # YAML - <code>.eslintrc.yaml</code>/<code>.eslintrc.yml</code>。 # JSON - <code>.eslintrc.json</code>,允许 JavaScript 风格注释。 # Deprecated - <code>.eslintrc</code>,文本格式为 JSON/YAML。 # package.json - 创建 <code>eslintConfig</code> 属性。 初始化和生成.eslintrc.*文件。 {{Shc_out|<syntaxhighlight lang="bash"> eslint --init </syntaxhighlight>|<syntaxhighlight lang="bash"> You can also run this command directly using 'npm init @eslint/config'. Need to install the following packages: @eslint/create-config Ok to proceed? (y) y ✔ How would you like to use ESLint? · problems ✔ What type of modules does your project use? · esm ✔ Which framework does your project use? · react ✔ Does your project use TypeScript? · No / Yes ✔ Where does your code run? · browser ✔ What format do you want your config file to be in? · YAML The config that you've selected requires the following dependencies: eslint-plugin-react@latest ✔ Would you like to install them now with npm? · No / Yes Installing eslint-plugin-react@latest Successfully created .eslintrc.yml file in /Users/kangjian/360/medici/client </syntaxhighlight>|➜ client git:(master_sketch_version) ✗}} === IDE === vscode: # 打开 extensions 搜索 ESLint 并安装。 # 点击 code 菜单,选中 setting/extensions,点击 edit in settings.json。 <syntaxhighlight lang="javascript"> { "eslint.codeAction.showDocumentation": { "enable": true }, "eslint.format.enable": true, "eslint.options": { }, "eslint.probe": [ "javascript", "javascriptreact", "html" ], }</syntaxhighlight> == 命令 == 执行检查命令 <syntaxhighlight lang="bash"> eslint add.js</syntaxhighlight> 指定配置文件 <syntaxhighlight lang="bash"> eslint - c config.json add.js</syntaxhighlight> 打印出eslint所使用的配置和规则 <syntaxhighlight lang="bash"> eslint --print-config xx.js > 1</syntaxhighlight> == 配置 == # 使用.eslintrc.*文件( 支持JSON和YAML两种语法) # 在package.json中添加eslintConfig配置块 # 使用JavaScript注释直接把配置嵌入到文件中 == 规则 == <div style="background: #E9FBE9;padding: .5em;page-break-inside: avoid;border-left: .5em solid #52E052;">"quotes": "error"</div> 第一部分:规则名 第二部分:规则的严重性(rule severity) * "off" * or 0 - turn the rule off 不验证 "warn" * or 1 - turn the rule on as a warning(doesn’ t affect exit code) 警告 "error" * or 2 - turn the rule on as an error(exit code is 1 when triggered) 错误 规则的选项(additional options) <div style="background: #E9FBE9;padding: .5em;page-break-inside: avoid;border-left: .5em solid #52E052;">"quotes": [2, "double"]</div> ☟ {| class="wikitable" |- | [[/规则 |规则]] |} == 示例 == {| class="wikitable" |- | [[/提交指定文件|提交指定文件]] || 示例 || 示例 |- | 示例 || 示例 || 示例 |- | 示例 || 示例 || 示例 |} == 技巧 == {| class="wikitable" |- | [[/忽略校验|忽略校验]] || [[/全局变量|全局变量]] || 示例 |- | 示例 || 示例 || 示例 |- | 示例 || 示例 || 示例 |} == 修复代码 == {{hc|package.json|<syntaxhighlight lang="javascript"> "scripts": { "lint": "eslint --fix --ext .js,.vue --ignore-path .gitignore .", }, </syntaxhighlight>}} 运行 <syntaxhighlight lang="bash"> npm run lint</syntaxhighlight> 修复代码样式 == 参数 == {| class="wikitable" |- | --ignore-path || 指定忽略配置文件,配置此参数,则 <code>.eslintignore</code> 失效 || <syntaxhighlight lang="bash"> eslint –ignore-path .gitignore</syntaxhighlight> |- | 示例 || 示例 || 示例 |- | 示例 || 示例 || 示例 |} == .eslintignore == [[/.eslintignore|.eslintignore]] == error == * ESLint: Expected indentation of 10 spaces but found 20 spaces.( *: <syntaxhighlight lang="javascript"> rules: { indent: ["error", 4, { SwitchCase: 1 }],</syntaxhighlight> * ESLint:please specify Node.js interpreter correctly *: webstorm 没有配置 nodejs 环境,我使用的是 wsl(windows subsystem for linux),所以没有找到命令 * eslint: please specify path to eslint package *: <code>npm i eslint</code> * ESLint: Unexpected side effect in "renderDataList" computed property.(vue/no-side-effects-in-computed-properties) *: 此规则旨在防止在计算属性中产生副作用的代码,报错代码如下: *: <syntaxhighlight lang="javascript"> renderDataList() { const HSF = this.list const dataFilters = HSF[0].filters this.isCountry = dataFilters[0].key this.isDepartment = dataFilters[1].key this.isCountryValue = dataFilters[0].options[0].value this.isDepartmentValue = dataFilters[1].options[0].value return { searchFilters: dataFilters } }</syntaxhighlight> *: 更改为如下 *: <syntaxhighlight lang="javascript"> computed: { renderDataList: { get() { const HSF = this.list if (!HSF[0].filters) { return [] } return { searchFilters: HSF[0].filters } }, set() { const param = this.renderDataList.searchFilters this.isCountry = param[0].key this.isDepartment = param[1].key this.isCountryValue = param[0].options[0].value this.isDepartmentValue = param[1].options[0].value } } },</syntaxhighlight> * Parsing error: setter must have exactly one formal parameter * 'util.isArray' was deprecated since v4.0.0. Use 'Array.isArray()' instead.(node/no-deprecated-api) * Arrow function has a complexity of 30. Maximum allowed is 20.(complexity) *: * Type of the default value for 'searchHotList' prop must be a function *: <syntaxhighlight lang="javascript"> props: { searchHotList: { type: Array, default: function () { return [] } } }</syntaxhighlight> *: <syntaxhighlight lang="javascript"> props: { searchHotList: { type: Array, default: () => [] // es6的箭头函数 } }</syntaxhighlight> * ESLint: Missing accessibility modifier on method definition constructor.(@typescript-eslint/explicit-member-accessibility) *: 缺少可访问的修饰符 * Environment key "es2021" is unknown * The eslint library loaded from /Users/kangjian/360/medici/client/node_modules/eslint/lib/api.js doesn't export a CLIEngine. You need at least eslint@1.0.0 *: {{Shc_out|<syntaxhighlight lang="bash"> which eslint</syntaxhighlight>|<syntaxhighlight lang="bash"> /usr/local/bin/eslint </syntaxhighlight>|➜ client git:(master_sketch_version) ✗}} == 雁阵规则 ==
此页面嵌入的页面:
模板:Hc
(
查看源代码
)
模板:Hc/style.css
(
查看源代码
)
模板:Shc out
(
查看源代码
)
模板:Shc out/style.css
(
查看源代码
)
返回
ESLint
。
导航菜单
康健生活
搜索
user-page
通知
user-interface-preferences
个人工具
登录
五一
多读书,多运动,多到远处走走,再看看不一样的世界。