From 89b7416ff796153eac85671c30b4dbab8463c536 Mon Sep 17 00:00:00 2001 From: nissy-dev Date: Mon, 26 Dec 2022 09:25:58 +0900 Subject: [PATCH 1/3] fix: refactor --- crates/rome_js_parser/src/state.rs | 29 ---- crates/rome_js_parser/src/syntax/expr.rs | 4 +- crates/rome_js_parser/src/syntax/stmt.rs | 2 +- .../rome_js_parser/src/syntax/typescript.rs | 2 +- .../src/syntax/typescript/statement.rs | 3 +- .../src/syntax/typescript/types.rs | 145 +++++++++++------- 6 files changed, 96 insertions(+), 89 deletions(-) diff --git a/crates/rome_js_parser/src/state.rs b/crates/rome_js_parser/src/state.rs index 07839dca0f8..bea8a043b45 100644 --- a/crates/rome_js_parser/src/state.rs +++ b/crates/rome_js_parser/src/state.rs @@ -164,12 +164,6 @@ impl ParserState { .contains(ParsingContextFlags::BREAK_ALLOWED) } - pub fn allow_conditional_type(&self) -> bool { - !self - .parsing_context - .contains(ParsingContextFlags::DISALLOW_CONDITIONAL_TYPE) - } - pub fn strict(&self) -> Option<&StrictMode> { self.strict.as_ref() } @@ -403,8 +397,6 @@ bitflags! { /// Whatever the parser is in a TypeScript ambient context const AMBIENT_CONTEXT = 1 << 7; - const DISALLOW_CONDITIONAL_TYPE = 1 << 8; - const LOOP = Self::BREAK_ALLOWED.bits | Self::CONTINUE_ALLOWED.bits; /// Bitmask of all the flags that must be reset (shouldn't be inherited) when the parser enters a function @@ -588,27 +580,6 @@ impl ChangeParserStateFlags for EnterType { } } -pub(crate) struct EnterConditionalTypes(bool); - -impl EnterConditionalTypes { - pub(crate) const fn allow() -> Self { - Self(true) - } - pub(crate) const fn disallow() -> Self { - Self(false) - } -} - -impl ChangeParserStateFlags for EnterConditionalTypes { - fn compute_new_flags(&self, existing: ParsingContextFlags) -> ParsingContextFlags { - if self.0 { - existing - ParsingContextFlags::DISALLOW_CONDITIONAL_TYPE - } else { - existing | ParsingContextFlags::DISALLOW_CONDITIONAL_TYPE - } - } -} - #[derive(Default)] pub(crate) struct EnterAmbientContextSnapshot { flags: ParsingContextFlags, diff --git a/crates/rome_js_parser/src/syntax/expr.rs b/crates/rome_js_parser/src/syntax/expr.rs index 682524473a8..11968fbcf74 100644 --- a/crates/rome_js_parser/src/syntax/expr.rs +++ b/crates/rome_js_parser/src/syntax/expr.rs @@ -583,7 +583,7 @@ fn parse_binary_or_logical_expression_recursive( // as; // let precedence = "hello" as const + 3 as number as number; if op == T![as] { - parse_ts_type(p).or_add_diagnostic(p, expected_ts_type); + parse_ts_type(p, TypeContext::default()).or_add_diagnostic(p, expected_ts_type); let mut as_expression = m.complete(p, TS_AS_EXPRESSION); if TypeScript.is_unsupported(p) { @@ -612,7 +612,7 @@ fn parse_binary_or_logical_expression_recursive( // test_err ts_satisfies_expression // let x = "hello" satisfies string; if op == T![satisfies] { - parse_ts_type(p).or_add_diagnostic(p, expected_ts_type); + parse_ts_type(p, TypeContext::default()).or_add_diagnostic(p, expected_ts_type); let mut satisfies_expression = m.complete(p, TS_SATISFIES_EXPRESSION); if TypeScript.is_unsupported(p) { diff --git a/crates/rome_js_parser/src/syntax/stmt.rs b/crates/rome_js_parser/src/syntax/stmt.rs index 1c6a8e5b7ce..d686badfad2 100644 --- a/crates/rome_js_parser/src/syntax/stmt.rs +++ b/crates/rome_js_parser/src/syntax/stmt.rs @@ -1835,7 +1835,7 @@ fn parse_catch_declaration(p: &mut JsParser) -> ParsedSyntax { let annotation = p.start(); p.bump(T![:]); - if let Some(ty) = parse_ts_type(p).or_add_diagnostic(p, expected_ts_type) { + if let Some(ty) = parse_ts_type(p, TypeContext::default()).or_add_diagnostic(p, expected_ts_type) { if !matches!(ty.kind(p), TS_ANY_TYPE | TS_UNKNOWN_TYPE) { p.error( p diff --git a/crates/rome_js_parser/src/syntax/typescript.rs b/crates/rome_js_parser/src/syntax/typescript.rs index 7b60214523b..8574c336cad 100644 --- a/crates/rome_js_parser/src/syntax/typescript.rs +++ b/crates/rome_js_parser/src/syntax/typescript.rs @@ -73,7 +73,7 @@ pub(crate) fn parse_ts_type_assertion_expression( let m = p.start(); p.bump(T![<]); - parse_ts_type(p).or_add_diagnostic(p, expected_ts_type); + parse_ts_type(p, TypeContext::default()).or_add_diagnostic(p, expected_ts_type); p.expect(T![>]); parse_unary_expr(p, context).or_add_diagnostic(p, expected_expression); Present(m.complete(p, TS_TYPE_ASSERTION_EXPRESSION)) diff --git a/crates/rome_js_parser/src/syntax/typescript/statement.rs b/crates/rome_js_parser/src/syntax/typescript/statement.rs index 523a5e3b7f3..2c6bbc0eaa0 100644 --- a/crates/rome_js_parser/src/syntax/typescript/statement.rs +++ b/crates/rome_js_parser/src/syntax/typescript/statement.rs @@ -7,6 +7,7 @@ use crate::syntax::class::parse_initializer_clause; use crate::syntax::expr::{is_nth_at_identifier, parse_name, ExpressionContext}; use super::ts_parse_error::expected_ts_enum_member; +use super::TypeContext; use crate::state::EnterAmbientContext; use crate::syntax::auxiliary::{is_nth_at_declaration_clause, parse_declaration_clause}; use crate::syntax::js_parse_error::{expected_identifier, expected_module_source}; @@ -209,7 +210,7 @@ pub(crate) fn parse_ts_type_alias_declaration(p: &mut JsParser) -> ParsedSyntax .or_add_diagnostic(p, expected_identifier); parse_ts_type_parameters(p).ok(); p.expect(T![=]); - parse_ts_type(p).or_add_diagnostic(p, expected_ts_type); + parse_ts_type(p, TypeContext::default()).or_add_diagnostic(p, expected_ts_type); semi(p, TextRange::new(start, p.cur_range().end())); diff --git a/crates/rome_js_parser/src/syntax/typescript/types.rs b/crates/rome_js_parser/src/syntax/typescript/types.rs index 1d577afe21a..b10670e56ec 100644 --- a/crates/rome_js_parser/src/syntax/typescript/types.rs +++ b/crates/rome_js_parser/src/syntax/typescript/types.rs @@ -1,6 +1,6 @@ use crate::parser::{RecoveryError, RecoveryResult}; use crate::prelude::*; -use crate::state::{EnterConditionalTypes, EnterType, SignatureFlags}; +use crate::state::{EnterType, SignatureFlags}; use crate::syntax::expr::{ is_at_identifier, is_nth_at_identifier, is_nth_at_identifier_or_keyword, parse_big_int_literal_expression, parse_identifier, parse_literal_expression, parse_name, @@ -26,12 +26,44 @@ use crate::lexer::{LexContext, ReLexContext}; use crate::span::Span; use crate::JsSyntaxFeature::TypeScript; use crate::{Absent, JsParser, ParseRecovery, ParsedSyntax, Present}; +use bitflags::bitflags; use rome_js_syntax::JsSyntaxKind::TS_TYPE_ANNOTATION; use rome_js_syntax::T; use rome_js_syntax::{JsSyntaxKind::*, *}; use super::{expect_ts_index_signature_member, is_at_ts_index_signature_member, MemberParent}; +#[derive(Debug, Clone, Copy, Eq, PartialEq)] +pub(crate) struct TypeContext(TypeContextFlags); + +bitflags! { + struct TypeContextFlags: u8 { + /// TODO: write the description + const ALLOW_CONDITIONAL_TYPE = 1 << 0; + } +} + +impl TypeContext { + pub(crate) fn and_conditional_type_allowed(self, allowed: bool) -> Self { + self.and(TypeContextFlags::ALLOW_CONDITIONAL_TYPE, allowed) + } + + pub(crate) const fn is_conditional_type_allowed(&self) -> bool { + self.0.contains(TypeContextFlags::ALLOW_CONDITIONAL_TYPE) + } + + /// Adds the `flag` if `set` is `true`, otherwise removes the `flag` + fn and(self, flag: TypeContextFlags, set: bool) -> Self { + TypeContext(if set { self.0 | flag } else { self.0 - flag }) + } +} + +impl Default for TypeContext { + fn default() -> Self { + TypeContext(TypeContextFlags::ALLOW_CONDITIONAL_TYPE) + } +} + pub(crate) fn is_reserved_type_name(name: &str) -> bool { name.len() <= 6 && name.len() >= 3 @@ -62,7 +94,7 @@ pub(crate) fn parse_ts_type_annotation(p: &mut JsParser) -> ParsedSyntax { let m = p.start(); p.bump(T![:]); - parse_ts_type(p).or_add_diagnostic(p, expected_ts_type); + parse_ts_type(p, TypeContext::default()).or_add_diagnostic(p, expected_ts_type); Present(m.complete(p, TS_TYPE_ANNOTATION)) } @@ -124,7 +156,7 @@ impl ParseSeparatedList for TsTypeParameterList { const LIST_KIND: Self::Kind = TS_TYPE_PARAMETER_LIST; fn parse_element(&mut self, p: &mut JsParser) -> ParsedSyntax { - parse_ts_type_parameter(p) + parse_ts_type_parameter(p, TypeContext::default()) } fn is_at_list_end(&self, p: &mut JsParser) -> bool { @@ -152,10 +184,10 @@ impl ParseSeparatedList for TsTypeParameterList { } } -fn parse_ts_type_parameter(p: &mut JsParser) -> ParsedSyntax { +fn parse_ts_type_parameter(p: &mut JsParser, context: TypeContext) -> ParsedSyntax { parse_ts_type_parameter_name(p).map(|name| { let m = name.precede(p); - parse_ts_type_constraint_clause(p).ok(); + parse_ts_type_constraint_clause(p, context).ok(); parse_ts_default_type_clause(p).ok(); m.complete(p, TS_TYPE_PARAMETER) }) @@ -164,7 +196,7 @@ fn parse_ts_type_parameter(p: &mut JsParser) -> ParsedSyntax { // test ts ts_type_constraint_clause // type A = X; // type B = { a: X } -fn parse_ts_type_constraint_clause(p: &mut JsParser) -> ParsedSyntax { +fn parse_ts_type_constraint_clause(p: &mut JsParser, context: TypeContext) -> ParsedSyntax { if !p.at(T![extends]) { return Absent; } @@ -172,7 +204,7 @@ fn parse_ts_type_constraint_clause(p: &mut JsParser) -> ParsedSyntax { let m = p.start(); p.expect(T![extends]); - parse_ts_type(p).or_add_diagnostic(p, expected_ts_type); + parse_ts_type(p, context).or_add_diagnostic(p, expected_ts_type); Present(m.complete(p, TS_TYPE_CONSTRAINT_CLAUSE)) } @@ -186,7 +218,7 @@ fn parse_ts_default_type_clause(p: &mut JsParser) -> ParsedSyntax { let m = p.start(); p.bump(T![=]); - parse_ts_type(p).or_add_diagnostic(p, expected_ts_type); + parse_ts_type(p, TypeContext::default()).or_add_diagnostic(p, expected_ts_type); Present(m.complete(p, TS_DEFAULT_TYPE_CLAUSE)) } @@ -195,21 +227,21 @@ fn is_nth_at_ts_type_parameters(p: &mut JsParser, n: usize) -> bool { } #[inline(always)] -pub(crate) fn parse_ts_type(p: &mut JsParser) -> ParsedSyntax { +pub(crate) fn parse_ts_type(p: &mut JsParser, context: TypeContext) -> ParsedSyntax { p.with_state(EnterType, |p| { if is_at_constructor_type(p) { - return p.with_state(EnterConditionalTypes::allow(), parse_ts_constructor_type); + return parse_ts_constructor_type(p); } if is_at_function_type(p) { - return p.with_state(EnterConditionalTypes::allow(), parse_ts_function_type); + return parse_ts_function_type(p); } - let left = parse_ts_union_type_or_higher(p); + let left = parse_ts_union_type_or_higher(p, context); // test ts ts_conditional_type_call_signature_lhs // type X = V extends (...args: any[]) => any ? (...args: Parameters) => void : Function; - if p.state().allow_conditional_type() { + if context.is_conditional_type_allowed() { left.map(|left| { // test ts ts_conditional_type // type A = number; @@ -226,12 +258,14 @@ pub(crate) fn parse_ts_type(p: &mut JsParser) -> ParsedSyntax { let m = left.precede(p); p.expect(T![extends]); - p.with_state(EnterConditionalTypes::disallow(), parse_ts_type) + parse_ts_type(p, context.and_conditional_type_allowed(false)) .or_add_diagnostic(p, expected_ts_type); p.expect(T![?]); - parse_ts_type(p).or_add_diagnostic(p, expected_ts_type); + parse_ts_type(p, context.and_conditional_type_allowed(true)) + .or_add_diagnostic(p, expected_ts_type); p.expect(T![:]); - parse_ts_type(p).or_add_diagnostic(p, expected_ts_type); + parse_ts_type(p, context.and_conditional_type_allowed(true)) + .or_add_diagnostic(p, expected_ts_type); m.complete(p, TS_CONDITIONAL_TYPE) } else { left @@ -247,15 +281,15 @@ pub(crate) fn parse_ts_type(p: &mut JsParser) -> ParsedSyntax { // type A = string | number; // type B = | A | void | null; // type C = A & C | C; -fn parse_ts_union_type_or_higher(p: &mut JsParser) -> ParsedSyntax { - parse_ts_union_or_intersection_type(p, IntersectionOrUnionType::Union) +fn parse_ts_union_type_or_higher(p: &mut JsParser, context: TypeContext) -> ParsedSyntax { + parse_ts_union_or_intersection_type(p, IntersectionOrUnionType::Union, context) } // test ts ts_intersection_type // type A = string & number; // type B = & A & void & null; -fn parse_ts_intersection_type_or_higher(p: &mut JsParser) -> ParsedSyntax { - parse_ts_union_or_intersection_type(p, IntersectionOrUnionType::Intersection) +fn parse_ts_intersection_type_or_higher(p: &mut JsParser, context: TypeContext) -> ParsedSyntax { + parse_ts_union_or_intersection_type(p, IntersectionOrUnionType::Intersection, context) } #[derive(Debug, Copy, Clone, Eq, PartialEq)] @@ -290,10 +324,10 @@ impl IntersectionOrUnionType { } #[inline] - fn parse_element(&self, p: &mut JsParser) -> ParsedSyntax { + fn parse_element(&self, p: &mut JsParser, context: TypeContext) -> ParsedSyntax { match self { - IntersectionOrUnionType::Union => parse_ts_intersection_type_or_higher(p), - IntersectionOrUnionType::Intersection => parse_ts_primary_type(p), + IntersectionOrUnionType::Union => parse_ts_intersection_type_or_higher(p, context), + IntersectionOrUnionType::Intersection => parse_ts_primary_type(p, context), } } } @@ -302,6 +336,7 @@ impl IntersectionOrUnionType { fn parse_ts_union_or_intersection_type( p: &mut JsParser, ty_kind: IntersectionOrUnionType, + context: TypeContext, ) -> ParsedSyntax { // Leading operator: `& A & B` if p.at(ty_kind.operator()) { @@ -309,21 +344,21 @@ fn parse_ts_union_or_intersection_type( p.bump(ty_kind.operator()); let list = p.start(); ty_kind - .parse_element(p) + .parse_element(p, context) .or_add_diagnostic(p, expected_ts_type); - eat_ts_union_or_intersection_type_elements(p, ty_kind); + eat_ts_union_or_intersection_type_elements(p, ty_kind, context); list.complete(p, ty_kind.list_kind()); Present(m.complete(p, ty_kind.kind())) } else { - let first = ty_kind.parse_element(p); + let first = ty_kind.parse_element(p, context); if p.at(ty_kind.operator()) { let list = first.precede(p); - eat_ts_union_or_intersection_type_elements(p, ty_kind); + eat_ts_union_or_intersection_type_elements(p, ty_kind, context); let completed_list = list.complete(p, ty_kind.list_kind()); let m = completed_list.precede(p); @@ -336,17 +371,21 @@ fn parse_ts_union_or_intersection_type( } #[inline] -fn eat_ts_union_or_intersection_type_elements(p: &mut JsParser, ty_kind: IntersectionOrUnionType) { +fn eat_ts_union_or_intersection_type_elements( + p: &mut JsParser, + ty_kind: IntersectionOrUnionType, + context: TypeContext, +) { while p.at(ty_kind.operator()) { p.bump(ty_kind.operator()); ty_kind - .parse_element(p) + .parse_element(p, context) .or_add_diagnostic(p, expected_ts_type); } } -fn parse_ts_primary_type(p: &mut JsParser) -> ParsedSyntax { +fn parse_ts_primary_type(p: &mut JsParser, context: TypeContext) -> ParsedSyntax { // test ts ts_inferred_type // type A = infer B; // type B = { a: infer U; b: infer U}; @@ -354,7 +393,7 @@ fn parse_ts_primary_type(p: &mut JsParser) -> ParsedSyntax { let m = p.start(); p.expect(T![infer]); parse_ts_type_parameter_name(p).or_add_diagnostic(p, expected_identifier); - try_parse_constraint_of_infer_type(p).ok(); + try_parse_constraint_of_infer_type(p, context).ok(); return Present(m.complete(p, TS_INFER_TYPE)); } @@ -367,30 +406,26 @@ fn parse_ts_primary_type(p: &mut JsParser) -> ParsedSyntax { if is_type_operator { let m = p.start(); p.bump_any(); - p.with_state(EnterConditionalTypes::allow(), parse_ts_primary_type) - .or_add_diagnostic(p, expected_ts_type); + parse_ts_primary_type(p, TypeContext::default()).or_add_diagnostic(p, expected_ts_type); return Present(m.complete(p, TS_TYPE_OPERATOR_TYPE)); } - p.with_state(EnterConditionalTypes::allow(), parse_postfix_type_or_higher) + parse_postfix_type_or_higher(p) } -fn try_parse_constraint_of_infer_type(p: &mut JsParser) -> ParsedSyntax { +fn try_parse_constraint_of_infer_type(p: &mut JsParser, context: TypeContext) -> ParsedSyntax { if !p.at(T![extends]) { return Absent; } try_parse(p, |p| { - let parsed = p - .with_state( - EnterConditionalTypes::disallow(), - parse_ts_type_constraint_clause, - ) - .expect("Type constraint clause because parser is positioned at expect clause"); + let parsed = + parse_ts_type_constraint_clause(p, context.and_conditional_type_allowed(false)) + .expect("Type constraint clause because parser is positioned at expect clause"); // Rewind if conditional types are allowed, and the parser is at the `?` token because // this should instead be parsed as a conditional type. - if p.state.allow_conditional_type() && p.at(T![?]) { + if context.is_conditional_type_allowed() && p.at(T![?]) { Err(()) } else { Ok(Present(parsed)) @@ -407,7 +442,7 @@ fn parse_postfix_type_or_higher(p: &mut JsParser) -> ParsedSyntax { let m = left.precede(p); p.bump(T!['[']); - left = if parse_ts_type(p).is_present() { + left = if parse_ts_type(p, TypeContext::default()).is_present() { // test ts ts_indexed_access_type // type A = string[number]; // type B = string[number][number][number][]; @@ -585,7 +620,7 @@ fn parse_ts_parenthesized_type(p: &mut JsParser) -> ParsedSyntax { let m = p.start(); p.bump(T!['(']); - parse_ts_type(p).or_add_diagnostic(p, expected_ts_type); + parse_ts_type(p, TypeContext::default()).or_add_diagnostic(p, expected_ts_type); p.expect(T![')']); Present(m.complete(p, TS_PARENTHESIZED_TYPE)) } @@ -640,7 +675,7 @@ fn parse_ts_mapped_type(p: &mut JsParser) -> ParsedSyntax { p.expect(T!['[']); parse_ts_type_parameter_name(p).or_add_diagnostic(p, expected_ts_type_parameter); p.expect(T![in]); - parse_ts_type(p).or_add_diagnostic(p, expected_ts_type); + parse_ts_type(p, TypeContext::default()).or_add_diagnostic(p, expected_ts_type); parse_ts_mapped_type_as_clause(p).ok(); p.expect(T![']']); parse_ts_mapped_type_optional_modifier_clause(p).ok(); @@ -658,7 +693,7 @@ fn parse_ts_mapped_type_as_clause(p: &mut JsParser) -> ParsedSyntax { let m = p.start(); p.bump_remap(T![as]); - parse_ts_type(p).or_add_diagnostic(p, expected_ts_type); + parse_ts_type(p, TypeContext::default()).or_add_diagnostic(p, expected_ts_type); Present(m.complete(p, TS_MAPPED_TYPE_AS_CLAUSE)) } @@ -959,7 +994,7 @@ impl ParseSeparatedList for TsTupleTypeElementList { parse_name(p).or_add_diagnostic(p, expected_identifier); let has_question_mark = p.eat(T![?]); p.bump(T![:]); - parse_ts_type(p).or_add_diagnostic(p, expected_ts_type); + parse_ts_type(p, TypeContext::default()).or_add_diagnostic(p, expected_ts_type); let mut syntax = m.complete(p, TS_NAMED_TUPLE_TYPE_ELEMENT); @@ -980,11 +1015,11 @@ impl ParseSeparatedList for TsTupleTypeElementList { if p.at(T![...]) { let m = p.start(); p.bump(T![...]); - parse_ts_type(p).or_add_diagnostic(p, expected_ts_type); + parse_ts_type(p, TypeContext::default()).or_add_diagnostic(p, expected_ts_type); return Present(m.complete(p, TS_REST_TUPLE_TYPE_ELEMENT)); } - let ty = parse_ts_type(p); + let ty = parse_ts_type(p, TypeContext::default()); if p.at(T![?]) { let m = ty.precede_or_add_diagnostic(p, expected_ts_type); @@ -1111,7 +1146,7 @@ fn parse_ts_template_literal_type(p: &mut JsParser) -> ParsedSyntax { TS_TEMPLATE_CHUNK_ELEMENT, TS_TEMPLATE_ELEMENT, false, - |p| parse_ts_type(p).or_add_diagnostic(p, expected_ts_type), + |p| parse_ts_type(p, TypeContext::default()).or_add_diagnostic(p, expected_ts_type), ); elements.complete(p, TS_TEMPLATE_ELEMENT_LIST); p.expect(BACKTICK); @@ -1140,7 +1175,7 @@ fn parse_ts_constructor_type(p: &mut JsParser) -> ParsedSyntax { parse_parameter_list(p, ParameterContext::Declaration, SignatureFlags::empty()) .or_add_diagnostic(p, expected_parameters); p.expect(T![=>]); - parse_ts_type(p).or_add_diagnostic(p, expected_ts_type); + parse_ts_type(p, TypeContext::default()).or_add_diagnostic(p, expected_ts_type); Present(m.complete(p, TS_CONSTRUCTOR_TYPE)) } @@ -1223,7 +1258,7 @@ fn parse_ts_return_type(p: &mut JsParser) -> ParsedSyntax { if !p.has_nth_preceding_line_break(1) && (is_asserts_predicate || is_is_predicate) { parse_ts_type_predicate(p) } else { - parse_ts_type(p) + parse_ts_type(p, TypeContext::default()) } } @@ -1244,11 +1279,11 @@ fn parse_ts_type_predicate(p: &mut JsParser) -> ParsedSyntax { if is_asserts && p.at(T![is]) { let condition = p.start(); p.expect(T![is]); - parse_ts_type(p).or_add_diagnostic(p, expected_ts_type); + parse_ts_type(p, TypeContext::default()).or_add_diagnostic(p, expected_ts_type); condition.complete(p, TS_ASSERTS_CONDITION); } else if !is_asserts { p.expect(T![is]); - parse_ts_type(p).or_add_diagnostic(p, expected_ts_type); + parse_ts_type(p, TypeContext::default()).or_add_diagnostic(p, expected_ts_type); } let kind = if is_asserts { @@ -1385,7 +1420,7 @@ impl ParseSeparatedList for TypeArgumentsList { const LIST_KIND: Self::Kind = TS_TYPE_ARGUMENT_LIST; fn parse_element(&mut self, p: &mut JsParser) -> ParsedSyntax { - parse_ts_type(p) + parse_ts_type(p, TypeContext::default()) } fn is_at_list_end(&self, p: &mut JsParser) -> bool { From bc96d1b0ed4a691b8aeea5dca9b0d54540f5d045 Mon Sep 17 00:00:00 2001 From: nissy-dev Date: Mon, 26 Dec 2022 09:30:55 +0900 Subject: [PATCH 2/3] fix: more refactoring --- crates/rome_js_parser/src/syntax/typescript/types.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/rome_js_parser/src/syntax/typescript/types.rs b/crates/rome_js_parser/src/syntax/typescript/types.rs index b10670e56ec..1e42234236d 100644 --- a/crates/rome_js_parser/src/syntax/typescript/types.rs +++ b/crates/rome_js_parser/src/syntax/typescript/types.rs @@ -156,7 +156,7 @@ impl ParseSeparatedList for TsTypeParameterList { const LIST_KIND: Self::Kind = TS_TYPE_PARAMETER_LIST; fn parse_element(&mut self, p: &mut JsParser) -> ParsedSyntax { - parse_ts_type_parameter(p, TypeContext::default()) + parse_ts_type_parameter(p) } fn is_at_list_end(&self, p: &mut JsParser) -> bool { @@ -184,10 +184,10 @@ impl ParseSeparatedList for TsTypeParameterList { } } -fn parse_ts_type_parameter(p: &mut JsParser, context: TypeContext) -> ParsedSyntax { +fn parse_ts_type_parameter(p: &mut JsParser) -> ParsedSyntax { parse_ts_type_parameter_name(p).map(|name| { let m = name.precede(p); - parse_ts_type_constraint_clause(p, context).ok(); + parse_ts_type_constraint_clause(p, TypeContext::default()).ok(); parse_ts_default_type_clause(p).ok(); m.complete(p, TS_TYPE_PARAMETER) }) From 05571c1b195ed2d57ec6db88b8c293bedd73ec4d Mon Sep 17 00:00:00 2001 From: nissy-dev Date: Mon, 26 Dec 2022 09:51:59 +0900 Subject: [PATCH 3/3] feat: add comments --- crates/rome_js_parser/src/syntax/typescript/types.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/rome_js_parser/src/syntax/typescript/types.rs b/crates/rome_js_parser/src/syntax/typescript/types.rs index 1e42234236d..dd6ac095fcb 100644 --- a/crates/rome_js_parser/src/syntax/typescript/types.rs +++ b/crates/rome_js_parser/src/syntax/typescript/types.rs @@ -38,7 +38,7 @@ pub(crate) struct TypeContext(TypeContextFlags); bitflags! { struct TypeContextFlags: u8 { - /// TODO: write the description + /// If `true` allows a conditional type const ALLOW_CONDITIONAL_TYPE = 1 << 0; } }