mirror of
https://github.com/hanzoai/next.js.git
synced 2026-09-24 17:00:51 +00:00
The `wasm` and `next-custom-transforms` crates were still on Rust edition 2018 while the rest of the crates in this repo use edition 2024. This caused a pre-commit hook conflict:
- `cargo fmt` passes the crate's own edition (2018) to `rustfmt`, which uses **case-insensitive** import sorting
- The lint-staged pre-commit hook runs `rustfmt --edition 2024 --`, which uses **ASCII order** import sorting (uppercase before lowercase)
These two sort orders disagree on items like `{custom_before_pass, TransformOptions}` vs `{TransformOptions, custom_before_pass}`, causing an infinite flip-flop: every commit would reorder imports, and the next `cargo fmt` would reorder them back.
## Changes
- Bump `crates/wasm` and `crates/next-custom-transforms` from edition 2018 to 2024
- Add `+ use<C>` precise capturing to `server_actions()` return type to satisfy edition 2024's stricter `impl Trait` lifetime capture rules
- Add `style_edition = "2024"` to `.rustfmt.toml` as a safety net so `cargo fmt` uses 2024-style formatting regardless of individual crate editions
- Reformat all affected files with `cargo fmt`
- Apply new clippy rules
110 lines
4.2 KiB
Rust
110 lines
4.2 KiB
Rust
use swc_core::{
|
|
ecma::{
|
|
ast::*,
|
|
visit::{VisitMut, VisitMutWith},
|
|
},
|
|
quote,
|
|
};
|
|
|
|
enum WrappedExpr {
|
|
Eval,
|
|
WasmCompile,
|
|
WasmInstantiate,
|
|
}
|
|
|
|
/// Replaces call / expr to dynamic evaluation in the give code to wrapped expression
|
|
/// (__next_eval__, __next_webassembly_compile__,..) to raise a (nicer) error message.
|
|
///
|
|
/// This should only be only used in development mode.
|
|
///
|
|
/// This transform is specific to edge runtime which are not allowed to call certain dynamic
|
|
/// evaluation (eval, webassembly.instantiate, etc)
|
|
///
|
|
/// check middleware-plugin for corresponding webpack side transform.
|
|
pub fn next_middleware_dynamic() -> MiddlewareDynamic {
|
|
MiddlewareDynamic {}
|
|
}
|
|
|
|
pub struct MiddlewareDynamic {}
|
|
|
|
impl VisitMut for MiddlewareDynamic {
|
|
fn visit_mut_expr(&mut self, expr: &mut Expr) {
|
|
let mut should_wrap = None;
|
|
|
|
expr.visit_mut_children_with(self);
|
|
|
|
if let Expr::Call(call_expr) = &expr {
|
|
let callee = &call_expr.callee;
|
|
if let Callee::Expr(callee) = callee {
|
|
// `eval('some')`, or `Function('some')`
|
|
if let Expr::Ident(ident) = &**callee
|
|
&& (ident.sym == "eval" || ident.sym == "Function")
|
|
{
|
|
should_wrap = Some(WrappedExpr::Eval);
|
|
}
|
|
|
|
if let Expr::Member(MemberExpr {
|
|
obj,
|
|
prop: MemberProp::Ident(prop_ident),
|
|
..
|
|
}) = &**callee
|
|
{
|
|
if let Expr::Ident(ident) = &**obj {
|
|
// `global.eval('some')`
|
|
if ident.sym == "global" && prop_ident.sym == "eval" {
|
|
should_wrap = Some(WrappedExpr::Eval);
|
|
}
|
|
|
|
// `WebAssembly.compile('some')` & `WebAssembly.instantiate('some')`
|
|
if ident.sym == "WebAssembly" {
|
|
if prop_ident.sym == "compile" {
|
|
should_wrap = Some(WrappedExpr::WasmCompile);
|
|
} else if prop_ident.sym == "instantiate" {
|
|
should_wrap = Some(WrappedExpr::WasmInstantiate);
|
|
}
|
|
}
|
|
}
|
|
|
|
if let Expr::Member(MemberExpr {
|
|
obj,
|
|
prop: MemberProp::Ident(member_prop_ident),
|
|
..
|
|
}) = &**obj
|
|
&& let Expr::Ident(ident) = &**obj
|
|
{
|
|
// `global.WebAssembly.compile('some')` &
|
|
// `global.WebAssembly.instantiate('some')`
|
|
if ident.sym == "global" && member_prop_ident.sym == "WebAssembly" {
|
|
if prop_ident.sym == "compile" {
|
|
should_wrap = Some(WrappedExpr::WasmCompile);
|
|
} else if prop_ident.sym == "instantiate" {
|
|
should_wrap = Some(WrappedExpr::WasmInstantiate);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
match should_wrap {
|
|
Some(WrappedExpr::Eval) => {
|
|
*expr = quote!("__next_eval__(function() { return $orig_call });" as Expr, orig_call: Expr = Expr::Call(call_expr.clone()));
|
|
}
|
|
Some(WrappedExpr::WasmCompile) => {
|
|
*expr = quote!("__next_webassembly_compile__(function() { return $orig_call });" as Expr, orig_call: Expr = Expr::Call(call_expr.clone()));
|
|
}
|
|
Some(WrappedExpr::WasmInstantiate) => {
|
|
*expr = quote!("__next_webassembly_instantiate__(function() { return $orig_call });" as Expr, orig_call: Expr = Expr::Call(call_expr.clone()));
|
|
}
|
|
None => {}
|
|
}
|
|
}
|
|
|
|
if let Expr::New(NewExpr { callee, .. }) = &expr
|
|
&& let Expr::Ident(ident) = &**callee
|
|
&& ident.sym == "Function"
|
|
{
|
|
*expr = quote!("__next_eval__(function() { return $orig_call });" as Expr, orig_call: Expr = expr.clone());
|
|
}
|
|
}
|
|
}
|