码迷,mamicode.com
首页 > 其他好文 > 详细

[AST Eslint] No Console allowed

时间:2020-01-23 21:18:20      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:methods   nis   const   rop   ide   let   inf   ret   style   

// 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}],
  }
}

[AST Eslint] No Console allowed

标签:methods   nis   const   rop   ide   let   inf   ret   style   

原文地址:https://www.cnblogs.com/Answer1215/p/12231325.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!