• [AST Eslint] No Console allowed


    // eslint exercise 1 (no-console)
    // When you're finished with this exercise, run
    //   "npm start exercise.eslint.2"
    //   to move on to the next exercise
    
    module.exports = {
      create(context) {
        return {
          CallExpression(node) {
            if (hasIdentifierObjectAsConsole(node) && hasPropertyWithInvalidMethods(node)) {
              context.report({
                node,
                message: 'Using console is not allowed',
              })
            }
          },
        }
      },
    }
    
    function hasPropertyWithInvalidMethods (node) {
        return (
          node.callee.property && 
          ['warn', 'log', 'info'].includes(node.callee.property.name)
      )
    }
    
    function hasIdentifierObjectAsConsole(node) {
      return node.callee.object && 
        node.callee.object.type === "Identifier" && 
        node.callee.object.name === "console";
    }
    // eslint exercise 1 (no-console)
    // When you're finished with this exercise, run
    //   "npm start exercise.eslint.2"
    //   to move on to the next exercise
    
    const {RuleTester} = require('eslint')
    const rule = require('./no-console')
    
    const ruleTester = new RuleTester()
    ruleTester.run('no-console', rule, {
      valid: ['info()', 'console', 'console.log', 'console.baz()'],
      invalid: [
        invalid('console.log()'),
        invalid('console.info()'),
        invalid('console.warn()'),
      ],
    })
    
    function invalid(code) {
      return {
        code,
        errors: [{message: 'Using console is not allowed'}],
      }
    }
  • 相关阅读:
    集成学习(一):概述
    机器学习:处理非平衡数据集的办法
    支撑向量机 SVM(一)
    集成学习(五):xgboost 学习总结
    数组的实现(重载[]、=、==、!=运算符重载)
    运算符重载总结
    运算符重载进阶
    运算符重载入门demo
    类模板的简单使用
    static成员变量和static成员函数例程
  • 原文地址:https://www.cnblogs.com/Answer1215/p/12231325.html
Copyright © 2020-2023  润新知