feat(ast_tools): Default enums to rename_all = "camelCase" (#6933)

Part of #6347
This commit is contained in:
ottomated 2024-10-28 01:39:25 +00:00
parent 1ca8cd2fd9
commit 169fa22350
7 changed files with 22 additions and 37 deletions

View file

@ -403,7 +403,6 @@ pub enum PropertyKey<'a> {
#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[generate_derive(CloneIn, ContentEq, ContentHash, ESTree)]
#[estree(rename_all = "camelCase")]
pub enum PropertyKind {
/// `a: 1` in `const obj = { a: 1 };`
Init = 0,
@ -1136,7 +1135,6 @@ pub struct VariableDeclaration<'a> {
#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[generate_derive(CloneIn, ContentEq, ContentHash, ESTree)]
#[estree(rename_all = "camelCase")]
pub enum VariableDeclarationKind {
Var = 0,
Const = 1,
@ -1696,6 +1694,7 @@ pub struct Function<'a> {
#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[generate_derive(CloneIn, ContentEq, ContentHash, ESTree)]
#[estree(no_rename_variants)]
pub enum FunctionType {
FunctionDeclaration = 0,
FunctionExpression = 1,
@ -1736,6 +1735,7 @@ pub struct FormalParameter<'a> {
#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[generate_derive(CloneIn, ContentEq, ContentHash, ESTree)]
#[estree(no_rename_variants)]
pub enum FormalParameterKind {
/// <https://tc39.es/ecma262/#prod-FormalParameters>
FormalParameter = 0,
@ -1868,6 +1868,7 @@ pub struct Class<'a> {
#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[generate_derive(CloneIn, ContentEq, ContentHash, ESTree)]
#[estree(no_rename_variants)]
pub enum ClassType {
/// Class declaration statement
/// ```ts
@ -1961,6 +1962,7 @@ pub struct MethodDefinition<'a> {
#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[generate_derive(CloneIn, ContentEq, ContentHash, ESTree)]
#[estree(no_rename_variants)]
pub enum MethodDefinitionType {
MethodDefinition = 0,
TSAbstractMethodDefinition = 1,
@ -2049,6 +2051,7 @@ pub struct PropertyDefinition<'a> {
#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[generate_derive(CloneIn, ContentEq, ContentHash, ESTree)]
#[estree(no_rename_variants)]
pub enum PropertyDefinitionType {
PropertyDefinition = 0,
TSAbstractPropertyDefinition = 1,
@ -2057,7 +2060,6 @@ pub enum PropertyDefinitionType {
#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[generate_derive(CloneIn, ContentEq, ContentHash, ESTree)]
#[estree(rename_all = "camelCase")]
pub enum MethodDefinitionKind {
/// Class constructor
Constructor = 0,
@ -2168,6 +2170,7 @@ pub use match_module_declaration;
#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[generate_derive(CloneIn, ContentEq, ContentHash, ESTree)]
#[estree(no_rename_variants)]
pub enum AccessorPropertyType {
AccessorProperty = 0,
TSAbstractAccessorProperty = 1,

View file

@ -409,7 +409,6 @@ pub struct TSTypeOperator<'a> {
#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[generate_derive(CloneIn, ContentEq, ContentHash, ESTree)]
#[estree(rename_all = "camelCase")]
pub enum TSTypeOperatorOperator {
Keyof = 0,
Unique = 1,
@ -893,7 +892,6 @@ pub struct TSTypeAliasDeclaration<'a> {
#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[generate_derive(CloneIn, ContentEq, ContentHash, ESTree)]
#[estree(rename_all = "camelCase")]
pub enum TSAccessibility {
Private = 0,
Protected = 1,
@ -1043,7 +1041,6 @@ pub struct TSCallSignatureDeclaration<'a> {
#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[generate_derive(CloneIn, ContentEq, ContentHash, ESTree)]
#[estree(rename_all = "camelCase")]
pub enum TSMethodSignatureKind {
Method = 0,
Get = 1,
@ -1229,7 +1226,6 @@ pub struct TSModuleDeclaration<'a> {
#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[generate_derive(CloneIn, ContentEq, ContentHash, ESTree)]
#[estree(rename_all = "camelCase")]
pub enum TSModuleDeclarationKind {
/// `declare global {}`
Global = 0,
@ -1511,7 +1507,6 @@ pub struct TSMappedType<'a> {
#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[generate_derive(CloneIn, ContentEq, ContentHash, ESTree)]
#[estree(rename_all = "camelCase")]
pub enum TSMappedTypeModifierOperator {
/// e.g. `?` in `{ [P in K]?: T }`
True = 0,
@ -1708,7 +1703,6 @@ pub struct TSInstantiationExpression<'a> {
#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[generate_derive(CloneIn, ContentEq, ContentHash, ESTree)]
#[estree(rename_all = "camelCase")]
pub enum ImportOrExportKind {
/// `import { foo } from './foo'`;
Value = 0,

View file

@ -88,7 +88,6 @@ pub struct BoundaryAssertion {
#[ast]
#[derive(Debug, Clone, PartialEq)]
#[generate_derive(CloneIn, ContentEq, ContentHash, ESTree)]
#[estree(rename_all = "camelCase")]
pub enum BoundaryAssertionKind {
Start = 0,
End = 1,
@ -111,7 +110,6 @@ pub struct LookAroundAssertion<'a> {
#[ast]
#[derive(Debug, Clone, PartialEq)]
#[generate_derive(CloneIn, ContentEq, ContentHash, ESTree)]
#[estree(rename_all = "camelCase")]
pub enum LookAroundAssertionKind {
Lookahead = 0,
NegativeLookahead = 1,
@ -150,7 +148,6 @@ pub struct Character {
#[ast]
#[derive(Debug, Clone, Copy, PartialEq)]
#[generate_derive(CloneIn, ContentEq, ContentHash, ESTree)]
#[estree(rename_all = "camelCase")]
pub enum CharacterKind {
ControlLetter = 0,
HexadecimalEscape = 1,
@ -179,7 +176,6 @@ pub struct CharacterClassEscape {
#[ast]
#[derive(Debug, Clone, Copy, PartialEq)]
#[generate_derive(CloneIn, ContentEq, ContentHash, ESTree)]
#[estree(rename_all = "camelCase")]
pub enum CharacterClassEscapeKind {
D = 0,
NegativeD = 1,
@ -233,7 +229,6 @@ pub struct CharacterClass<'a> {
#[ast]
#[derive(Debug, PartialEq)]
#[generate_derive(CloneIn, ContentEq, ContentHash, ESTree)]
#[estree(rename_all = "camelCase")]
pub enum CharacterClassContentsKind {
Union = 0,
/// `UnicodeSetsMode` only.

View file

@ -45,7 +45,6 @@ pub enum Language {
#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[generate_derive(ESTree)]
#[estree(rename_all = "camelCase")]
pub enum ModuleKind {
/// Regular JS script or CommonJS file
Script = 0,
@ -66,7 +65,6 @@ pub enum ModuleKind {
#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[generate_derive(ESTree)]
#[estree(rename_all = "camelCase")]
pub enum LanguageVariant {
/// Standard JavaScript or TypeScript without any language extensions. Stage
/// 3 proposals do not count as language extensions.

View file

@ -18,7 +18,6 @@ use crate::precedence::{GetPrecedence, Precedence};
#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[generate_derive(CloneIn, ContentEq, ContentHash, ESTree)]
#[estree(rename_all = "camelCase")]
pub enum AssignmentOperator {
/// `=`
#[estree(rename = "=")]
@ -125,7 +124,6 @@ impl AssignmentOperator {
#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[generate_derive(CloneIn, ContentEq, ContentHash, ESTree)]
#[estree(rename_all = "camelCase")]
pub enum BinaryOperator {
/// `==`
#[estree(rename = "==")]
@ -356,7 +354,6 @@ impl GetPrecedence for BinaryOperator {
#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[generate_derive(CloneIn, ContentEq, ContentHash, ESTree)]
#[estree(rename_all = "camelCase")]
pub enum LogicalOperator {
/// `||`
#[estree(rename = "||")]
@ -410,7 +407,6 @@ impl GetPrecedence for LogicalOperator {
#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[generate_derive(CloneIn, ContentEq, ContentHash, ESTree)]
#[estree(rename_all = "camelCase")]
pub enum UnaryOperator {
/// `-`
#[estree(rename = "-")]
@ -481,7 +477,6 @@ impl UnaryOperator {
#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[generate_derive(CloneIn, ContentEq, ContentHash, ESTree)]
#[estree(rename_all = "camelCase")]
pub enum UpdateOperator {
/// `++`
#[estree(rename = "++")]

View file

@ -124,25 +124,18 @@ impl Parse for ESTreeStructAttribute {
/// A struct representing the `#[estree(...)]` attributes that we implement for enums.
#[derive(Debug, Serialize, Default)]
pub struct ESTreeEnumAttribute {
pub rename_all: Option<String>,
pub no_rename_variants: bool,
pub custom_ts_def: bool,
}
impl Parse for ESTreeEnumAttribute {
fn parse(input: ParseStream) -> Result<Self, syn::Error> {
let mut rename_all = None;
let mut no_rename_variants = false;
let mut custom_ts_def = false;
loop {
let ident = input.call(Ident::parse_any).unwrap().to_string();
match ident.as_str() {
"rename_all" => {
input.parse::<Token![=]>()?;
assert!(
rename_all.replace(input.parse::<LitStr>()?.value()).is_none(),
"Duplicate estree(rename_all)"
);
}
"custom_ts_def" => {
if custom_ts_def {
panic!("Duplicate estree(custom_ts_def)");
@ -150,6 +143,13 @@ impl Parse for ESTreeEnumAttribute {
custom_ts_def = true;
}
}
"no_rename_variants" => {
if no_rename_variants {
panic!("Duplicate estree(no_rename_variants)");
} else {
no_rename_variants = true;
}
}
arg => panic!("Unsupported #[estree(...)] argument: {arg}"),
}
let comma = input.peek(Token![,]);
@ -159,7 +159,7 @@ impl Parse for ESTreeEnumAttribute {
break;
}
}
Ok(Self { rename_all, custom_ts_def })
Ok(Self { no_rename_variants, custom_ts_def })
}
}

View file

@ -7,13 +7,13 @@ use super::{EnumDef, StructDef, VariantDef};
pub fn enum_variant_name(var: &VariantDef, enm: &EnumDef) -> String {
match var.markers.derive_attributes.estree.rename.as_ref() {
Some(rename) => rename.to_string(),
None => match enm.markers.estree.rename_all.as_deref() {
Some("camelCase") => var.ident().to_string().to_case(Case::Camel),
Some(case) => {
panic!("Unsupported rename_all: {case} (on {})", enm.ident())
None => {
if enm.markers.estree.no_rename_variants {
var.ident().to_string()
} else {
var.ident().to_string().to_case(Case::Camel)
}
None => var.ident().to_string(),
},
}
}
}