Skip to content

Commit 40affe9

Browse files
committed
feat(formatter): correct ast_nodes generator
1 parent 76de684 commit 40affe9

File tree

4 files changed

+67
-54
lines changed

4 files changed

+67
-54
lines changed

crates/oxc_formatter/src/generated/ast_nodes.rs

Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4233,7 +4233,7 @@ impl<'a> AstNode<'a, StaticMemberExpression<'a>> {
42334233

42344234
#[inline]
42354235
pub fn property(&self) -> &AstNode<'a, IdentifierName<'a>> {
4236-
let following_node = self.following_node;
4236+
let following_node = None;
42374237
self.allocator.alloc(AstNode {
42384238
inner: &self.inner.property,
42394239
allocator: self.allocator,
@@ -5959,7 +5959,7 @@ impl<'a> AstNode<'a, VariableDeclaration<'a>> {
59595959

59605960
#[inline]
59615961
pub fn declarations(&self) -> &AstNode<'a, Vec<'a, VariableDeclarator<'a>>> {
5962-
let following_node = self.following_node;
5962+
let following_node = None;
59635963
self.allocator.alloc(AstNode {
59645964
inner: &self.inner.declarations,
59655965
allocator: self.allocator,
@@ -6120,8 +6120,7 @@ impl<'a> AstNode<'a, IfStatement<'a>> {
61206120

61216121
#[inline]
61226122
pub fn consequent(&self) -> &AstNode<'a, Statement<'a>> {
6123-
let following_node =
6124-
self.inner.alternate.as_ref().map(SiblingNode::from).or(self.following_node);
6123+
let following_node = self.inner.alternate.as_ref().map(SiblingNode::from);
61256124
self.allocator.alloc(AstNode {
61266125
inner: &self.inner.consequent,
61276126
allocator: self.allocator,
@@ -6674,14 +6673,7 @@ impl<'a> GetSpan for AstNode<'a, WithStatement<'a>> {
66746673
impl<'a> AstNode<'a, SwitchStatement<'a>> {
66756674
#[inline]
66766675
pub fn discriminant(&self) -> &AstNode<'a, Expression<'a>> {
6677-
let following_node = self
6678-
.inner
6679-
.cases
6680-
.first()
6681-
.as_ref()
6682-
.copied()
6683-
.map(SiblingNode::from)
6684-
.or(self.following_node);
6676+
let following_node = self.inner.cases.first().as_ref().copied().map(SiblingNode::from);
66856677
self.allocator.alloc(AstNode {
66866678
inner: &self.inner.discriminant,
66876679
allocator: self.allocator,
@@ -6860,8 +6852,7 @@ impl<'a> AstNode<'a, TryStatement<'a>> {
68606852
.handler
68616853
.as_deref()
68626854
.map(SiblingNode::from)
6863-
.or_else(|| self.inner.finalizer.as_deref().map(SiblingNode::from))
6864-
.or(self.following_node);
6855+
.or_else(|| self.inner.finalizer.as_deref().map(SiblingNode::from));
68656856
self.allocator.alloc(AstNode {
68666857
inner: self.inner.block.as_ref(),
68676858
allocator: self.allocator,
@@ -6872,8 +6863,7 @@ impl<'a> AstNode<'a, TryStatement<'a>> {
68726863

68736864
#[inline]
68746865
pub fn handler(&self) -> Option<&AstNode<'a, CatchClause<'a>>> {
6875-
let following_node =
6876-
self.inner.finalizer.as_deref().map(SiblingNode::from).or(self.following_node);
6866+
let following_node = self.inner.finalizer.as_deref().map(SiblingNode::from);
68776867
self.allocator
68786868
.alloc(self.inner.handler.as_ref().map(|inner| AstNode {
68796869
inner: inner.as_ref(),
@@ -7499,8 +7489,7 @@ impl<'a> AstNode<'a, FormalParameters<'a>> {
74997489

75007490
#[inline]
75017491
pub fn items(&self) -> &AstNode<'a, Vec<'a, FormalParameter<'a>>> {
7502-
let following_node =
7503-
self.inner.rest.as_deref().map(SiblingNode::from).or(self.following_node);
7492+
let following_node = self.inner.rest.as_deref().map(SiblingNode::from);
75047493
self.allocator.alloc(AstNode {
75057494
inner: &self.inner.items,
75067495
allocator: self.allocator,
@@ -8549,8 +8538,7 @@ impl<'a> AstNode<'a, ImportDeclaration<'a>> {
85498538

85508539
#[inline]
85518540
pub fn source(&self) -> &AstNode<'a, StringLiteral<'a>> {
8552-
let following_node =
8553-
self.inner.with_clause.as_deref().map(SiblingNode::from).or(self.following_node);
8541+
let following_node = self.inner.with_clause.as_deref().map(SiblingNode::from);
85548542
self.allocator.alloc(AstNode {
85558543
inner: &self.inner.source,
85568544
allocator: self.allocator,
@@ -8566,7 +8554,7 @@ impl<'a> AstNode<'a, ImportDeclaration<'a>> {
85668554

85678555
#[inline]
85688556
pub fn with_clause(&self) -> Option<&AstNode<'a, WithClause<'a>>> {
8569-
let following_node = self.following_node;
8557+
let following_node = None;
85708558
self.allocator
85718559
.alloc(self.inner.with_clause.as_ref().map(|inner| AstNode {
85728560
inner: inner.as_ref(),
@@ -8885,8 +8873,7 @@ impl<'a> AstNode<'a, ExportNamedDeclaration<'a>> {
88858873
.copied()
88868874
.map(SiblingNode::from)
88878875
.or_else(|| self.inner.source.as_ref().map(SiblingNode::from))
8888-
.or_else(|| self.inner.with_clause.as_deref().map(SiblingNode::from))
8889-
.or(self.following_node);
8876+
.or_else(|| self.inner.with_clause.as_deref().map(SiblingNode::from));
88908877
self.allocator
88918878
.alloc(self.inner.declaration.as_ref().map(|inner| AstNode {
88928879
inner,
@@ -8905,8 +8892,7 @@ impl<'a> AstNode<'a, ExportNamedDeclaration<'a>> {
89058892
.source
89068893
.as_ref()
89078894
.map(SiblingNode::from)
8908-
.or_else(|| self.inner.with_clause.as_deref().map(SiblingNode::from))
8909-
.or(self.following_node);
8895+
.or_else(|| self.inner.with_clause.as_deref().map(SiblingNode::from));
89108896
self.allocator.alloc(AstNode {
89118897
inner: &self.inner.specifiers,
89128898
allocator: self.allocator,
@@ -8917,8 +8903,7 @@ impl<'a> AstNode<'a, ExportNamedDeclaration<'a>> {
89178903

89188904
#[inline]
89198905
pub fn source(&self) -> Option<&AstNode<'a, StringLiteral<'a>>> {
8920-
let following_node =
8921-
self.inner.with_clause.as_deref().map(SiblingNode::from).or(self.following_node);
8906+
let following_node = self.inner.with_clause.as_deref().map(SiblingNode::from);
89228907
self.allocator
89238908
.alloc(self.inner.source.as_ref().map(|inner| AstNode {
89248909
inner,
@@ -8937,7 +8922,7 @@ impl<'a> AstNode<'a, ExportNamedDeclaration<'a>> {
89378922

89388923
#[inline]
89398924
pub fn with_clause(&self) -> Option<&AstNode<'a, WithClause<'a>>> {
8940-
let following_node = self.following_node;
8925+
let following_node = None;
89418926
self.allocator
89428927
.alloc(self.inner.with_clause.as_ref().map(|inner| AstNode {
89438928
inner: inner.as_ref(),
@@ -8973,7 +8958,7 @@ impl<'a> GetSpan for AstNode<'a, ExportNamedDeclaration<'a>> {
89738958
impl<'a> AstNode<'a, ExportDefaultDeclaration<'a>> {
89748959
#[inline]
89758960
pub fn declaration(&self) -> &AstNode<'a, ExportDefaultDeclarationKind<'a>> {
8976-
let following_node = self.following_node;
8961+
let following_node = None;
89778962
self.allocator.alloc(AstNode {
89788963
inner: &self.inner.declaration,
89798964
allocator: self.allocator,
@@ -9019,8 +9004,7 @@ impl<'a> AstNode<'a, ExportAllDeclaration<'a>> {
90199004

90209005
#[inline]
90219006
pub fn source(&self) -> &AstNode<'a, StringLiteral<'a>> {
9022-
let following_node =
9023-
self.inner.with_clause.as_deref().map(SiblingNode::from).or(self.following_node);
9007+
let following_node = self.inner.with_clause.as_deref().map(SiblingNode::from);
90249008
self.allocator.alloc(AstNode {
90259009
inner: &self.inner.source,
90269010
allocator: self.allocator,
@@ -9031,7 +9015,7 @@ impl<'a> AstNode<'a, ExportAllDeclaration<'a>> {
90319015

90329016
#[inline]
90339017
pub fn with_clause(&self) -> Option<&AstNode<'a, WithClause<'a>>> {
9034-
let following_node = self.following_node;
9018+
let following_node = None;
90359019
self.allocator
90369020
.alloc(self.inner.with_clause.as_ref().map(|inner| AstNode {
90379021
inner: inner.as_ref(),
@@ -10393,7 +10377,7 @@ impl<'a> AstNode<'a, TSEnumDeclaration<'a>> {
1039310377

1039410378
#[inline]
1039510379
pub fn body(&self) -> &AstNode<'a, TSEnumBody<'a>> {
10396-
let following_node = self.following_node;
10380+
let following_node = None;
1039710381
self.allocator.alloc(AstNode {
1039810382
inner: &self.inner.body,
1039910383
allocator: self.allocator,
@@ -12064,7 +12048,7 @@ impl<'a> AstNode<'a, TSTypeAliasDeclaration<'a>> {
1206412048

1206512049
#[inline]
1206612050
pub fn type_annotation(&self) -> &AstNode<'a, TSType<'a>> {
12067-
let following_node = self.following_node;
12051+
let following_node = None;
1206812052
self.allocator.alloc(AstNode {
1206912053
inner: &self.inner.type_annotation,
1207012054
allocator: self.allocator,
@@ -12198,7 +12182,7 @@ impl<'a> AstNode<'a, TSInterfaceDeclaration<'a>> {
1219812182

1219912183
#[inline]
1220012184
pub fn body(&self) -> &AstNode<'a, TSInterfaceBody<'a>> {
12201-
let following_node = self.following_node;
12185+
let following_node = None;
1220212186
self.allocator.alloc(AstNode {
1220312187
inner: self.inner.body.as_ref(),
1220412188
allocator: self.allocator,
@@ -12882,8 +12866,7 @@ impl<'a> GetSpan for AstNode<'a, TSTypePredicateName<'a>> {
1288212866
impl<'a> AstNode<'a, TSModuleDeclaration<'a>> {
1288312867
#[inline]
1288412868
pub fn id(&self) -> &AstNode<'a, TSModuleDeclarationName<'a>> {
12885-
let following_node =
12886-
self.inner.body.as_ref().map(SiblingNode::from).or(self.following_node);
12869+
let following_node = self.inner.body.as_ref().map(SiblingNode::from);
1288712870
self.allocator.alloc(AstNode {
1288812871
inner: &self.inner.id,
1288912872
allocator: self.allocator,
@@ -12894,7 +12877,7 @@ impl<'a> AstNode<'a, TSModuleDeclaration<'a>> {
1289412877

1289512878
#[inline]
1289612879
pub fn body(&self) -> Option<&AstNode<'a, TSModuleDeclarationBody<'a>>> {
12897-
let following_node = self.following_node;
12880+
let following_node = None;
1289812881
self.allocator
1289912882
.alloc(self.inner.body.as_ref().map(|inner| AstNode {
1290012883
inner,
@@ -13775,7 +13758,7 @@ impl<'a> AstNode<'a, TSImportEqualsDeclaration<'a>> {
1377513758

1377613759
#[inline]
1377713760
pub fn module_reference(&self) -> &AstNode<'a, TSModuleReference<'a>> {
13778-
let following_node = self.following_node;
13761+
let following_node = None;
1377913762
self.allocator.alloc(AstNode {
1378013763
inner: &self.inner.module_reference,
1378113764
allocator: self.allocator,
@@ -13948,7 +13931,7 @@ impl<'a> GetSpan for AstNode<'a, Decorator<'a>> {
1394813931
impl<'a> AstNode<'a, TSExportAssignment<'a>> {
1394913932
#[inline]
1395013933
pub fn expression(&self) -> &AstNode<'a, Expression<'a>> {
13951-
let following_node = self.following_node;
13934+
let following_node = None;
1395213935
self.allocator.alloc(AstNode {
1395313936
inner: &self.inner.expression,
1395413937
allocator: self.allocator,
@@ -13981,7 +13964,7 @@ impl<'a> GetSpan for AstNode<'a, TSExportAssignment<'a>> {
1398113964
impl<'a> AstNode<'a, TSNamespaceExportDeclaration<'a>> {
1398213965
#[inline]
1398313966
pub fn id(&self) -> &AstNode<'a, IdentifierName<'a>> {
13984-
let following_node = self.following_node;
13967+
let following_node = None;
1398513968
self.allocator.alloc(AstNode {
1398613969
inner: &self.inner.id,
1398713970
allocator: self.allocator,

tasks/ast_tools/src/generators/formatter/ast_nodes.rs

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
Codegen, Generator,
1010
generators::define_generator,
1111
output::{Output, output_path},
12-
schema::{Def, EnumDef, FieldDef, Schema, StructDef, TypeDef},
12+
schema::{self, Def, EnumDef, FieldDef, Schema, StructDef, TypeDef},
1313
};
1414

1515
pub fn get_node_type(ty: &TokenStream) -> TokenStream {
@@ -20,22 +20,25 @@ const FORMATTER_CRATE_PATH: &str = "crates/oxc_formatter";
2020

2121
/// Based on the printing comments algorithm, the last child of these AST nodes don't need to print comments.
2222
/// Without following nodes could lead to only print comments that before the end of the node, which is what we want.
23-
const AST_NODE_WITHOUT_FOLLOWING_NODE_LIST: &[&str] = &["AssignmentExpression", "FormalParameters"];
23+
const AST_NODE_WITHOUT_FOLLOWING_NODE_LIST: &[&str] =
24+
&["AssignmentExpression", "FormalParameters", "StaticMemberExpression"];
2425

2526
pub struct FormatterAstNodesGenerator;
2627

2728
define_generator!(FormatterAstNodesGenerator);
2829

2930
impl Generator for FormatterAstNodesGenerator {
3031
fn generate(&self, schema: &Schema, _codegen: &Codegen) -> Output {
32+
let all_statement_variants_names = get_all_statement_variants_names(schema);
33+
3134
let impls = schema
3235
.types
3336
.iter()
3437
.filter_map(|type_def| match type_def {
3538
TypeDef::Struct(struct_def)
3639
if struct_def.visit.has_visitor() && !struct_def.builder.skip =>
3740
{
38-
Some(generate_struct_impls(struct_def, schema))
41+
Some(generate_struct_impls(struct_def, &all_statement_variants_names, schema))
3942
}
4043
TypeDef::Enum(enum_def) if enum_def.visit.has_visitor() => {
4144
Some(generate_enum_impls(enum_def, schema))
@@ -193,7 +196,29 @@ impl Generator for FormatterAstNodesGenerator {
193196
}
194197
}
195198

196-
fn generate_struct_impls(struct_def: &StructDef, schema: &Schema) -> TokenStream {
199+
fn get_all_statement_variants_names(schema: &Schema) -> Vec<&str> {
200+
let Some(type_id) = schema.type_names.get("Statement") else {
201+
return vec![];
202+
};
203+
204+
let enum_def = schema.types.get(*type_id).unwrap().as_enum().unwrap();
205+
206+
enum_def
207+
.variants
208+
.iter()
209+
.map(schema::VariantDef::name)
210+
.chain(enum_def.inherits_types(schema).flat_map(|inherited_type| {
211+
let inherited_enum_def = inherited_type.as_enum().unwrap();
212+
inherited_enum_def.variants.iter().map(schema::VariantDef::name)
213+
}))
214+
.collect()
215+
}
216+
217+
fn generate_struct_impls(
218+
struct_def: &StructDef,
219+
all_statement_variants_names: &[&str],
220+
schema: &Schema,
221+
) -> TokenStream {
197222
let type_ty = struct_def.ty(schema);
198223
let has_kind = struct_def.kind.has_kind;
199224
let struct_name = struct_def.ident();
@@ -264,9 +289,10 @@ fn generate_struct_impls(struct_def: &StructDef, schema: &Schema) -> TokenStream
264289
quote! { &self.inner.#field_name }
265290
};
266291

267-
let mut following_node = if struct_def.name.as_str().ends_with("Statement")
268-
|| AST_NODE_WITHOUT_FOLLOWING_NODE_LIST.contains(&struct_def.name.as_str())
269-
{
292+
let should_not_have_following_node = all_statement_variants_names
293+
.contains(&struct_def.name.as_str())
294+
|| AST_NODE_WITHOUT_FOLLOWING_NODE_LIST.contains(&struct_def.name.as_str());
295+
let mut following_node = if should_not_have_following_node {
270296
quote! {
271297
None
272298
}
@@ -287,6 +313,7 @@ fn generate_struct_impls(struct_def: &StructDef, schema: &Schema) -> TokenStream
287313
{
288314
let or_else_following_nodes = build_following_node_chain_until_non_option(
289315
&fields[next_field_index + 1..],
316+
should_not_have_following_node,
290317
schema,
291318
);
292319
quote! {
@@ -425,6 +452,7 @@ fn generate_next_following_node(
425452

426453
fn build_following_node_chain_until_non_option(
427454
fields: &[FieldDef],
455+
should_not_have_following_node: bool,
428456
schema: &Schema,
429457
) -> TokenStream {
430458
let mut result = TokenStream::new();
@@ -441,7 +469,11 @@ fn build_following_node_chain_until_non_option(
441469
}
442470
}
443471

444-
quote! { #result.or(self.following_node) }
472+
if should_not_have_following_node {
473+
result
474+
} else {
475+
quote! { #result.or(self.following_node) }
476+
}
445477
}
446478

447479
fn generate_enum_impls(enum_def: &EnumDef, schema: &Schema) -> TokenStream {

tasks/prettier_conformance/snapshots/prettier.js.snap.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
js compatibility: 501/699 (71.67%)
1+
js compatibility: 503/699 (71.96%)
22

33
# Failed
44

@@ -29,7 +29,6 @@ js compatibility: 501/699 (71.67%)
2929
| js/comments/15661.js | 💥💥 | 53.63% |
3030
| js/comments/16398.js | 💥💥 | 80.00% |
3131
| js/comments/blank.js | 💥💥 | 95.24% |
32-
| js/comments/call_comment.js | 💥💥 | 90.91% |
3332
| js/comments/dangling_array.js | 💥✨ | 40.00% |
3433
| js/comments/dynamic_imports.js | 💥💥 | 71.43% |
3534
| js/comments/empty-statements.js | 💥💥 | 90.91% |
@@ -42,7 +41,6 @@ js compatibility: 501/699 (71.67%)
4241
| js/comments/jsdoc.js | 💥💥 | 47.83% |
4342
| js/comments/jsx.js | 💥💥 | 41.63% |
4443
| js/comments/last-arg.js | 💥💥 | 80.65% |
45-
| js/comments/multi-comments.js | 💥✨ | 44.74% |
4644
| js/comments/return-statement.js | 💥💥 | 89.35% |
4745
| js/comments/tagged-template-literal.js | 💥💥 | 69.23% |
4846
| js/comments/template-literal.js | 💥💥 | 30.43% |

tasks/prettier_conformance/snapshots/prettier.ts.snap.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ ts compatibility: 249/573 (43.46%)
168168
| typescript/custom/computedProperties/string.ts | 💥 | 73.33% |
169169
| typescript/custom/computedProperties/symbol.ts | 💥 | 71.43% |
170170
| typescript/custom/declare/declareModifier.d.ts | 💥 | 44.44% |
171-
| typescript/custom/modifiers/minustoken.ts | 💥 | 16.67% |
171+
| typescript/custom/modifiers/minustoken.ts | 💥 | 20.00% |
172172
| typescript/custom/modifiers/question.ts | 💥 | 0.00% |
173173
| typescript/custom/modifiers/readonly.ts | 💥 | 0.00% |
174174
| typescript/custom/module/global.ts | 💥 | 80.00% |
@@ -296,7 +296,7 @@ ts compatibility: 249/573 (43.46%)
296296
| typescript/tsx/react.tsx | 💥 | 50.00% |
297297
| typescript/tsx/type-parameters.tsx | 💥 | 90.48% |
298298
| typescript/tsx/url.tsx | 💥 | 58.06% |
299-
| typescript/tuple/dangling-comments.ts | 💥💥💥 | 13.95% |
299+
| typescript/tuple/dangling-comments.ts | 💥💥💥 | 21.28% |
300300
| typescript/tuple/trailing-comma-for-empty-tuples.ts | 💥💥💥 | 16.67% |
301301
| typescript/tuple/trailing-comma-trailing-rest.ts | 💥💥💥 | 0.00% |
302302
| typescript/tuple/trailing-comma.ts | 💥💥💥 | 15.38% |

0 commit comments

Comments
 (0)