插件

指定解析器

默认情况下,ESLint 使用 Espree 作为其解析器。您可以选择指定在配置文件中使用不同的解析器,只要解析器满足以下要求:

  1. 它必须是可从使用解析器的配置文件加载的 Node 模块。通常,这意味着您应该使用 npm 单独安装解析器包。
  2. 它必须符合 解析器接口

请注意,即使具有这些兼容性,也不能保证外部解析器可以与 ESLint 正确工作,并且 ESLint 不会修复与其他解析器不兼容相关的错误。

要指定 npm 模块用作解析器,请使用 .eslintrc 文件中的 parser 选项指定它。例如,以下指定使用 Esprima 而不是 Espree:

{
    "parser": "esprima",
    "rules": {
        "semi": "error"
    }
}

以下解析器与 ESLint 兼容:

bttnw0lcsws7IvtoXji4J+q+u0rHAZps8jDU5CvINX23RSP9yr/K0Y+Y4yPnXowXxqzXU3Uxts1RZXosBzrgHBfKnkr3g67bkc9f0nXaglDAcG4tOkMl3lp0NqhWpn3slH8rhO5TTh5gYyc7mHP5wjN+8bSKNbs2+bjLfdS8lLFRyWfnwQIvdLZeOHNG4xWRiDJ4Pe5Og5KJw5hAaf234I8rlx4PnAksy3mozv3N6lY+X/WGffB8W2aAr3gnGnKJYfqk8yXO7OCBrC3gwdldNiSnCa1a098/JvR2Hqh3D/7hIFmTm6SPG/5TX8rrpB8wvUwMko0nKyMiFYkGtG89fjAl3jIiKLG2IvHewdJVbPu/WPRT46tZ8KqfscWH25Hm5DVi2NSAGX1SVlH6ESbASg==

指定处理器

LeY7Bwe9mFkpkz5L1ssI62Xfio5UxlmjI+Xyfp+iWn74BnL0KspAcE0uXKXBiiU5h2HBG1NF9Hjbe3LlSfsHCajKWIsKB7B5xjZgqh1juDVZV2hGFuCX6OwFPwSGFp5REpBMEdyWh2UDG/tBWMQUQ516HCjYxxNlFaLZm8SFfhwYDXIJ/fRrBEWtYZeoTAytyR5lDejE7IGbpyH3OPfQQoe/w0yg4P4Z6vwb7b7G2fs60dHrt1vUXjgwCmabndNhf/olLwuuAFfXuEDj+AiwRJwX1bktUC2t1l2SCpdloZQCQOxZw+koYACv9wiptc0b

dvSF9U51Hom/MBOHSickBiw8u9QbgRwWFazhC+6LniihEZEfswf8oY9dZG3/TN9HNnEiW3ftJUEqgorZQYF6adLljGHYrvGo28lA8/jK6T/l024dVb00YenbE2MaIz0edebiNho7iy4WeJ1wo6LNwUYnCkWa+wP3zWyRcClZj68WxjIQ2LMkVnARzkWWFIZnCmW5d5lX4+P08uQ3hm3390NQA3Jyg+naJszKGFNvacoaJp371ZAGMbSiyOUey16tZDJRWbCyOR9bKE+vybxcA5DK7+Qk8IlRjpSwSVXKH1CBwmf+yfIlc5d7HLmHU6kfDeJe2R7OI09JxAdt19lIoQ==

{
    "plugins": ["a-plugin"],
    "processor": "a-plugin/a-processor"
}

0smxv5dSDcuD1xq9l77OnP8jtq7p2cDyL1HAyfZJB2W4aww1e3AW42qoQubvPHS8Kq9aN/WqgyyRBxHPnnKx7X5RRhfrVjPr9cDUghNdQp+5vgBNHSEyYiRYK0bX0ZRb7O0oALM/kaqi8NTG+Vhfxbg8o3IyEGMcPz/abwBN4wshq9BcrMyeoi6VUXLnLkiy3w7+lXZaAib9zN4OpMhpUHzB67/jjTpylFHtrVpWpJfKJW3v6k2Wav9h40e7uKADxTTe04h9a5ShcuHz7tOLNJI2JU0Cl8cbeemy70+Hwho=

{
    "plugins": ["a-plugin"],
    "overrides": [
        {
            "files": ["*.md"],
            "processor": "a-plugin/markdown"
        }
    ]
}

2Ak+OCVGM02l8sXcID9gA13MB2WyjLYNcP/2ojeqL/z3Weo5GpE6vw7GT476HCSAeqMTGujB44Et3TwXCnGS/oZY1acMhOFP/WOiz9fDFxRZqTGC6HNjdDYTzIseVLX6X1OcMqTwQPLKc7YYfWJh2caMYT1p5xYcLjJofTuf5xivoEhwstIKLnDx9b67p2NVMAQ0GwBDKoeZF7n+dLgN0HIa9WwVtXkzPRvkcupaN5BKjEZD/Kghh1V1OniC4Sx4rwoCsMIqx8csTrvM9RmMU3qNo71DDQvAvW2JW/oRHdnqTsDwPF50c4NydglkSmzLzDaV872vXVY9tmcs0SvIHbUk/8vZIt24/U4Gcpqv6Qj/INDsqyCiFZ6tEZQoG5ukOX9N8ZEhM5KZ9kDxgPY7rm0aL0zN+ld4wjdJeKY0Xb16e9WVPZoEVYsk9lUWdNhQ6asQ6QQ4gOt8PAI8/xdxJM+tlsZ+oU7Gz13BdId1ROQ=

{
    "plugins": ["a-plugin"],
    "overrides": [
        {
            "files": ["*.md"],
            "processor": "a-plugin/markdown"
        },
        {
            "files": ["**/*.md/*.js"],
            "rules": {
                "strict": "off"
            }
        }
    ]
}

J+flRNHddjWNH1tU85UJz6rNq1+t7Wt+FfzGVQL1a8eatuXOwRd1YhMuYEBoYolBvhmL5Ik/DoIly6nGRv2ykHKcBfCKFf/RDevhJzIZOsuUQ3t0iPdeFM/lFLJNFlrr82cX4GGnRLj4ObNkf51FPX5Mb09ykPwLe71RlB4f9bWYBN4EHDjtLuGTrAZM7IaUBHFgo6rB5OafnSkPOClVNOh0T1Z+bx8gzUXAsdaoi+OpEpb/KDkXTKiCf5aLaS01/iqKLjvzidD79upOZG5iUM6Zd/dBnyBFZum3SbqB/Hp6GC9n7BdwbyrexbTE05wOy2NwknDnHm4VekXqrVuF9A==

配置插件

gIGJ1OM115jVWxARYNo0A+eyUM8b9t6v1guqNO/DoVGDHCkSo/ev8xV3PRS6/W1/4gUwU0AOJCxgyWCiR4k+tk1DbhGtPeP0y8YYdHJhvUUrp1OfbNXFZk/Rox0MXv+e

dvSF9U51Hom/MBOHSickBuFNY4HR58YbFJd/+d6mQaNfNGnw93TayifAGGoA3K0RNbqQPfxhHf6ur3rp2LJ3n+loR3/R/OBeD1Npj1R7eBNL8gmG5fTbHX23UPKQdd9Ql/zERgGpubMAJlNKDLZRp0JAUiChNK6YN/q8K5WhLoe6rmBc5Y6XNCm89PAD+o58HRTBDAdWyyhkK2AAvgpXSY6EKXTSRTzhAqShHx8R/vQ=

{
    "plugins": [
        "plugin1",
        "eslint-plugin-plugin2"
    ]
}

lObsq6aAiA8MkzIPYrFeAw==

---
  plugins:
    - plugin1
    - eslint-plugin-plugin2

4y/020zCKZw9AYAGpZ/mLdfJkpfj9ne3G8xYWorueos=

  1. 插件相对于配置文件进行解析。换句话说,ESLint 将加载插件,就像用户通过在配置文件中运行 require('eslint-plugin-pluginname') 获得的一样。
  2. 基本配置中的插件(由 extends 设置加载)相对于派生的配置文件。例如,如果 ./.eslintrcextends: ["foo"],而 eslint-config-fooplugins: ["bar"],ESLint 会从 ./node_modules/(而不是 ./node_modules/eslint-config-foo/node_modules/)或祖先目录中找到 eslint-plugin-bar。因此,配置文件和基本配置中的每个插件都是唯一解析的。

命名约定

引入插件

MPvubEkkWkngaNNHWL2PLBCSByZYxPC4yUDB6VvDC619n/uLbopM8YzyyAmvf/yWq6J00M4jmyvI47eUaXvZZx9n/I2J41iqBHSTFOsC7ug=

{
    // ...
    "plugins": [
        "jquery", // means eslint-plugin-jquery
    ]
    // ...
}

xeJgRDO+CsiiKEl6sZf4qaI5DYfOvu06WR4YII8UKHFuyH4A9Nwes5d/F3jWozoY

{
    // ...
    "plugins": [
        "@jquery/jquery", // means @jquery/eslint-plugin-jquery
        "@foobar" // means @foobar/eslint-plugin
    ]
    // ...
}

使用插件

TmwLpEByYmRtPlY0P4lJZSOfujMb45lSTKFkufWP8TCG4bdf/cCyQq1Buh0uZqDLpplr+CnnFKtBtnnekHyQSGvWS38nHBOwV7lAEb+LYA5IprOMMq0HFeaF/5DdpMod

    dU6pzTC2HXjbnt//YLe11DKBl3dO8ML157y3lNjahPYCQzwQHuO4BvCcSofDh0hJ0cuZk2Y0S2B/bQ56oa9s0w3ppuzXlWuwkzSWmGiK78M67AyRNfZkwCVKWslr7Wvf3i8p44o8+sTbw56BGrIh2Cxg1RBGT5ua2z06YKaZjeh7POlK6El5KwT/nRwNuQiSctx5HRNr4iy1HMoxqzxaX/w2YQfgf5R4NgTbD8bas0yN/Cb+YMMLhrr7Y9r5r9Q29IhrwdwsFvzYSSXsu5dU8on4e/8l+483dCMboaGl2VN3LsPKi4nnO/drV48OGlka

Akr0b3VWyNVkmIJ2N7QdaA==

{
    // ...
    "plugins": [
        "jquery",   // eslint-plugin-jquery
        "@foo/foo", // @foo/eslint-plugin-foo
        "@bar"      // @bar/eslint-plugin
    ],
    "extends": [
        "plugin:@foo/foo/recommended",
        "plugin:@bar/recommended"
    ],
    "rules": {
        "jquery/a-rule": "error",
        "@foo/foo/some-rule": "error",
        "@bar/another-rule": "error"
    },
    "env": {
        "jquery/jquery": true,
        "@foo/foo/env-foo": true,
        "@bar/env-bar": true,
    }
    // ...
}