• [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
              }
  • 相关阅读:
    南阳97
    南阳96
    南阳94
    南阳77
    南阳75
    南阳74
    南阳65
    一般图匹配
    466E
    hdu5057 分块处理,当数值大于数据范围时树状数组 真是巧 将大数据分为小数据来处理
  • 原文地址:https://www.cnblogs.com/Answer1215/p/7616029.html
Copyright © 2020-2023  润新知