开发eslint插件目的:根据项目须要,自定义知足项目特殊须要的校验规则node
参考eslint官方文档展开阐述git
下面开始经过一个示例demo来介绍插件整个开发流程github
代码中出现的方法及变量的详细解释与相关文档,会在文末给你们列举出来,你们能够先把代码拷贝到本身的demo中而后结合本文第3部分
的变量|方法解释去理解代码npm
开发一个校验注释中是否包含指定关键词的插件(
eslint-plugin-comments-key
)json
. ├── README.md 插件介绍文档 ├── index.js 对外暴露插件 ├── lib │ └── rules 自定义规则 │ └── comments-key.js ├── package.json └── tests 测试自定义规则 └── lib └── rules └── comments-key.js
npm i eslint mocha -D
不包含自定义参数校验规则api
/lib/rules/comments-key.js数组
module.exports = { meta: { type: "suggestion", docs: { description: "Not allowed comment words", // 规则的简述 category: "Stylistic Issues", // 规则分类 recommended: true // 配置文件中的 "extends": "eslint:recommended"属性是否启用该规则 } }, create: function (context) { // context对象包含与规则上下文相关的信息 // 返回一个SourceCode对象,你可使用该对象处理传递给 ESLint 的源代码 const sourceCode = context.getSourceCode() // 定义不被容许出如今注释中的内容 const notAllowWords = ['fixme', 'xxx'] return { Program(node) { // 获取全部注释的节点 const comments = sourceCode.getAllComments() // 遍历注释节点判断是否有不符合规范的 comments.forEach(comment => { let { loc, value, type } = comment value = value.toLowerCase() let warnWord = '' // 判断注释内容是否包含不被容许的word for (const word of notAllowWords) { if (value.includes(word)) { warnWord = word } } if (warnWord) { context.report({ node: comment, // 可选 与问题有关的 AST 节点 message: `注释中含有不被容许的字符${warnWord}` // 有问题发出的消息 }) } }) } }; } };
/tests/lib/rules/comments-key.jsbash
const { RuleTester } = require('eslint') // 获取自定义的规则 const rule = require('../../../lib/rules/comments-key') // TESTS // 加入默认配置 const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2018 } }) const errMsg = warnWord => `注释中含有不被容许的字符${warnWord}` ruleTester.run('comments-key', rule, { valid: [ '// sssss', '// fixdddd', `/** * 容十三内水s是说 */` ], invalid: [ { code: "// fixme: DDL 2020-4-28 测试内容", errors: [{ message: errMsg('fixme') }] }, { code: "// FIXME: DDL 2020-5-23 测试内容", errors: [{ message: errMsg('fixme') }] }, { code: `/** * xxx * 内容 */`, errors: [{ message: errMsg('xxx') }] } ] })
加入编辑器
"scripts": { "test": "mocha tests/lib/rules" }
运行脚本查看测试结果ide
npm run test
上面的示例中限定的关键词是在代码中写死了的
一般的场景中如:
rules:{ "quotes": ["error", "double"], // 只容许双引号 "no-warning-comments": [ // 不容许注释开头出现 todo|fixme等内容 1, { "terms": [ "todo", "fixme" ], "location": "start" } ], }
大多数eslint规则都拥有可配置的属性
咱们能够经过context.options
获取配置的属性
下面示例加入可配置属性,用于自定义关键词的检测(代码中只包含修改部分,其他部分跟前面相同)
module.exports = { meta: { // ...code schema: [ // 指定该选项 这样的 ESLint 能够避免无效的规则配置 // 遵循 json schema 后文会有介绍文档 { "keyWords": { "type": "array", "items": { "type": "string" } } } ] }, create: function (context) { // ...code // 定义不被容许出如今注释中的内容 // 可使用 context.options检索一个规则的可选项,它是个数组,包含该规则的全部配置的可选项 // console.log(context.options); // 取得设置的keywords let [argv0] = context.options let keyWords = argv0 ? argv0.keyWords ? argv0.keyWords.length > 0 ? argv0.keyWords : undefined : undefined : undefined // 没有设置则使用默认的 let notAllowWords = keyWords || ['fixme', 'xxx'] // 忽略大小写 notAllowWords = notAllowWords.map(v => v.toLowerCase()) // ...code } };
// ...code ruleTester.run('comments-key', rule, { valid: [ '// sssss', '// fixdddd', `/** * 容十三内水s是说 */` ], invalid: [ { code: "// fixme: DDL 2020-4-28 测试内容", errors: [{ message: errMsg('ddl') }], options: [{ // 经过options 配置自定义参数 keyWords: ['ddl'] }] }, { code: '// FIXME: DDL 2020-5-23 测试内容 \n let a = "232"', errors: [{ message: errMsg('fixme') }], rules: { // 经过rules 配置eslint提供的一些规则 "quotes": ["error", "double"], }, options: [{ keyWords: ['abc', 'efg', 'fixme'] }] }, { code: `/** * xxx * 内容 */`, errors: [{ message: errMsg('xxx') }] }, { code: '// abds asa', errors: [{ message: errMsg('abd') }], options: [{ keyWords: ['abc', 'abd'] }] } ] })
getSourceCode()
返回一个SourceCode对象,你可使用该对象处理传递给 ESLint 的源代码
loc
注释在文档中的位置value
注释中的内容type
注释的类型 Block
|Line
message
有问题的消息提示node
(可选)与问题有关节点loc
(可选)用来指定问题位置的一个对象。若是同时指定的了 loc 和 node,那么位置将从loc获取而非nodedata
(可选) message的占位符fix
(可选) 一个用来解决问题的修复函数tips:AST在开发插件时没必要深刻研究,不一样地方AST的实现和结构都有所差别
至此咱们的插件算开发完成了,接下来编写对eslint暴露这个模块的代码
index.js
'use strict'; module.exports = { rules: { 'diy': require('./lib/rules/comments-key') }, rulesConfig: { 'diy': 1 } };
要在其它项目中使用的eslint-plugin插件的话,能够把整个插件的根目录拷贝到目标项目的node_modules中或者发布到npm
中去,其它项目直接经过npm install
安装这个依赖
下面介绍发布到npm的步骤
直接点击官网注册
npm config set registry https://registry.npmjs.org/
npm login
按提示依次输入帐号
,密码
,邮箱
登陆完成以后,查看当前npm用户,不报错说明登陆成功
npm whoami
README.md
方便指引他人使用{ "name": "eslint-plugin-comments-key", "version": "1.0.0", "description": "校验注释中是否包含指定关键词的插件", "main": "index.js", "directories": { "lib": "lib", "test": "tests" }, "scripts": { "test": "mocha tests/lib/rules" }, "keywords": [], "author": "", "license": "ISC", "devDependencies": { "eslint": "^7.0.0", "mocha": "^7.1.2" } }
npm publish
发布npm包至此发布整个流程完毕
You'll first need to install ESLint:
$ npm i eslint --save-dev
Next, install eslint-plugin-comments-key
:
$ npm install eslint-plugin-comments-key --save-dev
Note: If you installed ESLint globally (using the -g
flag) then you must also install eslint-plugin-comments-key
globally.
Add comments-key
to the plugins section of your .eslintrc
configuration file or package.json
. You can omit the eslint-plugin-
prefix:
package.json
demo
"eslintConfig": { "plugins": [ "comments-key" ], "rules": { "comments-key/diy":[1,{ "wordKeys":["fixme","xxx"] }] } }
tips: 若是编辑器中安装了Eslint插件,在编码的时候就会给予警告⚠️
因笔者水平有限,内容上若有阐述不明白之处,还请斧正