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
21 changes: 6 additions & 15 deletions crates/oxc_parser/src/js/arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ struct ArrowFunctionHead<'a> {
return_type: Option<Box<'a, TSTypeAnnotation<'a>>>,
r#async: bool,
span: u32,
has_return_colon: bool,
}

impl<'a> ParserImpl<'a> {
Expand Down Expand Up @@ -248,14 +247,7 @@ impl<'a> ParserImpl<'a> {
self.expect(Kind::Arrow);

self.parse_arrow_function_expression_body(
ArrowFunctionHead {
type_parameters: None,
params,
return_type: None,
r#async,
span,
has_return_colon: false,
},
ArrowFunctionHead { type_parameters: None, params, return_type: None, r#async, span },
allow_return_type_in_arrow_function,
)
}
Expand All @@ -279,8 +271,7 @@ impl<'a> ParserImpl<'a> {
self.error(diagnostics::ts_arrow_function_this_parameter(this_param.span));
}

let has_return_colon = self.is_ts && self.at(Kind::Colon);
let return_type = self.parse_ts_return_type_annotation(Kind::Arrow, false);
let return_type = if self.is_ts { self.parse_ts_return_type_annotation() } else { None };

self.ctx = self.ctx.and_await(has_await);

Expand All @@ -290,7 +281,7 @@ impl<'a> ParserImpl<'a> {

self.expect(Kind::Arrow);

ArrowFunctionHead { type_parameters, params, return_type, r#async, span, has_return_colon }
ArrowFunctionHead { type_parameters, params, return_type, r#async, span }
}

/// [ConciseBody](https://tc39.es/ecma262/#prod-ConciseBody)
Expand All @@ -303,7 +294,7 @@ impl<'a> ParserImpl<'a> {
arrow_function_head: ArrowFunctionHead<'a>,
allow_return_type_in_arrow_function: bool,
) -> Expression<'a> {
let ArrowFunctionHead { type_parameters, params, return_type, r#async, span, .. } =
let ArrowFunctionHead { type_parameters, params, return_type, r#async, span } =
arrow_function_head;
let has_await = self.ctx.has_await();
let has_yield = self.ctx.has_yield();
Expand Down Expand Up @@ -362,7 +353,7 @@ impl<'a> ParserImpl<'a> {
return None;
}

let has_return_colon = head.has_return_colon;
let has_return_type = head.return_type.is_some();

let body =
self.parse_arrow_function_expression_body(head, allow_return_type_in_arrow_function);
Expand All @@ -381,7 +372,7 @@ impl<'a> ParserImpl<'a> {
// a() ? (b: number, c?: string): void => d() : e
// is determined by isParenthesizedArrowFunctionExpression to unambiguously
// be an arrow expression, so we allow a return type.
if !allow_return_type_in_arrow_function && has_return_colon {
if !allow_return_type_in_arrow_function && has_return_type {
// However, if the arrow function we were able to parse is followed by another colon
// as in:
// a ? (x): string => x : null
Expand Down
3 changes: 1 addition & 2 deletions crates/oxc_parser/src/js/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ impl<'a> ParserImpl<'a> {
self.ctx = self.ctx.and_in(true).and_await(r#async).and_yield(generator);
let type_parameters = self.parse_ts_type_parameters();
let (this_param, params) = self.parse_formal_parameters(func_kind, param_kind);
let return_type =
self.parse_ts_return_type_annotation(Kind::Colon, /* is_type */ true);
let return_type = if self.is_ts { self.parse_ts_return_type_annotation() } else { None };
let body = if self.at(Kind::LCurly) { Some(self.parse_function_body()) } else { None };
self.ctx =
self.ctx.and_in(ctx.has_in()).and_await(ctx.has_await()).and_yield(ctx.has_yield());
Expand Down
53 changes: 13 additions & 40 deletions crates/oxc_parser/src/ts/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ impl<'a> ParserImpl<'a> {
self.parse_formal_parameters(FunctionKind::Declaration, FormalParameterKind::Signature);
let return_type = {
let return_type_span = self.start_span();
let Some(return_type) = self.parse_return_type(Kind::Arrow, /* is_type */ false) else {
return self.unexpected();
};
let return_type = self.parse_return_type();
self.ast.ts_type_annotation(self.end_span(return_type_span), return_type)
};

Expand Down Expand Up @@ -1038,47 +1036,22 @@ impl<'a> ParserImpl<'a> {

pub(crate) fn parse_ts_return_type_annotation(
&mut self,
kind: Kind,
is_type: bool,
) -> Option<Box<'a, TSTypeAnnotation<'a>>> {
if !self.is_ts {
return None;
}
if !self.at(Kind::Colon) {
return None;
}
let span = self.start_span();
self.parse_return_type(kind, is_type)
.map(|return_type| self.ast.alloc_ts_type_annotation(self.end_span(span), return_type))
let return_type = self.parse_return_type();
Some(self.ast.alloc_ts_type_annotation(self.end_span(span), return_type))
}

fn parse_return_type(&mut self, return_kind: Kind, is_type: bool) -> Option<TSType<'a>> {
if self.should_parse_return_type(return_kind, is_type) {
return Some(self.context(
Context::empty(),
Context::DisallowConditionalTypes,
Self::parse_type_or_type_predicate,
));
}
None
}

fn should_parse_return_type(&mut self, return_kind: Kind, _is_type: bool) -> bool {
if return_kind == Kind::Arrow {
self.bump_any();
return true;
}
if self.eat(Kind::Colon) {
return true;
}
// TODO
// if (isType && token() === SyntaxKind.EqualsGreaterThanToken) {
// // This is easy to get backward, especially in type contexts, so parse the type anyway
// parseErrorAtCurrentToken(Diagnostics._0_expected, tokenToString(SyntaxKind.ColonToken));
// nextToken();
// return true;
// }
false
fn parse_return_type(&mut self) -> TSType<'a> {
self.bump_any();
self.context(
Context::empty(),
Context::DisallowConditionalTypes,
Self::parse_type_or_type_predicate,
)
}

fn parse_type_or_type_predicate(&mut self) -> TSType<'a> {
Expand Down Expand Up @@ -1134,7 +1107,7 @@ impl<'a> ParserImpl<'a> {
self.error(diagnostics::ts_constructor_this_parameter(this_param.span));
}
}
let return_type = self.parse_ts_return_type_annotation(Kind::Colon, false);
let return_type = self.parse_ts_return_type_annotation();
self.parse_type_member_semicolon();
match kind {
CallOrConstructorSignature::Call => self.ast.ts_signature_call_signature_declaration(
Expand Down Expand Up @@ -1163,7 +1136,7 @@ impl<'a> ParserImpl<'a> {
let (key, computed) = self.parse_property_name();
let (this_param, params) =
self.parse_formal_parameters(FunctionKind::Declaration, FormalParameterKind::Signature);
let return_type = self.parse_ts_return_type_annotation(Kind::Colon, false);
let return_type = self.parse_ts_return_type_annotation();
self.parse_type_member_semicolon();
if kind == TSMethodSignatureKind::Set {
if let Some(return_type) = return_type.as_ref() {
Expand Down Expand Up @@ -1197,7 +1170,7 @@ impl<'a> ParserImpl<'a> {
let type_parameters = self.parse_ts_type_parameters();
let (this_param, params) = self
.parse_formal_parameters(FunctionKind::Declaration, FormalParameterKind::Signature);
let return_type = self.parse_ts_return_type_annotation(Kind::Colon, true);
let return_type = self.parse_ts_return_type_annotation();
self.parse_type_member_semicolon();
self.ast.ts_signature_method_signature(
self.end_span(span),
Expand Down
Loading