简体   繁体   English

如何将 ESLInt 自定义规则目录添加到 VSCode

[英]How can I add ESLInt custom rules directory to VSCode

I am running VSCode with the ESLint plugin.我正在使用 ESLint 插件运行 VSCode。 My project contains some custom rules, which live in a particular directory.我的项目包含一些自定义规则,它们位于特定目录中。

If I run ESLint from the command line and pass in --rulesdir=./custom-eslint-rules everything works as expected.如果我从命令行运行 ESLint 并传入--rulesdir=./custom-eslint-rules一切都按预期工作。

However, I am specifically referring to the linting that happens per file in the editor itself.但是,我特别指的是编辑器本身中每个文件发生的 linting。 There, it lints using the normal rules, but shows errors that the definitions for my custom rules are missing.在那里,它使用正常规则进行检查,但显示错误,我的自定义规则的定义丢失了。

How can I configure VSCode so that the per-file linting sees my custom rule definitions that live in a particular directory?我如何配置 VSCode,以便每个文件 linting 看到我在特定目录中的自定义规则定义?

In your setting.json:在您的 setting.json 中:

"eslint.options": {
  "rulePaths": "<path>"
}

Reference: https://eslint.org/docs/developer-guide/nodejs-api#cliengine参考: https : //eslint.org/docs/developer-guide/nodejs-api#cliengine

Is there a way to pass extra params to ignore some file in my custom rule dir.有没有办法传递额外的参数来忽略我的自定义规则目录中的某些文件。 In my case, I have test files(.spec.js) in the same dir as my custom rules.就我而言,我在与自定义规则相同的目录中有测试文件(.spec.js)。 And I'd like to ignore this test files in eslint.options.rulePaths .我想忽略eslint.options.rulePaths这个测试文件。

Write your own eslint plugin and use it with vs code compatibility编写自己的 eslint 插件并使用它与 vs 代码兼容

1. install eslint-plugin-rulesdir@0.2.1 1. 安装eslint-plugin-rulesdir@0.2.1

yarn add --dev eslint-plugin-rulesdir@0.2.1

2. add to.eslintrc.js 2. 添加到.eslintrc.js

import进口

const rulesDirPlugin = require("eslint-plugin-rulesdir");
rulesDirPlugin.RULES_DIR = "src/rules"; // it is example

plugins插件

plugins: [...,"rulesdir"]

rules规则

rules: {
  ...,
  "rulesdir/no-get-payments": "error"
}

3. create rule file src/rules/no-get-payments.js 3. 创建规则文件 src/rules/no-get-payments.js

module.exports = {
  create: function (context) {
    return {
      CallExpression: function (node) {
        if (node.callee.name === "getPayments") {
          context.report({
            node: node,
            message: "getPayments is depricated! ",
          });
        }
      },
    };
  },
};

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

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