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
5 changes: 2 additions & 3 deletions crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ pub struct ThisExpression {
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ESTree)]
pub struct ArrayExpression<'a> {
pub span: Span,
#[estree(ts_type = "Array<SpreadElement | Expression | null>")]
pub elements: Vec<'a, ArrayExpressionElement<'a>>,
/// Array trailing comma
/// <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Trailing_commas#arrays>
Expand All @@ -298,7 +297,6 @@ inherit_variants! {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ESTree)]
#[estree(no_ts_def)]
pub enum ArrayExpressionElement<'a> {
/// `...[3, 4]` in `const array = [1, 2, ...[3, 4], null];`
SpreadElement(Box<'a, SpreadElement<'a>>) = 64,
Expand All @@ -318,7 +316,8 @@ pub enum ArrayExpressionElement<'a> {
/// Serialized as `null` in JSON AST. See `serialize.rs`.
#[ast(visit)]
#[derive(Debug, Clone)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ESTree)]
#[estree(custom_serialize, ts_alias = "null")]
pub struct Elision {
pub span: Span,
}
Expand Down
4 changes: 3 additions & 1 deletion npm/oxc-types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,11 @@ export interface ThisExpression extends Span {

export interface ArrayExpression extends Span {
type: 'ArrayExpression';
elements: Array<SpreadElement | Expression | null>;
elements: Array<ArrayExpressionElement>;
}

export type ArrayExpressionElement = SpreadElement | null | Expression;

export interface ObjectExpression extends Span {
type: 'ObjectExpression';
properties: Array<ObjectPropertyKind>;
Expand Down
5 changes: 2 additions & 3 deletions tasks/ast_tools/src/derives/estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,8 @@ fn parse_estree_attr(location: AttrLocation, part: AttrPart) -> Result<()> {
AttrPart::String("custom_ts_def", value) => {
struct_def.estree.custom_ts_def = Some(value);
}
AttrPart::String("add_ts_def", value) => {
struct_def.estree.add_ts_def = Some(value);
}
AttrPart::String("ts_alias", value) => struct_def.estree.ts_alias = Some(value),
AttrPart::String("add_ts_def", value) => struct_def.estree.add_ts_def = Some(value),
AttrPart::String("rename", value) => struct_def.estree.rename = Some(value),
AttrPart::String("via", value) => struct_def.estree.via = Some(value),
_ => return Err(()),
Expand Down
13 changes: 12 additions & 1 deletion tasks/ast_tools/src/generators/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ fn generate_ts_type_def(type_def: &TypeDef, code: &mut String, schema: &Schema)

/// Generate Typescript type definition for a struct.
fn generate_ts_type_def_for_struct(struct_def: &StructDef, schema: &Schema) -> Option<String> {
// If struct marked with `#[estree(ts_alias = "...")]`, then it needs no type def
if struct_def.estree.ts_alias.is_some() {
return None;
}

// If struct is marked as `#[estree(flatten)]`, and only has a single field which isn't skipped,
// don't generate a type def. That single field will be inserted inline into structs which include
// this one rather than them extending this type.
Expand Down Expand Up @@ -252,7 +257,13 @@ fn generate_ts_type_def_for_enum(enum_def: &EnumDef, schema: &Schema) -> String
/// Get TS type name for a type.
fn ts_type_name<'s>(type_def: &'s TypeDef, schema: &'s Schema) -> Cow<'s, str> {
match type_def {
TypeDef::Struct(struct_def) => Cow::Borrowed(struct_def.name()),
TypeDef::Struct(struct_def) => {
if let Some(ts_alias) = &struct_def.estree.ts_alias {
Cow::Borrowed(ts_alias)
} else {
Cow::Borrowed(struct_def.name())
}
}
TypeDef::Enum(enum_def) => Cow::Borrowed(enum_def.name()),
TypeDef::Primitive(primitive_def) => Cow::Borrowed(match primitive_def.name() {
#[rustfmt::skip]
Expand Down
4 changes: 4 additions & 0 deletions tasks/ast_tools/src/schema/extensions/estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ pub struct ESTreeStruct {
/// Custom TS type definition. Does not include `export`.
/// Empty string if type should not have a TS type definition.
pub custom_ts_def: Option<String>,
/// TS alias.
/// e.g. `#[estree(ts_alias = "null")]` means this type won't have a type def generated,
/// and any struct / enum referencing it will substitute `null` as the type.
pub ts_alias: Option<String>,
/// Additional custom TS type definition to add along with the generated one.
/// Does not include `export`.
pub add_ts_def: Option<String>,
Expand Down
Loading