Skip to content

Commit de8558f

Browse files
committed
feat(formatter): correct ast_nodes generator
1 parent dd5283c commit de8558f

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
@@ -4209,7 +4209,7 @@ impl<'a> AstNode<'a, StaticMemberExpression<'a>> {
42094209

42104210
#[inline]
42114211
pub fn property(&self) -> &AstNode<'a, IdentifierName<'a>> {
4212-
let following_node = self.following_node;
4212+
let following_node = None;
42134213
self.allocator.alloc(AstNode {
42144214
inner: &self.inner.property,
42154215
allocator: self.allocator,
@@ -5935,7 +5935,7 @@ impl<'a> AstNode<'a, VariableDeclaration<'a>> {
59355935

59365936
#[inline]
59375937
pub fn declarations(&self) -> &AstNode<'a, Vec<'a, VariableDeclarator<'a>>> {
5938-
let following_node = self.following_node;
5938+
let following_node = None;
59395939
self.allocator.alloc(AstNode {
59405940
inner: &self.inner.declarations,
59415941
allocator: self.allocator,
@@ -6096,8 +6096,7 @@ impl<'a> AstNode<'a, IfStatement<'a>> {
60966096

60976097
#[inline]
60986098
pub fn consequent(&self) -> &AstNode<'a, Statement<'a>> {
6099-
let following_node =
6100-
self.inner.alternate.as_ref().map(SiblingNode::from).or(self.following_node);
6099+
let following_node = self.inner.alternate.as_ref().map(SiblingNode::from);
61016100
self.allocator.alloc(AstNode {
61026101
inner: &self.inner.consequent,
61036102
allocator: self.allocator,
@@ -6650,14 +6649,7 @@ impl<'a> GetSpan for AstNode<'a, WithStatement<'a>> {
66506649
impl<'a> AstNode<'a, SwitchStatement<'a>> {
66516650
#[inline]
66526651
pub fn discriminant(&self) -> &AstNode<'a, Expression<'a>> {
6653-
let following_node = self
6654-
.inner
6655-
.cases
6656-
.first()
6657-
.as_ref()
6658-
.copied()
6659-
.map(SiblingNode::from)
6660-
.or(self.following_node);
6652+
let following_node = self.inner.cases.first().as_ref().copied().map(SiblingNode::from);
66616653
self.allocator.alloc(AstNode {
66626654
inner: &self.inner.discriminant,
66636655
allocator: self.allocator,
@@ -6836,8 +6828,7 @@ impl<'a> AstNode<'a, TryStatement<'a>> {
68366828
.handler
68376829
.as_deref()
68386830
.map(SiblingNode::from)
6839-
.or_else(|| self.inner.finalizer.as_deref().map(SiblingNode::from))
6840-
.or(self.following_node);
6831+
.or_else(|| self.inner.finalizer.as_deref().map(SiblingNode::from));
68416832
self.allocator.alloc(AstNode {
68426833
inner: self.inner.block.as_ref(),
68436834
allocator: self.allocator,
@@ -6848,8 +6839,7 @@ impl<'a> AstNode<'a, TryStatement<'a>> {
68486839

68496840
#[inline]
68506841
pub fn handler(&self) -> Option<&AstNode<'a, CatchClause<'a>>> {
6851-
let following_node =
6852-
self.inner.finalizer.as_deref().map(SiblingNode::from).or(self.following_node);
6842+
let following_node = self.inner.finalizer.as_deref().map(SiblingNode::from);
68536843
self.allocator
68546844
.alloc(self.inner.handler.as_ref().map(|inner| AstNode {
68556845
inner: inner.as_ref(),
@@ -7475,8 +7465,7 @@ impl<'a> AstNode<'a, FormalParameters<'a>> {
74757465

74767466
#[inline]
74777467
pub fn items(&self) -> &AstNode<'a, Vec<'a, FormalParameter<'a>>> {
7478-
let following_node =
7479-
self.inner.rest.as_deref().map(SiblingNode::from).or(self.following_node);
7468+
let following_node = self.inner.rest.as_deref().map(SiblingNode::from);
74807469
self.allocator.alloc(AstNode {
74817470
inner: &self.inner.items,
74827471
allocator: self.allocator,
@@ -8525,8 +8514,7 @@ impl<'a> AstNode<'a, ImportDeclaration<'a>> {
85258514

85268515
#[inline]
85278516
pub fn source(&self) -> &AstNode<'a, StringLiteral<'a>> {
8528-
let following_node =
8529-
self.inner.with_clause.as_deref().map(SiblingNode::from).or(self.following_node);
8517+
let following_node = self.inner.with_clause.as_deref().map(SiblingNode::from);
85308518
self.allocator.alloc(AstNode {
85318519
inner: &self.inner.source,
85328520
allocator: self.allocator,
@@ -8542,7 +8530,7 @@ impl<'a> AstNode<'a, ImportDeclaration<'a>> {
85428530

85438531
#[inline]
85448532
pub fn with_clause(&self) -> Option<&AstNode<'a, WithClause<'a>>> {
8545-
let following_node = self.following_node;
8533+
let following_node = None;
85468534
self.allocator
85478535
.alloc(self.inner.with_clause.as_ref().map(|inner| AstNode {
85488536
inner: inner.as_ref(),
@@ -8861,8 +8849,7 @@ impl<'a> AstNode<'a, ExportNamedDeclaration<'a>> {
88618849
.copied()
88628850
.map(SiblingNode::from)
88638851
.or_else(|| self.inner.source.as_ref().map(SiblingNode::from))
8864-
.or_else(|| self.inner.with_clause.as_deref().map(SiblingNode::from))
8865-
.or(self.following_node);
8852+
.or_else(|| self.inner.with_clause.as_deref().map(SiblingNode::from));
88668853
self.allocator
88678854
.alloc(self.inner.declaration.as_ref().map(|inner| AstNode {
88688855
inner,
@@ -8881,8 +8868,7 @@ impl<'a> AstNode<'a, ExportNamedDeclaration<'a>> {
88818868
.source
88828869
.as_ref()
88838870
.map(SiblingNode::from)
8884-
.or_else(|| self.inner.with_clause.as_deref().map(SiblingNode::from))
8885-
.or(self.following_node);
8871+
.or_else(|| self.inner.with_clause.as_deref().map(SiblingNode::from));
88868872
self.allocator.alloc(AstNode {
88878873
inner: &self.inner.specifiers,
88888874
allocator: self.allocator,
@@ -8893,8 +8879,7 @@ impl<'a> AstNode<'a, ExportNamedDeclaration<'a>> {
88938879

88948880
#[inline]
88958881
pub fn source(&self) -> Option<&AstNode<'a, StringLiteral<'a>>> {
8896-
let following_node =
8897-
self.inner.with_clause.as_deref().map(SiblingNode::from).or(self.following_node);
8882+
let following_node = self.inner.with_clause.as_deref().map(SiblingNode::from);
88988883
self.allocator
88998884
.alloc(self.inner.source.as_ref().map(|inner| AstNode {
89008885
inner,
@@ -8913,7 +8898,7 @@ impl<'a> AstNode<'a, ExportNamedDeclaration<'a>> {
89138898

89148899
#[inline]
89158900
pub fn with_clause(&self) -> Option<&AstNode<'a, WithClause<'a>>> {
8916-
let following_node = self.following_node;
8901+
let following_node = None;
89178902
self.allocator
89188903
.alloc(self.inner.with_clause.as_ref().map(|inner| AstNode {
89198904
inner: inner.as_ref(),
@@ -8960,7 +8945,7 @@ impl<'a> AstNode<'a, ExportDefaultDeclaration<'a>> {
89608945

89618946
#[inline]
89628947
pub fn declaration(&self) -> &AstNode<'a, ExportDefaultDeclarationKind<'a>> {
8963-
let following_node = self.following_node;
8948+
let following_node = None;
89648949
self.allocator.alloc(AstNode {
89658950
inner: &self.inner.declaration,
89668951
allocator: self.allocator,
@@ -9006,8 +8991,7 @@ impl<'a> AstNode<'a, ExportAllDeclaration<'a>> {
90068991

90078992
#[inline]
90088993
pub fn source(&self) -> &AstNode<'a, StringLiteral<'a>> {
9009-
let following_node =
9010-
self.inner.with_clause.as_deref().map(SiblingNode::from).or(self.following_node);
8994+
let following_node = self.inner.with_clause.as_deref().map(SiblingNode::from);
90118995
self.allocator.alloc(AstNode {
90128996
inner: &self.inner.source,
90138997
allocator: self.allocator,
@@ -9018,7 +9002,7 @@ impl<'a> AstNode<'a, ExportAllDeclaration<'a>> {
90189002

90199003
#[inline]
90209004
pub fn with_clause(&self) -> Option<&AstNode<'a, WithClause<'a>>> {
9021-
let following_node = self.following_node;
9005+
let following_node = None;
90229006
self.allocator
90239007
.alloc(self.inner.with_clause.as_ref().map(|inner| AstNode {
90249008
inner: inner.as_ref(),
@@ -10380,7 +10364,7 @@ impl<'a> AstNode<'a, TSEnumDeclaration<'a>> {
1038010364

1038110365
#[inline]
1038210366
pub fn body(&self) -> &AstNode<'a, TSEnumBody<'a>> {
10383-
let following_node = self.following_node;
10367+
let following_node = None;
1038410368
self.allocator.alloc(AstNode {
1038510369
inner: &self.inner.body,
1038610370
allocator: self.allocator,
@@ -12051,7 +12035,7 @@ impl<'a> AstNode<'a, TSTypeAliasDeclaration<'a>> {
1205112035

1205212036
#[inline]
1205312037
pub fn type_annotation(&self) -> &AstNode<'a, TSType<'a>> {
12054-
let following_node = self.following_node;
12038+
let following_node = None;
1205512039
self.allocator.alloc(AstNode {
1205612040
inner: &self.inner.type_annotation,
1205712041
allocator: self.allocator,
@@ -12185,7 +12169,7 @@ impl<'a> AstNode<'a, TSInterfaceDeclaration<'a>> {
1218512169

1218612170
#[inline]
1218712171
pub fn body(&self) -> &AstNode<'a, TSInterfaceBody<'a>> {
12188-
let following_node = self.following_node;
12172+
let following_node = None;
1218912173
self.allocator.alloc(AstNode {
1219012174
inner: self.inner.body.as_ref(),
1219112175
allocator: self.allocator,
@@ -12869,8 +12853,7 @@ impl<'a> GetSpan for AstNode<'a, TSTypePredicateName<'a>> {
1286912853
impl<'a> AstNode<'a, TSModuleDeclaration<'a>> {
1287012854
#[inline]
1287112855
pub fn id(&self) -> &AstNode<'a, TSModuleDeclarationName<'a>> {
12872-
let following_node =
12873-
self.inner.body.as_ref().map(SiblingNode::from).or(self.following_node);
12856+
let following_node = self.inner.body.as_ref().map(SiblingNode::from);
1287412857
self.allocator.alloc(AstNode {
1287512858
inner: &self.inner.id,
1287612859
allocator: self.allocator,
@@ -12881,7 +12864,7 @@ impl<'a> AstNode<'a, TSModuleDeclaration<'a>> {
1288112864

1288212865
#[inline]
1288312866
pub fn body(&self) -> Option<&AstNode<'a, TSModuleDeclarationBody<'a>>> {
12884-
let following_node = self.following_node;
12867+
let following_node = None;
1288512868
self.allocator
1288612869
.alloc(self.inner.body.as_ref().map(|inner| AstNode {
1288712870
inner,
@@ -13685,7 +13668,7 @@ impl<'a> AstNode<'a, TSImportEqualsDeclaration<'a>> {
1368513668

1368613669
#[inline]
1368713670
pub fn module_reference(&self) -> &AstNode<'a, TSModuleReference<'a>> {
13688-
let following_node = self.following_node;
13671+
let following_node = None;
1368913672
self.allocator.alloc(AstNode {
1369013673
inner: &self.inner.module_reference,
1369113674
allocator: self.allocator,
@@ -13858,7 +13841,7 @@ impl<'a> GetSpan for AstNode<'a, Decorator<'a>> {
1385813841
impl<'a> AstNode<'a, TSExportAssignment<'a>> {
1385913842
#[inline]
1386013843
pub fn expression(&self) -> &AstNode<'a, Expression<'a>> {
13861-
let following_node = self.following_node;
13844+
let following_node = None;
1386213845
self.allocator.alloc(AstNode {
1386313846
inner: &self.inner.expression,
1386413847
allocator: self.allocator,
@@ -13891,7 +13874,7 @@ impl<'a> GetSpan for AstNode<'a, TSExportAssignment<'a>> {
1389113874
impl<'a> AstNode<'a, TSNamespaceExportDeclaration<'a>> {
1389213875
#[inline]
1389313876
pub fn id(&self) -> &AstNode<'a, IdentifierName<'a>> {
13894-
let following_node = self.following_node;
13877+
let following_node = None;
1389513878
self.allocator.alloc(AstNode {
1389613879
inner: &self.inner.id,
1389713880
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)