refactor(ast): move #[estree(skip)] to types (#8877)

Similar to #8876. Remove `#[estree(skip)]` attributes from struct fields containing semantic IDs, and add that attribute to the `ScopeId`, `SymbolId` and `ReferenceId` types instead. This reduces pointless repetition in the AST type definitions.
This commit is contained in:
overlookmotel committed 2025-02-04 02:48:18 +00:00
1 parent 3407d470aa
commit 6628e1d008
8 files changed
+34 -25

No files matched your search

-13
View File
@@ -40,7 +40,6 @@ pub struct Program<'a> {
pub hashbang: Option<Hashbang<'a>>,
pub directives: Vec<'a, Directive<'a>>,
pub body: Vec<'a, Statement<'a>>,
#[estree(skip)]
pub scope_id: Cell<Option<ScopeId>>,
}
@@ -225,7 +224,6 @@ pub struct IdentifierReference<'a> {
/// Identifies what identifier this refers to, and how it is used. This is
/// set in the bind step of semantic analysis, and will always be [`None`]
/// immediately after parsing.
#[estree(skip)]
pub reference_id: Cell<Option<ReferenceId>>,
}
@@ -248,7 +246,6 @@ pub struct BindingIdentifier<'a> {
/// you choose to skip semantic analysis, this will always be [`None`].
///
/// [`semantic analysis`]: <https://docs.rs/oxc_semantic/latest/oxc_semantic/struct.SemanticBuilder.html>
#[estree(skip)]
pub symbol_id: Cell<Option<SymbolId>>,
}
@@ -1039,7 +1036,6 @@ pub struct Hashbang<'a> {
pub struct BlockStatement<'a> {
pub span: Span,
pub body: Vec<'a, Statement<'a>>,
#[estree(skip)]
pub scope_id: Cell<Option<ScopeId>>,
}
@@ -1182,7 +1178,6 @@ pub struct ForStatement<'a> {
pub test: Option<Expression<'a>>,
pub update: Option<Expression<'a>>,
pub body: Statement<'a>,
#[estree(skip)]
pub scope_id: Cell<Option<ScopeId>>,
}
@@ -1212,7 +1207,6 @@ pub struct ForInStatement<'a> {
pub left: ForStatementLeft<'a>,
pub right: Expression<'a>,
pub body: Statement<'a>,
#[estree(skip)]
pub scope_id: Cell<Option<ScopeId>>,
}
@@ -1242,7 +1236,6 @@ pub struct ForOfStatement<'a> {
pub left: ForStatementLeft<'a>,
pub right: Expression<'a>,
pub body: Statement<'a>,
#[estree(skip)]
pub scope_id: Cell<Option<ScopeId>>,
}
@@ -1293,7 +1286,6 @@ pub struct SwitchStatement<'a> {
pub discriminant: Expression<'a>,
#[scope(enter_before)]
pub cases: Vec<'a, SwitchCase<'a>>,
#[estree(skip)]
pub scope_id: Cell<Option<ScopeId>>,
}
@@ -1382,7 +1374,6 @@ pub struct CatchClause<'a> {
pub param: Option<CatchParameter<'a>>,
/// The statements run when an error is caught
pub body: Box<'a, BlockStatement<'a>>,
#[estree(skip)]
pub scope_id: Cell<Option<ScopeId>>,
}
@@ -1615,7 +1606,6 @@ pub struct Function<'a> {
/// }
/// ```
pub body: Option<Box<'a, FunctionBody<'a>>>,
#[estree(skip)]
pub scope_id: Cell<Option<ScopeId>>,
}
@@ -1706,7 +1696,6 @@ pub struct ArrowFunctionExpression<'a> {
pub return_type: Option<Box<'a, TSTypeAnnotation<'a>>>,
/// See `expression` for whether this arrow expression returns an expression.
pub body: Box<'a, FunctionBody<'a>>,
#[estree(skip)]
pub scope_id: Cell<Option<ScopeId>>,
}
@@ -1792,7 +1781,6 @@ pub struct Class<'a> {
pub declare: bool,
/// Id of the scope created by the [`Class`], including type parameters and
/// statements within the [`ClassBody`].
#[estree(skip)]
pub scope_id: Cell<Option<ScopeId>>,
}
@@ -2043,7 +2031,6 @@ pub struct PrivateIdentifier<'a> {
pub struct StaticBlock<'a> {
pub span: Span,
pub body: Vec<'a, Statement<'a>>,
#[estree(skip)]
pub scope_id: Cell<Option<ScopeId>>,
}
-8
View File
@@ -71,7 +71,6 @@ pub struct TSEnumDeclaration<'a> {
/// `true` for const enums
pub r#const: bool,
pub declare: bool,
#[estree(skip)]
pub scope_id: Cell<Option<ScopeId>>,
}
@@ -302,7 +301,6 @@ pub struct TSConditionalType<'a> {
/// The type evaluated to if the test is false.
#[scope(exit_before)]
pub false_type: TSType<'a>,
#[estree(skip)]
pub scope_id: Cell<Option<ScopeId>>,
}
@@ -834,7 +832,6 @@ pub struct TSTypeAliasDeclaration<'a> {
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
pub type_annotation: TSType<'a>,
pub declare: bool,
#[estree(skip)]
pub scope_id: Cell<Option<ScopeId>>,
}
@@ -897,7 +894,6 @@ pub struct TSInterfaceDeclaration<'a> {
pub body: Box<'a, TSInterfaceBody<'a>>,
/// `true` for `declare interface Foo {}`
pub declare: bool,
#[estree(skip)]
pub scope_id: Cell<Option<ScopeId>>,
}
@@ -1015,7 +1011,6 @@ pub struct TSMethodSignature<'a> {
pub this_param: Option<Box<'a, TSThisParameter<'a>>>,
pub params: Box<'a, FormalParameters<'a>>,
pub return_type: Option<Box<'a, TSTypeAnnotation<'a>>>,
#[estree(skip)]
pub scope_id: Cell<Option<ScopeId>>,
}
@@ -1029,7 +1024,6 @@ pub struct TSConstructSignatureDeclaration<'a> {
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
pub params: Box<'a, FormalParameters<'a>>,
pub return_type: Option<Box<'a, TSTypeAnnotation<'a>>>,
#[estree(skip)]
pub scope_id: Cell<Option<ScopeId>>,
}
@@ -1153,7 +1147,6 @@ pub struct TSModuleDeclaration<'a> {
/// ```
pub kind: TSModuleDeclarationKind,
pub declare: bool,
#[estree(skip)]
pub scope_id: Cell<Option<ScopeId>>,
}
@@ -1423,7 +1416,6 @@ pub struct TSMappedType<'a> {
/// type Qux = { [P in keyof T]: T[P] } // None
/// ```
pub readonly: TSMappedTypeModifierOperator,
#[estree(skip)]
pub scope_id: Cell<Option<ScopeId>>,
}
+1
View File
@@ -15,6 +15,7 @@ use oxc_ast_macros::ast;
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[clone_in(default)]
#[content_eq(skip)]
#[estree(skip)]
pub struct ReferenceId(NonMaxU32);
impl Idx for ReferenceId {
+1
View File
@@ -11,6 +11,7 @@ use oxc_ast_macros::ast;
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[clone_in(default)]
#[content_eq(skip)]
#[estree(skip)]
pub struct ScopeId(NonMaxU32);
impl ScopeId {
+1
View File
@@ -11,6 +11,7 @@ use oxc_ast_macros::ast;
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[clone_in(default)]
#[content_eq(skip)]
#[estree(skip)]
pub struct SymbolId(NonMaxU32);
impl SymbolId {
+27 -2
View File
@@ -35,8 +35,12 @@ impl Derive for DeriveESTree {
}
/// Register that accept `#[estree]` attr on structs, enums, struct fields, or enum variants.
/// Allow attr on structs and enums which don't derive this trait.
fn attrs(&self) -> &[(&'static str, AttrPositions)] {
&[("estree", attr_positions!(Struct | Enum | StructField | EnumVariant))]
&[(
"estree",
attr_positions!(StructMaybeDerived | EnumMaybeDerived | StructField | EnumVariant),
)]
}
/// Parse `#[estree]` attr.
@@ -84,6 +88,7 @@ fn parse_estree_attr(location: AttrLocation, part: AttrPart) -> Result<()> {
match location {
// `#[estree]` attr on struct
AttrLocation::Struct(struct_def) => match part {
AttrPart::Tag("skip") => struct_def.estree.skip = true,
AttrPart::Tag("flatten") => struct_def.estree.flatten = true,
AttrPart::Tag("no_type") => struct_def.estree.no_type = true,
AttrPart::Tag("custom_serialize") => struct_def.estree.custom_serialize = true,
@@ -94,6 +99,7 @@ fn parse_estree_attr(location: AttrLocation, part: AttrPart) -> Result<()> {
},
// `#[estree]` attr on enum
AttrLocation::Enum(enum_def) => match part {
AttrPart::Tag("skip") => enum_def.estree.skip = true,
AttrPart::Tag("no_rename_variants") => enum_def.estree.no_rename_variants = true,
AttrPart::Tag("custom_ts_def") => enum_def.estree.custom_ts_def = true,
_ => return Err(()),
@@ -164,7 +170,7 @@ fn generate_body_for_struct(struct_def: &StructDef, schema: &Schema) -> TokenStr
}
for field in &struct_def.fields {
if !field.estree.skip {
if !should_skip_field(field, schema) {
stmts.extend(generate_stmt_for_struct_field(field, struct_def, schema));
}
}
@@ -263,6 +269,25 @@ pub fn should_add_type_field_to_struct(struct_def: &StructDef) -> bool {
}
}
/// Get if a struct field should be skipped when serializing.
///
/// Returns `true` if either the field has an `#[estree(skip)]` attr on it,
/// or the type that the field contains has an `#[estree(skip)]` attr.
///
/// This function also used by Typescript generator.
pub fn should_skip_field(field: &FieldDef, schema: &Schema) -> bool {
if field.estree.skip {
true
} else {
let innermost_type = field.type_def(schema).innermost_type(schema);
match innermost_type {
TypeDef::Struct(struct_def) => struct_def.estree.skip,
TypeDef::Enum(enum_def) => enum_def.estree.skip,
_ => false,
}
}
}
/// Get if should flatten a struct field.
///
/// Returns `true` if either the field has an `#[estree(flatten)]` attr on it,
+2 -2
View File
@@ -7,7 +7,7 @@ use itertools::Itertools;
use crate::{
derives::estree::{
get_fieldless_variant_value, get_struct_field_name, should_add_type_field_to_struct,
should_flatten_field,
should_flatten_field, should_skip_field,
},
output::Output,
schema::{Def, EnumDef, FieldDef, Schema, StructDef, TypeDef},
@@ -93,7 +93,7 @@ fn generate_ts_type_def_for_struct(struct_def: &StructDef, schema: &Schema) -> S
let mut output_as_type = false;
for field in &struct_def.fields {
if field.estree.skip {
if should_skip_field(field, schema) {
continue;
}
@@ -4,6 +4,7 @@ pub struct ESTreeStruct {
pub rename: Option<String>,
pub via: Option<String>,
pub add_ts: Option<String>,
pub skip: bool,
pub flatten: bool,
pub no_type: bool,
pub custom_serialize: bool,
@@ -12,6 +13,7 @@ pub struct ESTreeStruct {
/// Configuration for ESTree generator on an enum.
#[derive(Default, Debug)]
pub struct ESTreeEnum {
pub skip: bool,
pub no_rename_variants: bool,
pub custom_ts_def: bool,
}