[d565449] | 1 | /**
|
---|
| 2 | * @fileoverview Rule to disallow an empty pattern
|
---|
| 3 | * @author Alberto Rodríguez
|
---|
| 4 | */
|
---|
| 5 | "use strict";
|
---|
| 6 |
|
---|
| 7 | const astUtils = require("./utils/ast-utils");
|
---|
| 8 |
|
---|
| 9 | //------------------------------------------------------------------------------
|
---|
| 10 | // Rule Definition
|
---|
| 11 | //------------------------------------------------------------------------------
|
---|
| 12 |
|
---|
| 13 | /** @type {import('../shared/types').Rule} */
|
---|
| 14 | module.exports = {
|
---|
| 15 | meta: {
|
---|
| 16 | type: "problem",
|
---|
| 17 |
|
---|
| 18 | docs: {
|
---|
| 19 | description: "Disallow empty destructuring patterns",
|
---|
| 20 | recommended: true,
|
---|
| 21 | url: "https://eslint.org/docs/latest/rules/no-empty-pattern"
|
---|
| 22 | },
|
---|
| 23 |
|
---|
| 24 | schema: [
|
---|
| 25 | {
|
---|
| 26 | type: "object",
|
---|
| 27 | properties: {
|
---|
| 28 | allowObjectPatternsAsParameters: {
|
---|
| 29 | type: "boolean",
|
---|
| 30 | default: false
|
---|
| 31 | }
|
---|
| 32 | },
|
---|
| 33 | additionalProperties: false
|
---|
| 34 | }
|
---|
| 35 | ],
|
---|
| 36 |
|
---|
| 37 | messages: {
|
---|
| 38 | unexpected: "Unexpected empty {{type}} pattern."
|
---|
| 39 | }
|
---|
| 40 | },
|
---|
| 41 |
|
---|
| 42 | create(context) {
|
---|
| 43 | const options = context.options[0] || {},
|
---|
| 44 | allowObjectPatternsAsParameters = options.allowObjectPatternsAsParameters || false;
|
---|
| 45 |
|
---|
| 46 | return {
|
---|
| 47 | ObjectPattern(node) {
|
---|
| 48 |
|
---|
| 49 | if (node.properties.length > 0) {
|
---|
| 50 | return;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | // Allow {} and {} = {} empty object patterns as parameters when allowObjectPatternsAsParameters is true
|
---|
| 54 | if (
|
---|
| 55 | allowObjectPatternsAsParameters &&
|
---|
| 56 | (
|
---|
| 57 | astUtils.isFunction(node.parent) ||
|
---|
| 58 | (
|
---|
| 59 | node.parent.type === "AssignmentPattern" &&
|
---|
| 60 | astUtils.isFunction(node.parent.parent) &&
|
---|
| 61 | node.parent.right.type === "ObjectExpression" &&
|
---|
| 62 | node.parent.right.properties.length === 0
|
---|
| 63 | )
|
---|
| 64 | )
|
---|
| 65 | ) {
|
---|
| 66 | return;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | context.report({ node, messageId: "unexpected", data: { type: "object" } });
|
---|
| 70 | },
|
---|
| 71 | ArrayPattern(node) {
|
---|
| 72 | if (node.elements.length === 0) {
|
---|
| 73 | context.report({ node, messageId: "unexpected", data: { type: "array" } });
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 | };
|
---|
| 77 | }
|
---|
| 78 | };
|
---|