美文网首页
自定义实现eslint 规则 检查菜单是否重复

自定义实现eslint 规则 检查菜单是否重复

作者: 一碗米粉的故事 | 来源:发表于2020-04-27 15:53 被阅读0次

把如下js文件 放置一个根文件夹eslint-rules内 关键调试 哪个create方法体内
执行命令:
npm run lint

 "scripts": {
    "lint": "eslint  需要检查的某个文件.js  --rulesdir eslint-rules/"
  },

"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
let globalMark=[];
function checkMark(nextChild) {
    let bl=true;
    // if(!nextChild.mark || nextChild.mark===""){bl=false}
            if(nextChild.mark===null){return true}
            if(globalMark.indexOf(nextChild.mark)>-1){
                bl=false
            }else{
                globalMark.push(nextChild.mark)
            }
            return bl;
}
function getStaticPropertyName(node) {
    let prop;

    switch (node && node.type) {
        case "Property":
        case "MethodDefinition":
            if(node.key.name!=='mark'){
                return null
            }
            prop = node.value;
            break;

        case "MemberExpression":
            prop = node.property;
            break;

        // no default
    }

    switch (prop && prop.type) {
        case "Literal":
            return String(prop.value);

        case "TemplateLiteral":
            if (prop.expressions.length === 0 && prop.quasis.length === 1) {
                return prop.quasis[0].value.cooked;
            }
            break;

        case "Identifier":
            if (!node.computed) {
                return prop.value;
            }
            break;

        // no default
    }

    return null;
}

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

const GET_KIND = /^(?:init|get)$/u;
const SET_KIND = /^(?:init|set)$/u;

/**
 * The class which stores properties' information of an object.
 */
class ObjectInfo {

    /**
     * @param {ObjectInfo|null} upper - The information of the outer object.
     * @param {ASTNode} node - The ObjectExpression node of this information.
     */
    constructor(upper, node) {
        this.upper = upper;
        this.node = node;
        this.properties = new Map();
    }

    /**
     * Gets the information of the given Property node.
     * @param {ASTNode} node - The Property node to get.
     * @returns {{get: boolean, set: boolean}} The information of the property.
     */
    getPropertyInfo(node) {
        const name = getStaticPropertyName(node);

        if (!this.properties.has(name)) {
            this.properties.set(name, { get: false, set: false });
        }
        return this.properties.get(name);
    }

    /**
     * Checks whether the given property has been defined already or not.
     * @param {ASTNode} node - The Property node to check.
     * @returns {boolean} `true` if the property has been defined.
     */
    isPropertyDefined(node) {
        const entry = this.getPropertyInfo(node);

        return (
            (GET_KIND.test(node.kind) && entry.get) ||
            (SET_KIND.test(node.kind) && entry.set)
        );
    }

    /**
     * Defines the given property.
     * @param {ASTNode} node - The Property node to define.
     * @returns {void}
     */
    defineProperty(node) {
        const entry = this.getPropertyInfo(node);

        if (GET_KIND.test(node.kind)) {
            entry.get = true;
        }
        if (SET_KIND.test(node.kind)) {
            entry.set = true;
        }
    }
}

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = {
    meta: {
        type: "problem",

        docs: {
            description: "disallow duplicate values in object literals",
            category: "Possible Errors",
            recommended: true,
            url: ""
        },

        schema: [],

        messages: {
            unexpected: "Duplicate values '{{name}}'."
        }
    },

    create(context) {
        let info = null;

        return {
            ObjectExpression(node) {
                info = new ObjectInfo(info, node);
            },
            "ObjectExpression:exit"() {
                info = info.upper;
            },

            Property(node) {
                const name = getStaticPropertyName(node);
                if (!checkMark({mark:name})) {
                    context.report({
                        node: info.node,
                        loc: node.key.loc,
                        messageId: "unexpected",
                        data: { name }
                    });
                }
                // console.log(name);
                // console.log("======");

                // Skip destructuring.
                if (node.parent.type !== "ObjectExpression") {
                    return;
                }

                // Skip if the name is not static.
                if (!name) {
                    return;
                }

                // Reports if the name is defined already.
                // if (info.isPropertyDefined(node)) {
                //     context.report({
                //         node: info.node,
                //         loc: node.key.loc,
                //         messageId: "unexpected",
                //         data: { name }
                //     });
                // }


                // Update info.
                info.defineProperty(node);
            }
        };
    }
};

相关文章

  • 自定义实现eslint 规则 检查菜单是否重复

    把如下js文件 放置一个根文件夹eslint-rules内 关键调试 哪个create方法体内执行命令:npm r...

  • ESlint

    1、简介: eslint检查我们写的 JavaScript 代码是否满足指定规则的静态代码检查工具。 JSHint...

  • ESLint 介绍

    ESLint 是用来检查我们写的 JavaScript 代码是否满足指定规则的静态代码检查工具。 通过用 ESLi...

  • Eslint检查规则

    打开网页点击左侧蓝色英文查看详细规则 举个栗子 禁用 console (no-console) 配置文件中的 "e...

  • ESLint使用规则

    工作中如何使用的ESLint,以及如何自定义ESLint规则? 为什么要使用ESLint? JavaScript是...

  • 手把手教你如何扩展eslint的检查规则

    本文介绍了扩展eslint检查规则具体方法。下面我们就以一个具体的检查规则实例来介绍下扩展检查规则的具体方法。 规...

  • NGUI自动触发确认键导致点一下按钮执行两次

    1. 首先检查菜单栏Edit -> Preferences -> Input里面是否有重复的触发,若有,修改里面的...

  • Prettier 和 ESLint

    ESLint ESLint 是一个 JavaScript 代码检查工具,根据给定的规则,它包括两方面作用: 对代码...

  • webpack4进阶知识点(二)

    1.eslint js代码检查工具,帮助发现代码错误规则,保持团队的代码风格统一 :eslint-config-a...

  • VUE eslint编码规范检查

    ESLint 是一个代码规范检查工具 它定义了很多特定的规则, 一旦你的代码违背了某一规则, eslint会作出非...

网友评论

      本文标题:自定义实现eslint 规则 检查菜单是否重复

      本文链接:https://www.haomeiwen.com/subject/tetuwhtx.html