Rewrite your app in Rust, piece by piece, checked against the original at every step. Works with TypeScript, JavaScript, Python, Go, Ruby, Java, C, PHP, C#, Kotlin, PowerShell and Swift.
Rust agent-skillsclaude-codeclicode-migrationcodexffigolangjavajavascriptmcpmigrationnapiperformanceportingpyo3pythonrewriterubyrusttypescript
//! Small helpers to read the tree: literals, calls, members and objects.
use oxc_ast::ast::{
Argument, ArrayExpression, CallExpression, ChainElement, Expression, MemberExpression,
ObjectExpression, ObjectPropertyKind, PropertyKey,
};
use oxc_span::Span;
/// The line starts of a file, to convert a position into a line number.
pub(super) struct Lines(Vec<u32>);
impl Lines {
pub(super) fn new(source: &str) -> Self {
Self(
std::iter::once(0)
.chain(
source
.match_indices('\n')
.map(|(i, _)| u32::try_from(i + 1).unwrap_or(u32::MAX)),
)
.collect(),
)
}
/// The line (from 1) where `span` starts.
pub(super) fn of(&self, span: Span) -> u32 {
u32::try_from(self.0.partition_point(|&s| s <= span.start)).unwrap_or(u32::MAX)
}
}
/// The value of a text literal: string, or template without expressions.
pub(super) fn text(expr: &Expression<'_>) -> Option<String> {
match expr.get_inner_expression() {
Expression::StringLiteral(s) => s.value.as_str().map(str::to_string),
Expression::TemplateLiteral(t) if t.expressions.is_empty() => {
t.single_quasi()?.as_str().map(str::to_string)
}
_ => None,
}
}
pub(super) fn arg_text(arg: &Argument<'_>) -> Option<String> {
arg.as_expression().and_then(text)
}
/// A text, or an array of texts; `None` as soon as an element is not a literal.
pub(super) fn texts(expr: &Expression<'_>) -> Option<Vec<String>> {
match expr.get_inner_expression() {
Expression::ArrayExpression(array) => array_texts(array),
other => text(other).map(|t| vec![t]),
}
}
pub(super) fn array_texts(array: &ArrayExpression<'_>) -> Option<Vec<String>> {
array
.elements
.iter()
.map(|e| e.as_expression().and_then(text))
.collect()
}
/// The call the expression is (through parentheses, types and `?.`).
pub(super) fn as_call<'e, 'a>(expr: &'e Expression<'a>) -> Option<&'e CallExpression<'a>> {
match expr.get_inner_expression() {
Expression::CallExpression(call) => Some(&**call),
Expression::ChainExpression(chain) => match &chain.expression {
ChainElement::CallExpression(call) => Some(&**call),
_ => None,
},
_ => None,
}
}
/// The member access the expression is (through parentheses, types and `?.`).
pub(super) fn as_member<'e, 'a>(expr: &'e Expression<'a>) -> Option<&'e MemberExpression<'a>> {
match expr.get_inner_expression() {
Expression::ChainExpression(chain) => chain.expression.as_member_expression(),
other => other.as_member_expression(),
}
}
/// The name of the method called: `option` in `a.option(...)`.
pub(super) fn method(call: &CallExpression<'_>) -> Option<String> {
member_name(as_member(&call.callee)?)
}
pub(super) fn member_name(member: &MemberExpression<'_>) -> Option<String> {
member.static_property_name()?.as_str().map(str::to_string)
}
/// What the method is called on: `a` in `a.option(...)`.
pub(super) fn receiver<'e, 'a>(call: &'e CallExpression<'a>) -> Option<&'e Expression<'a>> {
Some(as_member(&call.callee)?.object())
}
/// Where to write the line of a method call: the method name, not the start of the chain
/// (a chain spans several lines).
pub(super) fn method_span(call: &CallExpression<'_>) -> Span {
as_member(&call.callee)
.and_then(MemberExpression::static_property_info)
.map_or(call.span, |(span, _)| span)
}
pub(super) fn ident<'a>(expr: &Expression<'a>) -> Option<&'a str> {
match expr.get_inner_expression() {
Expression::Identifier(id) => Some(id.name.as_str()),
_ => None,
}
}
/// The name of the function called directly: `parseArgs` in `parseArgs(...)`.
pub(super) fn callee_ident_name<'a>(call: &CallExpression<'a>) -> Option<&'a str> {
ident(&call.callee)
}
/// The name of the last segment of a reference: `app` for `app` or `this.app`.
pub(super) fn last_name(expr: &Expression<'_>) -> Option<String> {
match ident(expr) {
Some(name) => Some(name.to_string()),
None => member_name(as_member(expr)?),
}
}
pub(super) fn key_name(key: &PropertyKey<'_>) -> Option<String> {
key.static_name()
.and_then(|n| n.as_str().map(str::to_string))
}
/// The value of the property `name` of an object literal.
pub(super) fn prop<'e, 'a>(
obj: &'e ObjectExpression<'a>,
name: &str,
) -> Option<&'e Expression<'a>> {
obj.properties.iter().find_map(|p| match p {
ObjectPropertyKind::ObjectProperty(p) if key_name(&p.key).as_deref() == Some(name) => {
Some(&p.value)
}
_ => None,
})
}
pub(super) fn is_function(expr: &Expression<'_>) -> bool {
matches!(
expr.get_inner_expression(),
Expression::FunctionExpression(_) | Expression::ArrowFunctionExpression(_)
)
}
pub(super) fn object_of<'e, 'a>(expr: &'e Expression<'a>) -> Option<&'e ObjectExpression<'a>> {
match expr.get_inner_expression() {
Expression::ObjectExpression(obj) => Some(&**obj),
_ => None,
}
}
pub(super) fn arg_object<'e, 'a>(arg: &'e Argument<'a>) -> Option<&'e ObjectExpression<'a>> {
arg.as_expression().and_then(object_of)
}