The rule we want to write is show warning if user using console method:

// valid
foo.console()
console()
info()
console.baz() // invalid
console.log()
console.info()
console.warn()

Rule:

const disallowedMethods = ["log", "info", "warn", "error", "dir"];

module.exports = {
meta: {
docs: {
description: "Disallow use of console",
category: "Best Practices",
recommended: true
}
},
create(context) {
return {
Identifier(node) {
const isConsoleCall = looksLike(node, {
name: "console",
parent: {
type: "MemberExpression",
property: {
name: val => disallowedMethods.includes(val)
}
}
});
// find the identifier with name 'console'
if (!isConsoleCall) {
return;
} context.report({
node,
message: "Using console is not allowed"
});
}
};
}
}; 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);
}

'looksLike' & isPrimitive is pretty handy, you can save as until lib.

[Javascript AST] 3. Continue: Write ESLint rule的更多相关文章

  1. [Javascript AST] 4. Continue: Report ESLint error

    const disallowedMethods = ["log", "info", "warn", "error", & ...

  2. [Javascript AST] 1. Continue: Write a simple Babel plugin

    We want to write a Babel Plugin, which move 'const versionRegex = /(/d+)\.(/d+)\.(/d+)/gi' out of fu ...

  3. An internal error occurred during: "Requesting JavaScript AST from selection". GC overhead limit exc

    1.错误描述 An internal error occurred during: "Requesting JavaScript AST from selection".     ...

  4. [Javascript AST] 2. Introduction: Write a simple ESLint rule

    What we want to do is checking if user write nested if statements which actually can combine to one: ...

  5. JavaScript break和continue 跳出循环

    在JavaScript中,使用 break 和 continue 语句跳出循环: break语句的作用是立即跳出循环,即不再执行后面的所有循环: continue语句的作用是停止正在执行的循环,直接进 ...

  6. JavaScript Break 和 Continue 语句

    1.break:终止本层循坏,继续执行本次循坏后面的语句: 当循坏有多层时,break只会跳过一层循坏 2.continue:跳过本次循坏,继续执行下次循坏 对于for循环,continue执行后,继 ...

  7. 对比JavaScript中的Continue和Break

    译者按: 最好是不用,不过基础知识要掌握. 原文: JavaScript: Continue vs Break - Learn the difference between the continue ...

  8. 松软科技Web课堂:JavaScript Break 和 Continue

    break 语句“跳出”循环. continue 语句“跳过”循环中的一个迭代. Break 语句 在本教程稍早的章节中,您已见到了 break 语句.它被用于“跳出” switch 语句. brea ...

  9. javascript break 和continue

    break语句还可以跳出循环,也就是结束循环语句的执行. continue语句的作用为结束本次循环,接着进行下一次是否执行循环的判断. continue与break的区别是:break是彻底结束循环, ...

随机推荐

  1. 123.static静态函数和函数模板

    #include <iostream> using namespace std; //static成员,每个类型都会实例化,创建一个变量,类型一致则共享,否则不共享 template &l ...

  2. Zuul 2 : The Netflix Journey to Asynchronous, Non-Blocking Systems--转

    原文地址:http://techblog.netflix.com/2016/09/zuul-2-netflix-journey-to-asynchronous.html We recently mad ...

  3. Windows安全攻略:教你完全修复系统漏洞

    Windows安全攻略:教你完全修复系统漏洞 首发:http://safe.it168.com/a2012/0709/1369/000001369740.shtml 目前互联网上的病毒集团越来越猖狂, ...

  4. Codefroces A. Saitama Destroys Hotel

    A. Saitama Destroys Hotel time limit per test 1 second memory limit per test 256 megabytes input sta ...

  5. MyEclipse的代码自动提示功能

     一般默认情况下,Eclipse ,MyEclipse的代码提示功能是比Microsoft Visual Studio的差很多的,主要是Eclipse ,MyEclipse本身有很多选项是默认关闭的, ...

  6. ajax的几个面试题

    一.什么是AJAX(请谈一下你对Ajax的认识)AJAX是“Asynchronous JavaScript and XML”的缩写.他是指一种创建交互式网页应用的网页开发技术.Ajax包含下列技术:基 ...

  7. 紫书 习题 10-17 UVa 11105 (筛法)

    类似于素数筛的思想去做,不然暴力会超时而且还要判重 #include<cstdio> #include<cstring> #include<vector> #def ...

  8. 如何形象的解释 webhook 这个词

    就是一个 callback,只不过 callback 的操作是发送指定的 HTTP request 给一个指定的地址. callback 就是由甲传给乙,乙处理完之后通知甲传过来的方法或者请求甲方的 ...

  9. Safe and efficient allocation of memory

    Aspects of the present invention are directed at centrally managing the allocation of memory to exec ...

  10. 洛谷—— P1629 邮递员送信

    https://www.luogu.org/problem/show?pid=1629 题目描述 有一个邮递员要送东西,邮局在节点1.他总共要送N-1样东西,其目的地分别是2~N.由于这个城市的交通比 ...