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
11 changes: 11 additions & 0 deletions crates/oxc_parser/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,17 @@ pub fn new_optional_chain(span: Span) -> OxcDiagnostic {
.with_label(span)
}

#[cold]
pub fn invalid_new_optional_chain(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Invalid optional chain from new expression.").with_label(span)
}

#[cold]
pub fn decorator_optional(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Expression must be enclosed in parentheses to be used as a decorator.")
.with_label(span)
}

#[cold]
pub fn for_loop_async_of(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("The left-hand side of a `for...of` statement may not be `async`")
Expand Down
160 changes: 97 additions & 63 deletions crates/oxc_parser/src/js/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,9 @@ impl<'a> ParserImpl<'a> {
if !in_optional_chain {
return lhs;
}
if self.ctx.has_decorator() {
self.error(diagnostics::decorator_optional(lhs.span()));
}
// Add `ChainExpression` to `a?.c?.b<c>`;
if let Expression::TSInstantiationExpression(mut expr) = lhs {
expr.expression = self.map_to_chain_expression(
Expand Down Expand Up @@ -691,7 +694,12 @@ impl<'a> ParserImpl<'a> {
) -> Expression<'a> {
let span = self.start_span();
let lhs = self.parse_primary_expression();
self.parse_member_expression_rest(span, lhs, in_optional_chain)
self.parse_member_expression_rest(
span,
lhs,
in_optional_chain,
/* allow_optional_chain */ true,
)
}

/// Section 13.3 Super Call
Expand Down Expand Up @@ -719,60 +727,60 @@ impl<'a> ParserImpl<'a> {
lhs_span: u32,
lhs: Expression<'a>,
in_optional_chain: &mut bool,
allow_optional_chain: bool,
) -> Expression<'a> {
let mut lhs = lhs;
loop {
if self.fatal_error.is_some() {
return lhs;
}
lhs = match self.cur_kind() {
Kind::Dot => self.parse_static_member_expression(lhs_span, lhs, false),
Kind::QuestionDot => {
*in_optional_chain = true;
match self.peek_kind() {
Kind::LBrack if !self.ctx.has_decorator() => {
self.bump_any(); // bump `?.`
self.parse_computed_member_expression(lhs_span, lhs, true)
}
Kind::PrivateIdentifier => {
self.parse_static_member_expression(lhs_span, lhs, true)
}
kind if kind.is_identifier_name() => {
self.parse_static_member_expression(lhs_span, lhs, true)
}
Kind::Bang
| Kind::LAngle
| Kind::LParen
| Kind::NoSubstitutionTemplate
| Kind::ShiftLeft
| Kind::TemplateHead
| Kind::LBrack => break,
_ => {
return self.unexpected();
}
}
}
// computed member expression is not allowed in decorator
// class C { @dec ["1"]() { } }
// ^
Kind::LBrack if !self.ctx.has_decorator() => {
self.parse_computed_member_expression(lhs_span, lhs, false)
}
Kind::Bang if !self.cur_token().is_on_new_line && self.is_ts => {

let mut question_dot = false;
let is_property_access = if allow_optional_chain && self.at(Kind::QuestionDot) && {
let peek_kind = self.peek_kind();
peek_kind == Kind::LBrack
|| peek_kind.is_identifier_or_keyword()
|| peek_kind.is_template_start_of_tagged_template()
} {
self.bump_any();
*in_optional_chain = true;
question_dot = true;
self.cur_kind().is_identifier_or_keyword()
} else {
self.eat(Kind::Dot)
};

if is_property_access {
lhs = self.parse_static_member_expression(lhs_span, lhs, question_dot);
continue;
}

if (question_dot || !self.ctx.has_decorator()) && self.at(Kind::LBrack) {
lhs = self.parse_computed_member_expression(lhs_span, lhs, question_dot);
continue;
}

if self.cur_kind().is_template_start_of_tagged_template() {
let (expr, type_parameters) =
if let Expression::TSInstantiationExpression(instantiation_expr) = lhs {
let expr = instantiation_expr.unbox();
(expr.expression, Some(expr.type_arguments))
} else {
(lhs, None)
};
lhs =
self.parse_tagged_template(lhs_span, expr, *in_optional_chain, type_parameters);
continue;
}

if !question_dot {
if self.at(Kind::Bang) && !self.cur_token().is_on_new_line && self.is_ts {
self.bump_any();
self.ast.expression_ts_non_null(self.end_span(lhs_span), lhs)
}
kind if kind.is_template_start_of_tagged_template() => {
let (expr, type_parameters) =
if let Expression::TSInstantiationExpression(instantiation_expr) = lhs {
let expr = instantiation_expr.unbox();
(expr.expression, Some(expr.type_arguments))
} else {
(lhs, None)
};
self.parse_tagged_template(lhs_span, expr, *in_optional_chain, type_parameters)
lhs = self.ast.expression_ts_non_null(self.end_span(lhs_span), lhs);
continue;
}
Kind::LAngle | Kind::ShiftLeft => {

if matches!(self.cur_kind(), Kind::LAngle | Kind::ShiftLeft) {
if let Some(Some(arguments)) =
self.try_parse(Self::parse_type_arguments_in_expression)
{
Expand All @@ -783,12 +791,11 @@ impl<'a> ParserImpl<'a> {
);
continue;
}
break;
}
_ => break,
};
}

return lhs;
}
lhs
}

/// Section 13.3 `MemberExpression`
Expand All @@ -799,7 +806,6 @@ impl<'a> ParserImpl<'a> {
lhs: Expression<'a>,
optional: bool,
) -> Expression<'a> {
self.bump_any(); // advance `.` or `?.`
Expression::from(if self.cur_kind() == Kind::PrivateIdentifier {
let private_ident = self.parse_private_identifier();
self.ast.member_expression_private_field_expression(
Expand Down Expand Up @@ -843,10 +849,18 @@ impl<'a> ParserImpl<'a> {
self.fatal_error(diagnostics::new_target(self.end_span(span)))
};
}
let rhs_span = self.start_span();

let rhs_span = self.start_span();
let mut optional = false;
let mut callee = self.parse_member_expression_or_higher(&mut optional);
let mut callee = {
let lhs = self.parse_primary_expression();
self.parse_member_expression_rest(
rhs_span,
lhs,
&mut optional,
/* allow_optional_chain */ false,
)
};

let mut type_arguments = None;
if let Expression::TSInstantiationExpression(instantiation_expr) = callee {
Expand All @@ -855,6 +869,11 @@ impl<'a> ParserImpl<'a> {
callee = instantiation_expr.expression;
}

if self.at(Kind::QuestionDot) {
let error = diagnostics::invalid_new_optional_chain(self.cur_token().span());
return self.fatal_error(error);
}

// parse `new ident` without arguments
let arguments = if self.eat(Kind::LParen) {
// ArgumentList[Yield, Await] :
Expand Down Expand Up @@ -895,34 +914,49 @@ impl<'a> ParserImpl<'a> {
) -> Expression<'a> {
let mut lhs = lhs;
while self.fatal_error.is_none() {
let mut type_arguments = None;
lhs = self.parse_member_expression_rest(lhs_span, lhs, in_optional_chain);
let optional_call = self.eat(Kind::QuestionDot);
*in_optional_chain = if optional_call { true } else { *in_optional_chain };
lhs = self.parse_member_expression_rest(
lhs_span,
lhs,
in_optional_chain,
/* allow_optional_chain */ true,
);
let question_dot_span = self.at(Kind::QuestionDot).then(|| self.cur_token().span());
let question_dot = question_dot_span.is_some();
if question_dot {
self.bump_any();
*in_optional_chain = true;
}

if optional_call {
let mut type_arguments = None;
if question_dot {
if let Some(Some(args)) = self.try_parse(Self::parse_type_arguments_in_expression) {
type_arguments = Some(args);
}
if self.cur_kind().is_template_start_of_tagged_template() {
lhs = self.parse_tagged_template(lhs_span, lhs, optional_call, type_arguments);
lhs = self.parse_tagged_template(lhs_span, lhs, question_dot, type_arguments);
continue;
}
}

if type_arguments.is_some() || self.at(Kind::LParen) {
if !optional_call {
if !question_dot {
if let Expression::TSInstantiationExpression(expr) = lhs {
let expr = expr.unbox();
type_arguments.replace(expr.type_arguments);
lhs = expr.expression;
}
}

lhs =
self.parse_call_arguments(lhs_span, lhs, optional_call, type_arguments.take());
lhs = self.parse_call_arguments(lhs_span, lhs, question_dot, type_arguments.take());
continue;
}

if let Some(span) = question_dot_span {
// We parsed `?.` but then failed to parse anything, so report a missing identifier here.
let error = diagnostics::unexpected_token(span);
return self.fatal_error(error);
}

break;
}

Expand Down
4 changes: 2 additions & 2 deletions tasks/coverage/snapshots/codegen_typescript.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
commit: 15392346

codegen_typescript Summary:
AST Parsed : 6531/6531 (100.00%)
Positive Passed: 6531/6531 (100.00%)
AST Parsed : 6528/6528 (100.00%)
Positive Passed: 6528/6528 (100.00%)
4 changes: 2 additions & 2 deletions tasks/coverage/snapshots/estree_typescript.snap
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
commit: 15392346

estree_typescript Summary:
AST Parsed : 6481/6481 (100.00%)
Positive Passed: 6478/6481 (99.95%)
AST Parsed : 6480/6480 (100.00%)
Positive Passed: 6477/6480 (99.95%)
Mismatch: tasks/coverage/typescript/tests/cases/conformance/jsx/jsxReactTestSuite.tsx

Mismatch: tasks/coverage/typescript/tests/cases/conformance/jsx/tsxReactEmitEntities.tsx
Expand Down
Loading
Loading