[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‘}], } }
相关推荐
hualala 2020-06-13
tozeroblog 2020-06-09
tozeroblog 2020-06-08
hualala 2020-06-05
hualala 2020-05-29
www你所不知道的 2020-03-06
out有点man 2020-05-07
学知不足业精于勤 2020-04-30
hualala 2020-04-30
out有点man 2020-04-23
coderMozart 2020-04-20
coderMozart 2020-04-17
空谷足音 2020-03-06
tozeroblog 2020-03-06
tozeroblog 2020-02-21
tozeroblog 2020-02-17
hualala 2020-01-23