From 1af6eac5727080c809e37c07dc729b44ef24483c Mon Sep 17 00:00:00 2001 From: Tanuj Kanti <86398394+Tanujkanti4441@users.noreply.github.com> Date: Fri, 21 Jul 2023 21:00:32 +0530 Subject: [PATCH] feat: adds option for allowing empty object patterns as parameter (#17365) * 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 --- docs/src/rules/no-empty-pattern.md | 44 ++++++++++++ lib/rules/no-empty-pattern.js | 41 ++++++++++- tests/lib/rules/no-empty-pattern.js | 108 +++++++++++++++++++++++++++- 3 files changed, 189 insertions(+), 4 deletions(-) diff --git a/docs/src/rules/no-empty-pattern.md b/docs/src/rules/no-empty-pattern.md index d510f592d55..7edd933731a 100644 --- a/docs/src/rules/no-empty-pattern.md +++ b/docs/src/rules/no-empty-pattern.md @@ -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({} = {}) {} +``` + +::: diff --git a/lib/rules/no-empty-pattern.js b/lib/rules/no-empty-pattern.js index abb1a7c6ddb..fb75f6d25b3 100644 --- a/lib/rules/no-empty-pattern.js +++ b/lib/rules/no-empty-pattern.js @@ -4,6 +4,8 @@ */ "use strict"; +const astUtils = require("./utils/ast-utils"); + //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ @@ -19,7 +21,18 @@ 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." @@ -27,11 +40,33 @@ module.exports = { }, 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) { diff --git a/tests/lib/rules/no-empty-pattern.js b/tests/lib/rules/no-empty-pattern.js index 0d2d7ea80aa..2cd06c4be37 100644 --- a/tests/lib/rules/no-empty-pattern.js +++ b/tests/lib/rules/no-empty-pattern.js @@ -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 @@ -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" + }] } ] });