Repository: eslint/eslint
File: lib/rules/no-constant-condition.js (JavaScript)

Code before:
//------------------------------------------------------------------------------

/** @type {import('../shared/types').Rule} */
module.exports = {
    meta: {
        type: "problem",

        docs: {
            description: "Disallow constant expressions in conditions",
            recommended: true,
            url: "https://eslint.org/docs/latest/rules/no-constant-condition"
        },

        schema: [
            {
                type: "object",
                properties: {
                    checkLoops: {
                        oneOf: [
                            {
                                enum: ["all", "allExceptWhileTrue", "none"]
                            },
                            {
                                type: "boolean"
                            }
                        ]
                    }
                },
                additionalProperties: false
            }
        ],

        messages: {
            unexpected: "Unexpected constant condition."
        }
    },

    create(context) {
        const options = context.options[0] || {},
            checkLoops = options.checkLoops ?? "allExceptWhileTrue",
            loopSetStack = [];
        const sourceCode = context.sourceCode;

        let loopsInCurrentScope = new Set();

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

        /**
         * Tracks when the given node contains a constant condition.

Review comment (refactor):
```suggestion
                        enum: ["all", "allExceptWhileTrue", "none", true, false]
```

I think we can simplify the schema this way. We're already using this pattern in other rules, for example:

https://github.com/eslint/eslint/blob/113f51ec4e52d3082a74b9682239a6e28d1a70ee/lib/rules/no-mixed-spaces-and-tabs.js#L25-L29

Code after:
//------------------------------------------------------------------------------

/** @type {import('../shared/types').Rule} */
module.exports = {
    meta: {
        type: "problem",

        docs: {
            description: "Disallow constant expressions in conditions",
            recommended: true,
            url: "https://eslint.org/docs/latest/rules/no-constant-condition"
        },

        schema: [
            {
                type: "object",
                properties: {
                    checkLoops: {
                        enum: ["all", "allExceptWhileTrue", "none", true, false]
                    }
                },
                additionalProperties: false
            }
        ],

        messages: {
            unexpected: "Unexpected constant condition."
        }
    },

    create(context) {
        const options = context.options[0] || {};
        let checkLoops = options.checkLoops ?? "allExceptWhileTrue";
        const loopSetStack = [];
        const sourceCode = context.sourceCode;

        if (options.checkLoops === true) {
            checkLoops = "all";
        } else if (options.checkLoops === false) {
            checkLoops = "none";
        }

        let loopsInCurrentScope = new Set();

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

        /**
         * Tracks when the given node contains a constant condition.
         * @param {ASTNode} node The AST node to check.