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 3 commits
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
42 changes: 42 additions & 0 deletions docs/src/rules/no-empty-pattern.md
Expand Up @@ -65,3 +65,45 @@ function foo({a = []}) {}
```

:::

## Options

This rule has an object option for exceptions:

### allowObjectPatternsAsParameters

Set to `false` by default. Setting this option to `true` allows empty object patterns as function parameters.

**Note:** This rule doesn't allow empty array patterns as function parameters.

Examples of **incorrect** code for when `allowObjectPatternsAsParameters` is `true`:
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved

::: incorrect

```js
/*eslint no-empty-pattern: ["error", { "allowObjectPatternsAsParameters": true }]*/

function foo({a: {}}) {}
var foo = function({a: {}}) {};
var foo = ({a: {}}) => {};

function foo([]) {}
```

:::

Examples of **correct** code for when `allowObjectPatternsAsParameters` is `true`:
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved

::: correct

```js
/*eslint no-empty-pattern: ["error", { "allowObjectPatternsAsParameters": true }]*/

function foo({}) {}
var foo = function({}) {};
var foo = ({}) => {};

function foo({} = {}) {}
```

:::
40 changes: 37 additions & 3 deletions lib/rules/no-empty-pattern.js
Expand Up @@ -19,19 +19,53 @@ 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" } });

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;
}
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved

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