Skip to content

Commit

Permalink
feat(ast): remove explicit untagged marker on enums (oxc-project#6915)
Browse files Browse the repository at this point in the history
This assumes that any enums with exactly one field in each variant are untagged, and they're tagged otherwise.
  • Loading branch information
ottomated authored and Orenbek committed Oct 28, 2024
1 parent c94c9f6 commit 8a647dd
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 58 deletions.
24 changes: 1 addition & 23 deletions crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ inherit_variants! {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum Expression<'a> {
/// See [`BooleanLiteral`] for AST node details.
BooleanLiteral(Box<'a, BooleanLiteral>) = 0,
Expand Down Expand Up @@ -312,7 +311,7 @@ inherit_variants! {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged, custom_ts_def)]
#[estree(custom_ts_def)]
pub enum ArrayExpressionElement<'a> {
/// `...[3, 4]` in `const array = [1, 2, ...[3, 4], null];`
SpreadElement(Box<'a, SpreadElement<'a>>) = 64,
Expand Down Expand Up @@ -356,7 +355,6 @@ pub struct ObjectExpression<'a> {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum ObjectPropertyKind<'a> {
/// `a: 1` in `const obj = { a: 1 };`
ObjectProperty(Box<'a, ObjectProperty<'a>>) = 0,
Expand Down Expand Up @@ -391,7 +389,6 @@ inherit_variants! {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum PropertyKey<'a> {
/// `a` in `const obj = { a: 1 }; obj.a;`
StaticIdentifier(Box<'a, IdentifierName<'a>>) = 64,
Expand Down Expand Up @@ -479,7 +476,6 @@ pub struct TemplateElementValue<'a> {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum MemberExpression<'a> {
/// `ar[0]` in `const ar = [1, 2]; ar[0];`
ComputedMemberExpression(Box<'a, ComputedMemberExpression<'a>>) = 48,
Expand Down Expand Up @@ -628,7 +624,6 @@ inherit_variants! {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum Argument<'a> {
/// `...[1, 2]` in `const arr = [...[1, 2]];`
SpreadElement(Box<'a, SpreadElement<'a>>) = 64,
Expand Down Expand Up @@ -744,7 +739,6 @@ inherit_variants! {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum AssignmentTarget<'a> {
// `SimpleAssignmentTarget` variants added here by `inherit_variants!` macro
@inherit SimpleAssignmentTarget
Expand All @@ -762,7 +756,6 @@ inherit_variants! {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum SimpleAssignmentTarget<'a> {
AssignmentTargetIdentifier(Box<'a, IdentifierReference<'a>>) = 0,
TSAsExpression(Box<'a, TSAsExpression<'a>>) = 1,
Expand Down Expand Up @@ -816,7 +809,6 @@ pub use match_simple_assignment_target;
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum AssignmentTargetPattern<'a> {
ArrayAssignmentTarget(Box<'a, ArrayAssignmentTarget<'a>>) = 8,
ObjectAssignmentTarget(Box<'a, ObjectAssignmentTarget<'a>>) = 9,
Expand Down Expand Up @@ -888,7 +880,6 @@ inherit_variants! {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum AssignmentTargetMaybeDefault<'a> {
AssignmentTargetWithDefault(Box<'a, AssignmentTargetWithDefault<'a>>) = 16,
// `AssignmentTarget` variants added here by `inherit_variants!` macro
Expand All @@ -909,7 +900,6 @@ pub struct AssignmentTargetWithDefault<'a> {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum AssignmentTargetProperty<'a> {
AssignmentTargetPropertyIdentifier(Box<'a, AssignmentTargetPropertyIdentifier<'a>>) = 0,
AssignmentTargetPropertyProperty(Box<'a, AssignmentTargetPropertyProperty<'a>>) = 1,
Expand Down Expand Up @@ -997,7 +987,6 @@ inherit_variants! {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum ChainElement<'a> {
CallExpression(Box<'a, CallExpression<'a>>) = 0,
// `MemberExpression` variants added here by `inherit_variants!` macro
Expand Down Expand Up @@ -1027,7 +1016,6 @@ inherit_variants! {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum Statement<'a> {
// Statements
BlockStatement(Box<'a, BlockStatement<'a>>) = 0,
Expand Down Expand Up @@ -1102,7 +1090,6 @@ pub struct BlockStatement<'a> {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum Declaration<'a> {
VariableDeclaration(Box<'a, VariableDeclaration<'a>>) = 32,
#[visit(args(flags = ScopeFlags::Function))]
Expand Down Expand Up @@ -1259,7 +1246,6 @@ inherit_variants! {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum ForStatementInit<'a> {
VariableDeclaration(Box<'a, VariableDeclaration<'a>>) = 64,
// `Expression` variants added here by `inherit_variants!` macro
Expand Down Expand Up @@ -1292,7 +1278,6 @@ inherit_variants! {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum ForStatementLeft<'a> {
VariableDeclaration(Box<'a, VariableDeclaration<'a>>) = 16,
// `AssignmentTarget` variants added here by `inherit_variants!` macro
Expand Down Expand Up @@ -1525,7 +1510,6 @@ pub struct BindingPattern<'a> {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum BindingPatternKind<'a> {
/// `const a = 1`
BindingIdentifier(Box<'a, BindingIdentifier<'a>>) = 0,
Expand Down Expand Up @@ -1928,7 +1912,6 @@ pub struct ClassBody<'a> {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum ClassElement<'a> {
StaticBlock(Box<'a, StaticBlock<'a>>) = 0,
/// Class Methods
Expand Down Expand Up @@ -2150,7 +2133,6 @@ pub struct StaticBlock<'a> {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum ModuleDeclaration<'a> {
/// `import hello from './world.js';`
/// `import * as t from './world.js';`
Expand Down Expand Up @@ -2269,7 +2251,6 @@ pub struct ImportDeclaration<'a> {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum ImportDeclarationSpecifier<'a> {
/// import {imported} from "source"
/// import {imported as local} from "source"
Expand Down Expand Up @@ -2359,7 +2340,6 @@ pub struct ImportAttribute<'a> {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum ImportAttributeKey<'a> {
Identifier(IdentifierName<'a>) = 0,
StringLiteral(StringLiteral<'a>) = 1,
Expand Down Expand Up @@ -2464,7 +2444,6 @@ inherit_variants! {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum ExportDefaultDeclarationKind<'a> {
#[visit(args(flags = ScopeFlags::Function))]
FunctionDeclaration(Box<'a, Function<'a>>) = 64,
Expand All @@ -2487,7 +2466,6 @@ pub enum ExportDefaultDeclarationKind<'a> {
#[ast(visit)]
#[derive(Debug, Clone)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum ModuleExportName<'a> {
IdentifierName(IdentifierName<'a>) = 0,
/// For `local` in `ExportSpecifier`: `foo` in `export { foo }`
Expand Down
7 changes: 0 additions & 7 deletions crates/oxc_ast/src/ast/jsx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ pub struct JSXClosingFragment {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash)]
#[estree(untagged)]
pub enum JSXElementName<'a> {
/// `<div />`
Identifier(Box<'a, JSXIdentifier<'a>>) = 0,
Expand Down Expand Up @@ -233,7 +232,6 @@ pub struct JSXMemberExpression<'a> {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash)]
#[estree(untagged)]
pub enum JSXMemberExpressionObject<'a> {
/// `<Apple.Orange />`
IdentifierReference(Box<'a, IdentifierReference<'a>>) = 0,
Expand Down Expand Up @@ -277,7 +275,6 @@ inherit_variants! {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum JSXExpression<'a> {
/// An empty expression
///
Expand Down Expand Up @@ -316,7 +313,6 @@ pub struct JSXEmptyExpression {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum JSXAttributeItem<'a> {
/// A `key="value"` attribute
Attribute(Box<'a, JSXAttribute<'a>>) = 0,
Expand Down Expand Up @@ -386,7 +382,6 @@ pub struct JSXSpreadAttribute<'a> {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum JSXAttributeName<'a> {
/// An attribute name without a namespace prefix, e.g. `foo` in `foo="bar"`.
Identifier(Box<'a, JSXIdentifier<'a>>) = 0,
Expand Down Expand Up @@ -415,7 +410,6 @@ pub enum JSXAttributeName<'a> {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum JSXAttributeValue<'a> {
/// `<Component foo="bar" />`
StringLiteral(Box<'a, StringLiteral<'a>>) = 0,
Expand Down Expand Up @@ -451,7 +445,6 @@ pub struct JSXIdentifier<'a> {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum JSXChild<'a> {
/// `<Foo>Some Text</Foo>`
Text(Box<'a, JSXText<'a>>) = 0,
Expand Down
1 change: 0 additions & 1 deletion crates/oxc_ast/src/ast/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ pub struct RegExp<'a> {
#[ast]
#[derive(Debug)]
#[generate_derive(CloneIn, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum RegExpPattern<'a> {
/// Unparsed pattern. Contains string slice of the pattern.
/// Pattern was not parsed, so may be valid or invalid.
Expand Down
12 changes: 0 additions & 12 deletions crates/oxc_ast/src/ast/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ inherit_variants! {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum TSEnumMemberName<'a> {
StaticIdentifier(Box<'a, IdentifierName<'a>>) = 64,
StaticStringLiteral(Box<'a, StringLiteral<'a>>) = 65,
Expand Down Expand Up @@ -177,7 +176,6 @@ pub struct TSLiteralType<'a> {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum TSLiteral<'a> {
BooleanLiteral(Box<'a, BooleanLiteral>) = 0,
NullLiteral(Box<'a, NullLiteral>) = 1,
Expand All @@ -203,7 +201,6 @@ pub enum TSLiteral<'a> {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum TSType<'a> {
// Keyword
TSAnyKeyword(Box<'a, TSAnyKeyword>) = 0,
Expand Down Expand Up @@ -545,7 +542,6 @@ inherit_variants! {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum TSTupleElement<'a> {
// Discriminants start at 64, so that `TSTupleElement::is_ts_type` is a single
// bitwise AND operation on the discriminant (`discriminant & 63 != 0`).
Expand Down Expand Up @@ -781,7 +777,6 @@ pub struct TSTypeReference<'a> {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum TSTypeName<'a> {
IdentifierReference(Box<'a, IdentifierReference<'a>>) = 0,
QualifiedName(Box<'a, TSQualifiedName<'a>>) = 1,
Expand Down Expand Up @@ -1003,7 +998,6 @@ pub struct TSPropertySignature<'a> {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum TSSignature<'a> {
TSIndexSignature(Box<'a, TSIndexSignature<'a>>) = 0,
TSPropertySignature(Box<'a, TSPropertySignature<'a>>) = 1,
Expand Down Expand Up @@ -1165,7 +1159,6 @@ pub struct TSTypePredicate<'a> {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum TSTypePredicateName<'a> {
Identifier(Box<'a, IdentifierName<'a>>) = 0,
This(TSThisType) = 1,
Expand Down Expand Up @@ -1269,7 +1262,6 @@ pub enum TSModuleDeclarationKind {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum TSModuleDeclarationName<'a> {
Identifier(BindingIdentifier<'a>) = 0,
StringLiteral(StringLiteral<'a>) = 1,
Expand All @@ -1278,7 +1270,6 @@ pub enum TSModuleDeclarationName<'a> {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum TSModuleDeclarationBody<'a> {
TSModuleDeclaration(Box<'a, TSModuleDeclaration<'a>>) = 0,
TSModuleBlock(Box<'a, TSModuleBlock<'a>>) = 1,
Expand Down Expand Up @@ -1357,7 +1348,6 @@ inherit_variants! {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum TSTypeQueryExprName<'a> {
TSImportType(Box<'a, TSImportType<'a>>) = 2,
// `TSTypeName` variants added here by `inherit_variants!` macro
Expand Down Expand Up @@ -1402,7 +1392,6 @@ pub struct TSImportAttribute<'a> {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum TSImportAttributeName<'a> {
Identifier(IdentifierName<'a>) = 0,
StringLiteral(StringLiteral<'a>) = 1,
Expand Down Expand Up @@ -1623,7 +1612,6 @@ inherit_variants! {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum TSModuleReference<'a> {
ExternalModuleReference(Box<'a, TSExternalModuleReference<'a>>) = 2,
// `TSTypeName` variants added here by `inherit_variants!` macro
Expand Down
2 changes: 0 additions & 2 deletions crates/oxc_regular_expression/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ pub struct Alternative<'a> {
#[ast]
#[derive(Debug)]
#[generate_derive(CloneIn, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum Term<'a> {
// Assertion, QuantifiableAssertion
BoundaryAssertion(Box<'a, BoundaryAssertion>) = 0,
Expand Down Expand Up @@ -246,7 +245,6 @@ pub enum CharacterClassContentsKind {
#[ast]
#[derive(Debug)]
#[generate_derive(CloneIn, ContentEq, ContentHash, ESTree)]
#[estree(untagged)]
pub enum CharacterClassContents<'a> {
CharacterClassRange(Box<'a, CharacterClassRange>) = 0,
CharacterClassEscape(Box<'a, CharacterClassEscape>) = 1,
Expand Down
6 changes: 4 additions & 2 deletions tasks/ast_tools/src/derives/estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,12 @@ fn serialize_struct(def: &StructDef) -> TokenStream {
// 3. All other enums, which are camelCased.
fn serialize_enum(def: &EnumDef) -> TokenStream {
let ident = def.ident();
if def.markers.estree.untagged {

let is_untagged = def.all_variants().all(|var| var.fields.len() == 1);

if is_untagged {
let match_branches = def.all_variants().map(|var| {
let var_ident = var.ident();
assert!(var.fields.len() == 1, "Each variant of an untagged enum must have exactly one inner field (on {ident}::{var_ident})");
quote! {
#ident::#var_ident(x) => {
Serialize::serialize(x, serializer)
Expand Down
4 changes: 3 additions & 1 deletion tasks/ast_tools/src/generators/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ fn typescript_enum(def: &EnumDef) -> Option<String> {
return None;
}

let union = if def.markers.estree.untagged {
let is_untagged = def.all_variants().all(|var| var.fields.len() == 1);

let union = if is_untagged {
def.all_variants().map(|var| type_to_string(var.fields[0].typ.name())).join(" | ")
} else {
def.all_variants().map(|var| format!("'{}'", enum_variant_name(var, def))).join(" | ")
Expand Down
Loading

0 comments on commit 8a647dd

Please sign in to comment.