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. 3.菜鸟教你一步一步开发 web service 之 axis 服务端创建

    转自:https://blog.csdn.net/shfqbluestone/article/details/37610601 第一步,新建一个工程,如图: 选 Java 写一个工程名,选择好工程路径 ...

  2. 公司采购 流程flowable例子

    Name: Flowable BPMN 2.0 designer Location: http://flowable.org/designer/update/ 业务描述:1. 公司采购,因为办公用品价 ...

  3. 15:Challenge 11(主席树裸题)

    总时间限制:  10000ms 单个测试点时间限制:  1000ms 内存限制:  262144kB 描述 给一个长为N的数列,有M次操作,每次操作是以下两种之一: (1)修改数列中的一个数 (2)求 ...

  4. 用iptables抗御SYN Flood攻击

    1         引 言 网络安全是各种网络应用面临的一个首要问题.从网络普及的那天开始,网络犯罪就没有停止过,相反有愈演愈烈之势.研究发现,现今的网络攻击以分布式拒绝服务攻击(DDOS)为主 .其 ...

  5. vue 实现文本域还剩多少字符

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. mysql 5.6 安装教程

    首先是下载 mysql-installer-community-5.6.14.0.msi ,大家可以到 mysql 官方网去下载,也可以到笔者所提供的地址去下载,下载方法在这里就不多说了,我想大家都明 ...

  7. 记一次 CORS 跨域请求出现 OPTIONS 请求的问题及解决方法

    今天前后端在联调接口的时候,发生了跨域请求资源获取不到的问题. 首先说明下跨域问题的由来.引自HTTP 访问控制 的一段话: 当 Web 资源请求由其它域名或端口提供的资源时,会发起跨域 HTTP 请 ...

  8. https soap链接示例

    1.先安装soap扩展sudo yum install php-soap 2.安装openssL 3.function  issure($sn){//通过soap链接接口  进行确认是否是正确的sn码 ...

  9. idea中实现类快速重写service方法 快捷键

    1.在实现类中 CTRL+O 快捷键,会弹出所有方法 2.选择service中的方法,会自动重写

  10. 【转】python的zipfile压缩、解压缩

    网上搜索了很多关于python的zipfile压缩.解压缩.觉得讲述比较详细,例子也很明了.由于比较懒,就直接复制了. 以下内容大部分转于 http://blog.csdn.net/jgood/art ...