CoolFace
Apppublic

Umama-at-Bluchip/Quick-UI

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
max-depth.js157 linesDownload Raw Back to rules
1/**2 * @fileoverview A rule to set the maximum depth block can be nested in a function.3 * @author Ian Christian Myers4 */5 6"use strict";7 8//------------------------------------------------------------------------------9// Rule Definition10//------------------------------------------------------------------------------11 12/** @type {import('../shared/types').Rule} */13module.exports = {14    meta: {15        type: "suggestion",16 17        docs: {18            description: "Enforce a maximum depth that blocks can be nested",19            recommended: false,20            url: "https://eslint.org/docs/latest/rules/max-depth"21        },22 23        schema: [24            {25                oneOf: [26                    {27                        type: "integer",28                        minimum: 029                    },30                    {31                        type: "object",32                        properties: {33                            maximum: {34                                type: "integer",35                                minimum: 036                            },37                            max: {38                                type: "integer",39                                minimum: 040                            }41                        },42                        additionalProperties: false43                    }44                ]45            }46        ],47        messages: {48            tooDeeply: "Blocks are nested too deeply ({{depth}}). Maximum allowed is {{maxDepth}}."49        }50    },51 52    create(context) {53 54        //--------------------------------------------------------------------------55        // Helpers56        //--------------------------------------------------------------------------57 58        const functionStack = [],59            option = context.options[0];60        let maxDepth = 4;61 62        if (63            typeof option === "object" &&64            (Object.prototype.hasOwnProperty.call(option, "maximum") || Object.prototype.hasOwnProperty.call(option, "max"))65        ) {66            maxDepth = option.maximum || option.max;67        }68        if (typeof option === "number") {69            maxDepth = option;70        }71 72        /**73         * When parsing a new function, store it in our function stack74         * @returns {void}75         * @private76         */77        function startFunction() {78            functionStack.push(0);79        }80 81        /**82         * When parsing is done then pop out the reference83         * @returns {void}84         * @private85         */86        function endFunction() {87            functionStack.pop();88        }89 90        /**91         * Save the block and Evaluate the node92         * @param {ASTNode} node node to evaluate93         * @returns {void}94         * @private95         */96        function pushBlock(node) {97            const len = ++functionStack[functionStack.length - 1];98 99            if (len > maxDepth) {100                context.report({ node, messageId: "tooDeeply", data: { depth: len, maxDepth } });101            }102        }103 104        /**105         * Pop the saved block106         * @returns {void}107         * @private108         */109        function popBlock() {110            functionStack[functionStack.length - 1]--;111        }112 113        //--------------------------------------------------------------------------114        // Public API115        //--------------------------------------------------------------------------116 117        return {118            Program: startFunction,119            FunctionDeclaration: startFunction,120            FunctionExpression: startFunction,121            ArrowFunctionExpression: startFunction,122            StaticBlock: startFunction,123 124            IfStatement(node) {125                if (node.parent.type !== "IfStatement") {126                    pushBlock(node);127                }128            },129            SwitchStatement: pushBlock,130            TryStatement: pushBlock,131            DoWhileStatement: pushBlock,132            WhileStatement: pushBlock,133            WithStatement: pushBlock,134            ForStatement: pushBlock,135            ForInStatement: pushBlock,136            ForOfStatement: pushBlock,137 138            "IfStatement:exit": popBlock,139            "SwitchStatement:exit": popBlock,140            "TryStatement:exit": popBlock,141            "DoWhileStatement:exit": popBlock,142            "WhileStatement:exit": popBlock,143            "WithStatement:exit": popBlock,144            "ForStatement:exit": popBlock,145            "ForInStatement:exit": popBlock,146            "ForOfStatement:exit": popBlock,147 148            "FunctionDeclaration:exit": endFunction,149            "FunctionExpression:exit": endFunction,150            "ArrowFunctionExpression:exit": endFunction,151            "StaticBlock:exit": endFunction,152            "Program:exit": endFunction153        };154 155    }156};157