简体   繁体   English

仅对编辑过的文件启用 eslint

[英]Enable eslint only for edited files

Recently, I configured eslint one of my projects.最近,我在我的一个项目中配置了 eslint。 This project was consisting of hundreds of files, as soon as I configured eslint starts showing errors in all files.这个项目由数百个文件组成,一旦我配置了 eslint 就开始在所有文件中显示错误。

IMO, eslint traverses through all js files, which is not required currently. IMO,eslint遍历所有js文件,目前不需要。 Is there any way we can limit eslint only for edited files?有什么方法可以限制 eslint 仅用于编辑过的文件?

eslint --fix $(git diff --name-only HEAD | xargs)

I'm using this and it works like a charm我正在使用它,它就像一个魅力

LIST=`git diff-index --name-only --diff-filter=d HEAD | grep .*\\.js | grep -v json`; if [ \"$LIST\" ]; then eslint --fix $LIST; fi

I've added it to the package.json as a script under the name of lint:git and now I execute it with yarn lint:git我已经将它作为lint:git名称下的脚本添加到 package.json 中,现在我用yarn lint:git执行它

Taken from https://gist.github.com/oroce/11282380#gistcomment-2741146摘自https://gist.github.com/oroce/11282380#gistcomment-2741146

  1. If you want to lint staged and edited files, use:如果要对暂存和编辑的文件进行 lint,请使用:

     eslint $(git diff --name-only HEAD | grep -E '\\.(js|jsx)$' | xargs)

    or run it with yarn if you want to use it in the terminal:如果您想在终端中使用它,或者使用 yarn 运行它:

     yarn run eslint $(git diff --name-only HEAD | grep -E '\\.(js|jsx)$' | xargs)

    Explanation:解释:

    • git diff --name-only HEAD : name of edited files git diff --name-only HEAD : 已编辑文件的名称
    • grep -E '\\.(js|jsx)$' : Filter js and jsx files. grep -E '\\.(js|jsx)$' : 过滤 js 和 jsx 文件。 You can add your file formats here.您可以在此处添加文件格式。
    • xargs : get names as arguments xargs : 获取名称作为参数
  2. Also you can use lint-staged library to lint staged files.您也可以使用lint-staged库来 lint 暂存文件。 It's useful if you want to run it before each commit and make sure your repo always remains pretty and linted.如果您想在每次提交之前运行它并确保您的 repo 始终保持漂亮和 linted,它会很有用。
    Read more about it here .在此处阅读更多相关信息。

  3. If you want to lint last committed files, use:如果要对上次提交的文件进行 lint,请使用:

     eslint $(git diff --name-only HEAD HEAD~1 | grep -E '\\.(js|jsx)$' | xargs)

similar to @ELCAPITANBETO 's feedback类似于@ELCAPITANBETO 的反馈

we can do我们可以做的

eslint -c eslintrc.js $(git diff --name-only --diff-filter=ACMRTUXB master | grep -E \\"(.js$|.ts$|.tsx$)\\")

https://gist.github.com/seeliang/0f0de424d1cdc4541c338f4ee93b7e6a https://gist.github.com/seeliang/0f0de424d1cdc4541c338f4ee93b7e6a

For larger files where doing a pull-request with thousands of line changes isn't practical, you can use lint-diff对于较大的文件,在其中执行包含数千行更改的拉取请求是不切实际的,您可以使用lint-diff

This tool just lints the lines that were changed:此工具仅对已更改的行进行 lint:

https://github.com/grvcoelho/lint-diff https://github.com/grvcoelho/lint-diff

The following command is very convenient for me:以下命令对我来说非常方便:

git diff --name-only | xargs eslint --fix

It applys eslint --fix for each row of the git diff --name-only output via xargs command.它通过xargs命令为git diff --name-only输出的每一行应用eslint --fix

You could have multiple eslintrc and configure each to behave the way you want.您可以有多个 eslintrc 并将每个配置为您想要的方式。

Refer to cascading and hierarchy at the official docs.请参阅官方文档中的级联和层次结构。 https://eslint.org/docs/2.0.0/user-guide/configuring#configuration-cascading-and-hierarchy https://eslint.org/docs/2.0.0/user-guide/configuring#configuration-cascading-and-hierarchy

Essentially, you can have different rules for different folders and configure it accordingly.本质上,您可以为不同的文件夹设置不同的规则并进行相应的配置。

Assuming by edited files you mean your project specific code in src and not other library code in different folders假设edited files是指src项目特定代码,而不是不同文件夹中的其他库代码

The answer is: do not run Eslint globally for all files.答案是:不要对所有文件全局运行 Eslint。 I think what you want is to prevent the offending code to be commited to repository (of cause, it's already there, commited before linting was introduced, but you want to prevent new commits to be made that contain eslint errors).我认为您想要的是防止将有问题的代码提交到存储库(原因是,它已经存在,在引入 linting 之前提交,但您想防止进行包含 eslint 错误的提交)。
This means that it makes sense to run Eslint only on those files, that have changes in them and are about to be commited to repo, ie staged files.这意味着只在那些文件上运行 Eslint 是有意义的,这些文件中有更改并且即将提交到 repo,即暂存文件。 There is a package that does exactly that, lint-staged .有一个包可以做到这一点, lint-staged In your package.json, you add a configuration like this:在 package.json 中,添加如下配置:

"scripts": {
  "lint": "eslint --fix",
},
"lint-staged": {
  "*.{js,mjs,cjs,jsx,ts,tsx,vue}": [
     "npm run lint"
  ]
}

Then when you run npm run lint-staged , only the files that are staged will be passed as argument to lint command, so only those will be linted.然后当你运行npm run lint-staged ,只有被暂存的文件才会作为参数传递给 lint 命令,所以只有那些会被 lint。 Usually it makes sense to combine lint-staged with husky to automatically run linting and some other checks like formatting as a pre-commit hook (ie each time when you try to make a commit those checks will be executed) This way you can make sure that no code that contains errors will be commited.通常,将 lint-staged 与husky结合起来自动运行 linting 和其他一些检查,例如格式化为预提交钩子(即,每次尝试进行提交时,这些检查都将被执行)是有意义的,这样您就可以确保不会提交包含错误的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM