Skip to content

Commit

Permalink
feat(linter): add oxc/no-map-spread
Browse files Browse the repository at this point in the history
  • Loading branch information
DonIsaac committed Oct 21, 2024
1 parent 70efa47 commit 4eb3208
Show file tree
Hide file tree
Showing 4 changed files with 577 additions and 0 deletions.
32 changes: 32 additions & 0 deletions crates/oxc_linter/src/ast_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ pub fn nth_outermost_paren_parent<'a, 'b>(
.filter(|parent| !matches!(parent.kind(), AstKind::ParenthesizedExpression(_)))
.nth(n)
}

/// Iterate over parents of `node`, skipping nodes that are also ignored by
/// [`Expression::get_inner_expression`].
pub fn iter_outer_expressions<'a, 'ctx>(
Expand Down Expand Up @@ -414,3 +415,34 @@ pub fn get_function_like_declaration<'b>(

decl.id.get_binding_identifier()
}

/// Get the first identifier reference within a member expression chain or
/// standalone reference.
///
/// For example, when called on the right-hand side of this [`AssignmentExpression`]:
/// ```ts
/// let x = a
/// // ^
/// let y = a.b.c
/// // ^
/// ```
///
/// As this function walks down the member expression chain, if no identifier
/// reference is found, it returns [`Err`] with the leftmost expression.
/// ```ts
/// let x = 1 + 1
/// // ^^^^^ Err(BinaryExpression)
/// let y = this.foo.bar
/// // ^^^^ Err(ThisExpression)
/// ```
pub fn leftmost_identifier_reference<'a, 'b: 'a>(
expr: &'b Expression<'a>,
) -> Result<&'a IdentifierReference<'a>, &'b Expression<'a>> {
match expr {
Expression::Identifier(ident) => Ok(ident.as_ref()),
Expression::StaticMemberExpression(mem) => leftmost_identifier_reference(&mem.object),
Expression::ComputedMemberExpression(mem) => leftmost_identifier_reference(&mem.object),
Expression::PrivateFieldExpression(mem) => leftmost_identifier_reference(&mem.object),
_ => Err(expr),
}
}
2 changes: 2 additions & 0 deletions crates/oxc_linter/src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ mod oxc {
pub mod no_async_endpoint_handlers;
pub mod no_barrel_file;
pub mod no_const_enum;
pub mod no_map_spread;
pub mod no_optional_chaining;
pub mod no_rest_spread_properties;
pub mod number_arg_out_of_range;
Expand Down Expand Up @@ -761,6 +762,7 @@ oxc_macros::declare_all_lint_rules! {
oxc::no_async_endpoint_handlers,
oxc::no_barrel_file,
oxc::no_const_enum,
oxc::no_map_spread,
oxc::no_optional_chaining,
oxc::no_rest_spread_properties,
oxc::number_arg_out_of_range,
Expand Down
Loading

0 comments on commit 4eb3208

Please sign in to comment.