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
13 changes: 12 additions & 1 deletion crates/oxc_parser/src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ impl<'a> ParserImpl<'a> {
&mut self,
close: Kind,
separator: Kind,
opening_span: Span,
f: F,
) -> (Vec<'a, T>, Option<u32>)
where
Expand All @@ -450,7 +451,17 @@ impl<'a> ParserImpl<'a> {
{
return (list, None);
}
self.expect(separator);
if !self.at(separator) {
self.set_fatal_error(diagnostics::expect_closing_or_separator(
close.to_str(),
separator.to_str(),
kind.to_str(),
self.cur_token().span(),
opening_span,
));
return (list, None);
}
self.advance(separator);
if self.cur_kind() == close {
let trailing_separator = self.prev_token_end - 1;
return (list, Some(trailing_separator));
Expand Down
17 changes: 17 additions & 0 deletions crates/oxc_parser/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ pub fn expect_token(x0: &str, x1: &str, span: Span) -> OxcDiagnostic {
.with_label(span.label(format!("`{x0}` expected")))
}

#[cold]
pub fn expect_closing_or_separator(
expected_closing: &str,
expected_separator: &str,
actual: &str,
span: Span,
opening_span: Span,
) -> OxcDiagnostic {
OxcDiagnostic::error(format!(
"Expected `{expected_separator}` or `{expected_closing}` but found `{actual}`"
))
.with_labels([
span.primary_label(format!("`{expected_separator}` or `{expected_closing}` expected")),
opening_span.label("Opened here"),
])
}

#[cold]
pub fn expect_conditional_alternative(x: &str, span: Span, question_span: Span) -> OxcDiagnostic {
OxcDiagnostic::error(format!("Expected `:` but found `{x}`")).with_labels([
Expand Down
35 changes: 31 additions & 4 deletions crates/oxc_parser/src/js/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,14 @@ impl<'a> ParserImpl<'a> {

fn parse_parenthesized_expression(&mut self) -> Expression<'a> {
let span = self.start_span();
let opening_span = self.cur_token().span();
self.bump_any(); // `bump` `(`
let expr_span = self.start_span();
let (mut expressions, comma_span) = self.context(Context::In, Context::Decorator, |p| {
p.parse_delimited_list(
Kind::RParen,
Kind::Comma,
opening_span,
Self::parse_assignment_expression_or_higher,
)
});
Expand Down Expand Up @@ -443,9 +445,15 @@ impl<'a> ParserImpl<'a> {
/// [ `ElementList`[?Yield, ?Await] , Elisionopt ]
pub(crate) fn parse_array_expression(&mut self) -> Expression<'a> {
let span = self.start_span();
let opening_span = self.cur_token().span();
self.expect(Kind::LBrack);
let (elements, comma_span) = self.context_add(Context::In, |p| {
p.parse_delimited_list(Kind::RBrack, Kind::Comma, Self::parse_array_expression_element)
p.parse_delimited_list(
Kind::RBrack,
Kind::Comma,
opening_span,
Self::parse_array_expression_element,
)
});
if let Some(comma_span) = comma_span {
self.state.trailing_commas.insert(span, self.end_span(comma_span));
Expand Down Expand Up @@ -635,9 +643,15 @@ impl<'a> ParserImpl<'a> {
self.expect(Kind::Percent);
let name = self.parse_identifier_name();

let opening_span = self.cur_token().span();
self.expect(Kind::LParen);
let (arguments, _) = self.context(Context::In, Context::Decorator, |p| {
p.parse_delimited_list(Kind::RParen, Kind::Comma, Self::parse_v8_intrinsic_argument)
p.parse_delimited_list(
Kind::RParen,
Kind::Comma,
opening_span,
Self::parse_v8_intrinsic_argument,
)
});
self.expect(Kind::RParen);
self.ast.expression_v_8_intrinsic(self.end_span(span), name, arguments)
Expand Down Expand Up @@ -891,12 +905,19 @@ impl<'a> ParserImpl<'a> {
return self.fatal_error(error);
}

let opening_span = self.cur_token().span();

// parse `new ident` without arguments
let arguments = if self.eat(Kind::LParen) {
// ArgumentList[Yield, Await] :
// AssignmentExpression[+In, ?Yield, ?Await]
let (call_arguments, _) = self.context_add(Context::In, |p| {
p.parse_delimited_list(Kind::RParen, Kind::Comma, Self::parse_call_argument)
p.parse_delimited_list(
Kind::RParen,
Kind::Comma,
opening_span,
Self::parse_call_argument,
)
});
self.expect(Kind::RParen);
call_arguments
Expand Down Expand Up @@ -984,9 +1005,15 @@ impl<'a> ParserImpl<'a> {
) -> Expression<'a> {
// ArgumentList[Yield, Await] :
// AssignmentExpression[+In, ?Yield, ?Await]
let opening_span = self.cur_token().span();
self.expect(Kind::LParen);
let (call_arguments, _) = self.context(Context::In, Context::Decorator, |p| {
p.parse_delimited_list(Kind::RParen, Kind::Comma, Self::parse_call_argument)
p.parse_delimited_list(
Kind::RParen,
Kind::Comma,
opening_span,
Self::parse_call_argument,
)
});
self.expect(Kind::RParen);
self.ast.expression_call(
Expand Down
14 changes: 11 additions & 3 deletions crates/oxc_parser/src/js/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,10 @@ impl<'a> ParserImpl<'a> {
&mut self,
import_kind: ImportOrExportKind,
) -> Vec<'a, ImportDeclarationSpecifier<'a>> {
let opening_span = self.cur_token().span();
self.expect(Kind::LCurly);
let (list, _) = self.context_remove(self.ctx, |p| {
p.parse_delimited_list(Kind::RCurly, Kind::Comma, |parser| {
p.parse_delimited_list(Kind::RCurly, Kind::Comma, opening_span, |parser| {
parser.parse_import_specifier(import_kind)
})
});
Expand All @@ -301,9 +302,15 @@ impl<'a> ParserImpl<'a> {
self.bump_remap(keyword_kind);

let span = self.start_span();
let opening_span = self.cur_token().span();
self.expect(Kind::LCurly);
let (with_entries, _) = self.context_remove(self.ctx, |p| {
p.parse_delimited_list(Kind::RCurly, Kind::Comma, Self::parse_import_attribute)
p.parse_delimited_list(
Kind::RCurly,
Kind::Comma,
opening_span,
Self::parse_import_attribute,
)
});
self.expect(Kind::RCurly);

Expand Down Expand Up @@ -477,9 +484,10 @@ impl<'a> ParserImpl<'a> {
stmt_ctx: StatementContext,
) -> Box<'a, ExportNamedDeclaration<'a>> {
let export_kind = self.parse_import_or_export_kind();
let opening_span = self.cur_token().span();
self.expect(Kind::LCurly);
let (mut specifiers, _) = self.context_remove(self.ctx, |p| {
p.parse_delimited_list(Kind::RCurly, Kind::Comma, |parser| {
p.parse_delimited_list(Kind::RCurly, Kind::Comma, opening_span, |parser| {
parser.parse_export_specifier(export_kind)
})
});
Expand Down
2 changes: 2 additions & 0 deletions crates/oxc_parser/src/js/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ impl<'a> ParserImpl<'a> {
/// { `PropertyDefinitionList`[?Yield, ?Await] , }
pub(crate) fn parse_object_expression(&mut self) -> Box<'a, ObjectExpression<'a>> {
let span = self.start_span();
let opening_span = self.cur_token().span();
self.expect(Kind::LCurly);
let (object_expression_properties, comma_span) = self.context_add(Context::In, |p| {
p.parse_delimited_list(
Kind::RCurly,
Kind::Comma,
opening_span,
Self::parse_object_expression_property,
)
});
Expand Down
9 changes: 7 additions & 2 deletions crates/oxc_parser/src/ts/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,14 @@ impl<'a> ParserImpl<'a> {

pub(crate) fn parse_ts_enum_body(&mut self) -> TSEnumBody<'a> {
let span = self.start_span();
let opening_span = self.cur_token().span();
self.expect(Kind::LCurly);
let (members, _) =
self.parse_delimited_list(Kind::RCurly, Kind::Comma, Self::parse_ts_enum_member);
let (members, _) = self.parse_delimited_list(
Kind::RCurly,
Kind::Comma,
opening_span,
Self::parse_ts_enum_member,
);
self.expect(Kind::RCurly);
self.ast.ts_enum_body(self.end_span(span), members)
}
Expand Down
42 changes: 33 additions & 9 deletions crates/oxc_parser/src/ts/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,14 @@ impl<'a> ParserImpl<'a> {
return None;
}
let span = self.start_span();
let opening_span = self.cur_token().span();
self.expect(Kind::LAngle);
let (params, _) =
self.parse_delimited_list(Kind::RAngle, Kind::Comma, Self::parse_ts_type_parameter);
let (params, _) = self.parse_delimited_list(
Kind::RAngle,
Kind::Comma,
opening_span,
Self::parse_ts_type_parameter,
);
self.expect(Kind::RAngle);
let span = self.end_span(span);
if params.is_empty() {
Expand Down Expand Up @@ -806,9 +811,14 @@ impl<'a> ParserImpl<'a> {
) -> Option<Box<'a, TSTypeParameterInstantiation<'a>>> {
if self.at(Kind::LAngle) {
let span = self.start_span();
let opening_span = self.cur_token().span();
self.expect(Kind::LAngle);
let (params, _) =
self.parse_delimited_list(Kind::RAngle, Kind::Comma, Self::parse_ts_type);
let (params, _) = self.parse_delimited_list(
Kind::RAngle,
Kind::Comma,
opening_span,
Self::parse_ts_type,
);
self.expect(Kind::RAngle);
let span = self.end_span(span);
if params.is_empty() {
Expand All @@ -824,9 +834,14 @@ impl<'a> ParserImpl<'a> {
) -> Option<Box<'a, TSTypeParameterInstantiation<'a>>> {
if !self.cur_token().is_on_new_line() && self.re_lex_ts_l_angle() {
let span = self.start_span();
let opening_span = self.cur_token().span();
self.expect(Kind::LAngle);
let (params, _) =
self.parse_delimited_list(Kind::RAngle, Kind::Comma, Self::parse_ts_type);
let (params, _) = self.parse_delimited_list(
Kind::RAngle,
Kind::Comma,
opening_span,
Self::parse_ts_type,
);
self.expect(Kind::RAngle);
let span = self.end_span(span);
if params.is_empty() {
Expand All @@ -844,8 +859,10 @@ impl<'a> ParserImpl<'a> {
if !self.re_lex_ts_l_angle() {
return self.unexpected();
}
let opening_span = self.cur_token().span();
self.expect(Kind::LAngle);
let (params, _) = self.parse_delimited_list(Kind::RAngle, Kind::Comma, Self::parse_ts_type);
let (params, _) =
self.parse_delimited_list(Kind::RAngle, Kind::Comma, opening_span, Self::parse_ts_type);
// `a < b> = c`` is valid but `a < b >= c` is BinaryExpression
if matches!(self.re_lex_right_angle(), Kind::GtEq) {
return self.unexpected();
Expand Down Expand Up @@ -876,9 +893,14 @@ impl<'a> ParserImpl<'a> {

fn parse_tuple_type(&mut self) -> TSType<'a> {
let span = self.start_span();
let opening_span = self.cur_token().span();
self.expect(Kind::LBrack);
let (elements, _) =
self.parse_delimited_list(Kind::RBrack, Kind::Comma, Self::parse_tuple_element);
let (elements, _) = self.parse_delimited_list(
Kind::RBrack,
Kind::Comma,
opening_span,
Self::parse_tuple_element,
);
self.expect(Kind::RBrack);
self.ast.ts_type_tuple_type(self.end_span(span), elements)
}
Expand Down Expand Up @@ -1216,10 +1238,12 @@ impl<'a> ParserImpl<'a> {
span: u32,
modifiers: &Modifiers<'a>,
) -> Box<'a, TSIndexSignature<'a>> {
let opening_span = self.cur_token().span();
self.expect(Kind::LBrack);
let (params, comma_span) = self.parse_delimited_list(
Kind::RBrack,
Kind::Comma,
opening_span,
Self::parse_ts_index_signature_name,
);
if let Some(comma_span) = comma_span {
Expand Down
2 changes: 2 additions & 0 deletions tasks/coverage/misc/fail/imbalanced-array-expr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const foo = [0, 1
const bar = 2
2 changes: 2 additions & 0 deletions tasks/coverage/misc/fail/imbalanced-call-expr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const foo = bar(1
const bar = 2;
4 changes: 4 additions & 0 deletions tasks/coverage/misc/fail/imbalanced-enum-expr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
enum Foo {
A

const bar = 1;
3 changes: 3 additions & 0 deletions tasks/coverage/misc/fail/imbalanced-export-expr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { foo

const foo = ""
1 change: 1 addition & 0 deletions tasks/coverage/misc/fail/imbalanced-import-decl-attr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { } from "foo" with { "type": "json";
1 change: 1 addition & 0 deletions tasks/coverage/misc/fail/imbalanced-import-decl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { from "foo";
3 changes: 3 additions & 0 deletions tasks/coverage/misc/fail/imbalanced-index-signature-decl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type Foo = {
[key: string: number;
}
2 changes: 2 additions & 0 deletions tasks/coverage/misc/fail/imbalanced-new-expr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const foo = new Foo(1
const bar = 2;
2 changes: 2 additions & 0 deletions tasks/coverage/misc/fail/imbalanced-object-expr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const foo = { a: 1
const bar = 2;
1 change: 1 addition & 0 deletions tasks/coverage/misc/fail/imbalanced-parenthesized-expr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const foo = (0, eval('1 + 2');
3 changes: 3 additions & 0 deletions tasks/coverage/misc/fail/imbalanced-tuple-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type A = [number

const bar = 1;
Loading
Loading