欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 养生 > 自定义 eslint 规则

自定义 eslint 规则

2025/6/18 13:15:10 来源:https://blog.csdn.net/qq_41798568/article/details/148653108  浏览:    关键词:自定义 eslint 规则

自定义 eslint 规则

  1. 起名规范

eslint-plugin-guojieEslint
eslint-plugin-xxxx

  1. npm init
    初始化一个项目
//  package.json
{"name": "eslint-plugin-guojieeslint","version": "1.0.0","description": "","main": "index.js","author": "","license": "ISC"
}
  1. 主逻辑
//  amount-check.js
module.exports = {meta: {type: "problem",docs: {description: "禁止直接对金额变量进行加法运算,防止精度丢失",category: "Possible Errors",recommended: false,},messages: {noAmountPlus:"金额运算不能直接用加法,可能会有精度丢失,请使用专用金额运算方法。",},},create(context) {return {BinaryExpression(node) {console.log(node.left.name, node.right.name);if (node.operator === "+" &&node.left.type === "Identifier" &&node.right.type === "Identifier" &&/Amount$/.test(node.left.name) &&/Amount$/.test(node.right.name)) {context.report({node,messageId: "noAmountPlus",});}},};},
};
//  index.js
const amountCheck = require("./amount-check.js");
module.exports = {rules: {"amount-check": amountCheck,},
};
  1. 这样一个简单的 eslint 规则就完成了,如何使用
    环境 "eslint": "^8.57.1"
    命令 "test": "npx lint index.js"

     4.1 先在 我的自定义elsint 规则中 执行 `npm link `4.2 再在我们项目根目录 执行 `npm link eslint-plugin-guojieeslint`配置项:在项目中`.eslintrc.js`
    
    module.exports = {plugins: ["guojieeslint"],parserOptions: {ecmaVersion: 2018,sourceType: "module",},rules: {"guojieeslint/amount-check": "error",},
    };
    

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词