mirror of
https://github.com/danbulant/oxc
synced 2026-05-21 05:08:45 +00:00
fix(linter): fix no_empty_pattern broken on rest elements
This commit is contained in:
parent
13207193bf
commit
072abcc2a6
2 changed files with 16 additions and 4 deletions
|
|
@ -1277,6 +1277,12 @@ pub struct ObjectPattern<'a> {
|
|||
pub rest: Option<Box<'a, RestElement<'a>>>,
|
||||
}
|
||||
|
||||
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<Box<'a, RestElement<'a>>>,
|
||||
}
|
||||
|
||||
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> {
|
||||
|
|
|
|||
|
|
@ -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![
|
||||
|
|
|
|||
Loading…
Reference in a new issue