Repository: eslint/eslint
File: lib/rules/no-empty-pattern.js (JavaScript)

Code before:
        const options = context.options[0] || {},
            allowObjectPatternsAsParameters = options.allowObjectPatternsAsParameters || false;

        return {
            ObjectPattern(node) {

                // Allow {} empty object patterns in Arrow functions as parameters when allowObjectPatternsAsParameters is true
                if (node.parent.type === "ArrowFunctionExpression" &&
                allowObjectPatternsAsParameters === true &&
                node.properties.length === 0) {
                    return;
                }

                // Allow {} = {} empty object patterns in Arrow functions as parameters when allowObjectPatternsAsParameters is true
                if (node.parent.type === "AssignmentPattern" &&
                node.parent.parent.type === "ArrowFunctionExpression" &&
                node.parent.right.type === "ObjectExpression" &&
                node.parent.right.properties.length === 0 &&
                allowObjectPatternsAsParameters === true &&
                node.properties.length === 0) {
                    return;
                }

                if ((node.parent.type === "ArrowFunctionExpression" &&
                allowObjectPatternsAsParameters === false &&
                node.properties.length === 0) ||
                node.properties.length === 0) {
                    context.report({
                        node,
                        messageId: "unexpected",
                        data: { type: "object" }
                    });
                }
            },
            ArrayPattern(node) {
                if (node.elements.length === 0) {
                    context.report({ node, messageId: "unexpected", data: { type: "array" } });
                }
            }
        };
    }
};


Review comment (suggestion):
These conditions are redundant with the previous checks. At this point, this can be just `if (node.properties.length === 0) { contex.report(...); }`.

Or even better, we could have an early exit for non-empty patterns:

```js
ObjectPattern(node) {
    if (node.properties.length > 0) {
        return;
    }

    // check and exit if allowObjectPatternsAsParameters applies

    context.report({ node, messageId: "unexpected", data: { type: "object" } });
}
```



Code after:

    create(context) {
        const options = context.options[0] || {},
            allowObjectPatternsAsParameters = options.allowObjectPatternsAsParameters || false;

        return {
            ObjectPattern(node) {

                if (node.properties.length > 0) {
                    return;
                }

                // Allow {} and {} = {} empty object patterns as parameters when allowObjectPatternsAsParameters is true
                if (
                    allowObjectPatternsAsParameters &&
                    (
                        astUtils.isFunction(node.parent) ||
                        (
                            node.parent.type === "AssignmentPattern" &&
                            astUtils.isFunction(node.parent.parent) &&
                            node.parent.right.type === "ObjectExpression" &&
                            node.parent.right.properties.length === 0
                        )
                    )
                ) {
                    return;
                }

                context.report({ node, messageId: "unexpected", data: { type: "object" } });
            },
            ArrayPattern(node) {
                if (node.elements.length === 0) {
                    context.report({ node, messageId: "unexpected", data: { type: "array" } });
                }
            }
        };
    }
};