[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 资源列表,内容包括:包管理器.加载器.测试框架 ...
随机推荐
- apicloud,aliyunlive,测试成功
1.推流 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...
- lightoj--1008--Fibsieve`s Fantabulous Birthday(水题)
Fibsieve`s Fantabulous Birthday Time Limit: 500MS Memory Limit: 32768KB 64bit IO Format: %lld &a ...
- 判断DataGridView滚动条是否滚动到当前已加载的数据行底部
private void dataGridView1_Scroll(object sender, ScrollEventArgs e) { if (e.ScrollOrientation == S ...
- centos中mysql 安装以及配置,建库
1.检测系统内部有没有安装其他的mysql数据库 rpm -qa | grep mysql 然后如果有的话删除这些mysql yum remove 查出来的所有名字 2.彻底删除系统中mysql的目录 ...
- 获取cpu使用率
http://blog.csdn.net/u010515761/article/details/43225621 http://stackoverflow.com/questions/74674/ho ...
- effective stl读书笔记 & stl里面提供的算法 & emplace & ostream_iterator
加锁和解锁,也可以在构造函数和析构函数里面,自动调用. 相等和等价的关系:等价是用在排序的时候,跟less函数有关. vector,deque,string 要用erase-remove组合:而关联容 ...
- 【Tika基础教程之中的一个】Tika基础教程
一.高速入门 1.Tika是一个用于文本解释的框架.其本身并不提供不论什么的库用于解释文本,而是调用各种各样的库,如POI,PDFBox等. 使用Tika.能够提取文件里的作者.标题.创建时间.正文等 ...
- BZOJ 3671 NOI 2014 随机数生成器 贪心
题目大意:实在是太难说明了,自己看pdf吧.. 思路:优先依照它说明的方法处理数组,然后为了让数列中尽可能多的出现小的数字,所以1是必需要出现的,这样才干使整个数列的排序后字典序最小. 我们思考,假设 ...
- 如何才能正确的关闭Socket连接
从TCP协议角度来看,一个已建立的TCP连接有两种关闭方式,一种是正常关闭,即四次挥手关闭连接:还有一种则是异常关闭,我们通常称之为连接重置(RESET).首先说一下正常关闭时四次挥手的状态变迁,关闭 ...
- Js将类数组转化为数组
说起伪数组,大家可能会想到arguments, 这个我们函数参数的一个类数组,是类数组的代表. 1.拥有length属性,可以使用下标来访问元素,这两点和数组相同. 2.不能使用数组的方法,他们不能使 ...