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. Controller接口控制器3

    11.AbstractWizardFormController 向导控制器类提供了多步骤(向导)表单的支持(如完善个人资料时分步骤填写基本信息.工作信息.学校信息等) 假设现在做一个完善个人信息的功能 ...

  2. phpMyAdmin出现找不到mysql扩展和Cannot log in to the MySQL server问题

    环境:Centos6.5,Apache2.4, PHP5.5, MySql5.6. phpMyAdmin版本:https://files.phpmyadmin.net/phpMyAdmin/4.4.1 ...

  3. beego orm commonDAL通用方法汇总

    orm 通用方法——QueryModelById 主键查询 orm 通用方法——GetOneModel 条件查询一个对象 orm 通用方法——QueryModelCount条件查询记录数 orm 通用 ...

  4. 简单的字符串压缩--C代码

    #include <stdio.h> #include <string.h> bool compress(char *str) { char *p=str,c; ; if(!s ...

  5. python学习二,字符串常用操作

    字符串可以说是在日常开发中应用最广泛的了,现在来总结下有关python中有关字符串一些常用操作 首先我们声明一个字符串变量 str = "hello world" 下面我们来依次介 ...

  6. mysql的my.cnf文件详解

    一.缘由 最近要接手数据库的维护工作,公司首选MySQL.对于MySQL的理解,我认为很多性能优化工作.主从主主复制都是在调整参数,来适应不同时期不同数量级的数据. 故,理解透彻my.cnf里的参数是 ...

  7. JS数组去重的6种算法实现

    1.遍历数组法 最简单的去重方法,实现思路:新建一新数组,遍历传入数组,值不在新数组就加入该新数组中:注意点:判断值是否在数组的方法"indexOf"是ECMAScript5 方法 ...

  8. snmpd修改端口

    http://blog.csdn.net/cau99/article/details/5077239 http://blog.csdn.net/gua___gua/article/details/48 ...

  9. struts2的字符串参数

    一定要熟记一个东西,一层引号的是变量,两层引号的是字符串 如"蓝"/'蓝'是变量,而" '蓝' "/ ' "蓝" '是字符串 打代码时要警惕 ...

  10. 发送邮件被退回,提示: Helo command rejected: Invalid name 错误

    我自己配置的 postfix + dovecot server, 配置了outlook 后, 相同的账号. 在有的电脑上能收发成功, 在有的电脑上发送邮件就出现退信.提示 Helo command r ...