{"id":17640912,"url":"https://github.com/beace/doc-eslint","last_synced_at":"2025-03-30T05:24:17.919Z","repository":{"id":98535820,"uuid":"83286056","full_name":"Beace/doc-eslint","owner":"Beace","description":"eslint","archived":false,"fork":false,"pushed_at":"2017-03-06T09:51:06.000Z","size":276,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-05T07:31:15.439Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://beace.github.io/doc-eslint/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Beace.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-02-27T08:22:39.000Z","updated_at":"2017-03-06T09:51:29.000Z","dependencies_parsed_at":"2023-05-29T09:15:27.495Z","dependency_job_id":null,"html_url":"https://github.com/Beace/doc-eslint","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Beace%2Fdoc-eslint","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Beace%2Fdoc-eslint/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Beace%2Fdoc-eslint/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Beace%2Fdoc-eslint/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Beace","download_url":"https://codeload.github.com/Beace/doc-eslint/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246279833,"owners_count":20752018,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-23T06:24:43.694Z","updated_at":"2025-03-30T05:24:17.876Z","avatar_url":"https://github.com/Beace.png","language":"JavaScript","readme":"# [ESLint](http://eslint.org/) 的使用\n\n[ESLint](http://eslint.org/)可以作为代码审查的工具，来强制的制定一些代码规则或规范来管理、统一一个团队的代码风格。\n\n你可以将它作为一个代码检查的工具，应用它的规则可以检测出代码中某些潜在的问题或者值得优化的代码，并且，你也可以将它作为一个代码规范的标准来养成你的书写习惯。\n\n## 历史\n\n[ESLint](http://eslint.org/)作为一个开源项目，是由Nicholas C. Zakas于2013年6月创建。\n\n## 用法\n\n[ESLint](http://eslint.org/)的用法非常简单，首先在本地初始化项目，安装`ESLint`。\n\n```sh\nnpm init -y\nnpm install --save eslint\n```\n\n在项目根目录下建立`.eslintrc`\n\n在这里，记录下`rc`结尾的文件是什么含义。\n\n### rc file\n\n\u003e Actually rc stands for. runtime configuration.the rc files configure what software (application) and services are configured to start at runtime, therefore: runtime configuration. —— wiki\n\n在`Linux`下，rc（runtime configuration）代表运行时配置。表示在某种运行环境下自动执行的配置文件。前端常用的例如`.babelrc`、`.jsbeautifyrc`。\n\n\n在[ESLint](http://eslint.org)中最常用的就是其规则（[rule](http://eslint.org/docs/rules/))。在`.eslintrc`中写入以下规则\n\n```json\n//.eslintrc\n{\n  \"rules\": {\n    \"no-console\": 2\n  }\n}\n\n```\n\n这条规则表示当代码中出现类似`console.log()`这种调试代码时报错。我们可以先新建一个`my.js`文件来故意写一个错误。\n\n```js\nfunction a() {\n  console.log(\"fsafs\");\n}\na();\n```\n\n可以执行命令先来看下运行结果。\n\n```sh\n./node_modules/.bin/eslint my.js\n```\n\n**screenshot**\n\n![eslint](https://images-manager.oss-cn-shanghai.aliyuncs.com/static/eslint/eslint.png)\n\n\u003e 从上图可以看出，[ESLint](http://eslint.org)给出了错误提示，并且告诉了我们代码错误的原因以及错误出现的行列数。该错误为第三行第三列的代码中出现了`console`的表达式。\n\n在`rule`的书写方式中，看到了有`\"no-console\": 2`，这样的写法。其实有三个等级。\n\n- 0 表示正确\n- 1 表示警告`warning`\n- 2 表示错误`error`\n\n我们可以将其程度调节到0和1分别看下效果。\n\n\u003e 0的时候没有任何错误输出，1的时候出现了一个警告错误。\n\n**screenshot**\n![eslint](https://images-manager.oss-cn-shanghai.aliyuncs.com/static/eslint/eslint-warning.png)\n\n## 规范继承\n定制一个规范要在`rule`中写很多的配置项，我们可以和一些一线互联网公司遵守相同的代码规范。只需要在配置文件中加入如下代码。下面代码使用了`Google`的代码规范。\n\n```json\n//.eslintrc\n{\n  \"extends\": [\"eslint-config-google\"]\n}\n```\n\n当然还需要安装依赖。\n\n```sh\nnpm install --save-dev eslint-config-google\n```\n\n## 插件\n\n[ESLint](http://eslint.org/)提供插件配置，例如`react`、`babel`等。\n\n```json\n//.eslintrc\n{\n  \"plugins\": [\n    \"react\",\n    \"babel\"\n  ]\n}\n```\n\n当然，也需要添加相应的依赖。\n\n```json\n{\n  \"eslint-plugin-babel\": \"^4.0.0\",\n  \"eslint-plugin-react\": \"^6.9.0\"\n}\n\n```\n\n## 总结\n\n一个团队的代码风格，以及代码规范是非常重要的。不仅仅能够让团队协作更加高效（别人能够读的懂你的代码），而且对于代码可维护性、辨识度、准确度有明显的提高。\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeace%2Fdoc-eslint","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbeace%2Fdoc-eslint","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeace%2Fdoc-eslint/lists"}