Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/oxc_ast/src/ast_impl/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ impl<'a> From<Argument<'a>> for ArrayExpressionElement<'a> {
fn from(argument: Argument<'a>) -> Self {
match argument {
Argument::SpreadElement(spread) => Self::SpreadElement(spread),
match_expression!(Argument) => Self::from(argument.into_expression()),
_ => Self::from(argument.into_expression()),
}
}
}
Expand Down
95 changes: 94 additions & 1 deletion crates/oxc_ast/src/ast_kind_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,54 @@ impl<'a> AstKind<'a> {
matches!(self, Self::Function(_) | Self::ArrowFunctionExpression(_))
}

/// Check if this CallExpression or NewExpression has an argument with the given span
///
/// This is useful for determining if a node is an argument to a call expression
/// when traversing the AST, particularly after the removal of `AstKind::Argument`.
///
/// # Examples
///
/// ```rust,ignore
/// // Check if a node is an argument to its parent call expression
/// if parent.has_argument_with_span(node.span()) {
/// // This node is an argument
/// }
/// ```
#[inline]
pub fn has_argument_with_span(&self, span: oxc_span::Span) -> bool {
match self {
Self::CallExpression(call) => call.arguments.iter().any(|arg| arg.span() == span),
Self::NewExpression(new_expr) => {
new_expr.arguments.iter().any(|arg| arg.span() == span)
}
_ => false,
}
}

/// Check if this CallExpression or NewExpression has the given span as its callee
///
/// This is useful for determining if a node is the callee of a call expression
/// when traversing the AST.
///
/// # Examples
///
/// ```rust,ignore
/// // Detect eval() calls
/// if let AstKind::IdentifierReference(ident) = node.kind() {
/// if parent.is_callee_with_span(ident.span) && ident.name == "eval" {
/// // This is an eval() call
/// }
/// }
/// ```
#[inline]
pub fn is_callee_with_span(&self, span: oxc_span::Span) -> bool {
match self {
Self::CallExpression(call) => call.callee.span() == span,
Self::NewExpression(new_expr) => new_expr.callee.span() == span,
_ => false,
}
}

/// Get the name of an identifier node
///
/// Returns the identifier name if this is any kind of identifier node,
Expand Down Expand Up @@ -415,7 +463,6 @@ impl AstKind<'_> {
Self::ObjectProperty(p) => {
format!("ObjectProperty({})", p.key.name().unwrap_or(COMPUTED)).into()
}
Self::Argument(_) => "Argument".into(),
Self::ArrayAssignmentTarget(_) => "ArrayAssignmentTarget".into(),
Self::ObjectAssignmentTarget(_) => "ObjectAssignmentTarget".into(),
Self::AssignmentTargetWithDefault(_) => "AssignmentTargetWithDefault".into(),
Expand Down Expand Up @@ -789,3 +836,49 @@ impl GetAddress for PropertyKeyKind<'_> {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use oxc_span::Span;

// Note: These tests verify the logic of the methods.
// Integration tests using real parsed AST are in the linter crate.

#[test]
fn test_has_argument_with_span_returns_false_for_non_call_expressions() {
// Test that non-CallExpression/NewExpression AstKinds always return false
let test_span = Span::new(0, 5);

let num_lit = NumericLiteral {
span: test_span,
value: 42.0,
raw: None,
base: oxc_syntax::number::NumberBase::Decimal,
};
let num_kind = AstKind::NumericLiteral(&num_lit);
assert!(!num_kind.has_argument_with_span(test_span));

let bool_lit = BooleanLiteral { span: test_span, value: true };
let bool_kind = AstKind::BooleanLiteral(&bool_lit);
assert!(!bool_kind.has_argument_with_span(test_span));
}

#[test]
fn test_is_callee_with_span_returns_false_for_non_call_expressions() {
// Test that non-CallExpression/NewExpression AstKinds always return false
let test_span = Span::new(0, 5);

let num_lit = NumericLiteral {
span: test_span,
value: 42.0,
raw: None,
base: oxc_syntax::number::NumberBase::Decimal,
};
let num_kind = AstKind::NumericLiteral(&num_lit);
assert!(!num_kind.is_callee_with_span(test_span));

let bool_lit = BooleanLiteral { span: test_span, value: true };
let bool_kind = AstKind::BooleanLiteral(&bool_lit);
assert!(!bool_kind.is_callee_with_span(test_span));
}
}
Loading
Loading