Skip to content

Commit 0a4c70d

Browse files
committed
SA transforms: collect Ident instead of Id and Span separately (#70722)
1 parent 7a30e50 commit 0a4c70d

File tree

1 file changed

+62
-107
lines changed

1 file changed

+62
-107
lines changed

crates/next-custom-transforms/src/transforms/server_actions.rs

Lines changed: 62 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,15 @@ struct ServerActions<C: Comments> {
105105
should_track_names: bool,
106106

107107
names: Vec<Name>,
108-
declared_idents: Vec<Id>,
108+
declared_idents: Vec<Ident>,
109109

110110
// This flag allows us to rewrite `function foo() {}` to `const foo = createProxy(...)`.
111111
rewrite_fn_decl_to_proxy_decl: Option<VarDecl>,
112112
rewrite_default_fn_expr_to_proxy_expr: Option<Box<Expr>>,
113113
rewrite_expr_to_proxy_expr: Option<Box<Expr>>,
114114

115-
// (ident, export name, span)
116-
exported_idents: Vec<(Id, String, Span)>,
115+
// (ident, export name)
116+
exported_idents: Vec<(Ident, String)>,
117117

118118
annotations: Vec<Stmt>,
119119
extra_items: Vec<ModuleItem>,
@@ -123,22 +123,6 @@ struct ServerActions<C: Comments> {
123123
private_ctxt: SyntaxContext,
124124
}
125125

126-
trait IdentCollector {
127-
fn collect(&mut self, id: Id, span: Option<Span>);
128-
}
129-
130-
impl IdentCollector for Vec<Id> {
131-
fn collect(&mut self, id: Id, _span: Option<Span>) {
132-
self.push(id);
133-
}
134-
}
135-
136-
impl IdentCollector for Vec<(Id, Span)> {
137-
fn collect(&mut self, id: Id, span: Option<Span>) {
138-
self.push((id, span.expect("Span is required")));
139-
}
140-
}
141-
142126
impl<C: Comments> ServerActions<C> {
143127
// Check if the function or arrow function is an action function
144128
fn get_body_info(&mut self, maybe_body: Option<&mut BlockStmt>) -> (bool, Option<String>) {
@@ -1225,19 +1209,17 @@ impl<C: Comments> VisitMut for ServerActions<C> {
12251209
match decl {
12261210
Decl::Fn(f) => {
12271211
// export function foo() {}
1228-
self.exported_idents.push((
1229-
f.ident.to_id(),
1230-
f.ident.sym.to_string(),
1231-
f.ident.span,
1232-
));
1212+
self.exported_idents
1213+
.push((f.ident.clone(), f.ident.sym.to_string()));
12331214
}
12341215
Decl::Var(var) => {
12351216
// export const foo = 1
1236-
let mut ids: Vec<(Id, Span)> = Vec::new();
1237-
collect_idents_in_var_decls(&var.decls, &mut ids);
1217+
let mut idents: Vec<Ident> = Vec::new();
1218+
collect_idents_in_var_decls(&var.decls, &mut idents);
12381219
self.exported_idents.extend(
1239-
ids.into_iter()
1240-
.map(|(id, span)| (id.clone(), id.0.to_string(), span)),
1220+
idents
1221+
.into_iter()
1222+
.map(|ident| (ident.clone(), ident.to_id().0.to_string())),
12411223
);
12421224

12431225
for decl in &mut var.decls {
@@ -1270,26 +1252,17 @@ impl<C: Comments> VisitMut for ServerActions<C> {
12701252
export_name
12711253
{
12721254
// export { foo as bar }
1273-
self.exported_idents.push((
1274-
ident.to_id(),
1275-
sym.to_string(),
1276-
ident.span,
1277-
));
1255+
self.exported_idents
1256+
.push((ident.clone(), sym.to_string()));
12781257
} else if let ModuleExportName::Str(str) = export_name {
12791258
// export { foo as "bar" }
1280-
self.exported_idents.push((
1281-
ident.to_id(),
1282-
str.value.to_string(),
1283-
ident.span,
1284-
));
1259+
self.exported_idents
1260+
.push((ident.clone(), str.value.to_string()));
12851261
}
12861262
} else {
12871263
// export { foo }
1288-
self.exported_idents.push((
1289-
ident.to_id(),
1290-
ident.sym.to_string(),
1291-
ident.span,
1292-
));
1264+
self.exported_idents
1265+
.push((ident.clone(), ident.sym.to_string()));
12931266
}
12941267
} else {
12951268
disallowed_export_span = named.span;
@@ -1305,11 +1278,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
13051278
DefaultDecl::Fn(f) => {
13061279
if let Some(ident) = &f.ident {
13071280
// export default function foo() {}
1308-
self.exported_idents.push((
1309-
ident.to_id(),
1310-
"default".into(),
1311-
ident.span,
1312-
));
1281+
self.exported_idents.push((ident.clone(), "default".into()));
13131282
} else {
13141283
// export default function() {}
13151284
// Use the span from the function expression
@@ -1323,11 +1292,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
13231292

13241293
f.ident = Some(new_ident.clone());
13251294

1326-
self.exported_idents.push((
1327-
new_ident.to_id(),
1328-
"default".into(),
1329-
span,
1330-
));
1295+
self.exported_idents.push((new_ident, "default".into()));
13311296
}
13321297
}
13331298
_ => {
@@ -1351,11 +1316,8 @@ impl<C: Comments> VisitMut for ServerActions<C> {
13511316
self.private_ctxt,
13521317
);
13531318

1354-
self.exported_idents.push((
1355-
new_ident.to_id(),
1356-
"default".into(),
1357-
span,
1358-
));
1319+
self.exported_idents
1320+
.push((new_ident.clone(), "default".into()));
13591321

13601322
*default_expr.expr = attach_name_to_expr(
13611323
new_ident,
@@ -1366,11 +1328,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
13661328
}
13671329
Expr::Ident(ident) => {
13681330
// export default foo
1369-
self.exported_idents.push((
1370-
ident.to_id(),
1371-
"default".into(),
1372-
ident.span,
1373-
));
1331+
self.exported_idents.push((ident.clone(), "default".into()));
13741332
}
13751333
Expr::Call(call) => {
13761334
// export default fn()
@@ -1383,11 +1341,8 @@ impl<C: Comments> VisitMut for ServerActions<C> {
13831341
self.private_ctxt,
13841342
);
13851343

1386-
self.exported_idents.push((
1387-
new_ident.to_id(),
1388-
"default".into(),
1389-
span,
1390-
));
1344+
self.exported_idents
1345+
.push((new_ident.clone(), "default".into()));
13911346

13921347
*default_expr.expr = attach_name_to_expr(
13931348
new_ident,
@@ -1490,9 +1445,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
14901445
new.rotate_right(1);
14911446
}
14921447

1493-
for (id, export_name, span) in self.exported_idents.iter() {
1494-
let ident = Ident::new(id.0.clone(), *span, id.1);
1495-
1448+
for (ident, export_name) in self.exported_idents.iter() {
14961449
if !self.config.is_react_server_layer {
14971450
let action_id =
14981451
generate_action_id(&self.config.hash_salt, &self.file_name, export_name);
@@ -1502,7 +1455,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
15021455
if export_name == "default" {
15031456
let export_expr = ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultExpr(
15041457
ExportDefaultExpr {
1505-
span: *span,
1458+
span: ident.span,
15061459
expr: Box::new(Expr::Call(CallExpr {
15071460
span: call_expr_span,
15081461
callee: Callee::Expr(Box::new(Expr::Ident(
@@ -1530,7 +1483,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
15301483
decls: vec![VarDeclarator {
15311484
span: DUMMY_SP,
15321485
name: Pat::Ident(
1533-
IdentName::new(export_name.clone().into(), *span)
1486+
IdentName::new(export_name.clone().into(), ident.span)
15341487
.into(),
15351488
),
15361489
init: Some(Box::new(Expr::Call(CallExpr {
@@ -1606,14 +1559,10 @@ impl<C: Comments> VisitMut for ServerActions<C> {
16061559
elems: self
16071560
.exported_idents
16081561
.iter()
1609-
.map(|e| {
1562+
.map(|(ident, _span)| {
16101563
Some(ExprOrSpread {
16111564
spread: None,
1612-
expr: Box::new(Expr::Ident(Ident::new(
1613-
e.0 .0.clone(),
1614-
DUMMY_SP,
1615-
e.0 .1,
1616-
))),
1565+
expr: Box::new(Expr::Ident(ident.clone())),
16171566
})
16181567
})
16191568
.collect(),
@@ -1758,7 +1707,10 @@ impl<C: Comments> VisitMut for ServerActions<C> {
17581707
noop_visit_mut_type!();
17591708
}
17601709

1761-
fn retain_names_from_declared_idents(child_names: &mut Vec<Name>, current_declared_idents: &[Id]) {
1710+
fn retain_names_from_declared_idents(
1711+
child_names: &mut Vec<Name>,
1712+
current_declared_idents: &[Ident],
1713+
) {
17621714
// Collect the names to retain in a separate vector
17631715
let mut retained_names = Vec::new();
17641716

@@ -1790,7 +1742,9 @@ fn retain_names_from_declared_idents(child_names: &mut Vec<Name>, current_declar
17901742
}
17911743

17921744
if should_retain
1793-
&& current_declared_idents.contains(&name.0)
1745+
&& current_declared_idents
1746+
.iter()
1747+
.any(|ident| ident.to_id() == name.0)
17941748
&& !retained_names.contains(name)
17951749
{
17961750
retained_names.push(name.clone());
@@ -2255,99 +2209,100 @@ fn remove_server_directive_index_in_fn(
22552209
});
22562210
}
22572211

2258-
fn collect_idents_in_array_pat<C: IdentCollector>(elems: &[Option<Pat>], collector: &mut C) {
2212+
fn collect_idents_in_array_pat(elems: &[Option<Pat>], idents: &mut Vec<Ident>) {
22592213
for elem in elems.iter().flatten() {
22602214
match elem {
22612215
Pat::Ident(ident) => {
2262-
collector.collect(ident.id.to_id(), Some(ident.id.span));
2216+
idents.push(ident.id.clone());
22632217
}
22642218
Pat::Array(array) => {
2265-
collect_idents_in_array_pat(&array.elems, collector);
2219+
collect_idents_in_array_pat(&array.elems, idents);
22662220
}
22672221
Pat::Object(object) => {
2268-
collect_idents_in_object_pat(&object.props, collector);
2222+
collect_idents_in_object_pat(&object.props, idents);
22692223
}
22702224
Pat::Rest(rest) => {
22712225
if let Pat::Ident(ident) = &*rest.arg {
2272-
collector.collect(ident.id.to_id(), Some(ident.id.span));
2226+
idents.push(ident.id.clone());
22732227
}
22742228
}
22752229
Pat::Assign(AssignPat { left, .. }) => {
2276-
collect_idents_in_pat(left, collector);
2230+
collect_idents_in_pat(left, idents);
22772231
}
22782232
Pat::Expr(..) | Pat::Invalid(..) => {}
22792233
}
22802234
}
22812235
}
22822236

2283-
fn collect_idents_in_object_pat<C: IdentCollector>(props: &[ObjectPatProp], collector: &mut C) {
2237+
fn collect_idents_in_object_pat(props: &[ObjectPatProp], idents: &mut Vec<Ident>) {
22842238
for prop in props {
22852239
match prop {
22862240
ObjectPatProp::KeyValue(KeyValuePatProp { key, value }) => {
22872241
if let PropName::Ident(ident) = key {
2288-
collector.collect(
2289-
(ident.sym.clone(), SyntaxContext::empty()),
2290-
Some(ident.span),
2291-
);
2242+
idents.push(Ident::new(
2243+
ident.sym.clone(),
2244+
ident.span,
2245+
SyntaxContext::empty(),
2246+
));
22922247
}
22932248

22942249
match &**value {
22952250
Pat::Ident(ident) => {
2296-
collector.collect(ident.id.to_id(), Some(ident.id.span));
2251+
idents.push(ident.id.clone());
22972252
}
22982253
Pat::Array(array) => {
2299-
collect_idents_in_array_pat(&array.elems, collector);
2254+
collect_idents_in_array_pat(&array.elems, idents);
23002255
}
23012256
Pat::Object(object) => {
2302-
collect_idents_in_object_pat(&object.props, collector);
2257+
collect_idents_in_object_pat(&object.props, idents);
23032258
}
23042259
_ => {}
23052260
}
23062261
}
23072262
ObjectPatProp::Assign(AssignPatProp { key, .. }) => {
2308-
collector.collect(key.to_id(), Some(key.id.span));
2263+
idents.push(key.id.clone());
23092264
}
23102265
ObjectPatProp::Rest(RestPat { arg, .. }) => {
23112266
if let Pat::Ident(ident) = &**arg {
2312-
collector.collect(ident.id.to_id(), Some(ident.id.span));
2267+
idents.push(ident.id.clone());
23132268
}
23142269
}
23152270
}
23162271
}
23172272
}
23182273

2319-
fn collect_idents_in_var_decls<C: IdentCollector>(decls: &[VarDeclarator], collector: &mut C) {
2274+
fn collect_idents_in_var_decls(decls: &[VarDeclarator], idents: &mut Vec<Ident>) {
23202275
for decl in decls {
2321-
collect_idents_in_pat(&decl.name, collector);
2276+
collect_idents_in_pat(&decl.name, idents);
23222277
}
23232278
}
23242279

2325-
fn collect_idents_in_pat<C: IdentCollector>(pat: &Pat, collector: &mut C) {
2280+
fn collect_idents_in_pat(pat: &Pat, idents: &mut Vec<Ident>) {
23262281
match pat {
23272282
Pat::Ident(ident) => {
2328-
collector.collect(ident.id.to_id(), Some(ident.id.span));
2283+
idents.push(ident.id.clone());
23292284
}
23302285
Pat::Array(array) => {
2331-
collect_idents_in_array_pat(&array.elems, collector);
2286+
collect_idents_in_array_pat(&array.elems, idents);
23322287
}
23332288
Pat::Object(object) => {
2334-
collect_idents_in_object_pat(&object.props, collector);
2289+
collect_idents_in_object_pat(&object.props, idents);
23352290
}
23362291
Pat::Assign(AssignPat { left, .. }) => {
2337-
collect_idents_in_pat(left, collector);
2292+
collect_idents_in_pat(left, idents);
23382293
}
23392294
Pat::Rest(RestPat { arg, .. }) => {
23402295
if let Pat::Ident(ident) = &**arg {
2341-
collector.collect(ident.id.to_id(), Some(ident.id.span));
2296+
idents.push(ident.id.clone());
23422297
}
23432298
}
23442299
Pat::Expr(..) | Pat::Invalid(..) => {}
23452300
}
23462301
}
23472302

2348-
fn collect_decl_idents_in_stmt<C: IdentCollector>(stmt: &Stmt, collector: &mut C) {
2303+
fn collect_decl_idents_in_stmt(stmt: &Stmt, idents: &mut Vec<Ident>) {
23492304
if let Stmt::Decl(Decl::Var(var)) = &stmt {
2350-
collect_idents_in_var_decls(&var.decls, collector);
2305+
collect_idents_in_var_decls(&var.decls, idents);
23512306
}
23522307
}
23532308

0 commit comments

Comments
 (0)