[AST ESlint] Prevent Console log, edge case, variable reference
2023-09-14 09:00:48 时间
For eslint prevent console plugin, we also want to prevent user do so:
var csl = console
csl.log()
Code:
const disallowedMethods = ['log', 'info', 'warn', 'error', 'dir'] module.exports = { meta: { docs: { description: 'Disallow use of console', category: 'Best Practices', recommended: true, }, schema: [ { type: 'object', properties: { allowedMethods: { type: 'array', items: { enum: ['log', 'info', 'warn', 'error', 'dir'], }, minItems: 1, uniqueItems: true, }, }, }, ], }, create(context) { const config = context.options[0] || {} const allowedMethods = config.allowedMethods || [] const consoleUsage = [] return { Identifier(node) { if (node.name !== 'console') { return } consoleUsage.push(node) }, /** * By the time * var csl = console csl.log() compiler hasn't exected csl.log We have to do in 'Program:exit' */ 'Program:exit'() { consoleUsage.forEach(identifier => { if (isDisallowedFunctionCall(identifier)) { context.report({ node: identifier.parent.property, message: 'Using console is not allowed', }) } else { const variableDeclaratorParent = findParent( identifier, parent => parent.type === 'VariableDeclarator', ) if (variableDeclaratorParent) { const references = context .getDeclaredVariables(variableDeclaratorParent)[0] .references.slice(1) references.forEach(reference => { if ( !looksLike(reference, { identifier: { parent: { property: isDisallowedFunctionCall, }, }, }) ) { return } context.report({ node: reference.identifier.parent.property, message: 'Using console is not allowed', }) }) } } }) }, } function isDisallowedFunctionCall(identifier) { return looksLike(identifier, { parent: { type: 'MemberExpression', parent: {type: 'CallExpression'}, property: { name: val => !allowedMethods.includes(val) && disallowedMethods.includes(val), }, }, }) } }, } function findParent(node, test) { if (test(node)) { return node } else if (node.parent) { return findParent(node.parent, test) } return null } 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) }
Test:
const {RuleTester} = require('eslint') const rule = require('./no-console-5') const ruleTester = new RuleTester() ruleTester.run('no-console', rule, { valid: [ 'info()', 'console', 'console.log', 'console.baz()', {code: 'console.warn()', options: [{allowedMethods: ['warn']}]}, ], invalid: [ invalid('console.log()'), invalid('console.info()'), invalid('console.warn()'), invalid( ` var csl = console csl.log() `, ), ], }) function invalid(code) { return { code, errors: [{message: 'Using console is not allowed'}], } }
相关文章
- 【无标题】ubuntu 18.04安装edge浏览器「建议收藏」
- 关于微软 Edge 浏览器无法访问笔者 SAP UI5 教程示例代码的问题
- ORA-16029: cannot change LOG_ARCHIVE_MIN_SUCCEED_DEST, no archive log destinations ORACLE 报错 故障修复 远程处理
- MySQL启用Log记录技术深度剖析(mysql开启log)
- Microsoft Edge Android版不再落后:微软计划迁移到通用代码库
- 摩托罗拉Edge 20系列发布会定档 1亿像素主摄到来
- 微软 Edge 浏览器「睡眠标签」会显示已节省多少资源
- [图]摩托罗拉Edge 20系列三款机型高清谍照曝光
- 适用于Edge的Outlook扩展程序发布:无需打开新选项卡即可收发电邮
- :考勤机事件记录挖掘:MSSQL Log分析(考勤机mssql中log)
- 探索Oracle中Log位置的奥秘(oracle中log位置)
- 利用Oracle Log表优化数据库性能(oracle log表)
- Edge Canary新版选项卡右键菜单新增“搜索标签页”功能