refactor(ast): add explicit enum discriminants. (#4689)

Related to #4622 but doesn't close it.
This commit is contained in:
rzvxa 2024-08-06 17:05:29 +00:00
parent 75f22072be
commit 83b6ca93d9
4 changed files with 110 additions and 109 deletions

View file

@ -365,9 +365,9 @@ pub struct ObjectExpression<'a> {
#[serde(untagged)]
pub enum ObjectPropertyKind<'a> {
/// `a: 1` in `const obj = { a: 1 };`
ObjectProperty(Box<'a, ObjectProperty<'a>>),
ObjectProperty(Box<'a, ObjectProperty<'a>>) = 0,
/// `...{ a: 1 }` in `const obj = { ...{ a: 1 } };`
SpreadProperty(Box<'a, SpreadElement<'a>>),
SpreadProperty(Box<'a, SpreadElement<'a>>) = 1,
}
/// `a: 1` in `const obj = { a: 1 };`
@ -416,11 +416,11 @@ pub enum PropertyKey<'a> {
#[serde(rename_all = "camelCase")]
pub enum PropertyKind {
/// `a: 1` in `const obj = { a: 1 };`
Init,
Init = 0,
/// `get a() { return 1; }` in `const obj = { get a() { return 1; } };`
Get,
Get = 1,
/// `set a(value) { this._a = value; }` in `const obj = { set a(value) { this._a = value; } };`
Set,
Set = 2,
}
/// `` `Hello, ${name}` `` in `` const foo = `Hello, ${name}` ``
@ -935,8 +935,8 @@ pub struct AssignmentTargetWithDefault<'a> {
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged)]
pub enum AssignmentTargetProperty<'a> {
AssignmentTargetPropertyIdentifier(Box<'a, AssignmentTargetPropertyIdentifier<'a>>),
AssignmentTargetPropertyProperty(Box<'a, AssignmentTargetPropertyProperty<'a>>),
AssignmentTargetPropertyIdentifier(Box<'a, AssignmentTargetPropertyIdentifier<'a>>) = 0,
AssignmentTargetPropertyProperty(Box<'a, AssignmentTargetPropertyProperty<'a>>) = 1,
}
/// `foo` in `({ foo } = obj);`
@ -1186,9 +1186,9 @@ pub struct VariableDeclaration<'a> {
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(rename_all = "camelCase")]
pub enum VariableDeclarationKind {
Var,
Const,
Let,
Var = 0,
Const = 1,
Let = 2,
}
#[ast(visit)]
@ -1522,16 +1522,16 @@ pub struct BindingPattern<'a> {
#[serde(untagged)]
pub enum BindingPatternKind<'a> {
/// `const a = 1`
BindingIdentifier(Box<'a, BindingIdentifier<'a>>),
BindingIdentifier(Box<'a, BindingIdentifier<'a>>) = 0,
/// `const {a} = 1`
ObjectPattern(Box<'a, ObjectPattern<'a>>),
ObjectPattern(Box<'a, ObjectPattern<'a>>) = 1,
/// `const [a] = 1`
ArrayPattern(Box<'a, ArrayPattern<'a>>),
ArrayPattern(Box<'a, ArrayPattern<'a>>) = 2,
/// A defaulted binding pattern, i.e.:
/// `const {a = 1} = 1`
/// the assignment pattern is `a = 1`
/// it has an inner left that has a BindingIdentifier
AssignmentPattern(Box<'a, AssignmentPattern<'a>>),
AssignmentPattern(Box<'a, AssignmentPattern<'a>>) = 3,
}
#[ast(visit)]
@ -1641,11 +1641,11 @@ pub struct Function<'a> {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
pub enum FunctionType {
FunctionDeclaration,
FunctionExpression,
TSDeclareFunction,
FunctionDeclaration = 0,
FunctionExpression = 1,
TSDeclareFunction = 2,
/// <https://github.com/typescript-eslint/typescript-eslint/pull/1289>
TSEmptyBodyFunctionExpression,
TSEmptyBodyFunctionExpression = 3,
}
/// <https://tc39.es/ecma262/#prod-FormalParameters>
@ -1683,13 +1683,13 @@ pub struct FormalParameter<'a> {
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
pub enum FormalParameterKind {
/// <https://tc39.es/ecma262/#prod-FormalParameters>
FormalParameter,
FormalParameter = 0,
/// <https://tc39.es/ecma262/#prod-UniqueFormalParameters>
UniqueFormalParameters,
UniqueFormalParameters = 1,
/// <https://tc39.es/ecma262/#prod-ArrowFormalParameters>
ArrowFormalParameters,
ArrowFormalParameters = 2,
/// Part of TypeScript type signatures
Signature,
Signature = 3,
}
/// <https://tc39.es/ecma262/#prod-FunctionBody>
@ -1819,13 +1819,13 @@ pub enum ClassType {
/// ```ts
/// class Foo { }
/// ```
ClassDeclaration,
ClassDeclaration = 0,
/// Class expression
///
/// ```ts
/// const Foo = class {}
/// ```
ClassExpression,
ClassExpression = 1,
}
#[ast(visit)]
@ -1861,13 +1861,13 @@ pub struct ClassBody<'a> {
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged)]
pub enum ClassElement<'a> {
StaticBlock(Box<'a, StaticBlock<'a>>),
StaticBlock(Box<'a, StaticBlock<'a>>) = 0,
/// Class Methods
///
/// Includes static and non-static methods, constructors, getters, and setters.
MethodDefinition(Box<'a, MethodDefinition<'a>>),
PropertyDefinition(Box<'a, PropertyDefinition<'a>>),
AccessorProperty(Box<'a, AccessorProperty<'a>>),
MethodDefinition(Box<'a, MethodDefinition<'a>>) = 1,
PropertyDefinition(Box<'a, PropertyDefinition<'a>>) = 2,
AccessorProperty(Box<'a, AccessorProperty<'a>>) = 3,
/// Index Signature
///
/// ## Example
@ -1876,7 +1876,7 @@ pub enum ClassElement<'a> {
/// [keys: string]: string
/// }
/// ```
TSIndexSignature(Box<'a, TSIndexSignature<'a>>),
TSIndexSignature(Box<'a, TSIndexSignature<'a>>) = 4,
}
#[ast(visit)]
@ -1911,8 +1911,8 @@ pub struct MethodDefinition<'a> {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
pub enum MethodDefinitionType {
MethodDefinition,
TSAbstractMethodDefinition,
MethodDefinition = 0,
TSAbstractMethodDefinition = 1,
}
#[ast(visit)]
@ -2000,8 +2000,8 @@ pub struct PropertyDefinition<'a> {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
pub enum PropertyDefinitionType {
PropertyDefinition,
TSAbstractPropertyDefinition,
PropertyDefinition = 0,
TSAbstractPropertyDefinition = 1,
}
#[ast]
@ -2010,13 +2010,13 @@ pub enum PropertyDefinitionType {
#[serde(rename_all = "camelCase")]
pub enum MethodDefinitionKind {
/// Class constructor
Constructor,
Constructor = 0,
/// Static or instance method
Method,
Method = 1,
/// Getter method
Get,
Get = 2,
/// Setter method
Set,
Set = 3,
}
/// An identifier for a private class member.
@ -2120,8 +2120,8 @@ pub use match_module_declaration;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
pub enum AccessorPropertyType {
AccessorProperty,
TSAbstractAccessorProperty,
AccessorProperty = 0,
TSAbstractAccessorProperty = 1,
}
/// Class Accessor Property
@ -2185,11 +2185,11 @@ pub struct ImportDeclaration<'a> {
pub enum ImportDeclarationSpecifier<'a> {
/// import {imported} from "source"
/// import {imported as local} from "source"
ImportSpecifier(Box<'a, ImportSpecifier<'a>>),
ImportSpecifier(Box<'a, ImportSpecifier<'a>>) = 0,
/// import local from "source"
ImportDefaultSpecifier(Box<'a, ImportDefaultSpecifier<'a>>),
ImportDefaultSpecifier(Box<'a, ImportDefaultSpecifier<'a>>) = 1,
/// import * as local from "source"
ImportNamespaceSpecifier(Box<'a, ImportNamespaceSpecifier<'a>>),
ImportNamespaceSpecifier(Box<'a, ImportNamespaceSpecifier<'a>>) = 2,
}
// import {imported} from "source"
@ -2278,8 +2278,8 @@ pub struct ImportAttribute<'a> {
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged)]
pub enum ImportAttributeKey<'a> {
Identifier(IdentifierName<'a>),
StringLiteral(StringLiteral<'a>),
Identifier(IdentifierName<'a>) = 0,
StringLiteral(StringLiteral<'a>) = 1,
}
/// Named Export Declaration
@ -2410,8 +2410,8 @@ pub enum ExportDefaultDeclarationKind<'a> {
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged)]
pub enum ModuleExportName<'a> {
IdentifierName(IdentifierName<'a>),
IdentifierName(IdentifierName<'a>) = 0,
/// For `local` in `ExportSpecifier`: `foo` in `export { foo }`
IdentifierReference(IdentifierReference<'a>),
StringLiteral(StringLiteral<'a>),
IdentifierReference(IdentifierReference<'a>) = 1,
StringLiteral(StringLiteral<'a>) = 2,
}

View file

@ -145,11 +145,11 @@ pub struct JSXClosingFragment {
#[serde(untagged)]
pub enum JSXElementName<'a> {
/// `<Apple />`
Identifier(Box<'a, JSXIdentifier<'a>>),
Identifier(Box<'a, JSXIdentifier<'a>>) = 0,
/// `<Apple:Orange />`
NamespacedName(Box<'a, JSXNamespacedName<'a>>),
NamespacedName(Box<'a, JSXNamespacedName<'a>>) = 1,
/// `<Apple.Orange />`
MemberExpression(Box<'a, JSXMemberExpression<'a>>),
MemberExpression(Box<'a, JSXMemberExpression<'a>>) = 2,
}
/// JSX Namespaced Name
@ -205,8 +205,8 @@ pub struct JSXMemberExpression<'a> {
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged)]
pub enum JSXMemberExpressionObject<'a> {
Identifier(Box<'a, JSXIdentifier<'a>>),
MemberExpression(Box<'a, JSXMemberExpression<'a>>),
Identifier(Box<'a, JSXIdentifier<'a>>) = 0,
MemberExpression(Box<'a, JSXMemberExpression<'a>>) = 1,
}
/// JSX Expression Container
@ -268,8 +268,8 @@ pub struct JSXEmptyExpression {
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged)]
pub enum JSXAttributeItem<'a> {
Attribute(Box<'a, JSXAttribute<'a>>),
SpreadAttribute(Box<'a, JSXSpreadAttribute<'a>>),
Attribute(Box<'a, JSXAttribute<'a>>) = 0,
SpreadAttribute(Box<'a, JSXSpreadAttribute<'a>>) = 1,
}
/// JSX Attribute
@ -315,8 +315,8 @@ pub struct JSXSpreadAttribute<'a> {
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged)]
pub enum JSXAttributeName<'a> {
Identifier(Box<'a, JSXIdentifier<'a>>),
NamespacedName(Box<'a, JSXNamespacedName<'a>>),
Identifier(Box<'a, JSXIdentifier<'a>>) = 0,
NamespacedName(Box<'a, JSXNamespacedName<'a>>) = 1,
}
/// JSX Attribute Value
@ -327,10 +327,10 @@ pub enum JSXAttributeName<'a> {
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged)]
pub enum JSXAttributeValue<'a> {
StringLiteral(Box<'a, StringLiteral<'a>>),
ExpressionContainer(Box<'a, JSXExpressionContainer<'a>>),
Element(Box<'a, JSXElement<'a>>),
Fragment(Box<'a, JSXFragment<'a>>),
StringLiteral(Box<'a, StringLiteral<'a>>) = 0,
ExpressionContainer(Box<'a, JSXExpressionContainer<'a>>) = 1,
Element(Box<'a, JSXElement<'a>>) = 2,
Fragment(Box<'a, JSXFragment<'a>>) = 3,
}
/// JSX Identifier
@ -359,20 +359,15 @@ pub struct JSXIdentifier<'a> {
#[serde(untagged)]
pub enum JSXChild<'a> {
/// `<Foo>Some Text</Foo>`
Text(Box<'a, JSXText<'a>>),
Text(Box<'a, JSXText<'a>>) = 0,
/// `<Foo><Child /></Foo>`
Element(Box<'a, JSXElement<'a>>),
Element(Box<'a, JSXElement<'a>>) = 1,
/// `<Foo><></></Foo>`
Fragment(Box<'a, JSXFragment<'a>>),
Fragment(Box<'a, JSXFragment<'a>>) = 2,
/// `<Foo>{expression}</Foo>`
ExpressionContainer(Box<'a, JSXExpressionContainer<'a>>),
ExpressionContainer(Box<'a, JSXExpressionContainer<'a>>) = 3,
/// `<Foo>{...spread}</Foo>`
Spread(Box<'a, JSXSpreadChild<'a>>),
}
impl<'a> JSXChild<'a> {
pub const fn is_expression_container(&self) -> bool {
matches!(self, Self::ExpressionContainer(_))
}
Spread(Box<'a, JSXSpreadChild<'a>>) = 4,
}
/// JSX Spread Child.

View file

@ -175,14 +175,14 @@ pub struct TSLiteralType<'a> {
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged, rename_all = "camelCase")]
pub enum TSLiteral<'a> {
BooleanLiteral(Box<'a, BooleanLiteral>),
NullLiteral(Box<'a, NullLiteral>),
NumericLiteral(Box<'a, NumericLiteral<'a>>),
BigIntLiteral(Box<'a, BigIntLiteral<'a>>),
RegExpLiteral(Box<'a, RegExpLiteral<'a>>),
StringLiteral(Box<'a, StringLiteral<'a>>),
TemplateLiteral(Box<'a, TemplateLiteral<'a>>),
UnaryExpression(Box<'a, UnaryExpression<'a>>),
BooleanLiteral(Box<'a, BooleanLiteral>) = 0,
NullLiteral(Box<'a, NullLiteral>) = 1,
NumericLiteral(Box<'a, NumericLiteral<'a>>) = 2,
BigIntLiteral(Box<'a, BigIntLiteral<'a>>) = 3,
RegExpLiteral(Box<'a, RegExpLiteral<'a>>) = 4,
StringLiteral(Box<'a, StringLiteral<'a>>) = 5,
TemplateLiteral(Box<'a, TemplateLiteral<'a>>) = 6,
UnaryExpression(Box<'a, UnaryExpression<'a>>) = 7,
}
/// TypeScript Type
@ -373,9 +373,9 @@ pub struct TSTypeOperator<'a> {
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(rename_all = "camelCase")]
pub enum TSTypeOperatorOperator {
Keyof,
Unique,
Readonly,
Keyof = 0,
Unique = 1,
Readonly = 2,
}
/// TypeScript Array Type
@ -723,9 +723,9 @@ pub struct TSTypeAliasDeclaration<'a> {
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(rename_all = "camelCase")]
pub enum TSAccessibility {
Private,
Protected,
Public,
Private = 0,
Protected = 1,
Public = 2,
}
#[ast(visit)]
@ -789,11 +789,11 @@ pub struct TSPropertySignature<'a> {
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged, rename_all = "camelCase")]
pub enum TSSignature<'a> {
TSIndexSignature(Box<'a, TSIndexSignature<'a>>),
TSPropertySignature(Box<'a, TSPropertySignature<'a>>),
TSCallSignatureDeclaration(Box<'a, TSCallSignatureDeclaration<'a>>),
TSConstructSignatureDeclaration(Box<'a, TSConstructSignatureDeclaration<'a>>),
TSMethodSignature(Box<'a, TSMethodSignature<'a>>),
TSIndexSignature(Box<'a, TSIndexSignature<'a>>) = 0,
TSPropertySignature(Box<'a, TSPropertySignature<'a>>) = 1,
TSCallSignatureDeclaration(Box<'a, TSCallSignatureDeclaration<'a>>) = 2,
TSConstructSignatureDeclaration(Box<'a, TSConstructSignatureDeclaration<'a>>) = 3,
TSMethodSignature(Box<'a, TSMethodSignature<'a>>) = 4,
}
/// An index signature within a class, type alias, etc.
@ -837,9 +837,9 @@ pub struct TSCallSignatureDeclaration<'a> {
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(rename_all = "camelCase")]
pub enum TSMethodSignatureKind {
Method,
Get,
Set,
Method = 0,
Get = 1,
Set = 2,
}
#[ast(visit)]
@ -914,8 +914,8 @@ pub struct TSTypePredicate<'a> {
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged, rename_all = "camelCase")]
pub enum TSTypePredicateName<'a> {
Identifier(Box<'a, IdentifierName<'a>>),
This(TSThisType),
Identifier(Box<'a, IdentifierName<'a>>) = 0,
This(TSThisType) = 1,
}
#[ast(visit)]
@ -951,9 +951,9 @@ pub struct TSModuleDeclaration<'a> {
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(rename_all = "camelCase")]
pub enum TSModuleDeclarationKind {
Global,
Module,
Namespace,
Global = 0,
Module = 1,
Namespace = 2,
}
impl TSModuleDeclarationKind {
@ -967,8 +967,8 @@ impl TSModuleDeclarationKind {
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged)]
pub enum TSModuleDeclarationName<'a> {
Identifier(IdentifierName<'a>),
StringLiteral(StringLiteral<'a>),
Identifier(IdentifierName<'a>) = 0,
StringLiteral(StringLiteral<'a>) = 1,
}
#[ast(visit)]
@ -976,8 +976,8 @@ pub enum TSModuleDeclarationName<'a> {
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged)]
pub enum TSModuleDeclarationBody<'a> {
TSModuleDeclaration(Box<'a, TSModuleDeclaration<'a>>),
TSModuleBlock(Box<'a, TSModuleBlock<'a>>),
TSModuleDeclaration(Box<'a, TSModuleDeclaration<'a>>) = 0,
TSModuleBlock(Box<'a, TSModuleBlock<'a>>) = 1,
}
// See serializer in serialize.rs
@ -1082,8 +1082,8 @@ pub struct TSImportAttribute<'a> {
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged)]
pub enum TSImportAttributeName<'a> {
Identifier(IdentifierName<'a>),
StringLiteral(StringLiteral<'a>),
Identifier(IdentifierName<'a>) = 0,
StringLiteral(StringLiteral<'a>) = 1,
}
#[ast(visit)]
@ -1133,12 +1133,12 @@ pub struct TSMappedType<'a> {
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(rename_all = "camelCase")]
pub enum TSMappedTypeModifierOperator {
True,
True = 0,
#[serde(rename = "+")]
Plus,
Plus = 1,
#[serde(rename = "-")]
Minus,
None,
Minus = 2,
None = 3,
}
#[ast(visit)]
@ -1312,9 +1312,9 @@ pub struct TSInstantiationExpression<'a> {
#[serde(rename_all = "camelCase")]
pub enum ImportOrExportKind {
/// `import { foo } from './foo'`;
Value,
Value = 0,
/// `import type { foo } from './foo'`;
Type,
Type = 1,
}
// [`JSDoc`](https://github.com/microsoft/TypeScript/blob/54a554d8af2657630307cbfa8a3e4f3946e36507/src/compiler/types.ts#L393)

View file

@ -95,3 +95,9 @@ impl<'a> JSXAttribute<'a> {
self.is_identifier("key")
}
}
impl<'a> JSXChild<'a> {
pub const fn is_expression_container(&self) -> bool {
matches!(self, Self::ExpressionContainer(_))
}
}