Skip to content

Commit 6628e1d

Browse files
committed
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.
1 parent 3407d47 commit 6628e1d

File tree

8 files changed

+34
-25
lines changed

8 files changed

+34
-25
lines changed

crates/oxc_ast/src/ast/js.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ pub struct Program<'a> {
4040
pub hashbang: Option<Hashbang<'a>>,
4141
pub directives: Vec<'a, Directive<'a>>,
4242
pub body: Vec<'a, Statement<'a>>,
43-
#[estree(skip)]
4443
pub scope_id: Cell<Option<ScopeId>>,
4544
}
4645

@@ -225,7 +224,6 @@ pub struct IdentifierReference<'a> {
225224
/// Identifies what identifier this refers to, and how it is used. This is
226225
/// set in the bind step of semantic analysis, and will always be [`None`]
227226
/// immediately after parsing.
228-
#[estree(skip)]
229227
pub reference_id: Cell<Option<ReferenceId>>,
230228
}
231229

@@ -248,7 +246,6 @@ pub struct BindingIdentifier<'a> {
248246
/// you choose to skip semantic analysis, this will always be [`None`].
249247
///
250248
/// [`semantic analysis`]: <https://docs.rs/oxc_semantic/latest/oxc_semantic/struct.SemanticBuilder.html>
251-
#[estree(skip)]
252249
pub symbol_id: Cell<Option<SymbolId>>,
253250
}
254251

@@ -1039,7 +1036,6 @@ pub struct Hashbang<'a> {
10391036
pub struct BlockStatement<'a> {
10401037
pub span: Span,
10411038
pub body: Vec<'a, Statement<'a>>,
1042-
#[estree(skip)]
10431039
pub scope_id: Cell<Option<ScopeId>>,
10441040
}
10451041

@@ -1182,7 +1178,6 @@ pub struct ForStatement<'a> {
11821178
pub test: Option<Expression<'a>>,
11831179
pub update: Option<Expression<'a>>,
11841180
pub body: Statement<'a>,
1185-
#[estree(skip)]
11861181
pub scope_id: Cell<Option<ScopeId>>,
11871182
}
11881183

@@ -1212,7 +1207,6 @@ pub struct ForInStatement<'a> {
12121207
pub left: ForStatementLeft<'a>,
12131208
pub right: Expression<'a>,
12141209
pub body: Statement<'a>,
1215-
#[estree(skip)]
12161210
pub scope_id: Cell<Option<ScopeId>>,
12171211
}
12181212

@@ -1242,7 +1236,6 @@ pub struct ForOfStatement<'a> {
12421236
pub left: ForStatementLeft<'a>,
12431237
pub right: Expression<'a>,
12441238
pub body: Statement<'a>,
1245-
#[estree(skip)]
12461239
pub scope_id: Cell<Option<ScopeId>>,
12471240
}
12481241

@@ -1293,7 +1286,6 @@ pub struct SwitchStatement<'a> {
12931286
pub discriminant: Expression<'a>,
12941287
#[scope(enter_before)]
12951288
pub cases: Vec<'a, SwitchCase<'a>>,
1296-
#[estree(skip)]
12971289
pub scope_id: Cell<Option<ScopeId>>,
12981290
}
12991291

@@ -1382,7 +1374,6 @@ pub struct CatchClause<'a> {
13821374
pub param: Option<CatchParameter<'a>>,
13831375
/// The statements run when an error is caught
13841376
pub body: Box<'a, BlockStatement<'a>>,
1385-
#[estree(skip)]
13861377
pub scope_id: Cell<Option<ScopeId>>,
13871378
}
13881379

@@ -1615,7 +1606,6 @@ pub struct Function<'a> {
16151606
/// }
16161607
/// ```
16171608
pub body: Option<Box<'a, FunctionBody<'a>>>,
1618-
#[estree(skip)]
16191609
pub scope_id: Cell<Option<ScopeId>>,
16201610
}
16211611

@@ -1706,7 +1696,6 @@ pub struct ArrowFunctionExpression<'a> {
17061696
pub return_type: Option<Box<'a, TSTypeAnnotation<'a>>>,
17071697
/// See `expression` for whether this arrow expression returns an expression.
17081698
pub body: Box<'a, FunctionBody<'a>>,
1709-
#[estree(skip)]
17101699
pub scope_id: Cell<Option<ScopeId>>,
17111700
}
17121701

@@ -1792,7 +1781,6 @@ pub struct Class<'a> {
17921781
pub declare: bool,
17931782
/// Id of the scope created by the [`Class`], including type parameters and
17941783
/// statements within the [`ClassBody`].
1795-
#[estree(skip)]
17961784
pub scope_id: Cell<Option<ScopeId>>,
17971785
}
17981786

@@ -2043,7 +2031,6 @@ pub struct PrivateIdentifier<'a> {
20432031
pub struct StaticBlock<'a> {
20442032
pub span: Span,
20452033
pub body: Vec<'a, Statement<'a>>,
2046-
#[estree(skip)]
20472034
pub scope_id: Cell<Option<ScopeId>>,
20482035
}
20492036

crates/oxc_ast/src/ast/ts.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ pub struct TSEnumDeclaration<'a> {
7171
/// `true` for const enums
7272
pub r#const: bool,
7373
pub declare: bool,
74-
#[estree(skip)]
7574
pub scope_id: Cell<Option<ScopeId>>,
7675
}
7776

@@ -302,7 +301,6 @@ pub struct TSConditionalType<'a> {
302301
/// The type evaluated to if the test is false.
303302
#[scope(exit_before)]
304303
pub false_type: TSType<'a>,
305-
#[estree(skip)]
306304
pub scope_id: Cell<Option<ScopeId>>,
307305
}
308306

@@ -834,7 +832,6 @@ pub struct TSTypeAliasDeclaration<'a> {
834832
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
835833
pub type_annotation: TSType<'a>,
836834
pub declare: bool,
837-
#[estree(skip)]
838835
pub scope_id: Cell<Option<ScopeId>>,
839836
}
840837

@@ -897,7 +894,6 @@ pub struct TSInterfaceDeclaration<'a> {
897894
pub body: Box<'a, TSInterfaceBody<'a>>,
898895
/// `true` for `declare interface Foo {}`
899896
pub declare: bool,
900-
#[estree(skip)]
901897
pub scope_id: Cell<Option<ScopeId>>,
902898
}
903899

@@ -1015,7 +1011,6 @@ pub struct TSMethodSignature<'a> {
10151011
pub this_param: Option<Box<'a, TSThisParameter<'a>>>,
10161012
pub params: Box<'a, FormalParameters<'a>>,
10171013
pub return_type: Option<Box<'a, TSTypeAnnotation<'a>>>,
1018-
#[estree(skip)]
10191014
pub scope_id: Cell<Option<ScopeId>>,
10201015
}
10211016

@@ -1029,7 +1024,6 @@ pub struct TSConstructSignatureDeclaration<'a> {
10291024
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
10301025
pub params: Box<'a, FormalParameters<'a>>,
10311026
pub return_type: Option<Box<'a, TSTypeAnnotation<'a>>>,
1032-
#[estree(skip)]
10331027
pub scope_id: Cell<Option<ScopeId>>,
10341028
}
10351029

@@ -1153,7 +1147,6 @@ pub struct TSModuleDeclaration<'a> {
11531147
/// ```
11541148
pub kind: TSModuleDeclarationKind,
11551149
pub declare: bool,
1156-
#[estree(skip)]
11571150
pub scope_id: Cell<Option<ScopeId>>,
11581151
}
11591152

@@ -1423,7 +1416,6 @@ pub struct TSMappedType<'a> {
14231416
/// type Qux = { [P in keyof T]: T[P] } // None
14241417
/// ```
14251418
pub readonly: TSMappedTypeModifierOperator,
1426-
#[estree(skip)]
14271419
pub scope_id: Cell<Option<ScopeId>>,
14281420
}
14291421

crates/oxc_syntax/src/reference.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use oxc_ast_macros::ast;
1515
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
1616
#[clone_in(default)]
1717
#[content_eq(skip)]
18+
#[estree(skip)]
1819
pub struct ReferenceId(NonMaxU32);
1920

2021
impl Idx for ReferenceId {

crates/oxc_syntax/src/scope.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use oxc_ast_macros::ast;
1111
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
1212
#[clone_in(default)]
1313
#[content_eq(skip)]
14+
#[estree(skip)]
1415
pub struct ScopeId(NonMaxU32);
1516

1617
impl ScopeId {

crates/oxc_syntax/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use oxc_ast_macros::ast;
1111
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
1212
#[clone_in(default)]
1313
#[content_eq(skip)]
14+
#[estree(skip)]
1415
pub struct SymbolId(NonMaxU32);
1516

1617
impl SymbolId {

tasks/ast_tools/src/derives/estree.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,12 @@ impl Derive for DeriveESTree {
3535
}
3636

3737
/// Register that accept `#[estree]` attr on structs, enums, struct fields, or enum variants.
38+
/// Allow attr on structs and enums which don't derive this trait.
3839
fn attrs(&self) -> &[(&'static str, AttrPositions)] {
39-
&[("estree", attr_positions!(Struct | Enum | StructField | EnumVariant))]
40+
&[(
41+
"estree",
42+
attr_positions!(StructMaybeDerived | EnumMaybeDerived | StructField | EnumVariant),
43+
)]
4044
}
4145

4246
/// Parse `#[estree]` attr.
@@ -84,6 +88,7 @@ fn parse_estree_attr(location: AttrLocation, part: AttrPart) -> Result<()> {
8488
match location {
8589
// `#[estree]` attr on struct
8690
AttrLocation::Struct(struct_def) => match part {
91+
AttrPart::Tag("skip") => struct_def.estree.skip = true,
8792
AttrPart::Tag("flatten") => struct_def.estree.flatten = true,
8893
AttrPart::Tag("no_type") => struct_def.estree.no_type = true,
8994
AttrPart::Tag("custom_serialize") => struct_def.estree.custom_serialize = true,
@@ -94,6 +99,7 @@ fn parse_estree_attr(location: AttrLocation, part: AttrPart) -> Result<()> {
9499
},
95100
// `#[estree]` attr on enum
96101
AttrLocation::Enum(enum_def) => match part {
102+
AttrPart::Tag("skip") => enum_def.estree.skip = true,
97103
AttrPart::Tag("no_rename_variants") => enum_def.estree.no_rename_variants = true,
98104
AttrPart::Tag("custom_ts_def") => enum_def.estree.custom_ts_def = true,
99105
_ => return Err(()),
@@ -164,7 +170,7 @@ fn generate_body_for_struct(struct_def: &StructDef, schema: &Schema) -> TokenStr
164170
}
165171

166172
for field in &struct_def.fields {
167-
if !field.estree.skip {
173+
if !should_skip_field(field, schema) {
168174
stmts.extend(generate_stmt_for_struct_field(field, struct_def, schema));
169175
}
170176
}
@@ -263,6 +269,25 @@ pub fn should_add_type_field_to_struct(struct_def: &StructDef) -> bool {
263269
}
264270
}
265271

272+
/// Get if a struct field should be skipped when serializing.
273+
///
274+
/// Returns `true` if either the field has an `#[estree(skip)]` attr on it,
275+
/// or the type that the field contains has an `#[estree(skip)]` attr.
276+
///
277+
/// This function also used by Typescript generator.
278+
pub fn should_skip_field(field: &FieldDef, schema: &Schema) -> bool {
279+
if field.estree.skip {
280+
true
281+
} else {
282+
let innermost_type = field.type_def(schema).innermost_type(schema);
283+
match innermost_type {
284+
TypeDef::Struct(struct_def) => struct_def.estree.skip,
285+
TypeDef::Enum(enum_def) => enum_def.estree.skip,
286+
_ => false,
287+
}
288+
}
289+
}
290+
266291
/// Get if should flatten a struct field.
267292
///
268293
/// Returns `true` if either the field has an `#[estree(flatten)]` attr on it,

tasks/ast_tools/src/generators/typescript.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use itertools::Itertools;
77
use crate::{
88
derives::estree::{
99
get_fieldless_variant_value, get_struct_field_name, should_add_type_field_to_struct,
10-
should_flatten_field,
10+
should_flatten_field, should_skip_field,
1111
},
1212
output::Output,
1313
schema::{Def, EnumDef, FieldDef, Schema, StructDef, TypeDef},
@@ -93,7 +93,7 @@ fn generate_ts_type_def_for_struct(struct_def: &StructDef, schema: &Schema) -> S
9393

9494
let mut output_as_type = false;
9595
for field in &struct_def.fields {
96-
if field.estree.skip {
96+
if should_skip_field(field, schema) {
9797
continue;
9898
}
9999

tasks/ast_tools/src/schema/extensions/estree.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pub struct ESTreeStruct {
44
pub rename: Option<String>,
55
pub via: Option<String>,
66
pub add_ts: Option<String>,
7+
pub skip: bool,
78
pub flatten: bool,
89
pub no_type: bool,
910
pub custom_serialize: bool,
@@ -12,6 +13,7 @@ pub struct ESTreeStruct {
1213
/// Configuration for ESTree generator on an enum.
1314
#[derive(Default, Debug)]
1415
pub struct ESTreeEnum {
16+
pub skip: bool,
1517
pub no_rename_variants: bool,
1618
pub custom_ts_def: bool,
1719
}

0 commit comments

Comments
 (0)