Skip to content

Commit 94db4d8

Browse files
authored
feat: support the satisfies expression (#441)
1 parent de97de7 commit 94db4d8

File tree

4 files changed

+86
-26
lines changed

4 files changed

+86
-26
lines changed

Cargo.lock

Lines changed: 37 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ tracing = ["dprint-core/tracing"]
2626

2727
[dependencies]
2828
anyhow = "1.0.64"
29-
deno_ast = { version = "0.19.0", features = ["view"] }
29+
deno_ast = { version = "0.20.0", features = ["view"] }
3030
dprint-core = { version = "0.59.0", features = ["formatting"] }
3131
rustc-hash = "1.1.0"
3232
serde = { version = "1.0.144", features = ["derive"] }

src/generation/generate.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ fn gen_node_with_inner_gen<'a>(node: Node<'a>, context: &mut Context<'a>, inner_
202202
Node::Tpl(node) => gen_tpl(node, context),
203203
Node::TplElement(node) => gen_tpl_element(node, context),
204204
Node::TsAsExpr(node) => gen_as_expr(node, context),
205+
Node::TsSatisfactionExpr(node) => gen_satisfaction_expr(node, context),
205206
Node::TsConstAssertion(node) => gen_const_assertion(node, context),
206207
Node::TsExprWithTypeArgs(node) => gen_expr_with_type_args(node, context),
207208
Node::TsNonNullExpr(node) => gen_non_null_expr(node, context),
@@ -1743,17 +1744,33 @@ struct AsExprLike<'a> {
17431744
}
17441745

17451746
fn gen_as_expr_like<'a>(node: AsExprLike<'a>, context: &mut Context<'a>) -> PrintItems {
1746-
let mut items = PrintItems::new();
1747-
items.extend(gen_node(node.expr, context));
1748-
items.push_str(" as");
1747+
let mut items = gen_node(node.expr, context);
1748+
// todo: remove this condition once https://github.com/swc-project/swc/issues/6238 is fixed
1749+
let is_as = node
1750+
.expr
1751+
.next_token_fast(context.program)
1752+
.map(|t| t.text_fast(context.program) == "as")
1753+
.unwrap_or_default();
1754+
if is_as {
1755+
items.push_str(" as");
1756+
} else {
1757+
items.push_str(" satisfies");
1758+
}
17491759
items.push_signal(Signal::SpaceIfNotTrailing);
17501760
items.push_condition(conditions::with_indent_if_start_of_line_indented(gen_node(node.type_ann, context)));
17511761
items
17521762
}
17531763

1764+
fn gen_satisfaction_expr<'a>(node: &'a TsSatisfactionExpr<'a>, context: &mut Context<'a>) -> PrintItems {
1765+
let mut items = gen_node(node.expr.into(), context);
1766+
items.push_str(" satisfies");
1767+
items.push_signal(Signal::SpaceIfNotTrailing);
1768+
items.push_condition(conditions::with_indent_if_start_of_line_indented(gen_node(node.type_ann.into(), context)));
1769+
items
1770+
}
1771+
17541772
fn gen_const_assertion<'a>(node: &'a TsConstAssertion, context: &mut Context<'a>) -> PrintItems {
1755-
let mut items = PrintItems::new();
1756-
items.extend(gen_node(node.expr.into(), context));
1773+
let mut items = gen_node(node.expr.into(), context);
17571774
items.push_str(" as const");
17581775
items
17591776
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
~~ lineWidth: 40 ~~
2+
== should format ==
3+
Testing satisfies Test;
4+
5+
[expect]
6+
Testing satisfies Test;
7+
8+
== should format when multiple lines ==
9+
something satisfies string | number | other | other;
10+
11+
[expect]
12+
something satisfies
13+
| string
14+
| number
15+
| other
16+
| other;
17+
18+
== should indent the expression when initial expression is hanging and the expression is multi-line ==
19+
testingtesting()
20+
.find(p => p.testing) satisfies string | undefined;
21+
22+
[expect]
23+
testingtesting()
24+
.find(p => p.testing) satisfies
25+
| string
26+
| undefined;

0 commit comments

Comments
 (0)