zoukankan      html  css  js  c++  java
  • [AST ESlint] Prevent Console log, edge case, variable reference

    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'}],
      }
    }
  • 相关阅读:
    full gc
    C#调用C++编写的DLL
    用C#调用C++DLL提示找不到DLL解决方法【转】
    VS2015 编写C++的DLL,并防止DLL导出的函数名出现乱码(以串口通信为例,实现串口通信)
    VS2015 C++ 获取 Edit Control 控件的文本内容,以及把获取到的CString类型的内容转换为 int 型
    VS2015 建立一个C++的MFC简易窗体程序项目
    C# 实现串口发送数据(不用串口控件版)
    STM32 HAL库的定时器中断回调函数跟串口中断回调函数
    把 Python 脚本打包成可以直接双击运行的 .exe 文件 【转】
    Python 实现把 .cvs 文件保存为 Excel 文件
  • 原文地址:https://www.cnblogs.com/Answer1215/p/12323107.html
Copyright © 2011-2022 走看看