feat(codegen): optimize cast codegen for enums
MODIFY
crates/rslint_parser/src/ast/generated/nodes.rs
crates/rslint_parser/src/ast/generated/nodes.rs
#![allow(clippy::enum_variant_names)]
#![allow(clippy::match_like_matches_macro)]
-#![allow(clippy::redundant_clone)]
use crate::{
ast::*,
SyntaxKind::{self, *},
@@ -4451,7 +4450,7 @@ impl AstNode for Stmt {
DEBUGGER_STMT => Stmt::DebuggerStmt(DebuggerStmt { syntax }),
JS_UNKNOWN_STATEMENT => Stmt::JsUnknownStatement(JsUnknownStatement { syntax }),
_ => {
- if let Some(decl) = Decl::cast(syntax.clone()) {
+ if let Some(decl) = Decl::cast(syntax) {
return Some(Stmt::Decl(decl));
}
return None;
@@ -4541,7 +4540,7 @@ impl AstNode for ModuleItem {
ModuleItem::TsNamespaceExportDecl(TsNamespaceExportDecl { syntax })
}
_ => {
- if let Some(stmt) = Stmt::cast(syntax.clone()) {
+ if let Some(stmt) = Stmt::cast(syntax) {
return Some(ModuleItem::Stmt(stmt));
}
return None;
@@ -4847,7 +4846,7 @@ impl AstNode for ForHead {
let res = match syntax.kind() {
VAR_DECL => ForHead::VarDecl(VarDecl { syntax }),
_ => {
- if let Some(expr) = Expr::cast(syntax.clone()) {
+ if let Some(expr) = Expr::cast(syntax) {
return Some(ForHead::Expr(expr));
}
return None;
@@ -5165,7 +5164,7 @@ impl AstNode for ExprOrBlock {
let res = match syntax.kind() {
BLOCK_STMT => ExprOrBlock::BlockStmt(BlockStmt { syntax }),
_ => {
- if let Some(expr) = Expr::cast(syntax.clone()) {
+ if let Some(expr) = Expr::cast(syntax) {
return Some(ExprOrBlock::Expr(expr));
}
return None;
@@ -5367,7 +5366,7 @@ impl AstNode for ConstructorParamOrPat {
ConstructorParamOrPat::TsConstructorParam(TsConstructorParam { syntax })
}
_ => {
- if let Some(pattern) = Pattern::cast(syntax.clone()) {
+ if let Some(pattern) = Pattern::cast(syntax) {
return Some(ConstructorParamOrPat::Pattern(pattern));
}
return None;
@@ -5401,7 +5400,7 @@ impl AstNode for ExprOrSpread {
SPREAD_ELEMENT => ExprOrSpread::SpreadElement(SpreadElement { syntax }),
GROUPING_EXPR => ExprOrSpread::GroupingExpr(GroupingExpr { syntax }),
_ => {
- if let Some(expr) = Expr::cast(syntax.clone()) {
+ if let Some(expr) = Expr::cast(syntax) {
return Some(ExprOrSpread::Expr(expr));
}
return None;
@@ -5429,7 +5428,7 @@ impl AstNode for PatternOrExpr {
if let Some(pattern) = Pattern::cast(syntax.clone()) {
return Some(PatternOrExpr::Pattern(pattern));
}
- if let Some(expr) = Expr::cast(syntax.clone()) {
+ if let Some(expr) = Expr::cast(syntax) {
return Some(PatternOrExpr::Expr(expr));
}
None
@@ -5574,7 +5573,7 @@ impl AstNode for TsModuleRef {
TsModuleRef::TsExternalModuleRef(TsExternalModuleRef { syntax })
}
_ => {
- if let Some(ts_entity_name) = TsEntityName::cast(syntax.clone()) {
+ if let Some(ts_entity_name) = TsEntityName::cast(syntax) {
return Some(TsModuleRef::TsEntityName(ts_entity_name));
}
return None;

MODIFY
xtask/src/codegen/generate_nodes.rs
xtask/src/codegen/generate_nodes.rs
@@ -216,15 +216,23 @@ pub fn generate_nodes(ast: &AstSrc) -> Result<String> {
// variant of variants
let vv: Vec<_> = variant_of_variants
.iter()
- .map(|en| {
+ .enumerate()
+ .map(|(i, en)| {
let variant_name = format_ident!("{}", en);
let variable_name = format_ident!("{}", to_lower_snake_case(en.as_str()));
(
// cast() code
+ if i != variant_of_variants.len() - 1 {
quote! {
if let Some(#variable_name) = #variant_name::cast(syntax.clone()) {
return Some(#name::#variant_name(#variable_name));
- }
+ }}
+ } else {
+ // if this is the last variant, do not clone syntax
+ quote! {
+ if let Some(#variable_name) = #variant_name::cast(syntax) {
+ return Some(#name::#variant_name(#variable_name));
+ }}
},
// can_cast() code
quote! {
@@ -349,9 +357,6 @@ pub fn generate_nodes(ast: &AstSrc) -> Result<String> {
#![allow(clippy::enum_variant_names)]
// sometimes we generate comparison of simple tokens
#![allow(clippy::match_like_matches_macro)]
- // sometimes we have only one enum and the .clone() is redundant.
- // It's needed when we match against multiple enums
- #![allow(clippy::redundant_clone)]
use crate::{
ast::*,

feat(codegen): optimize cast codegen for enums (#1764)