mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
refactor(parser): use ast_builder in more places. (#4612)
This commit is contained in:
parent
84d80c124f
commit
d25dea7b94
4 changed files with 28 additions and 48 deletions
|
|
@ -124,7 +124,7 @@ impl<'a> ParserImpl<'a> {
|
|||
.context(Context::In, Context::empty(), |p| p.parse_initializer(init_span, pattern))?;
|
||||
let span = self.end_span(span);
|
||||
|
||||
Ok(BindingRestElement { span, argument })
|
||||
Ok(self.ast.binding_rest_element(span, argument))
|
||||
}
|
||||
|
||||
/// `BindingProperty`[Yield, Await] :
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
use std::cell::Cell;
|
||||
|
||||
use oxc_allocator::Box;
|
||||
use oxc_ast::ast::*;
|
||||
use oxc_diagnostics::Result;
|
||||
|
|
@ -77,7 +75,7 @@ impl<'a> ParserImpl<'a> {
|
|||
}
|
||||
let (span, name) = self.parse_identifier_kind(Kind::Ident);
|
||||
self.check_identifier(span, &name);
|
||||
Ok(BindingIdentifier { span, name, symbol_id: Cell::default() })
|
||||
Ok(self.ast.binding_identifier(span, name))
|
||||
}
|
||||
|
||||
pub(crate) fn parse_label_identifier(&mut self) -> Result<LabelIdentifier<'a>> {
|
||||
|
|
@ -86,7 +84,7 @@ impl<'a> ParserImpl<'a> {
|
|||
}
|
||||
let (span, name) = self.parse_identifier_kind(Kind::Ident);
|
||||
self.check_identifier(span, &name);
|
||||
Ok(LabelIdentifier { span, name })
|
||||
Ok(self.ast.label_identifier(span, name))
|
||||
}
|
||||
|
||||
pub(crate) fn parse_identifier_name(&mut self) -> Result<IdentifierName<'a>> {
|
||||
|
|
@ -94,13 +92,13 @@ impl<'a> ParserImpl<'a> {
|
|||
return Err(self.unexpected());
|
||||
}
|
||||
let (span, name) = self.parse_identifier_kind(Kind::Ident);
|
||||
Ok(IdentifierName { span, name })
|
||||
Ok(self.ast.identifier_name(span, name))
|
||||
}
|
||||
|
||||
/// Parse keyword kind as identifier
|
||||
pub(crate) fn parse_keyword_identifier(&mut self, kind: Kind) -> IdentifierName<'a> {
|
||||
let (span, name) = self.parse_identifier_kind(kind);
|
||||
IdentifierName { span, name }
|
||||
self.ast.identifier_name(span, name)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
|
@ -130,7 +128,7 @@ impl<'a> ParserImpl<'a> {
|
|||
let span = self.start_span();
|
||||
let name = Atom::from(self.cur_string());
|
||||
self.bump_any();
|
||||
PrivateIdentifier { span: self.end_span(span), name }
|
||||
self.ast.private_identifier(self.end_span(span), name)
|
||||
}
|
||||
|
||||
/// Section [Primary Expression](https://tc39.es/ecma262/#sec-primary-expression)
|
||||
|
|
@ -277,13 +275,13 @@ impl<'a> ParserImpl<'a> {
|
|||
_ => return Err(self.unexpected()),
|
||||
};
|
||||
self.bump_any();
|
||||
Ok(BooleanLiteral { span: self.end_span(span), value })
|
||||
Ok(self.ast.boolean_literal(self.end_span(span), value))
|
||||
}
|
||||
|
||||
pub(crate) fn parse_literal_null(&mut self) -> NullLiteral {
|
||||
let span = self.start_span();
|
||||
self.bump_any(); // bump `null`
|
||||
NullLiteral { span: self.end_span(span) }
|
||||
self.ast.null_literal(self.end_span(span))
|
||||
}
|
||||
|
||||
pub(crate) fn parse_literal_number(&mut self) -> Result<NumericLiteral<'a>> {
|
||||
|
|
@ -360,7 +358,7 @@ impl<'a> ParserImpl<'a> {
|
|||
let value = self.cur_string();
|
||||
let span = self.start_span();
|
||||
self.bump_any();
|
||||
Ok(StringLiteral { span: self.end_span(span), value: value.into() })
|
||||
Ok(self.ast.string_literal(self.end_span(span), value))
|
||||
}
|
||||
|
||||
/// Section [Array Expression](https://tc39.es/ecma262/#prod-ArrayLiteral)
|
||||
|
|
@ -400,7 +398,7 @@ impl<'a> ParserImpl<'a> {
|
|||
/// ,
|
||||
/// Elision ,
|
||||
pub(crate) fn parse_elision(&mut self) -> ArrayExpressionElement<'a> {
|
||||
ArrayExpressionElement::Elision(Elision { span: self.cur_token().span() })
|
||||
self.ast.array_expression_element_elision(self.cur_token().span())
|
||||
}
|
||||
|
||||
/// Section [Template Literal](https://tc39.es/ecma262/#prod-TemplateLiteral)
|
||||
|
|
@ -907,12 +905,7 @@ impl<'a> ParserImpl<'a> {
|
|||
let left = self.parse_private_identifier();
|
||||
self.expect(Kind::In)?;
|
||||
let right = self.parse_unary_expression_or_higher(lhs_span)?;
|
||||
Expression::PrivateInExpression(self.ast.alloc(PrivateInExpression {
|
||||
span: self.end_span(lhs_span),
|
||||
left,
|
||||
operator: BinaryOperator::In,
|
||||
right,
|
||||
}))
|
||||
self.ast.expression_private_in(self.end_span(lhs_span), left, BinaryOperator::In, right)
|
||||
} else {
|
||||
self.parse_unary_expression_or_higher(lhs_span)?
|
||||
};
|
||||
|
|
|
|||
|
|
@ -64,7 +64,7 @@ impl<'a> ParserImpl<'a> {
|
|||
self.error(diagnostics::invalid_assignment(span));
|
||||
}
|
||||
|
||||
Ok(TSEnumMember { span, id, initializer })
|
||||
Ok(self.ast.ts_enum_member(span, id, initializer))
|
||||
}
|
||||
|
||||
fn parse_ts_enum_member_name(&mut self) -> Result<TSEnumMemberName<'a>> {
|
||||
|
|
@ -475,7 +475,7 @@ impl<'a> ParserImpl<'a> {
|
|||
self.eat_decorators()?;
|
||||
let this = {
|
||||
let (span, name) = self.parse_identifier_kind(Kind::This);
|
||||
IdentifierName { span, name }
|
||||
self.ast.identifier_name(span, name)
|
||||
};
|
||||
let type_annotation = self.parse_ts_type_annotation()?;
|
||||
Ok(self.ast.ts_this_parameter(self.end_span(span), this, type_annotation))
|
||||
|
|
|
|||
|
|
@ -762,11 +762,7 @@ impl<'a> ParserImpl<'a> {
|
|||
let mut left = TSTypeName::IdentifierReference(self.ast.alloc(ident));
|
||||
while self.eat(Kind::Dot) {
|
||||
let right = self.parse_identifier_name()?;
|
||||
left = TSTypeName::QualifiedName(self.ast.alloc(TSQualifiedName {
|
||||
span: self.end_span(span),
|
||||
left,
|
||||
right,
|
||||
}));
|
||||
left = self.ast.ts_type_name_qualified_name(self.end_span(span), left, right);
|
||||
}
|
||||
Ok(left)
|
||||
}
|
||||
|
|
@ -883,24 +879,22 @@ impl<'a> ParserImpl<'a> {
|
|||
return Ok(if dotdotdot {
|
||||
TSTupleElement::TSRestType(self.ast.alloc(TSRestType {
|
||||
span,
|
||||
type_annotation: TSType::TSNamedTupleMember(self.ast.alloc(
|
||||
TSNamedTupleMember {
|
||||
span: self.end_span(member_span),
|
||||
element_type,
|
||||
label,
|
||||
// TODO: A tuple member cannot be both optional and rest. (TS5085)
|
||||
// See typescript suite <conformance/types/tuple/restTupleElements1.ts>
|
||||
optional,
|
||||
},
|
||||
)),
|
||||
type_annotation: self.ast.ts_type_named_tuple_member(
|
||||
self.end_span(member_span),
|
||||
element_type,
|
||||
label,
|
||||
// TODO: A tuple member cannot be both optional and rest. (TS5085)
|
||||
// See typescript suite <conformance/types/tuple/restTupleElements1.ts>
|
||||
optional,
|
||||
),
|
||||
}))
|
||||
} else {
|
||||
TSTupleElement::TSNamedTupleMember(self.ast.alloc(TSNamedTupleMember {
|
||||
TSTupleElement::from(self.ast.ts_type_named_tuple_member(
|
||||
span,
|
||||
element_type,
|
||||
label,
|
||||
optional,
|
||||
}))
|
||||
))
|
||||
});
|
||||
}
|
||||
self.parse_tuple_element_type()
|
||||
|
|
@ -933,10 +927,7 @@ impl<'a> ParserImpl<'a> {
|
|||
let ty = self.parse_ts_type()?;
|
||||
if let TSType::JSDocNullableType(ty) = ty {
|
||||
if ty.span.start == ty.type_annotation.span().start {
|
||||
Ok(TSTupleElement::TSOptionalType(self.ast.alloc(TSOptionalType {
|
||||
span: ty.span,
|
||||
type_annotation: ty.unbox().type_annotation,
|
||||
})))
|
||||
Ok(self.ast.ts_tuple_element_optional_type(ty.span, ty.unbox().type_annotation))
|
||||
} else {
|
||||
Ok(TSTupleElement::JSDocNullableType(ty))
|
||||
}
|
||||
|
|
@ -1023,7 +1014,7 @@ impl<'a> ParserImpl<'a> {
|
|||
)?;
|
||||
self.expect(Kind::RCurly)?;
|
||||
self.expect(Kind::RCurly)?;
|
||||
Ok(TSImportAttributes { span, elements })
|
||||
Ok(self.ast.ts_import_attributes(span, elements))
|
||||
}
|
||||
|
||||
fn parse_ts_import_attribute(&mut self) -> Result<TSImportAttribute<'a>> {
|
||||
|
|
@ -1035,7 +1026,7 @@ impl<'a> ParserImpl<'a> {
|
|||
|
||||
self.expect(Kind::Colon)?;
|
||||
let value = self.parse_expr()?;
|
||||
Ok(TSImportAttribute { span: self.end_span(span), name, value })
|
||||
Ok(self.ast.ts_import_attribute(self.end_span(span), name, value))
|
||||
}
|
||||
|
||||
fn try_parse_constraint_of_infer_type(&mut self) -> Result<Option<TSType<'a>>> {
|
||||
|
|
@ -1316,11 +1307,7 @@ impl<'a> ParserImpl<'a> {
|
|||
return Err(self.unexpected());
|
||||
}
|
||||
|
||||
Ok(TSIndexSignatureName {
|
||||
span: self.end_span(span),
|
||||
name,
|
||||
type_annotation: type_annotation.unwrap(),
|
||||
})
|
||||
Ok(self.ast.ts_index_signature_name(self.end_span(span), name, type_annotation.unwrap()))
|
||||
}
|
||||
|
||||
pub(crate) fn parse_class_element_modifiers(
|
||||
|
|
|
|||
Loading…
Reference in a new issue