[Javascript AST] 3. Continue: Write ESLint rule
JavaScript Write rule continue eslint AST
2023-09-14 09:00:51 时间
The rule we want to write is show warning if user using console method:
// valid foo.console() console() info() console.baz() // invalid console.log() console.info() console.warn()
Rule:
const disallowedMethods = ["log", "info", "warn", "error", "dir"]; module.exports = { meta: { docs: { description: "Disallow use of console", category: "Best Practices", recommended: true } }, create(context) { return { Identifier(node) { const isConsoleCall = looksLike(node, { name: "console", parent: { type: "MemberExpression", property: { name: val => disallowedMethods.includes(val) } } }); // find the identifier with name 'console' if (!isConsoleCall) { return; } context.report({ node, message: "Using console is not allowed" }); } }; } }; function looksLike(a, b) { return ( a && b && Object.keys(b).every(bKey => { const bVal = b[bKey]; const aVal = a[bKey]; if (typeof bVal === "function") { return bVal(aVal); } return isPrimitive(bVal) ? bVal === aVal : looksLike(aVal, bVal); }) ); } function isPrimitive(val) { return val == null || /^[sbn]/.test(typeof val); }
'looksLike' & isPrimitive is pretty handy, you can save as until lib.
相关文章
- Javascript之旅——第二站:对象和数组
- Javascript创建类的七种方法
- Win10系列:JavaScript 数据绑定
- javascript 实现 对XML文件 2级/3级联动操作
- [Algorithm] Write a Depth First Search Algorithm for Graphs in JavaScript
- [Javascript AST] 2. Introduction: Write a simple ESLint rule
- [Javascript AST] 1. Continue: Write a simple Babel plugin
- [WASM] Write to WebAssembly Memory from JavaScript
- [Javascript] How to write a Javascript libarary
- [Javascript] Advanced Flattening
- javascript document.write
- javascript: 用图片加载演示promise的应用(chrome 107.0.5304.110)
- JavaScript 消息框,警告框,确认框,提示框
- 【JavaScript】underscore
- [Javascript] JSON.parse, structuredClone, loadsh.cloneDeep
- [Algorithm] Write your own Math.pow function in Javascript, using Recursive approach
- [Algorithm] Write a Depth First Search Algorithm for Graphs in JavaScript
- [Javascript AST] 1. Continue: Write a simple Babel plugin
- [Javascript AST] 0. Introduction: Write a simple BabelJS plugin
- [WASM] Write to WebAssembly Memory from JavaScript
- [Javascript] Javascript 'in' opreator
- [Javascript] Intro to Recursion - Refactoring to a Pure Function
- [Javascript]Clouse Cove, 2 ,Modifying Bound Values After Closure
- postman测试重定向接口,We're sorry but system-cn doesn't work properly without JavaScript enabled. Please enable it to continue
- 几种常用的排序算法之 JavaScript 实现
- 从零开始学_JavaScript_系列(24)——查看对象属性,合并数组
- javascript基础修炼——前端路由的基本原理
- javascript基础修炼(1)——一道十面埋伏的原型链面试题