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 all 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
44 changes: 44 additions & 0 deletions docs/src/rules/no-empty-pattern.md
Expand Up @@ -65,3 +65,47 @@ 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 this rule with the `{"allowObjectPatternsAsParameters": true}` option:

::: incorrect

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

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

function foo([]) {}
```

:::

Examples of **correct** code for this rule with the `{"allowObjectPatternsAsParameters": true}` option:

::: correct

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

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

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

:::
41 changes: 38 additions & 3 deletions lib/rules/no-empty-pattern.js
Expand Up @@ -4,6 +4,8 @@
*/
"use strict";

const astUtils = require("./utils/ast-utils");

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand All @@ -19,19 +21,52 @@ 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 {} 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) {
Expand Down
108 changes: 107 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,106 @@ ruleTester.run("no-empty-pattern", rule, {
data: { type: "array" },
type: "ArrayPattern"
}]
},
{
code: "function foo({}) {}",
options: [{}],
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: [{}],
parserOptions: { ecmaVersion: 6 },
errors: [{
messageId: "unexpected",
data: { type: "object" },
type: "ObjectPattern"
}]
},
{
code: "var foo = ({}) => {}",
options: [{}],
parserOptions: { ecmaVersion: 6 },
errors: [{
messageId: "unexpected",
data: { type: "object" },
type: "ObjectPattern"
}]
},
{
code: "function foo({} = {}) {}",
options: [{}],
parserOptions: { ecmaVersion: 6 },
errors: [{
messageId: "unexpected",
data: { type: "object" },
type: "ObjectPattern"
}]
},
{
code: "var foo = function({} = {}) {}",
options: [{}],
parserOptions: { ecmaVersion: 6 },
errors: [{
messageId: "unexpected",
data: { type: "object" },
type: "ObjectPattern"
}]
},
{
code: "var foo = ({} = {}) => {}",
options: [{}],
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 = ({} = bar) => {}",
options: [{ allowObjectPatternsAsParameters: true }],
parserOptions: { ecmaVersion: 6 },
errors: [{
messageId: "unexpected",
data: { type: "object" },
type: "ObjectPattern"
}]
},
{
code: "var foo = ({} = { bar: 1 }) => {}",
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
}
]
});