Skip to content

Commit 9ef5df9

Browse files
taearlscamc314
authored andcommitted
Remove AstKind for Argument
1 parent 81e179c commit 9ef5df9

File tree

87 files changed

+2349
-931
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+2349
-931
lines changed

crates/oxc_ast/src/ast_impl/js.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ impl<'a> From<Argument<'a>> for ArrayExpressionElement<'a> {
430430
fn from(argument: Argument<'a>) -> Self {
431431
match argument {
432432
Argument::SpreadElement(spread) => Self::SpreadElement(spread),
433-
match_expression!(Argument) => Self::from(argument.into_expression()),
433+
_ => Self::from(argument.into_expression()),
434434
}
435435
}
436436
}

crates/oxc_ast/src/ast_kind_impl.rs

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,54 @@ impl<'a> AstKind<'a> {
122122
matches!(self, Self::Function(_) | Self::ArrowFunctionExpression(_))
123123
}
124124

125+
/// Check if this CallExpression or NewExpression has an argument with the given span
126+
///
127+
/// This is useful for determining if a node is an argument to a call expression
128+
/// when traversing the AST, particularly after the removal of `AstKind::Argument`.
129+
///
130+
/// # Examples
131+
///
132+
/// ```rust,ignore
133+
/// // Check if a node is an argument to its parent call expression
134+
/// if parent.has_argument_with_span(node.span()) {
135+
/// // This node is an argument
136+
/// }
137+
/// ```
138+
#[inline]
139+
pub fn has_argument_with_span(&self, span: oxc_span::Span) -> bool {
140+
match self {
141+
Self::CallExpression(call) => call.arguments.iter().any(|arg| arg.span() == span),
142+
Self::NewExpression(new_expr) => {
143+
new_expr.arguments.iter().any(|arg| arg.span() == span)
144+
}
145+
_ => false,
146+
}
147+
}
148+
149+
/// Check if this CallExpression or NewExpression has the given span as its callee
150+
///
151+
/// This is useful for determining if a node is the callee of a call expression
152+
/// when traversing the AST.
153+
///
154+
/// # Examples
155+
///
156+
/// ```rust,ignore
157+
/// // Detect eval() calls
158+
/// if let AstKind::IdentifierReference(ident) = node.kind() {
159+
/// if parent.is_callee_with_span(ident.span) && ident.name == "eval" {
160+
/// // This is an eval() call
161+
/// }
162+
/// }
163+
/// ```
164+
#[inline]
165+
pub fn is_callee_with_span(&self, span: oxc_span::Span) -> bool {
166+
match self {
167+
Self::CallExpression(call) => call.callee.span() == span,
168+
Self::NewExpression(new_expr) => new_expr.callee.span() == span,
169+
_ => false,
170+
}
171+
}
172+
125173
/// Get the name of an identifier node
126174
///
127175
/// Returns the identifier name if this is any kind of identifier node,
@@ -415,7 +463,6 @@ impl AstKind<'_> {
415463
Self::ObjectProperty(p) => {
416464
format!("ObjectProperty({})", p.key.name().unwrap_or(COMPUTED)).into()
417465
}
418-
Self::Argument(_) => "Argument".into(),
419466
Self::ArrayAssignmentTarget(_) => "ArrayAssignmentTarget".into(),
420467
Self::ObjectAssignmentTarget(_) => "ObjectAssignmentTarget".into(),
421468
Self::AssignmentTargetWithDefault(_) => "AssignmentTargetWithDefault".into(),
@@ -789,3 +836,49 @@ impl GetAddress for PropertyKeyKind<'_> {
789836
}
790837
}
791838
}
839+
#[cfg(test)]
840+
mod tests {
841+
use super::*;
842+
use oxc_span::Span;
843+
844+
// Note: These tests verify the logic of the methods.
845+
// Integration tests using real parsed AST are in the linter crate.
846+
847+
#[test]
848+
fn test_has_argument_with_span_returns_false_for_non_call_expressions() {
849+
// Test that non-CallExpression/NewExpression AstKinds always return false
850+
let test_span = Span::new(0, 5);
851+
852+
let num_lit = NumericLiteral {
853+
span: test_span,
854+
value: 42.0,
855+
raw: None,
856+
base: oxc_syntax::number::NumberBase::Decimal,
857+
};
858+
let num_kind = AstKind::NumericLiteral(&num_lit);
859+
assert!(!num_kind.has_argument_with_span(test_span));
860+
861+
let bool_lit = BooleanLiteral { span: test_span, value: true };
862+
let bool_kind = AstKind::BooleanLiteral(&bool_lit);
863+
assert!(!bool_kind.has_argument_with_span(test_span));
864+
}
865+
866+
#[test]
867+
fn test_is_callee_with_span_returns_false_for_non_call_expressions() {
868+
// Test that non-CallExpression/NewExpression AstKinds always return false
869+
let test_span = Span::new(0, 5);
870+
871+
let num_lit = NumericLiteral {
872+
span: test_span,
873+
value: 42.0,
874+
raw: None,
875+
base: oxc_syntax::number::NumberBase::Decimal,
876+
};
877+
let num_kind = AstKind::NumericLiteral(&num_lit);
878+
assert!(!num_kind.is_callee_with_span(test_span));
879+
880+
let bool_lit = BooleanLiteral { span: test_span, value: true };
881+
let bool_kind = AstKind::BooleanLiteral(&bool_lit);
882+
assert!(!bool_kind.is_callee_with_span(test_span));
883+
}
884+
}

0 commit comments

Comments
 (0)