[Javascript AST] 2. 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:

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");
      }
    }
  };
}
时间: 2024-10-07 23:51:29

[Javascript AST] 2. Write a simple ESLint rule的相关文章

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". GC overhead limit exceeded 单击"OK"后,提示如下图所示: 2.错误原因 由于用Eclipse编写JavaScript时,出现了return,位置不对,导致错误 3.解决办法 Windows--->Preference--->JavaScript (1)图一

Eclipse--解决eclipse下的Requesting JavaScript AST from selection

解决eclipse下编辑js的报错提示 Requesting JavaScript AST from selection 解决办法: eclipse设置 Window-->Preferences->Javascript-->Editor-->Mark Occurrences 然后取消选中 'Mark occurrences of the selected element in the current file'

[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 function scope and put it into global scope. Code: function getVersion(versionString) { const versionRegex = /(\d+)\.(\d+)\.(\d+)/gi var x = /foo/.text(

[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 write is: var foo = 'o' var bar = 'o' foo === bar function foo(foo, bar) { foo === bar; } We want to trasnform the code which highlighted in foo() fu

[Javascript AST] 4. Continue: Report ESLint error

const disallowedMethods = ["log", "info", "warn", "error", "dir"]; module.exports = { meta: { docs: { description: "Disallow use of console", category: "Best Practices", recommended: tr

[AST Babel] Create a simple babel plugin

For example, we have the source code: getVersison('3.4.5') function getVersion(versionString) { const versionRegex = /(\d)\.(\d)\.(\d+)/ const [, major, minor, patch] = versionRegex.exec(versionString) return {major, minor, patch} } We want to transf

javascript中null和undefined的区别到底是什么?

8年前我开始学习js的时候,对我来说比较诡异的一个事情是undefined和null都代表空值.那么他们之间明确的不同点是什么呢?他们都能去定义空值,而且null == undefined的值也是TRUE. 大部分现代语言像Ruby,Python,或者Java都只有一个空值nil 或者null,  这是很明智的方法. 而js中,如果一个变量或者一个对象没有进行初始化,(编译器)就会返回一个undefined. 例如: let company; company; // => undefined l

12款简化 Web 开发的 JavaScript 开发框架

前端框架简化了开发过程中,像 Bootstrap 和 Foundation 就是前端框架的佼佼者.在这篇文章了,我们编制了一组新鲜的,实用的,可以帮助您建立高质量的 Web 应用程序的 JavaScript 框架清单. 您可能感兴趣的相关文章 网站开发中很有用的 jQuery 效果[附源码] 分享35个让人惊讶的 CSS3 动画效果演示 十分惊艳的8个 HTML5 & JavaScript 特效 Web 开发中很实用的10个效果[源码下载] 12款经典的白富美型 jQuery 图片轮播插件 1.

A Plain English Guide to JavaScript Prototypes

When I first started learning about JavaScript object model my reaction was of horror and disbelief. I was totally puzzled by its prototype nature as it was my first encounter with a prototype based language. I didn’t help that JavaScript has a uniqu