Javascript 自定义规则ESLint,用于使用Array.push而不是直接赋值将项添加到数组

Javascript 自定义规则ESLint,用于使用Array.push而不是直接赋值将项添加到数组,javascript,eslint,rules,Javascript,Eslint,Rules,我想创建一个自定义规则,防止直接给数组赋值,并鼓励使用array.push e、 g 我在ast explorer中尝试过这样做,但仍然没有成功 "use strict"; module.exports = { meta: { type: "suggestion", docs: { description: "disallow direct assign to array&quo

我想创建一个自定义规则,防止直接给数组赋值,并鼓励使用array.push

e、 g

我在ast explorer中尝试过这样做,但仍然没有成功

"use strict";

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

        docs: {
            description: "disallow direct assign to array",
            category: "Stylistic Issues",
        },
        schema: [],
        messages: {
            preferLiteral: "Using `array.push` insted of direct assign value to array"
        }
    },

    create(context) {
        function check(node) {
            if (
                node.parent.type === "VariableDeclarator"
            ) {
                console.log(node)
                context.report({ node, messageId: "preferLiteral" });
            }
        }

        return {
            ArrayExpression: check
        };

    }
};

也许数组优先查找规则的源代码将是一个有用的指南。也许数组优先查找规则的源代码将是一个有用的指南。
"use strict";

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

        docs: {
            description: "disallow direct assign to array",
            category: "Stylistic Issues",
        },
        schema: [],
        messages: {
            preferLiteral: "Using `array.push` insted of direct assign value to array"
        }
    },

    create(context) {
        function check(node) {
            if (
                node.parent.type === "VariableDeclarator"
            ) {
                console.log(node)
                context.report({ node, messageId: "preferLiteral" });
            }
        }

        return {
            ArrayExpression: check
        };

    }
};