Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

feat(rome_js_parser): instantiation expressions #3147 #4035

Merged
merged 2 commits into from
Dec 20, 2022
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

This file was deleted.

32 changes: 30 additions & 2 deletions crates/rome_js_parser/src/syntax/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,15 @@ pub(super) fn parse_conditional_expr(p: &mut JsParser, context: ExpressionContex
}
}

pub(crate) fn is_at_binary_operator(p: &JsParser, context: ExpressionContext) -> bool {
let cur_kind = p.cur();

match cur_kind {
T![in] => context.is_in_included(),
kind => OperatorPrecedence::try_from_binary_operator(kind).is_some(),
}
}

/// A binary expression such as `2 + 2` or `foo * bar + 2` or a logical expression 'a || b'
fn parse_binary_or_logical_expression(
p: &mut JsParser,
Expand Down Expand Up @@ -768,7 +777,7 @@ fn parse_member_expression_rest(
}
T![<] | T![<<] => {
// only those two possible token in cur position `parse_ts_type_arguments_in_expression` could possibly return a `Present(_)`
if let Present(_) = parse_ts_type_arguments_in_expression(p) {
if let Present(_) = parse_ts_type_arguments_in_expression(p, context) {
let new_marker = lhs.precede(p);
lhs = new_marker.complete(p, JsSyntaxKind::TS_INSTANTIATION_EXPRESSION);
continue;
Expand Down Expand Up @@ -930,6 +939,24 @@ fn parse_static_member_expression(
lhs: CompletedMarker,
operator: JsSyntaxKind,
) -> ParsedSyntax {
// test ts ts_instantiation_expression_property_access
// f<b>?.(c);
// f<b>?.[c];
// (f<b>).c;
// (f<b>)?.c;
// (f<b>)?.[c];
if lhs.kind(p) == TS_INSTANTIATION_EXPRESSION {
// test_err ts ts_instantiation_expression_property_access
// f<b>.c;
// f<b>?.c;
// a?.f<c>.d;
// f<a>.g<b>;
p.error(p.err_builder(
"An instantiation expression cannot be followed by a property access.",
lhs.range(p),
).hint("You can either wrap the instantiation expression in parentheses, or delete the type arguments."));
}

let m = lhs.precede(p);
p.expect(operator);

Expand Down Expand Up @@ -1233,6 +1260,7 @@ pub(crate) fn is_nth_at_expression(p: &mut JsParser, n: usize) -> bool {
| TRUE_KW
| FALSE_KW
| JS_NUMBER_LITERAL
| JS_BIG_INT_LITERAL
| JS_STRING_LITERAL
| NULL_KW => true,
t => t.is_contextual_keyword() || t.is_future_reserved_keyword(),
Expand Down Expand Up @@ -1766,7 +1794,7 @@ fn parse_call_expression_rest(
// a<<T>(arg: T) => number, number, string>();

let type_arguments = if optional_chain_call {
let type_arguments = parse_ts_type_arguments_in_expression(p).ok();
let type_arguments = parse_ts_type_arguments_in_expression(p, context).ok();
if p.cur() == BACKTICK {
// test ts ts_tagged_template_literal
// html<A, B>`abcd`
Expand Down
Loading