簡體   English   中英

如何將 ESLInt 自定義規則目錄添加到 VSCode

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

我正在使用 ESLint 插件運行 VSCode。 我的項目包含一些自定義規則,它們位於特定目錄中。

如果我從命令行運行 ESLint 並傳入--rulesdir=./custom-eslint-rules一切都按預期工作。

但是,我特別指的是編輯器本身中每個文件發生的 linting。 在那里,它使用正常規則進行檢查,但顯示錯誤,我的自定義規則的定義丟失了。

我如何配置 VSCode,以便每個文件 linting 看到我在特定目錄中的自定義規則定義?

在您的 setting.json 中:

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

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

有沒有辦法傳遞額外的參數來忽略我的自定義規則目錄中的某些文件。 就我而言,我在與自定義規則相同的目錄中有測試文件(.spec.js)。 我想忽略eslint.options.rulePaths這個測試文件。

編寫自己的 eslint 插件並使用它與 vs 代碼兼容

1. 安裝eslint-plugin-rulesdir@0.2.1

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

2. 添加到.eslintrc.js

進口

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

插件

plugins: [...,"rulesdir"]

規則

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

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