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的更多相关文章

  1. [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 ...

  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] 4. Continue: Report ESLint error

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

  5. [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 ...

  6. JavaScript Patterns 1 Introduction

    1.1 Pattern "theme of recurring events or objects… it can be a template or model which can be u ...

  7. 【AST篇】教你如何编写 Eslint 插件

    前言 虽然现在已经有很多实用的 ESLint 插件了,但随着项目不断迭代发展,你可能会遇到已有 ESLint 插件不能满足现在团队开发的情况.这时候,你需要自己来创建一个 ESLint 插件. 本文我 ...

  8. JavaScript资源大全中文版(Awesome最新版)

    Awesome系列的JavaScript资源整理.awesome-javascript是sorrycc发起维护的 JS 资源列表,内容包括:包管理器.加载器.测试框架.运行器.QA.MVC框架和库.模 ...

  9. Awesome Javascript(中文翻译版)

    [导读]:GitHub 上有一个 Awesome – XXX 系列的资源整理.awesome-javascript 是 sorrycc 发起维护的 JS 资源列表,内容包括:包管理器.加载器.测试框架 ...

随机推荐

  1. IComparable接口实现自定义类型的排序

    IComparable接口实现自定义类型的排序   CompareTo(Object) 方法的实现必须返回有三个值之一 如下表中所示. 返回值 参数比较 大于0 x>y 等于0 x=y 小于0 ...

  2. 水 hdu5208 2015-04-20 21:03 36人阅读 评论(0) 收藏

    题意: 选择数列中两个数,使得最大公约数最大 分析: 类似筛选法,因为数值不大,可以用b[i]计算i是多少个数的因子.最后取最大的i即可. #include <bits/stdc++.h> ...

  3. 解决The requested resource is not available的办法

    1.问题描述: eclipse中使用tomcat来运行HelloWorld时出现The requested resource is not available. 在报错中有一行Setting prop ...

  4. JS的解析与执行过程—全局预处理阶段之命名冲突的处理策略

    有如下代码: <body> <script> alert(f); function f() { console.log("fff"); } var f = ...

  5. 如何更新 CentOS 镜像源

    话不多说, 直接上教程. 首先备份/etc/yum.repos.d/CentOS-Base.repo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.rep ...

  6. [Python] for.. not in.. Remove Deduplication

    Write a function, remove_duplicates that takes a list as its argument and returns a new list contain ...

  7. C#实现窗口拖动时各个控件同比自己主动放缩大小

    实现方式主要是利用panel控件为主题.对于每一个控件的大小位置和字体这几个属性进行记录. 然后依据窗口改变的大小同一时候放缩. 简要过程例如以下: 1 创建C#窗口程序项目. 2  Panel放置到 ...

  8. js call 和 apply

    前言 call 和 apply 都是为了改变某个函数运行时的 context 即上下文而存在的,换句话说,就是为了改变函数体内部 this 的指向. call 和 apply二者的作用完全一样,只是接 ...

  9. 懒加载js实现和优化

    1.懒加载的作用和原理 在我们展示多图片的场景下,类似淘宝或者百度图片,由于图片的数目过多,全部从服务器请求会给用户糟糕的用户体验,为了提升用户体验,我们这里使用懒加载,随着下拉逐步加载. 每个图片的 ...

  10. 39.C语言操作数据库

    一.准备工作: sqlite3工具集:链接:https://pan.baidu.com/s/1mjufXZa 密码:2ui7 安装步骤: 打开如下文件夹,找到sqlite3.dll,并放入系统目录 2 ...