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) {

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

                // Allow {} empty object patterns in Arrow functions as parameters when allowObjectPatternsAsParameters is true
                if ((node.parent.type === "ArrowFunctionExpression" || node.parent.type === "FunctionDeclaration" || node.parent.type === "FunctionExpression") &&
                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.parent.type === "FunctionDeclaration" || node.parent.parent.type === "FunctionExpression") &&
                node.parent.right.type === "ObjectExpression" &&
                node.parent.right.properties.length === 0 &&
                allowObjectPatternsAsParameters === true &&
                node.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" } });
                }
            }
        };
    }
};


Review comment (refactor):
There's no need to check `node.properties.length === 0` since we already know that at this point. Also, we can use [`astUtils.isFunction`](https://github.com/eslint/eslint/blob/42faa17b1c93f801b14bea2840d1d528e25c7211/lib/rules/utils/ast-utils.js#L117) helper here:

```js
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;
}
```

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" } });
                }
            }
        };
    }
};