[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: true
    }
  },
  create(context) {
    return {
      Identifier(node) {

        const isConsoleCall = looksLike(node, {
          name: "console",
          parent: {
            type: "MemberExpression",
            property: {
              name: val => disallowedMethods.includes(val)
            }
          }
        });
        // find the identifier with name ‘console‘
        if (!isConsoleCall) {
          return;
        }

        context.report({
          node,
          message: "Using {{identifier}} is not allowed",
          data: {
             identifier: node.name // console
          }
        });
      }
    };
  }
};

function looksLike(a, b) {
  return (
    a &&
    b &&
    Object.keys(b).every(bKey => {
      const bVal = b[bKey];
      const aVal = a[bKey];
      if (typeof bVal === "function") {
        return bVal(aVal);
      }
      return isPrimitive(bVal) ? bVal === aVal : looksLike(aVal, bVal);
    })
  );
}

function isPrimitive(val) {
  return val == null || /^[sbn]/.test(typeof val);
}

We can use placeholder for more detail information:

"Using {{identifier}} is not allowed"

The placeholder can be found in data prop:

          data: {
             identifier: node.name // console
          }
时间: 2024-10-14 04:02:39

[Javascript AST] 4. Continue: Report ESLint error的相关文章

[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(

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] 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) {

对比JavaScript中的Continue和Break

译者按: 最好是不用,不过基础知识要掌握. 原文: JavaScript: Continue vs Break - Learn the difference between the continue and break statements. 译者: Fundebug 为了保证可读性,本文采用意译而非直译.另外,本文版权归原作者所有,翻译仅用于学习. 在这篇文章中,我们会详细介绍continue和break,分析它们的相同和不同之处,甚至用一些可运行的实例. continue和break都是用于

javascript break 和continue

break语句还可以跳出循环,也就是结束循环语句的执行. continue语句的作用为结束本次循环,接着进行下一次是否执行循环的判断. continue与break的区别是:break是彻底结束循环,而continue是结束本次循环

[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 If...Else、Switch、For、While、Break、Continue语句

一,JavaScript If...Else 语句 条件语句 通常在写代码时,您总是需要为不同的决定来执行不同的动作.您可以在代码中使用条件语句来完成该任务. 在 JavaScript 中,我们可使用以下条件语句: if 语句 - 只有当指定条件为 true 时,使用该语句来执行代码 if...else 语句 - 当条件为 true 时执行代码,当条件为 false 时执行其他代码 if...else if....else 语句 - 使用该语句来选择多个代码块之一来执行 switch 语句 -

javaScript的break和continue

break 语句 使用 break 语句来终止循环. continue 语句 使用 continue 语句来终止当前的循环,然后从下一个值继续执行. JavaScript break 和 continue 语句 有两种特殊的语句可用在循环内部:break 和 continue. Break break 命令可以终止循环的运行,然后继续执行循环之后的代码(如果循环之后有代码的话). 实例: <html> <body> <script type="text/javasc