Skip to content

Commit

Permalink
feat: adds option for allowing empty object patterns as parameter (#1…
Browse files Browse the repository at this point in the history
…7365)

* feat: adds option for allowing empty object patterns as parameter

* feat: refactor previous code

* docs: adds option details for no-empty-pattern rule

* feat: refactor previous code

* feat: update no-empty-pattern rule file
  • Loading branch information
Tanujkanti4441 committed Jul 21, 2023
1 parent 9803c7c commit 1af6eac
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 4 deletions.
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"
}]
},
{
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"
}]
}
]
});

0 comments on commit 1af6eac

Please sign in to comment.