粥里有勺糖

vuePress-theme-reco 粥里有勺糖    2018 - 2023
粥里有勺糖 粥里有勺糖

Choose mode

  • dark
  • auto
  • light
关于我
备战春秋
  • 心得总结
  • 校招考点汇总
  • 面经汇总
  • 复习自查
技术笔记
  • 技术教程
  • 模板工程
  • 源码学习
  • 技术概念
  • 个人作品
  • 学习笔记
计算机基础
  • 算法与数据结构
  • 操作系统
  • 计算机网络
  • 设计模式
  • 剑指offer
大前端
  • javascript
  • vue
  • html
  • css
  • 🌏浏览器专题
  • Web性能优化
  • regexp
  • node
面试
  • 问解
  • javascript
  • css
  • 手撕代码
  • 性能优化
  • 综合问题
  • 面经汇总
  • 小程序
手撕代码
  • 数据结构与算法
  • javascript
  • css
个人站点
  • GitHub (opens new window)
  • 博客园 (opens new window)
  • 掘金 (opens new window)
线上作品
  • 轻取(文件收集) (opens new window)
  • 个人图床 (opens new window)
  • 考勤小程序 (opens new window)
  • 时光恋人 (opens new window)
  • 在线简历生成 (opens new window)
留言板
Github (opens new window)
author-avatar

粥里有勺糖

285

文章

40

标签

关于我
备战春秋
  • 心得总结
  • 校招考点汇总
  • 面经汇总
  • 复习自查
技术笔记
  • 技术教程
  • 模板工程
  • 源码学习
  • 技术概念
  • 个人作品
  • 学习笔记
计算机基础
  • 算法与数据结构
  • 操作系统
  • 计算机网络
  • 设计模式
  • 剑指offer
大前端
  • javascript
  • vue
  • html
  • css
  • 🌏浏览器专题
  • Web性能优化
  • regexp
  • node
面试
  • 问解
  • javascript
  • css
  • 手撕代码
  • 性能优化
  • 综合问题
  • 面经汇总
  • 小程序
手撕代码
  • 数据结构与算法
  • javascript
  • css
个人站点
  • GitHub (opens new window)
  • 博客园 (opens new window)
  • 掘金 (opens new window)
线上作品
  • 轻取(文件收集) (opens new window)
  • 个人图床 (opens new window)
  • 考勤小程序 (opens new window)
  • 时光恋人 (opens new window)
  • 在线简历生成 (opens new window)
留言板
Github (opens new window)
  • devlearn

    • 开发教程
    • 实践:利用ArrayBuffer实现预览指定目录下的所有文件的内容
    • 在linux-deepin上使用deepin-wine5完美运行腾讯会议/QQ/微信等此类应用
    • eslint插件开发教程
    • ServerLess之云函数实践-天气API
    • 移动端阻止弹窗下层页面被滑动
    • 小技巧:for of中获取index
    • Git常用的一些基本操作
    • 向页面注入js实现为图片和文字元素添加透明蒙层
    • 实践:使用jsencrypt配合axios实现非对称加密传输的数据
    • 封装dotenv库实现类似Vite加载环境变量的行为
    • 30行代码实现合并指定目录下的所有文件的内容
    • 马上中秋了,把鼠标指针变为小玉兔
    • Node中require与fs.readFile读取JSON文件的对比
    • 使用免费的七牛云OSS(10G)搭建个人的在线图床
    • 分享封装的一些七牛云OSS操作方法
    • 本地配置SSH免密远程登录服务器
    • 工具方法汇总
    • 腾讯云Serverless实践-Node.js服务部署
    • 腾讯云Serverless实践-静态网站托管
    • 为什么'\x1B'.length === 1?\x与\u知识延伸
    • Vite插件开发纪实:vite-plugin-monitor(上)
    • Vite插件开发纪实:vite-plugin-monitor(中)
    • Vite插件开发纪实:vite-plugin-monitor(下)
    • 解决Vite-React项目中js使用jsx语法报错的问题
    • webpack 项目接入Vite的通用方案介绍
    • webpack 项目接入Vite的通用方案介绍-草稿
    • 优雅的处理挂载window上的函数可能不存在的情况
    • Mac上抓包秒通关羊了个羊

eslint插件开发教程

vuePress-theme-reco 粥里有勺糖    2018 - 2023

eslint插件开发教程

粥里有勺糖 2020-05-20 技术笔记技术教程

# eslint插件开发教程

开发eslint插件目的:根据项目需要,自定义满足项目特殊需要的校验规则

参考eslint (opens new window)官方文档展开阐述

  • 插件开发 (opens new window)
  • 自定义规则 (opens new window)
  • 单元测试 (opens new window)

下面开始通过一个示例demo来介绍插件整个开发流程

代码中出现的方法及变量的详细解释与相关文档,会在文末给大家列举出来,大家可以先把代码拷贝到自己的demo中然后结合本文第3部分的变量|方法解释去理解代码

开发一个校验注释中是否包含指定关键词的插件(eslint-plugin-comments-key)

# 1. 环境准备

# 目录结构

.
├── README.md                   插件介绍文档
├── index.js                    对外暴露插件
├── lib                         
│   └── rules                   自定义规则
│       └── comments-key.js     
├── package.json
└── tests                       测试自定义规则
    └── lib
        └── rules
            └── comments-key.js
1
2
3
4
5
6
7
8
9
10
11

# 安装依赖

  • eslint
  • mocha
npm i eslint mocha -D
1

# 2. 开始编码

# 编写自定义规则

不包含自定义参数校验规则

/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}` // 有问题发出的消息
                        })
                    }
                })
            }
        };
    }
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

# 编写测试用例

/tests/lib/rules/comments-key.js

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') }]
        }
    ]
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

# 修改package.json

加入

"scripts": {
  "test": "mocha tests/lib/rules"
}
1
2
3

运行脚本查看测试结果

npm run test
1

上面的示例中限定的关键词是在代码中写死了的

通常的场景中如:

rules:{
    "quotes": ["error", "double"], // 只允许双引号
    "no-warning-comments": [ // 不允许注释开头出现 todo|fixme等内容
        1,
        {
          "terms": [
            "todo",
            "fixme"
          ],
          "location": "start"
        }
      ],
}
1
2
3
4
5
6
7
8
9
10
11
12
13

大多数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
    }
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

# 完善我们的单元测试

// ...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']
            }]
        }
    ]
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

# 3.文中一些变量|方法的解释及其文档

  • meta (opens new window) (object) 包含规则的元数据
    • schema (opens new window) 指定该选项 这样的 ESLint 可以避免无效的规则配置
      • 遵循 json schema (opens new window) 规范
  • create (opens new window) (function) 返回一个对象,其中包含了 ESLint 在遍历 JavaScript 代码的抽象语法树 AST (ESTree (opens new window) 定义的 AST) 时,用来访问节点的方法
    • context (opens new window) 包含与规则上下文相关的信息
      • options (opens new window) 检索一个规则的可选项,它是个数组,包含该规则的所有配置的可选项
      • getSourceCode() 返回一个SourceCode (opens new window)对象,你可以使用该对象处理传递给 ESLint 的源代码
        • getAllComments() (opens new window) 获取所有注释节点
          • 每个注释节点的属性
            • loc 注释在文档中的位置
            • value 注释中的内容
            • type 注释的类型 Block|Line
      • report() (opens new window) 它用来发布警告或错误(取决于你所使用的配置)。该方法只接收一个参数,是个对象
        • message 有问题的消息提示
        • node (可选)与问题有关节点
        • loc (可选)用来指定问题位置的一个对象。如果同时指定的了 loc 和 node,那么位置将从loc获取而非node
        • data (可选) message的占位符
        • fix (可选) 一个用来解决问题的修复函数
  • RuleTester (opens new window) 单元测试示例介绍

tips:AST在开发插件时不必深入研究,不同地方AST的实现和结构都有所差异

# 4.导出

至此我们的插件算开发完成了,接下来编写对eslint暴露这个模块的代码

index.js

'use strict';
module.exports = {
  rules: {
    'diy': require('./lib/rules/comments-key') 
  },
  rulesConfig: {
    'diy': 1
  }
};
1
2
3
4
5
6
7
8
9

# 5.发布npm

要在其它项目中使用的eslint-plugin插件的话,可以把整个插件的根目录拷贝到目标项目的node_modules中或者发布到npm中去,其它项目直接通过npm install 安装这个依赖

下面介绍发布到npm的步骤

  1. 注册npm账号(有的话直接跳过这步骤)

直接点击官网 (opens new window)注册

  1. 设置登陆的账号 登录之前修改registry为原来的,因为国内一般用的镜像源例如淘宝源:https://registry.npm.taobao.org
npm config set registry https://registry.npmjs.org/
1
npm login
1

按提示依次输入账号,密码,邮箱

登录完成之后,查看当前npm用户,不报错说明登录成功

npm whoami
1
  1. 编写README.md方便指引他人使用
  2. 修改packgae.json
{
  "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"
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  1. 运行npm publish发布npm包

至此发布整个流程完毕

# 6.项目中引入

# Installation

You'll first need to install ESLint (opens new window):

$ npm i eslint --save-dev
1

Next, install eslint-plugin-comments-key:

$ npm install eslint-plugin-comments-key --save-dev
1

Note: If you installed ESLint globally (using the -g flag) then you must also install eslint-plugin-comments-key globally.

# Usage

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"]
      }]
    }
}
1
2
3
4
5
6
7
8
9
10

tips: 如果编辑器中安装了Eslint插件,在编码的时候就会给予警告⚠️

# 最后

# eslint-plugin-comments-key相关地址

  • npm (opens new window)
  • github (opens new window)

因笔者水平有限,内容上如有阐述不明白之处,还请斧正

Edit this page (opens new window)
Last Updated: 2022/5/15 12:46:34