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
13 changes: 0 additions & 13 deletions crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>>,
}

Expand Down Expand Up @@ -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>>,
}

Expand All @@ -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>>,
}

Expand Down Expand Up @@ -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>>,
}

Expand Down Expand Up @@ -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>>,
}

Expand Down Expand Up @@ -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>>,
}

Expand Down Expand Up @@ -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>>,
}

Expand Down Expand Up @@ -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>>,
}

Expand Down Expand Up @@ -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>>,
}

Expand Down Expand Up @@ -1615,7 +1606,6 @@ pub struct Function<'a> {
/// }
/// ```
pub body: Option<Box<'a, FunctionBody<'a>>>,
#[estree(skip)]
pub scope_id: Cell<Option<ScopeId>>,
}

Expand Down Expand Up @@ -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>>,
}

Expand Down Expand Up @@ -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>>,
}

Expand Down Expand Up @@ -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>>,
}

Expand Down
8 changes: 0 additions & 8 deletions crates/oxc_ast/src/ast/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>>,
}

Expand Down Expand Up @@ -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>>,
}

Expand Down Expand Up @@ -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>>,
}

Expand Down Expand Up @@ -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>>,
}

Expand Down Expand Up @@ -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>>,
}

Expand All @@ -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>>,
}

Expand Down Expand Up @@ -1153,7 +1147,6 @@ pub struct TSModuleDeclaration<'a> {
/// ```
pub kind: TSModuleDeclarationKind,
pub declare: bool,
#[estree(skip)]
pub scope_id: Cell<Option<ScopeId>>,
}

Expand Down Expand Up @@ -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>>,
}

Expand Down
1 change: 1 addition & 0 deletions crates/oxc_syntax/src/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_syntax/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_syntax/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
29 changes: 27 additions & 2 deletions tasks/ast_tools/src/derives/estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -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(()),
Expand Down Expand Up @@ -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));
}
}
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions tasks/ast_tools/src/generators/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 2 additions & 0 deletions tasks/ast_tools/src/schema/extensions/estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
}
Expand Down
Loading