Skip to content

Commit

Permalink
feat(ast): add #[ast] attribute to non-visited AST types. (#4309)
Browse files Browse the repository at this point in the history
Mark everything mentioned in #3815 (comment) as AST.

We now error on the occurrence of non-ast items in the source of truth. It doesn't make sure that all fields and variants are `#[ast]` and therefore `repr_stable` but there are only a handful of non-AST types used here(mainly Atom and Span). Since we don't have access to the external types we can't make sure of it unless we find a way to const assert it.

The best we can do until then is to check all field/variant types to be either `#[ast]` or in a white list. I can add this check to the codegen in an upcoming PR.
  • Loading branch information
rzvxa committed Jul 20, 2024
1 parent 5f1c7ec commit d345b84
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 1 deletion.
10 changes: 10 additions & 0 deletions crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ pub enum PropertyKey<'a> {
}
}

#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(rename_all = "lowercase"))]
Expand Down Expand Up @@ -369,6 +370,7 @@ pub struct TemplateElement<'a> {
}

/// See [template-strings-cooked-vs-raw](https://exploringjs.com/impatient-js/ch_template-literals.html#template-strings-cooked-vs-raw)
#[ast]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
pub struct TemplateElementValue<'a> {
Expand Down Expand Up @@ -1012,6 +1014,7 @@ pub struct VariableDeclaration<'a> {
pub declare: bool,
}

#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(rename_all = "lowercase"))]
Expand Down Expand Up @@ -1475,6 +1478,7 @@ pub struct Function<'a> {
pub scope_id: Cell<Option<ScopeId>>,
}

#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
pub enum FunctionType {
Expand Down Expand Up @@ -1518,6 +1522,7 @@ pub struct FormalParameter<'a> {
pub r#override: bool,
}

#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
pub enum FormalParameterKind {
Expand Down Expand Up @@ -1602,6 +1607,7 @@ pub struct Class<'a> {
pub scope_id: Cell<Option<ScopeId>>,
}

#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
pub enum ClassType {
Expand Down Expand Up @@ -1656,6 +1662,7 @@ pub struct MethodDefinition<'a> {
pub accessibility: Option<TSAccessibility>,
}

#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
pub enum MethodDefinitionType {
Expand Down Expand Up @@ -1685,13 +1692,15 @@ pub struct PropertyDefinition<'a> {
pub accessibility: Option<TSAccessibility>,
}

#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
pub enum PropertyDefinitionType {
PropertyDefinition,
TSAbstractPropertyDefinition,
}

#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(rename_all = "lowercase"))]
Expand Down Expand Up @@ -1761,6 +1770,7 @@ macro_rules! match_module_declaration {
}
pub use match_module_declaration;

#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
pub enum AccessorPropertyType {
Expand Down
2 changes: 2 additions & 0 deletions crates/oxc_ast/src/ast/jsx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub struct JSXFragment<'a> {
pub children: Vec<'a, JSXChild<'a>>,
}

#[ast]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(tag = "type"))]
Expand All @@ -78,6 +79,7 @@ pub struct JSXOpeningFragment {
pub span: Span,
}

#[ast]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(tag = "type"))]
Expand Down
2 changes: 2 additions & 0 deletions crates/oxc_ast/src/ast/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,15 @@ pub struct RegExpLiteral<'a> {
pub regex: RegExp<'a>,
}

#[ast]
#[derive(Debug, Clone, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
pub struct RegExp<'a> {
pub pattern: Atom<'a>,
pub flags: RegExpFlags,
}

#[ast]
#[derive(Debug, Clone, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
pub struct EmptyObject;
Expand Down
6 changes: 6 additions & 0 deletions crates/oxc_ast/src/ast/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ pub struct TSTypeOperator<'a> {
pub type_annotation: TSType<'a>,
}

#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(rename_all = "lowercase"))]
Expand Down Expand Up @@ -621,6 +622,7 @@ pub struct TSTypeAliasDeclaration<'a> {
pub scope_id: Cell<Option<ScopeId>>,
}

#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(rename_all = "lowercase"))]
Expand Down Expand Up @@ -722,6 +724,7 @@ pub struct TSCallSignatureDeclaration<'a> {
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
}

#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(rename_all = "lowercase"))]
Expand Down Expand Up @@ -838,6 +841,7 @@ pub struct TSModuleDeclaration<'a> {
pub scope_id: Cell<Option<ScopeId>>,
}

#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(rename_all = "lowercase"))]
Expand Down Expand Up @@ -1019,6 +1023,7 @@ pub struct TSMappedType<'a> {
pub scope_id: Cell<Option<ScopeId>>,
}

#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(rename_all = "camelCase"))]
Expand Down Expand Up @@ -1172,6 +1177,7 @@ pub struct TSInstantiationExpression<'a> {
pub type_parameters: Box<'a, TSTypeParameterInstantiation<'a>>,
}

#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(rename_all = "camelCase"))]
Expand Down
2 changes: 1 addition & 1 deletion tasks/ast_codegen/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ pub fn analyze(type_def: &TypeRef) -> Result<()> {
// AST without visit!
type_def.borrow_mut().set_ast(true)?;
}
Some(AstAttr::None) => { /* non AST types */ }
Some(AstAttr::None) => return Err(String::from("All `enums` and `structs` defined in the source of truth should be marked with an `#[ast]` attribute!")),
None => { /* unrelated items like `use`, `type` and `macro` definitions */ }
}

Expand Down

0 comments on commit d345b84

Please sign in to comment.