-
Notifications
You must be signed in to change notification settings - Fork 666
feat(rome_rowan): add a declare_union
macro for one-off union AstNodes
#2691
Conversation
Parser conformance results on ubuntu-latestjs/262
jsx/babel
symbols/microsoft
|
Test result | main count |
This PR count | Difference |
---|---|---|---|
Total | 588 | 588 | 0 |
Passed | 519 | 519 | 0 |
Failed | 69 | 69 | 0 |
Panics | 0 | 0 | 0 |
Coverage | 88.27% | 88.27% | 0.00% |
ts/microsoft
Test result | main count |
This PR count | Difference |
---|---|---|---|
Total | 16257 | 16257 | 0 |
Passed | 12391 | 12391 | 0 |
Failed | 3866 | 3866 | 0 |
Panics | 0 | 0 | 0 |
Coverage | 76.22% | 76.22% | 0.00% |
crates/rome_rowan/src/macros.rs
Outdated
/// } | ||
/// ``` | ||
#[macro_export] | ||
macro_rules! declare_union { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: could we call declare_language_union
or declare_node_union
(this is maybe better, because it applies AstNode
)?
The name, right now, is so generic that we could misuse it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll change it to declare_node_union
, I also think it's the better option since the type being declared and all the types it contains must be AST nodes
type Language: Language; | ||
} | ||
|
||
macro_rules! impl_union_language { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I can't really understand the purpose of this macro. Could you explain please?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This macro is an implementation detail of declare_union
(it's not exported from the macros
module) aimed at making it easier to implement the UnionLanguage
trait. This trait is implemented for tuples of various arity, but because tuples are heterogeneous the implementation needs to declare one generic type for each element of the tuple. This in turn means it's impossible to implement the trait once for all tuples size, instead implementing the trait would look like this:
impl<T1> UnionLanguage for (T1,) { ... }
impl<T1, T2> UnionLanguage for (T1, T2) { ... }
impl<T1, T2, T3> UnionLanguage for (T1, T2, T3) { ... }
All these impl blocks share the same code with the only difference being the number of generic types, so it was simpler to write that code in a macro so it can easily be repeated for various numbers of generic arguments
Deploying with Cloudflare Pages
|
Summary
This PR add a new
declare_union
for creating union AST nodes scoped to a local module, without having to add them tojs.ungram
androme_js_syntax
. Its usage looks like the following example:The resulting union type is an enum with a variant for each node type in the union and implementing:
AstNode
traitDebug
andDisplay
traitsClone
,PartialEq
,Eq
andHash
traits with deriveFrom
trait for each variantsInto
trait forSyntaxNode
andSyntaxElement
The macro is designed to fail to compile when the union would contain node types from different languages, as the
AstNode
trait must have a singleLanguage
associated type. I'm not really sure if and how this is a restriction we may lift in the future.Test Plan
This macro is now used to implement the
JsAnyBinaryLikeExpression
andJsAnyBinaryLikeLeftExpression
types in the formatting of binary-like expressions