[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:
// BAD
if (a) {
console.log("a");
} else {
if (b) {
console.log("b");
}
} // GOOD
if (a) {
console.log("a");
} else if (b) {
console.log("b");
}
} //////////////////////// // BAD
if (a) {
if (b) {
console.log("b");
}
} // GOOD
if (a) {
console.log("a");
if (b) {
console.log("b");
}
} // GOOD
if (a && b) {
console.log("b");
}
Notice that if statement can write with block statement or without block statem, such as:
if(a)
if(b)
console.log('b')
Rule:
We can export a default 'create' function.
export default function(context) {
return {
// rules
}
} // the same as module.exports = {
create: (context) => {
return {
// rules
}
}
}
export default function(context) {
return {
IfStatement(node) {
var ancestors = context.getAncestors(),
parent = ancestors.pop(),
grandparent = ancestors.pop(); if (typeof grandparent === "undefined") {
return;
} if (
(parent.type === "BlockStatement" && // if has if() { if() {}}, nested if's parent is a BlockStatement
parent.body.length === 1 && // if() { console.log(); if() {} }, we consider this is fine
grandparent.type === "IfStatement") || // grandparent should be a if statement
parent.consequent === node // sometime we write if() something, don't have blockstatement, then we check consequent should be the node iteself
) {
context.report(node, "nested if statement");
}
}
};
}
[Javascript AST] 2. Introduction: Write a simple ESLint rule的更多相关文章
- [Javascript AST] 0. Introduction: Write a simple BabelJS plugin
To write a simple Babel plugin, we can use http://astexplorer.net/ to help us. The plugin we want to ...
- [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 ...
- 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". ...
- [Javascript AST] 4. Continue: Report ESLint error
const disallowedMethods = ["log", "info", "warn", "error", & ...
- [Javascript AST] 3. Continue: Write ESLint rule
The rule we want to write is show warning if user using console method: // valid foo.console() conso ...
- JavaScript Patterns 1 Introduction
1.1 Pattern "theme of recurring events or objects… it can be a template or model which can be u ...
- 【AST篇】教你如何编写 Eslint 插件
前言 虽然现在已经有很多实用的 ESLint 插件了,但随着项目不断迭代发展,你可能会遇到已有 ESLint 插件不能满足现在团队开发的情况.这时候,你需要自己来创建一个 ESLint 插件. 本文我 ...
- JavaScript资源大全中文版(Awesome最新版)
Awesome系列的JavaScript资源整理.awesome-javascript是sorrycc发起维护的 JS 资源列表,内容包括:包管理器.加载器.测试框架.运行器.QA.MVC框架和库.模 ...
- Awesome Javascript(中文翻译版)
[导读]:GitHub 上有一个 Awesome – XXX 系列的资源整理.awesome-javascript 是 sorrycc 发起维护的 JS 资源列表,内容包括:包管理器.加载器.测试框架 ...
随机推荐
- 深入理解Android(5)——从MediaScanner分析Android中的JNI
前面几篇介绍了Android中的JNI和基本用法,这一篇我们通过分析Android源代码中的JNI实例,来对JNI部分做一个总结. 一.通向两个不同世界的桥梁 在前面我们说过,JNI就像一个桥梁,将J ...
- 简单的字符串压缩--C代码
#include <stdio.h> #include <string.h> bool compress(char *str) { char *p=str,c; ; if(!s ...
- 异常:error 0152: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'
error 0152: No Entity Framework provider found for the ADO.NET provider with invariant name 'System. ...
- How Blink works
How Blink works Author: haraken@ Last update: 2018 Aug 14 Status: PUBLIC Working on Blink is not eas ...
- 51nod 正整数分组
将一堆正整数分为2组,要求2组的和相差最小. 显然我们可以把所有可能组合成的数求出来. 然后从总和的中间开始往大找,找到了就是其中一个的分组,就可以求出答案了. #include<cstdio& ...
- .js控制一次加载一张图片,加载完后再加载下一张
js怎么控制一次加载一张图片,加载完后再加载下一张 (1)方法1 (1)方法2
- Gonet2 游戏server框架解析之Agent(3)
客户端消息在Agent中的预处理流程. Agent定义好的三种请求: //api.go var RCode = map[int16]string{ 0: "heart_beat_req&qu ...
- 利用HTTP代理录制Jmeter脚本
1 測试计划中加入一个线程组1 2在"工作台"-非測试元件-加入"HTTP代理server" port: 代理server的port,默认8080,可自行改动, ...
- jquery10 闭包示例
o = { a:1, o:{ b:2, f : function(){ alert(o.a); alert(o.b);//undefined } } } o.o.f(); o = { a:7, o : ...
- vim-normal多行操作命令的使用
命令行命令-<:normal>这个命令可以重复上一个操作.他其实就跟.命令的效果查不到.不同的是,他可以把.的效果,作用于你用可视模式下的多行.例如,如果你想在下面的文字里在每一行加一个; ...