Skip to content

Commit 86b3441

Browse files
committed
feat: Support TS 4.0.
1 parent 1052076 commit 86b3441

File tree

4 files changed

+48
-10
lines changed

4 files changed

+48
-10
lines changed

src/parsing/parser.rs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ fn parse_node_with_inner_parse<'a>(node: Node<'a>, context: &mut Context<'a>, in
262262
Node::TsRestType(node) => parse_rest_type(node, context),
263263
Node::TsThisType(_) => "this".into(),
264264
Node::TsTupleType(node) => parse_tuple_type(node, context),
265+
Node::TsTupleElement(node) => parse_tuple_element(node, context),
265266
Node::TsTypeAnn(node) => parse_type_ann(node, context),
266267
Node::TsTypeParam(node) => parse_type_param(node, context),
267268
Node::TsTypeParamDecl(node) => parse_type_parameters(TypeParamNode::Decl(node), context),
@@ -271,7 +272,6 @@ fn parse_node_with_inner_parse<'a>(node: Node<'a>, context: &mut Context<'a>, in
271272
Node::TsTypeQuery(node) => parse_type_query(node, context),
272273
Node::TsTypeRef(node) => parse_type_reference(node, context),
273274
Node::TsUnionType(node) => parse_union_type(node, context),
274-
Node::TsTupleElement(node) => parse_node((&node.ty).into(), context),
275275
/* unknown */
276276
_ => parse_raw_string(node.text(context).into()),
277277
}
@@ -4069,6 +4069,17 @@ fn parse_tuple_type<'a>(node: &'a TsTupleType, context: &mut Context<'a>) -> Pri
40694069
}, context)
40704070
}
40714071

4072+
fn parse_tuple_element<'a>(node: &'a TsTupleElement, context: &mut Context<'a>) -> PrintItems {
4073+
if let Some(label) = &node.label {
4074+
let mut items = PrintItems::new();
4075+
items.extend(parse_node(label.into(), context));
4076+
items.extend(parse_type_ann_with_colon_for_type(&node.ty, context));
4077+
items
4078+
} else {
4079+
parse_node((&node.ty).into(), context)
4080+
}
4081+
}
4082+
40724083
fn parse_type_ann<'a>(node: &'a TsTypeAnn, context: &mut Context<'a>) -> PrintItems {
40734084
parse_node((&node.type_ann).into(), context)
40744085
}
@@ -5055,18 +5066,24 @@ fn parse_node_with_semi_colon<'a>(value: Option<Node<'a>>, parsed_semi_colon: Pr
50555066
}
50565067
}
50575068

5058-
/// For some reason, some nodes don't have a TsTypeAnn, but instead of a Box<TsType>
5069+
/// Some nodes don't have a TsTypeAnn, but instead a Box<TsType>
50595070
fn parse_type_ann_with_colon_if_exists_for_type<'a>(type_ann: &'a Option<Box<TsType>>, context: &mut Context<'a>) -> PrintItems {
5060-
let mut items = PrintItems::new();
50615071
if let Some(type_ann) = type_ann {
5062-
if context.config.type_annotation_space_before_colon {
5063-
items.push_str(" ");
5064-
}
5065-
let colon_token = context.token_finder.get_previous_token_if_colon(&**type_ann);
5066-
#[cfg(debug_assertions)]
5067-
assert_has_op(":", colon_token, context);
5068-
items.extend(parse_type_ann_with_colon(type_ann.into(), colon_token, context));
5072+
parse_type_ann_with_colon_for_type(type_ann, context)
5073+
} else {
5074+
PrintItems::new()
50695075
}
5076+
}
5077+
5078+
fn parse_type_ann_with_colon_for_type<'a>(type_ann: &'a TsType, context: &mut Context<'a>) -> PrintItems {
5079+
let mut items = PrintItems::new();
5080+
if context.config.type_annotation_space_before_colon {
5081+
items.push_str(" ");
5082+
}
5083+
let colon_token = context.token_finder.get_previous_token_if_colon(type_ann);
5084+
#[cfg(debug_assertions)]
5085+
assert_has_op(":", colon_token, context);
5086+
items.extend(parse_type_ann_with_colon(type_ann.into(), colon_token, context));
50705087
items
50715088
}
50725089

tests/specs/expressions/AssignmentExpression/AssignmentExpression_All.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,13 @@ a = ttttttttttttttttttttttttttttttttttttttttttttt;
3535

3636
[expect]
3737
a = ttttttttttttttttttttttttttttttttttttttttttttt;
38+
39+
== should support these new TS 4.0 assignments ==
40+
a &&= b;
41+
a ||= b;
42+
a ??= b;
43+
44+
[expect]
45+
a &&= b;
46+
a ||= b;
47+
a ??= b;

tests/specs/types/TupleType/TupleType_All.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,9 @@ type Test = Array<[
5252
number,
5353
string,
5454
]>;
55+
56+
== should support rest elements in tuples ==
57+
type Test = [...string, ...number];
58+
59+
[expect]
60+
type Test = [...string, ...number];
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
== should format ==
2+
type Test = [label: string];
3+
4+
[expect]
5+
type Test = [label: string];

0 commit comments

Comments
 (0)