Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 24 additions & 41 deletions crates/oxc_formatter/src/generated/ast_nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4233,7 +4233,7 @@ impl<'a> AstNode<'a, StaticMemberExpression<'a>> {

#[inline]
pub fn property(&self) -> &AstNode<'a, IdentifierName<'a>> {
let following_node = self.following_node;
let following_node = None;
self.allocator.alloc(AstNode {
inner: &self.inner.property,
allocator: self.allocator,
Expand Down Expand Up @@ -5959,7 +5959,7 @@ impl<'a> AstNode<'a, VariableDeclaration<'a>> {

#[inline]
pub fn declarations(&self) -> &AstNode<'a, Vec<'a, VariableDeclarator<'a>>> {
let following_node = self.following_node;
let following_node = None;
self.allocator.alloc(AstNode {
inner: &self.inner.declarations,
allocator: self.allocator,
Expand Down Expand Up @@ -6120,8 +6120,7 @@ impl<'a> AstNode<'a, IfStatement<'a>> {

#[inline]
pub fn consequent(&self) -> &AstNode<'a, Statement<'a>> {
let following_node =
self.inner.alternate.as_ref().map(SiblingNode::from).or(self.following_node);
let following_node = self.inner.alternate.as_ref().map(SiblingNode::from);
self.allocator.alloc(AstNode {
inner: &self.inner.consequent,
allocator: self.allocator,
Expand Down Expand Up @@ -6674,14 +6673,7 @@ impl<'a> GetSpan for AstNode<'a, WithStatement<'a>> {
impl<'a> AstNode<'a, SwitchStatement<'a>> {
#[inline]
pub fn discriminant(&self) -> &AstNode<'a, Expression<'a>> {
let following_node = self
.inner
.cases
.first()
.as_ref()
.copied()
.map(SiblingNode::from)
.or(self.following_node);
let following_node = self.inner.cases.first().as_ref().copied().map(SiblingNode::from);
self.allocator.alloc(AstNode {
inner: &self.inner.discriminant,
allocator: self.allocator,
Expand Down Expand Up @@ -6860,8 +6852,7 @@ impl<'a> AstNode<'a, TryStatement<'a>> {
.handler
.as_deref()
.map(SiblingNode::from)
.or_else(|| self.inner.finalizer.as_deref().map(SiblingNode::from))
.or(self.following_node);
.or_else(|| self.inner.finalizer.as_deref().map(SiblingNode::from));
self.allocator.alloc(AstNode {
inner: self.inner.block.as_ref(),
allocator: self.allocator,
Expand All @@ -6872,8 +6863,7 @@ impl<'a> AstNode<'a, TryStatement<'a>> {

#[inline]
pub fn handler(&self) -> Option<&AstNode<'a, CatchClause<'a>>> {
let following_node =
self.inner.finalizer.as_deref().map(SiblingNode::from).or(self.following_node);
let following_node = self.inner.finalizer.as_deref().map(SiblingNode::from);
self.allocator
.alloc(self.inner.handler.as_ref().map(|inner| AstNode {
inner: inner.as_ref(),
Expand Down Expand Up @@ -7499,8 +7489,7 @@ impl<'a> AstNode<'a, FormalParameters<'a>> {

#[inline]
pub fn items(&self) -> &AstNode<'a, Vec<'a, FormalParameter<'a>>> {
let following_node =
self.inner.rest.as_deref().map(SiblingNode::from).or(self.following_node);
let following_node = self.inner.rest.as_deref().map(SiblingNode::from);
self.allocator.alloc(AstNode {
inner: &self.inner.items,
allocator: self.allocator,
Expand Down Expand Up @@ -8549,8 +8538,7 @@ impl<'a> AstNode<'a, ImportDeclaration<'a>> {

#[inline]
pub fn source(&self) -> &AstNode<'a, StringLiteral<'a>> {
let following_node =
self.inner.with_clause.as_deref().map(SiblingNode::from).or(self.following_node);
let following_node = self.inner.with_clause.as_deref().map(SiblingNode::from);
self.allocator.alloc(AstNode {
inner: &self.inner.source,
allocator: self.allocator,
Expand All @@ -8566,7 +8554,7 @@ impl<'a> AstNode<'a, ImportDeclaration<'a>> {

#[inline]
pub fn with_clause(&self) -> Option<&AstNode<'a, WithClause<'a>>> {
let following_node = self.following_node;
let following_node = None;
self.allocator
.alloc(self.inner.with_clause.as_ref().map(|inner| AstNode {
inner: inner.as_ref(),
Expand Down Expand Up @@ -8885,8 +8873,7 @@ impl<'a> AstNode<'a, ExportNamedDeclaration<'a>> {
.copied()
.map(SiblingNode::from)
.or_else(|| self.inner.source.as_ref().map(SiblingNode::from))
.or_else(|| self.inner.with_clause.as_deref().map(SiblingNode::from))
.or(self.following_node);
.or_else(|| self.inner.with_clause.as_deref().map(SiblingNode::from));
self.allocator
.alloc(self.inner.declaration.as_ref().map(|inner| AstNode {
inner,
Expand All @@ -8905,8 +8892,7 @@ impl<'a> AstNode<'a, ExportNamedDeclaration<'a>> {
.source
.as_ref()
.map(SiblingNode::from)
.or_else(|| self.inner.with_clause.as_deref().map(SiblingNode::from))
.or(self.following_node);
.or_else(|| self.inner.with_clause.as_deref().map(SiblingNode::from));
self.allocator.alloc(AstNode {
inner: &self.inner.specifiers,
allocator: self.allocator,
Expand All @@ -8917,8 +8903,7 @@ impl<'a> AstNode<'a, ExportNamedDeclaration<'a>> {

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

#[inline]
pub fn with_clause(&self) -> Option<&AstNode<'a, WithClause<'a>>> {
let following_node = self.following_node;
let following_node = None;
self.allocator
.alloc(self.inner.with_clause.as_ref().map(|inner| AstNode {
inner: inner.as_ref(),
Expand Down Expand Up @@ -8973,7 +8958,7 @@ impl<'a> GetSpan for AstNode<'a, ExportNamedDeclaration<'a>> {
impl<'a> AstNode<'a, ExportDefaultDeclaration<'a>> {
#[inline]
pub fn declaration(&self) -> &AstNode<'a, ExportDefaultDeclarationKind<'a>> {
let following_node = self.following_node;
let following_node = None;
self.allocator.alloc(AstNode {
inner: &self.inner.declaration,
allocator: self.allocator,
Expand Down Expand Up @@ -9019,8 +9004,7 @@ impl<'a> AstNode<'a, ExportAllDeclaration<'a>> {

#[inline]
pub fn source(&self) -> &AstNode<'a, StringLiteral<'a>> {
let following_node =
self.inner.with_clause.as_deref().map(SiblingNode::from).or(self.following_node);
let following_node = self.inner.with_clause.as_deref().map(SiblingNode::from);
self.allocator.alloc(AstNode {
inner: &self.inner.source,
allocator: self.allocator,
Expand All @@ -9031,7 +9015,7 @@ impl<'a> AstNode<'a, ExportAllDeclaration<'a>> {

#[inline]
pub fn with_clause(&self) -> Option<&AstNode<'a, WithClause<'a>>> {
let following_node = self.following_node;
let following_node = None;
self.allocator
.alloc(self.inner.with_clause.as_ref().map(|inner| AstNode {
inner: inner.as_ref(),
Expand Down Expand Up @@ -10393,7 +10377,7 @@ impl<'a> AstNode<'a, TSEnumDeclaration<'a>> {

#[inline]
pub fn body(&self) -> &AstNode<'a, TSEnumBody<'a>> {
let following_node = self.following_node;
let following_node = None;
self.allocator.alloc(AstNode {
inner: &self.inner.body,
allocator: self.allocator,
Expand Down Expand Up @@ -12064,7 +12048,7 @@ impl<'a> AstNode<'a, TSTypeAliasDeclaration<'a>> {

#[inline]
pub fn type_annotation(&self) -> &AstNode<'a, TSType<'a>> {
let following_node = self.following_node;
let following_node = None;
self.allocator.alloc(AstNode {
inner: &self.inner.type_annotation,
allocator: self.allocator,
Expand Down Expand Up @@ -12198,7 +12182,7 @@ impl<'a> AstNode<'a, TSInterfaceDeclaration<'a>> {

#[inline]
pub fn body(&self) -> &AstNode<'a, TSInterfaceBody<'a>> {
let following_node = self.following_node;
let following_node = None;
self.allocator.alloc(AstNode {
inner: self.inner.body.as_ref(),
allocator: self.allocator,
Expand Down Expand Up @@ -12882,8 +12866,7 @@ impl<'a> GetSpan for AstNode<'a, TSTypePredicateName<'a>> {
impl<'a> AstNode<'a, TSModuleDeclaration<'a>> {
#[inline]
pub fn id(&self) -> &AstNode<'a, TSModuleDeclarationName<'a>> {
let following_node =
self.inner.body.as_ref().map(SiblingNode::from).or(self.following_node);
let following_node = self.inner.body.as_ref().map(SiblingNode::from);
self.allocator.alloc(AstNode {
inner: &self.inner.id,
allocator: self.allocator,
Expand All @@ -12894,7 +12877,7 @@ impl<'a> AstNode<'a, TSModuleDeclaration<'a>> {

#[inline]
pub fn body(&self) -> Option<&AstNode<'a, TSModuleDeclarationBody<'a>>> {
let following_node = self.following_node;
let following_node = None;
self.allocator
.alloc(self.inner.body.as_ref().map(|inner| AstNode {
inner,
Expand Down Expand Up @@ -13775,7 +13758,7 @@ impl<'a> AstNode<'a, TSImportEqualsDeclaration<'a>> {

#[inline]
pub fn module_reference(&self) -> &AstNode<'a, TSModuleReference<'a>> {
let following_node = self.following_node;
let following_node = None;
self.allocator.alloc(AstNode {
inner: &self.inner.module_reference,
allocator: self.allocator,
Expand Down Expand Up @@ -13948,7 +13931,7 @@ impl<'a> GetSpan for AstNode<'a, Decorator<'a>> {
impl<'a> AstNode<'a, TSExportAssignment<'a>> {
#[inline]
pub fn expression(&self) -> &AstNode<'a, Expression<'a>> {
let following_node = self.following_node;
let following_node = None;
self.allocator.alloc(AstNode {
inner: &self.inner.expression,
allocator: self.allocator,
Expand Down Expand Up @@ -13981,7 +13964,7 @@ impl<'a> GetSpan for AstNode<'a, TSExportAssignment<'a>> {
impl<'a> AstNode<'a, TSNamespaceExportDeclaration<'a>> {
#[inline]
pub fn id(&self) -> &AstNode<'a, IdentifierName<'a>> {
let following_node = self.following_node;
let following_node = None;
self.allocator.alloc(AstNode {
inner: &self.inner.id,
allocator: self.allocator,
Expand Down
48 changes: 40 additions & 8 deletions tasks/ast_tools/src/generators/formatter/ast_nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
Codegen, Generator,
generators::define_generator,
output::{Output, output_path},
schema::{Def, EnumDef, FieldDef, Schema, StructDef, TypeDef},
schema::{self, Def, EnumDef, FieldDef, Schema, StructDef, TypeDef},
};

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

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

pub struct FormatterAstNodesGenerator;

define_generator!(FormatterAstNodesGenerator);

impl Generator for FormatterAstNodesGenerator {
fn generate(&self, schema: &Schema, _codegen: &Codegen) -> Output {
let all_statement_variants_names = get_all_statement_variants_names(schema);

let impls = schema
.types
.iter()
.filter_map(|type_def| match type_def {
TypeDef::Struct(struct_def)
if struct_def.visit.has_visitor() && !struct_def.builder.skip =>
{
Some(generate_struct_impls(struct_def, schema))
Some(generate_struct_impls(struct_def, &all_statement_variants_names, schema))
}
TypeDef::Enum(enum_def) if enum_def.visit.has_visitor() => {
Some(generate_enum_impls(enum_def, schema))
Expand Down Expand Up @@ -193,7 +196,29 @@ impl Generator for FormatterAstNodesGenerator {
}
}

fn generate_struct_impls(struct_def: &StructDef, schema: &Schema) -> TokenStream {
fn get_all_statement_variants_names(schema: &Schema) -> Vec<&str> {
let Some(type_id) = schema.type_names.get("Statement") else {
return vec![];
};

let enum_def = schema.types.get(*type_id).unwrap().as_enum().unwrap();

enum_def
.variants
.iter()
.map(schema::VariantDef::name)
.chain(enum_def.inherits_types(schema).flat_map(|inherited_type| {
let inherited_enum_def = inherited_type.as_enum().unwrap();
inherited_enum_def.variants.iter().map(schema::VariantDef::name)
}))
.collect()
}

fn generate_struct_impls(
struct_def: &StructDef,
all_statement_variants_names: &[&str],
schema: &Schema,
) -> TokenStream {
let type_ty = struct_def.ty(schema);
let has_kind = struct_def.kind.has_kind;
let struct_name = struct_def.ident();
Expand Down Expand Up @@ -264,9 +289,10 @@ fn generate_struct_impls(struct_def: &StructDef, schema: &Schema) -> TokenStream
quote! { &self.inner.#field_name }
};

let mut following_node = if struct_def.name.as_str().ends_with("Statement")
|| AST_NODE_WITHOUT_FOLLOWING_NODE_LIST.contains(&struct_def.name.as_str())
{
let should_not_have_following_node = all_statement_variants_names
.contains(&struct_def.name.as_str())
|| AST_NODE_WITHOUT_FOLLOWING_NODE_LIST.contains(&struct_def.name.as_str());
let mut following_node = if should_not_have_following_node {
quote! {
None
}
Expand All @@ -287,6 +313,7 @@ fn generate_struct_impls(struct_def: &StructDef, schema: &Schema) -> TokenStream
{
let or_else_following_nodes = build_following_node_chain_until_non_option(
&fields[next_field_index + 1..],
should_not_have_following_node,
schema,
);
quote! {
Expand Down Expand Up @@ -425,6 +452,7 @@ fn generate_next_following_node(

fn build_following_node_chain_until_non_option(
fields: &[FieldDef],
should_not_have_following_node: bool,
schema: &Schema,
) -> TokenStream {
let mut result = TokenStream::new();
Expand All @@ -441,7 +469,11 @@ fn build_following_node_chain_until_non_option(
}
}

quote! { #result.or(self.following_node) }
if should_not_have_following_node {
result
} else {
quote! { #result.or(self.following_node) }
}
}

fn generate_enum_impls(enum_def: &EnumDef, schema: &Schema) -> TokenStream {
Expand Down
4 changes: 1 addition & 3 deletions tasks/prettier_conformance/snapshots/prettier.js.snap.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
js compatibility: 501/699 (71.67%)
js compatibility: 503/699 (71.96%)

# Failed

Expand Down Expand Up @@ -29,7 +29,6 @@ js compatibility: 501/699 (71.67%)
| js/comments/15661.js | 💥💥 | 53.63% |
| js/comments/16398.js | 💥💥 | 80.00% |
| js/comments/blank.js | 💥💥 | 95.24% |
| js/comments/call_comment.js | 💥💥 | 90.91% |
| js/comments/dangling_array.js | 💥✨ | 40.00% |
| js/comments/dynamic_imports.js | 💥💥 | 71.43% |
| js/comments/empty-statements.js | 💥💥 | 90.91% |
Expand All @@ -42,7 +41,6 @@ js compatibility: 501/699 (71.67%)
| js/comments/jsdoc.js | 💥💥 | 47.83% |
| js/comments/jsx.js | 💥💥 | 41.63% |
| js/comments/last-arg.js | 💥💥 | 80.65% |
| js/comments/multi-comments.js | 💥✨ | 44.74% |
| js/comments/return-statement.js | 💥💥 | 89.35% |
| js/comments/tagged-template-literal.js | 💥💥 | 69.23% |
| js/comments/template-literal.js | 💥💥 | 30.43% |
Expand Down
4 changes: 2 additions & 2 deletions tasks/prettier_conformance/snapshots/prettier.ts.snap.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ ts compatibility: 249/573 (43.46%)
| typescript/custom/computedProperties/string.ts | 💥 | 73.33% |
| typescript/custom/computedProperties/symbol.ts | 💥 | 71.43% |
| typescript/custom/declare/declareModifier.d.ts | 💥 | 44.44% |
| typescript/custom/modifiers/minustoken.ts | 💥 | 16.67% |
| typescript/custom/modifiers/minustoken.ts | 💥 | 20.00% |
| typescript/custom/modifiers/question.ts | 💥 | 0.00% |
| typescript/custom/modifiers/readonly.ts | 💥 | 0.00% |
| typescript/custom/module/global.ts | 💥 | 80.00% |
Expand Down Expand Up @@ -296,7 +296,7 @@ ts compatibility: 249/573 (43.46%)
| typescript/tsx/react.tsx | 💥 | 50.00% |
| typescript/tsx/type-parameters.tsx | 💥 | 90.48% |
| typescript/tsx/url.tsx | 💥 | 58.06% |
| typescript/tuple/dangling-comments.ts | 💥💥💥 | 13.95% |
| typescript/tuple/dangling-comments.ts | 💥💥💥 | 21.28% |
| typescript/tuple/trailing-comma-for-empty-tuples.ts | 💥💥💥 | 16.67% |
| typescript/tuple/trailing-comma-trailing-rest.ts | 💥💥💥 | 0.00% |
| typescript/tuple/trailing-comma.ts | 💥💥💥 | 15.38% |
Expand Down
Loading