feat(ast): Label AST fields with #[ts] (#6987)

I think I found all of the AST fields that are typescript-only but I could be wrong. I did label decorators as `#[ts]` but not sure if that's correct.
This commit is contained in:
ottomated 2024-10-30 03:44:05 +00:00
parent 147e2e4858
commit 854870e296
3 changed files with 56 additions and 1 deletions

View file

@ -425,6 +425,7 @@ pub struct TaggedTemplateExpression<'a> {
pub span: Span, pub span: Span,
pub tag: Expression<'a>, pub tag: Expression<'a>,
pub quasi: TemplateLiteral<'a>, pub quasi: TemplateLiteral<'a>,
#[ts]
pub type_parameters: Option<Box<'a, TSTypeParameterInstantiation<'a>>>, pub type_parameters: Option<Box<'a, TSTypeParameterInstantiation<'a>>>,
} }
@ -544,6 +545,7 @@ pub struct PrivateFieldExpression<'a> {
pub struct CallExpression<'a> { pub struct CallExpression<'a> {
pub span: Span, pub span: Span,
pub callee: Expression<'a>, pub callee: Expression<'a>,
#[ts]
pub type_parameters: Option<Box<'a, TSTypeParameterInstantiation<'a>>>, pub type_parameters: Option<Box<'a, TSTypeParameterInstantiation<'a>>>,
pub arguments: Vec<'a, Argument<'a>>, pub arguments: Vec<'a, Argument<'a>>,
pub optional: bool, // for optional chaining pub optional: bool, // for optional chaining
@ -568,6 +570,7 @@ pub struct NewExpression<'a> {
pub span: Span, pub span: Span,
pub callee: Expression<'a>, pub callee: Expression<'a>,
pub arguments: Vec<'a, Argument<'a>>, pub arguments: Vec<'a, Argument<'a>>,
#[ts]
pub type_parameters: Option<Box<'a, TSTypeParameterInstantiation<'a>>>, pub type_parameters: Option<Box<'a, TSTypeParameterInstantiation<'a>>>,
} }
@ -1088,6 +1091,7 @@ pub struct VariableDeclaration<'a> {
pub span: Span, pub span: Span,
pub kind: VariableDeclarationKind, pub kind: VariableDeclarationKind,
pub declarations: Vec<'a, VariableDeclarator<'a>>, pub declarations: Vec<'a, VariableDeclarator<'a>>,
#[ts]
pub declare: bool, pub declare: bool,
} }
@ -1120,6 +1124,7 @@ pub struct VariableDeclarator<'a> {
pub kind: VariableDeclarationKind, pub kind: VariableDeclarationKind,
pub id: BindingPattern<'a>, pub id: BindingPattern<'a>,
pub init: Option<Expression<'a>>, pub init: Option<Expression<'a>>,
#[ts]
pub definite: bool, pub definite: bool,
} }
@ -1441,7 +1446,9 @@ pub struct BindingPattern<'a> {
)] )]
#[span] #[span]
pub kind: BindingPatternKind<'a>, pub kind: BindingPatternKind<'a>,
#[ts]
pub type_annotation: Option<Box<'a, TSTypeAnnotation<'a>>>, pub type_annotation: Option<Box<'a, TSTypeAnnotation<'a>>>,
#[ts]
pub optional: bool, pub optional: bool,
} }
@ -1582,7 +1589,9 @@ pub struct Function<'a> {
/// ``` /// ```
pub generator: bool, pub generator: bool,
pub r#async: bool, pub r#async: bool,
#[ts]
pub declare: bool, pub declare: bool,
#[ts]
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>, pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
/// Declaring `this` in a Function <https://www.typescriptlang.org/docs/handbook/2/functions.html#declaring-this-in-a-function> /// Declaring `this` in a Function <https://www.typescriptlang.org/docs/handbook/2/functions.html#declaring-this-in-a-function>
/// ///
@ -1600,12 +1609,14 @@ pub struct Function<'a> {
/// return this.admin; /// return this.admin;
/// }); /// });
/// ``` /// ```
#[ts]
pub this_param: Option<Box<'a, TSThisParameter<'a>>>, pub this_param: Option<Box<'a, TSThisParameter<'a>>>,
/// Function parameters. /// Function parameters.
/// ///
/// Does not include `this` parameters used by some TypeScript functions. /// Does not include `this` parameters used by some TypeScript functions.
pub params: Box<'a, FormalParameters<'a>>, pub params: Box<'a, FormalParameters<'a>>,
/// The TypeScript return type annotation. /// The TypeScript return type annotation.
#[ts]
pub return_type: Option<Box<'a, TSTypeAnnotation<'a>>>, pub return_type: Option<Box<'a, TSTypeAnnotation<'a>>>,
/// The function body. /// The function body.
/// ///
@ -1657,10 +1668,14 @@ pub struct FormalParameters<'a> {
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)] #[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
pub struct FormalParameter<'a> { pub struct FormalParameter<'a> {
pub span: Span, pub span: Span,
#[ts]
pub decorators: Vec<'a, Decorator<'a>>, pub decorators: Vec<'a, Decorator<'a>>,
pub pattern: BindingPattern<'a>, pub pattern: BindingPattern<'a>,
#[ts]
pub accessibility: Option<TSAccessibility>, pub accessibility: Option<TSAccessibility>,
#[ts]
pub readonly: bool, pub readonly: bool,
#[ts]
pub r#override: bool, pub r#override: bool,
} }
@ -1702,8 +1717,10 @@ pub struct ArrowFunctionExpression<'a> {
/// Is the function body an arrow expression? i.e. `() => expr` instead of `() => {}` /// Is the function body an arrow expression? i.e. `() => expr` instead of `() => {}`
pub expression: bool, pub expression: bool,
pub r#async: bool, pub r#async: bool,
#[ts]
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>, pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
pub params: Box<'a, FormalParameters<'a>>, pub params: Box<'a, FormalParameters<'a>>,
#[ts]
pub return_type: Option<Box<'a, TSTypeAnnotation<'a>>>, pub return_type: Option<Box<'a, TSTypeAnnotation<'a>>>,
/// See `expression` for whether this arrow expression returns an expression. /// See `expression` for whether this arrow expression returns an expression.
pub body: Box<'a, FunctionBody<'a>>, pub body: Box<'a, FunctionBody<'a>>,
@ -1740,10 +1757,12 @@ pub struct Class<'a> {
/// @Bar() // <-- Decorator /// @Bar() // <-- Decorator
/// class Foo {} /// class Foo {}
/// ``` /// ```
#[ts]
pub decorators: Vec<'a, Decorator<'a>>, pub decorators: Vec<'a, Decorator<'a>>,
/// Class identifier, AKA the name /// Class identifier, AKA the name
pub id: Option<BindingIdentifier<'a>>, pub id: Option<BindingIdentifier<'a>>,
#[scope(enter_before)] #[scope(enter_before)]
#[ts]
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>, pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
/// Super class. When present, this will usually be an [`IdentifierReference`]. /// Super class. When present, this will usually be an [`IdentifierReference`].
/// ///
@ -1760,6 +1779,7 @@ pub struct Class<'a> {
/// class Foo<T> extends Bar<T> {} /// class Foo<T> extends Bar<T> {}
/// // ^ /// // ^
/// ``` /// ```
#[ts]
pub super_type_parameters: Option<Box<'a, TSTypeParameterInstantiation<'a>>>, pub super_type_parameters: Option<Box<'a, TSTypeParameterInstantiation<'a>>>,
/// Interface implementation clause for TypeScript classes. /// Interface implementation clause for TypeScript classes.
/// ///
@ -1769,6 +1789,7 @@ pub struct Class<'a> {
/// class Foo implements Bar {} /// class Foo implements Bar {}
/// // ^^^ /// // ^^^
/// ``` /// ```
#[ts]
pub implements: Option<Vec<'a, TSClassImplements<'a>>>, pub implements: Option<Vec<'a, TSClassImplements<'a>>>,
pub body: Box<'a, ClassBody<'a>>, pub body: Box<'a, ClassBody<'a>>,
/// Whether the class is abstract /// Whether the class is abstract
@ -1778,6 +1799,7 @@ pub struct Class<'a> {
/// class Foo {} // true /// class Foo {} // true
/// abstract class Bar {} // false /// abstract class Bar {} // false
/// ``` /// ```
#[ts]
pub r#abstract: bool, pub r#abstract: bool,
/// Whether the class was `declare`ed /// Whether the class was `declare`ed
/// ///
@ -1785,6 +1807,7 @@ pub struct Class<'a> {
/// ```ts /// ```ts
/// declare class Foo {} /// declare class Foo {}
/// ``` /// ```
#[ts]
pub declare: bool, pub declare: bool,
/// Id of the scope created by the [`Class`], including type parameters and /// Id of the scope created by the [`Class`], including type parameters and
/// statements within the [`ClassBody`]. /// statements within the [`ClassBody`].
@ -1868,6 +1891,7 @@ pub struct MethodDefinition<'a> {
/// This will always be true when an `abstract` modifier is used on the method. /// This will always be true when an `abstract` modifier is used on the method.
pub r#type: MethodDefinitionType, pub r#type: MethodDefinitionType,
pub span: Span, pub span: Span,
#[ts]
pub decorators: Vec<'a, Decorator<'a>>, pub decorators: Vec<'a, Decorator<'a>>,
pub key: PropertyKey<'a>, pub key: PropertyKey<'a>,
#[visit(args(flags = match self.kind { #[visit(args(flags = match self.kind {
@ -1880,8 +1904,11 @@ pub struct MethodDefinition<'a> {
pub kind: MethodDefinitionKind, pub kind: MethodDefinitionKind,
pub computed: bool, pub computed: bool,
pub r#static: bool, pub r#static: bool,
#[ts]
pub r#override: bool, pub r#override: bool,
#[ts]
pub optional: bool, pub optional: bool,
#[ts]
pub accessibility: Option<TSAccessibility>, pub accessibility: Option<TSAccessibility>,
} }
@ -1903,6 +1930,7 @@ pub struct PropertyDefinition<'a> {
/// Decorators applied to the property. /// Decorators applied to the property.
/// ///
/// See [`Decorator`] for more information. /// See [`Decorator`] for more information.
#[ts]
pub decorators: Vec<'a, Decorator<'a>>, pub decorators: Vec<'a, Decorator<'a>>,
/// The expression used to declare the property. /// The expression used to declare the property.
pub key: PropertyKey<'a>, pub key: PropertyKey<'a>,
@ -1945,16 +1973,22 @@ pub struct PropertyDefinition<'a> {
/// x: number // false /// x: number // false
/// } /// }
/// ``` /// ```
#[ts]
pub declare: bool, pub declare: bool,
#[ts]
pub r#override: bool, pub r#override: bool,
/// `true` when created with an optional modifier (`?`) /// `true` when created with an optional modifier (`?`)
#[ts]
pub optional: bool, pub optional: bool,
#[ts]
pub definite: bool, pub definite: bool,
/// `true` when declared with a `readonly` modifier /// `true` when declared with a `readonly` modifier
#[ts]
pub readonly: bool, pub readonly: bool,
/// Type annotation on the property. /// Type annotation on the property.
/// ///
/// Will only ever be [`Some`] for TypeScript files. /// Will only ever be [`Some`] for TypeScript files.
#[ts]
pub type_annotation: Option<Box<'a, TSTypeAnnotation<'a>>>, pub type_annotation: Option<Box<'a, TSTypeAnnotation<'a>>>,
/// Accessibility modifier. /// Accessibility modifier.
/// ///
@ -1970,6 +2004,7 @@ pub struct PropertyDefinition<'a> {
/// readonly z // None /// readonly z // None
/// } /// }
/// ``` /// ```
#[ts]
pub accessibility: Option<TSAccessibility>, pub accessibility: Option<TSAccessibility>,
} }
@ -2116,6 +2151,7 @@ pub struct AccessorProperty<'a> {
/// Decorators applied to the accessor property. /// Decorators applied to the accessor property.
/// ///
/// See [`Decorator`] for more information. /// See [`Decorator`] for more information.
#[ts]
pub decorators: Vec<'a, Decorator<'a>>, pub decorators: Vec<'a, Decorator<'a>>,
/// The expression used to declare the property. /// The expression used to declare the property.
pub key: PropertyKey<'a>, pub key: PropertyKey<'a>,
@ -2126,10 +2162,12 @@ pub struct AccessorProperty<'a> {
/// Property was declared with a `static` modifier /// Property was declared with a `static` modifier
pub r#static: bool, pub r#static: bool,
/// Property has a `!` after its key. /// Property has a `!` after its key.
#[ts]
pub definite: bool, pub definite: bool,
/// Type annotation on the property. /// Type annotation on the property.
/// ///
/// Will only ever be [`Some`] for TypeScript files. /// Will only ever be [`Some`] for TypeScript files.
#[ts]
pub type_annotation: Option<Box<'a, TSTypeAnnotation<'a>>>, pub type_annotation: Option<Box<'a, TSTypeAnnotation<'a>>>,
/// Accessibility modifier. /// Accessibility modifier.
/// ///
@ -2145,6 +2183,7 @@ pub struct AccessorProperty<'a> {
/// accessor z // None /// accessor z // None
/// } /// }
/// ``` /// ```
#[ts]
pub accessibility: Option<TSAccessibility>, pub accessibility: Option<TSAccessibility>,
} }
@ -2166,8 +2205,10 @@ pub struct ImportDeclaration<'a> {
pub specifiers: Option<Vec<'a, ImportDeclarationSpecifier<'a>>>, pub specifiers: Option<Vec<'a, ImportDeclarationSpecifier<'a>>>,
pub source: StringLiteral<'a>, pub source: StringLiteral<'a>,
/// Some(vec![]) for empty assertion /// Some(vec![]) for empty assertion
#[ts]
pub with_clause: Option<Box<'a, WithClause<'a>>>, pub with_clause: Option<Box<'a, WithClause<'a>>>,
/// `import type { foo } from 'bar'` /// `import type { foo } from 'bar'`
#[ts]
pub import_kind: ImportOrExportKind, pub import_kind: ImportOrExportKind,
} }
@ -2204,6 +2245,7 @@ pub struct ImportSpecifier<'a> {
/// // ^^^ /// // ^^^
/// ``` /// ```
pub local: BindingIdentifier<'a>, pub local: BindingIdentifier<'a>,
#[ts]
pub import_kind: ImportOrExportKind, pub import_kind: ImportOrExportKind,
} }
@ -2283,8 +2325,10 @@ pub struct ExportNamedDeclaration<'a> {
pub specifiers: Vec<'a, ExportSpecifier<'a>>, pub specifiers: Vec<'a, ExportSpecifier<'a>>,
pub source: Option<StringLiteral<'a>>, pub source: Option<StringLiteral<'a>>,
/// `export type { foo }` /// `export type { foo }`
#[ts]
pub export_kind: ImportOrExportKind, pub export_kind: ImportOrExportKind,
/// Some(vec![]) for empty assertion /// Some(vec![]) for empty assertion
#[ts]
pub with_clause: Option<Box<'a, WithClause<'a>>>, pub with_clause: Option<Box<'a, WithClause<'a>>>,
} }
@ -2324,7 +2368,9 @@ pub struct ExportAllDeclaration<'a> {
pub exported: Option<ModuleExportName<'a>>, pub exported: Option<ModuleExportName<'a>>,
pub source: StringLiteral<'a>, pub source: StringLiteral<'a>,
/// Will be `Some(vec![])` for empty assertion /// Will be `Some(vec![])` for empty assertion
#[ts]
pub with_clause: Option<Box<'a, WithClause<'a>>>, // Some(vec![]) for empty assertion pub with_clause: Option<Box<'a, WithClause<'a>>>, // Some(vec![]) for empty assertion
#[ts]
pub export_kind: ImportOrExportKind, // `export type *` pub export_kind: ImportOrExportKind, // `export type *`
} }
@ -2346,6 +2392,7 @@ pub struct ExportSpecifier<'a> {
pub span: Span, pub span: Span,
pub local: ModuleExportName<'a>, pub local: ModuleExportName<'a>,
pub exported: ModuleExportName<'a>, pub exported: ModuleExportName<'a>,
#[ts]
pub export_kind: ImportOrExportKind, // `export type *` pub export_kind: ImportOrExportKind, // `export type *`
} }

View file

@ -78,6 +78,7 @@ pub struct JSXOpeningElement<'a> {
/// List of JSX attributes. In React-like applications, these become props. /// List of JSX attributes. In React-like applications, these become props.
pub attributes: Vec<'a, JSXAttributeItem<'a>>, pub attributes: Vec<'a, JSXAttributeItem<'a>>,
/// Type parameters for generic JSX elements. /// Type parameters for generic JSX elements.
#[ts]
pub type_parameters: Option<Box<'a, TSTypeParameterInstantiation<'a>>>, pub type_parameters: Option<Box<'a, TSTypeParameterInstantiation<'a>>>,
} }

View file

@ -53,6 +53,10 @@ mod ast;
/// However, Instead of expanding the derive at compile-time, We do this process on PR submits via `ast_tools` code generation. /// However, Instead of expanding the derive at compile-time, We do this process on PR submits via `ast_tools` code generation.
/// These derived implementations would be output in the `crates/oxc_ast/src/generated` directory. /// These derived implementations would be output in the `crates/oxc_ast/src/generated` directory.
/// ///
/// ## `#[ts]`:
///
/// Marks a struct field as only relevant for TypeScript ASTs.
///
/// # Derive Helper Attributes: /// # Derive Helper Attributes:
/// ///
/// These are helper attributes that are only meaningful when their respective trait is derived via `generate_derive`. /// These are helper attributes that are only meaningful when their respective trait is derived via `generate_derive`.
@ -81,7 +85,10 @@ pub fn ast(_args: TokenStream, input: TokenStream) -> TokenStream {
/// The only purpose is to allow the occurrence of helper attributes used with the `tasks/ast_tools`. /// The only purpose is to allow the occurrence of helper attributes used with the `tasks/ast_tools`.
/// ///
/// Read [`macro@ast`] for further details. /// Read [`macro@ast`] for further details.
#[proc_macro_derive(Ast, attributes(scope, visit, span, generate_derive, clone_in, estree, tsify))] #[proc_macro_derive(
Ast,
attributes(scope, visit, span, generate_derive, clone_in, estree, tsify, ts)
)]
pub fn ast_derive(_input: TokenStream) -> TokenStream { pub fn ast_derive(_input: TokenStream) -> TokenStream {
TokenStream::new() TokenStream::new()
} }