Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds option for allowing empty object patterns as parameter #17365

Merged
merged 5 commits into from Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 42 additions & 3 deletions lib/rules/no-empty-pattern.js
Expand Up @@ -19,18 +19,57 @@ module.exports = {
url: "https://eslint.org/docs/latest/rules/no-empty-pattern"
},

schema: [],
schema: [
{
type: "object",
properties: {
allowObjectPatternsAsParameters: {
type: "boolean",
default: false
}
},
additionalProperties: false
}
],

messages: {
unexpected: "Unexpected empty {{type}} pattern."
}
},

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

return {
ObjectPattern(node) {
if (node.properties.length === 0) {
context.report({ node, messageId: "unexpected", data: { type: "object" } });

// 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) ||
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
node.properties.length === 0) {
context.report({
node,
messageId: "unexpected",
data: { type: "object" }
});
}
},
ArrayPattern(node) {
Expand Down
44 changes: 43 additions & 1 deletion tests/lib/rules/no-empty-pattern.js
Expand Up @@ -26,7 +26,9 @@ ruleTester.run("no-empty-pattern", rule, {
{ code: "var {a = []} = foo;", parserOptions: { ecmaVersion: 6 } },
{ code: "function foo({a = {}}) {}", parserOptions: { ecmaVersion: 6 } },
{ code: "function foo({a = []}) {}", parserOptions: { ecmaVersion: 6 } },
{ code: "var [a] = foo", parserOptions: { ecmaVersion: 6 } }
{ code: "var [a] = foo", parserOptions: { ecmaVersion: 6 } },
{ code: "var foo = ({}) => {}", options: [{ allowObjectPatternsAsParameters: true }], parserOptions: { ecmaVersion: 6 } },
{ code: "var foo = ({} = {}) => {}", options: [{ allowObjectPatternsAsParameters: true }], parserOptions: { ecmaVersion: 6 } }
],

// Examples of code that should trigger the rule
Expand Down Expand Up @@ -111,6 +113,46 @@ ruleTester.run("no-empty-pattern", rule, {
data: { type: "array" },
type: "ArrayPattern"
}]
},
{
code: "var foo = ({}) => {}",
options: [{ allowObjectPatternsAsParameters: false }],
parserOptions: { ecmaVersion: 6 },
errors: [{
messageId: "unexpected",
data: { type: "object" },
type: "ObjectPattern"
}]
},
{
code: "var foo = ({} = {}) => {}",
options: [{ allowObjectPatternsAsParameters: false }],
parserOptions: { ecmaVersion: 6 },
errors: [{
messageId: "unexpected",
data: { type: "object" },
type: "ObjectPattern"
}]
},
{
code: "var foo = ({a: {}}) => {}",
options: [{ allowObjectPatternsAsParameters: true }],
parserOptions: { ecmaVersion: 6 },
errors: [{
messageId: "unexpected",
data: { type: "object" },
type: "ObjectPattern"
}]
},
{
code: "var foo = ([]) => {}",
options: [{ allowObjectPatternsAsParameters: true }],
parserOptions: { ecmaVersion: 6 },
errors: [{
messageId: "unexpected",
data: { type: "array" },
type: "ArrayPattern"
}]
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
}
]
});