From 072abcc2a6bf06b4776cee40bf02ced30cfc6480 Mon Sep 17 00:00:00 2001 From: Boshen Date: Wed, 28 Jun 2023 17:23:04 +0800 Subject: [PATCH] fix(linter): fix no_empty_pattern broken on rest elements --- crates/oxc_ast/src/ast/js.rs | 12 ++++++++++++ .../oxc_linter/src/rules/eslint/no_empty_pattern.rs | 8 ++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/crates/oxc_ast/src/ast/js.rs b/crates/oxc_ast/src/ast/js.rs index e760b3e58..b04aadd2e 100644 --- a/crates/oxc_ast/src/ast/js.rs +++ b/crates/oxc_ast/src/ast/js.rs @@ -1277,6 +1277,12 @@ pub struct ObjectPattern<'a> { pub rest: Option>>, } +impl<'a> ObjectPattern<'a> { + pub fn is_empty(&self) -> bool { + self.properties.is_empty() && self.rest.is_none() + } +} + #[derive(Debug, Hash)] #[cfg_attr(feature = "serde", derive(Serialize), serde(tag = "type"))] pub struct BindingProperty<'a> { @@ -1297,6 +1303,12 @@ pub struct ArrayPattern<'a> { pub rest: Option>>, } +impl<'a> ArrayPattern<'a> { + pub fn is_empty(&self) -> bool { + self.elements.is_empty() && self.rest.is_none() + } +} + #[derive(Debug, Hash)] #[cfg_attr(feature = "serde", derive(Serialize), serde(tag = "type"))] pub struct RestElement<'a> { diff --git a/crates/oxc_linter/src/rules/eslint/no_empty_pattern.rs b/crates/oxc_linter/src/rules/eslint/no_empty_pattern.rs index 74af9779e..21c4ba264 100644 --- a/crates/oxc_linter/src/rules/eslint/no_empty_pattern.rs +++ b/crates/oxc_linter/src/rules/eslint/no_empty_pattern.rs @@ -77,10 +77,8 @@ declare_oxc_lint!( impl Rule for NoEmptyPattern { fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { let (pattern_type, span) = match node.kind() { - AstKind::ArrayPattern(array) if array.elements.is_empty() => ("array", array.span), - AstKind::ObjectPattern(object) if object.properties.is_empty() => { - ("object", object.span) - } + AstKind::ArrayPattern(array) if array.is_empty() => ("array", array.span), + AstKind::ObjectPattern(object) if object.is_empty() => ("object", object.span), _ => return, }; @@ -99,6 +97,8 @@ fn test() { ("function foo({a = {}}) {}", None), ("function foo({a = []}) {}", None), ("var [a] = foo", None), + ("var {...x} = foo;", None), + ("var [...x] = foo;", None), ]; let fail = vec![