Unverified Commit f93a867d authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

Update wasm and next-custom-transforms crates to Rust edition 2024 (#90350)

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
parent 757ab17e
edition = "2024"
style_edition = "2024"
max_width = 100
comment_width = 100
......
[package]
edition = "2018"
edition = "2024"
name = "next-custom-transforms"
version = "0.0.0"
publish = false
......
......@@ -8,12 +8,12 @@ use serde::Deserialize;
use swc_core::{
atoms::Atom,
common::{
FileName, Mark, SourceFile, SourceMap, SyntaxContext,
comments::{Comments, NoopComments},
pass::Optional,
FileName, Mark, SourceFile, SourceMap, SyntaxContext,
},
ecma::{
ast::{fn_pass, noop_pass, EsVersion, Pass},
ast::{EsVersion, Pass, fn_pass, noop_pass},
parser::parse_file_as_module,
visit::visit_mut_pass,
},
......@@ -23,7 +23,7 @@ use crate::{
linter::linter,
transforms::{
cjs_finder::contains_cjs,
dynamic::{next_dynamic, NextDynamicMode},
dynamic::{NextDynamicMode, next_dynamic},
fonts::next_font_loaders,
lint_codemod_comments::lint_codemod_comments,
react_server_components,
......
use swc_core::ecma::{
ast::Pass,
visit::{visit_pass, Visit},
visit::{Visit, visit_pass},
};
pub fn linter<V>(visitor: V) -> impl Pass
......
......@@ -23,15 +23,13 @@ struct Finder {
impl Visit for Finder {
fn visit_callee(&mut self, node: &Callee) {
if self.is_interested {
if let Callee::Expr(e) = node {
if let Expr::Ident(c) = &**e {
if c.sym.starts_with("use") {
self.found = true;
return;
}
}
}
if self.is_interested
&& let Callee::Expr(e) = node
&& let Expr::Ident(c) = &**e
&& c.sym.starts_with("use")
{
self.found = true;
return;
}
node.visit_children_with(self);
......@@ -130,7 +128,7 @@ impl Visit for Finder {
mod tests {
use swc_core::{
common::FileName,
ecma::parser::{parse_file_as_program, EsSyntax},
ecma::parser::{EsSyntax, parse_file_as_program},
};
use testing::run_test2;
......
......@@ -56,33 +56,22 @@ impl Visit for CjsFinder {
// Note that `Object.defineProperty(module.exports, ...)` will be handled by
// `visit_member_expr`.
fn visit_call_expr(&mut self, e: &CallExpr) {
if !self.ignore_exports {
if let Callee::Expr(expr) = &e.callee {
if let Expr::Member(member_expr) = &**expr {
if let (Expr::Ident(obj), MemberProp::Ident(prop)) =
(&*member_expr.obj, &member_expr.prop)
{
if &*obj.sym == "Object" && &*prop.sym == "defineProperty" {
if let Some(ExprOrSpread { expr: expr0, .. }) = e.args.first() {
if let Expr::Ident(arg0) = &**expr0 {
if &*arg0.sym == "exports" {
if let Some(ExprOrSpread { expr: expr1, .. }) =
e.args.get(1)
{
if let Expr::Lit(Lit::Str(arg1)) = &**expr1 {
if &*arg1.value == "__esModule" {
self.found = true;
return;
}
}
}
}
}
}
}
}
}
}
if !self.ignore_exports
&& let Callee::Expr(expr) = &e.callee
&& let Expr::Member(member_expr) = &**expr
&& let (Expr::Ident(obj), MemberProp::Ident(prop)) =
(&*member_expr.obj, &member_expr.prop)
&& &*obj.sym == "Object"
&& &*prop.sym == "defineProperty"
&& let Some(ExprOrSpread { expr: expr0, .. }) = e.args.first()
&& let Expr::Ident(arg0) = &**expr0
&& &*arg0.sym == "exports"
&& let Some(ExprOrSpread { expr: expr1, .. }) = e.args.get(1)
&& let Expr::Lit(Lit::Str(arg1)) = &**expr1
&& &*arg1.value == "__esModule"
{
self.found = true;
return;
}
e.callee.visit_with(self);
......@@ -113,15 +102,15 @@ impl Visit for CjsFinder {
}
fn visit_member_expr(&mut self, e: &MemberExpr) {
if let Expr::Ident(obj) = &*e.obj {
if let MemberProp::Ident(prop) = &e.prop {
// Detect `module.exports` and `exports.__esModule`
if (!self.ignore_module && &*obj.sym == "module" && &*prop.sym == "exports")
|| (!self.ignore_exports && &*obj.sym == "exports")
{
self.found = true;
return;
}
if let Expr::Ident(obj) = &*e.obj
&& let MemberProp::Ident(prop) = &e.prop
{
// Detect `module.exports` and `exports.__esModule`
if (!self.ignore_module && &*obj.sym == "module" && &*prop.sym == "exports")
|| (!self.ignore_exports && &*obj.sym == "exports")
{
self.found = true;
return;
}
}
......
......@@ -2,14 +2,14 @@ use std::fmt::Write;
use swc_core::{
atoms::Atom,
common::{util::take::Take, DUMMY_SP},
common::{DUMMY_SP, util::take::Take},
ecma::{
ast::{
CallExpr, Callee, ExportDefaultDecl, ExportDefaultExpr, Expr, FnDecl, FnExpr,
KeyValueProp, MemberProp, ObjectLit, Pass, PropOrSpread, VarDeclarator,
},
utils::ExprFactory,
visit::{visit_mut_pass, VisitMut, VisitMutWith},
visit::{VisitMut, VisitMutWith, visit_mut_pass},
},
};
......
......@@ -2,7 +2,7 @@ use swc_core::{
common::{Span, Spanned},
ecma::{
ast::*,
visit::{fold_pass, Fold},
visit::{Fold, fold_pass},
},
quote,
};
......@@ -52,12 +52,11 @@ fn find_var_init_span(items: &[ModuleItem], local_name: &str) -> Option<Span> {
_ => continue,
};
for d in &decl.decls {
if let Pat::Ident(ident) = &d.name {
if ident.id.sym == local_name {
if let Some(init) = &d.init {
return Some(init.span());
}
}
if let Pat::Ident(ident) = &d.name
&& ident.id.sym == local_name
&& let Some(init) = &d.init
{
return Some(init.span());
}
}
}
......@@ -72,12 +71,11 @@ impl Fold for DebugInstantStack {
ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(export_decl)) => {
if let Decl::Var(var_decl) = &export_decl.decl {
for decl in &var_decl.decls {
if let Pat::Ident(ident) = &decl.name {
if ident.id.sym == "unstable_instant" {
if let Some(init) = &decl.init {
self.instant_export_span = Some(init.span());
}
}
if let Pat::Ident(ident) = &decl.name
&& ident.id.sym == "unstable_instant"
&& let Some(init) = &decl.init
{
self.instant_export_span = Some(init.span());
}
}
}
......
......@@ -2,7 +2,7 @@ use swc_core::{
common::errors::HANDLER,
ecma::{
ast::*,
visit::{noop_visit_type, Visit},
visit::{Visit, noop_visit_type},
},
};
pub struct FindFunctionsOutsideModuleScope<'a> {
......
......@@ -3,7 +3,7 @@ use swc_core::{
common::errors::HANDLER,
ecma::{
ast::*,
visit::{noop_visit_type, Visit},
visit::{Visit, noop_visit_type},
},
};
......
......@@ -6,7 +6,7 @@ use swc_core::{
ecma::{
ast::{Id, ModuleItem, Pass},
atoms::Atom,
visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitWith},
visit::{VisitMut, VisitWith, noop_visit_mut_type, visit_mut_pass},
},
};
......
use rustc_hash::{FxHashMap, FxHashSet};
use swc_core::{
atoms::{atom, Atom, Wtf8Atom},
atoms::{Atom, Wtf8Atom, atom},
ecma::{
ast::{
Expr, Id, ImportDecl, ImportNamedSpecifier, ImportSpecifier, MemberExpr, MemberProp,
Module,
},
visit::{noop_visit_type, Visit, VisitWith},
visit::{Visit, VisitWith, noop_visit_type},
},
};
......
......@@ -37,10 +37,10 @@ impl VisitMut for MiddlewareDynamic {
let callee = &call_expr.callee;
if let Callee::Expr(callee) = callee {
// `eval('some')`, or `Function('some')`
if let Expr::Ident(ident) = &**callee {
if ident.sym == "eval" || ident.sym == "Function" {
should_wrap = Some(WrappedExpr::Eval);
}
if let Expr::Ident(ident) = &**callee
&& (ident.sym == "eval" || ident.sym == "Function")
{
should_wrap = Some(WrappedExpr::Eval);
}
if let Expr::Member(MemberExpr {
......@@ -70,16 +70,15 @@ impl VisitMut for MiddlewareDynamic {
prop: MemberProp::Ident(member_prop_ident),
..
}) = &**obj
&& let Expr::Ident(ident) = &**obj
{
if 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);
}
// `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);
}
}
}
......@@ -100,12 +99,11 @@ impl VisitMut for MiddlewareDynamic {
}
}
if let Expr::New(NewExpr { callee, .. }) = &expr {
if let Expr::Ident(ident) = &**callee {
if ident.sym == "Function" {
*expr = quote!("__next_eval__(function() { return $orig_call });" as Expr, orig_call: Expr = expr.clone());
}
}
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());
}
}
}
......@@ -5,7 +5,7 @@ use swc_core::{
common::DUMMY_SP,
ecma::{
ast::*,
visit::{visit_mut_pass, VisitMut},
visit::{VisitMut, visit_mut_pass},
},
};
......
use std::{cell::RefCell, mem::take, rc::Rc};
use easy_error::{bail, Error};
use easy_error::{Error, bail};
use rustc_hash::FxHashSet;
use swc_core::{
atoms::{atom, Atom},
atoms::{Atom, atom},
common::{
DUMMY_SP,
errors::HANDLER,
pass::{Repeat, Repeated},
DUMMY_SP,
},
ecma::{
ast::*,
visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith},
visit::{VisitMut, VisitMutWith, noop_visit_mut_type, visit_mut_pass},
},
};
......@@ -131,10 +131,10 @@ impl VisitMut for Analyzer<'_> {
}
fn visit_mut_export_named_specifier(&mut self, s: &mut ExportNamedSpecifier) {
if let ModuleExportName::Ident(id) = &s.orig {
if !SSG_EXPORTS.contains(&&*id.sym) {
self.add_ref(id.to_id());
}
if let ModuleExportName::Ident(id) = &s.orig
&& !SSG_EXPORTS.contains(&&*id.sym)
{
self.add_ref(id.to_id());
}
}
......@@ -145,10 +145,10 @@ impl VisitMut for Analyzer<'_> {
}
for decl in &d.decls {
if let Pat::Ident(id) = &decl.name {
if !SSG_EXPORTS.contains(&&*id.id.sym) {
self.add_ref(id.to_id());
}
if let Pat::Ident(id) = &decl.name
&& !SSG_EXPORTS.contains(&&*id.id.sym)
{
self.add_ref(id.to_id());
}
}
}
......@@ -240,10 +240,10 @@ impl VisitMut for Analyzer<'_> {
match &e.decl {
Decl::Fn(f) => {
// Drop getStaticProps.
if let Ok(is_data_identifier) = self.state.is_data_identifier(&f.ident) {
if is_data_identifier {
*s = ModuleItem::Stmt(Stmt::Empty(EmptyStmt { span: DUMMY_SP }));
}
if let Ok(is_data_identifier) = self.state.is_data_identifier(&f.ident)
&& is_data_identifier
{
*s = ModuleItem::Stmt(Stmt::Empty(EmptyStmt { span: DUMMY_SP }));
}
}
......@@ -468,18 +468,17 @@ impl VisitMut for NextSsg {
| ModuleDecl::ExportDefaultDecl(..)
| ModuleDecl::ExportDefaultExpr(..),
) = &item
&& let Some(var) = var.take()
{
if let Some(var) = var.take() {
new.push(ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(ExportDecl {
new.push(ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(ExportDecl {
span: DUMMY_SP,
decl: Decl::Var(Box::new(VarDecl {
span: DUMMY_SP,
decl: Decl::Var(Box::new(VarDecl {
span: DUMMY_SP,
kind: VarDeclKind::Var,
decls: vec![var],
..Default::default()
})),
})))
}
kind: VarDeclKind::Var,
decls: vec![var],
..Default::default()
})),
})))
}
new.push(item);
......@@ -599,12 +598,12 @@ impl VisitMut for NextSsg {
#[allow(clippy::single_match)]
fn visit_mut_stmt(&mut self, s: &mut Stmt) {
if let Stmt::Decl(Decl::Fn(f)) = s {
if self.should_remove(f.ident.to_id()) {
self.mark_as_candidate(&mut f.function);
*s = Stmt::Empty(EmptyStmt { span: DUMMY_SP });
return;
}
if let Stmt::Decl(Decl::Fn(f)) = s
&& self.should_remove(f.ident.to_id())
{
self.mark_as_candidate(&mut f.function);
*s = Stmt::Empty(EmptyStmt { span: DUMMY_SP });
return;
}
s.visit_mut_children_with(self);
......
......@@ -2,12 +2,12 @@ use std::collections::HashMap;
use serde::Deserialize;
use swc_core::{
atoms::{atom, Atom, Wtf8Atom},
atoms::{Atom, Wtf8Atom, atom},
common::DUMMY_SP,
ecma::{
ast::*,
utils::private_ident,
visit::{fold_pass, Fold},
visit::{Fold, fold_pass},
},
};
......
......@@ -10,7 +10,7 @@ use swc_core::{
common::DUMMY_SP,
ecma::{
ast::*,
visit::{fold_pass, Fold, FoldWith},
visit::{Fold, FoldWith, fold_pass},
},
};
......@@ -139,31 +139,30 @@ impl Fold for OptimizeServerReact {
if let Expr::Call(call) = &expr {
if let Callee::Expr(box Expr::Ident(f)) = &call.callee {
// Mark `useEffect` as DCE'able
if let Some(use_effect_ident) = &self.use_effect_ident {
if &f.to_id() == use_effect_ident && !effect_has_side_effect_deps(call) {
// return Expr::Lit(Lit::Null(Null { span: DUMMY_SP }));
return wrap_expr_with_env_prod_condition(call.clone());
}
if let Some(use_effect_ident) = &self.use_effect_ident
&& &f.to_id() == use_effect_ident
&& !effect_has_side_effect_deps(call)
{
// return Expr::Lit(Lit::Null(Null { span: DUMMY_SP }));
return wrap_expr_with_env_prod_condition(call.clone());
}
// Mark `useLayoutEffect` as DCE'able
if let Some(use_layout_effect_ident) = &self.use_layout_effect_ident {
if &f.to_id() == use_layout_effect_ident && !effect_has_side_effect_deps(call) {
return wrap_expr_with_env_prod_condition(call.clone());
}
if let Some(use_layout_effect_ident) = &self.use_layout_effect_ident
&& &f.to_id() == use_layout_effect_ident
&& !effect_has_side_effect_deps(call)
{
return wrap_expr_with_env_prod_condition(call.clone());
}
} else if let Some(react_ident) = &self.react_ident {
if let Callee::Expr(box Expr::Member(member)) = &call.callee {
if let box Expr::Ident(f) = &member.obj {
if &f.to_id() == react_ident {
if let MemberProp::Ident(i) = &member.prop {
// Mark `React.useEffect` and `React.useLayoutEffect` as DCE'able
// calls in production
if i.sym == "useEffect" || i.sym == "useLayoutEffect" {
return wrap_expr_with_env_prod_condition(call.clone());
}
}
}
}
} else if let Some(react_ident) = &self.react_ident
&& let Callee::Expr(box Expr::Member(member)) = &call.callee
&& let box Expr::Ident(f) = &member.obj
&& &f.to_id() == react_ident
&& let MemberProp::Ident(i) = &member.prop
{
// Mark `React.useEffect` and `React.useLayoutEffect` as DCE'able
// calls in production
if i.sym == "useEffect" || i.sym == "useLayoutEffect" {
return wrap_expr_with_env_prod_condition(call.clone());
}
}
}
......@@ -178,53 +177,48 @@ impl Fold for OptimizeServerReact {
return decl;
}
if let Pat::Array(array_pat) = &decl.name {
if array_pat.elems.len() == 2 {
if let Some(box Expr::Call(call)) = &decl.init {
if let Callee::Expr(box Expr::Ident(f)) = &call.callee {
if let Some(use_state_ident) = &self.use_state_ident {
if &f.to_id() == use_state_ident && call.args.len() == 1 {
// We do the optimization only if the arg is a literal or a
// type that we can
// be sure is not a function (e.g. {} or [] lit).
// This is because useState allows a function as the
// initialiser.
match &call.args[0].expr {
box Expr::Lit(_) | box Expr::Object(_) | box Expr::Array(_) => {
// const [state, setState] = [x, () => {}];
return VarDeclarator {
definite: false,
name: decl.name.clone(),
init: Some(Box::new(Expr::Array(ArrayLit {
elems: vec![
Some(call.args[0].expr.clone().into()),
Some(
Expr::Arrow(ArrowExpr {
span: DUMMY_SP,
body: Box::new(BlockStmtOrExpr::Expr(
Box::new(Expr::Lit(Lit::Null(
Null { span: DUMMY_SP },
))),
)),
is_async: false,
is_generator: false,
params: vec![],
..Default::default()
})
.into(),
),
],
span: DUMMY_SP,
}))),
span: DUMMY_SP,
};
}
_ => {}
}
}
}
}
if let Pat::Array(array_pat) = &decl.name
&& array_pat.elems.len() == 2
&& let Some(box Expr::Call(call)) = &decl.init
&& let Callee::Expr(box Expr::Ident(f)) = &call.callee
&& let Some(use_state_ident) = &self.use_state_ident
&& &f.to_id() == use_state_ident
&& call.args.len() == 1
{
// We do the optimization only if the arg is a literal or a
// type that we can
// be sure is not a function (e.g. {} or [] lit).
// This is because useState allows a function as the
// initialiser.
match &call.args[0].expr {
box Expr::Lit(_) | box Expr::Object(_) | box Expr::Array(_) => {
// const [state, setState] = [x, () => {}];
return VarDeclarator {
definite: false,
name: decl.name.clone(),
init: Some(Box::new(Expr::Array(ArrayLit {
elems: vec![
Some(call.args[0].expr.clone().into()),
Some(
Expr::Arrow(ArrowExpr {
span: DUMMY_SP,
body: Box::new(BlockStmtOrExpr::Expr(Box::new(Expr::Lit(
Lit::Null(Null { span: DUMMY_SP }),
)))),
is_async: false,
is_generator: false,
params: vec![],
..Default::default()
})
.into(),
),
],
span: DUMMY_SP,
}))),
span: DUMMY_SP,
};
}
_ => {}
}
}
......
use chrono::Utc;
use swc_core::{
common::{errors::HANDLER, Span, DUMMY_SP},
common::{DUMMY_SP, Span, errors::HANDLER},
ecma::{
ast::*,
visit::{fold_pass, Fold, FoldWith},
visit::{Fold, FoldWith, fold_pass},
},
};
......@@ -68,10 +68,10 @@ impl Fold for PageConfig {
if let Decl::Var(var_decl) = &export.decl {
for decl in &var_decl.decls {
let mut is_config = false;
if let Pat::Ident(ident) = &decl.name {
if ident.id.sym == CONFIG_KEY {
is_config = true;
}
if let Pat::Ident(ident) = &decl.name
&& ident.id.sym == CONFIG_KEY
{
is_config = true;
}
if is_config {
......@@ -120,17 +120,17 @@ impl Fold for PageConfig {
) -> ExportNamedSpecifier {
match &specifier.exported {
Some(ident) => {
if let ModuleExportName::Ident(ident) = ident {
if ident.sym == CONFIG_KEY {
self.handle_error("Config cannot be re-exported.", specifier.span)
}
if let ModuleExportName::Ident(ident) = ident
&& ident.sym == CONFIG_KEY
{
self.handle_error("Config cannot be re-exported.", specifier.span)
}
}
None => {
if let ModuleExportName::Ident(ident) = &specifier.orig {
if ident.sym == CONFIG_KEY {
self.handle_error("Config cannot be re-exported.", specifier.span)
}
if let ModuleExportName::Ident(ident) = &specifier.orig
&& ident.sym == CONFIG_KEY
{
self.handle_error("Config cannot be re-exported.", specifier.span)
}
}
}
......
use swc_core::{
common::{comments::Comments, errors::HANDLER, util::take::Take, Span, Spanned, DUMMY_SP},
common::{DUMMY_SP, Span, Spanned, comments::Comments, errors::HANDLER, util::take::Take},
ecma::{
ast::{CallExpr, Callee, EmptyStmt, Expr, Module, ModuleDecl, ModuleItem, Stmt},
visit::{noop_visit_mut_type, VisitMut, VisitMutWith},
visit::{VisitMut, VisitMutWith, noop_visit_mut_type},
},
};
......@@ -74,11 +74,11 @@ where
}
fn visit_mut_module_item(&mut self, m: &mut ModuleItem) {
if let ModuleItem::ModuleDecl(ModuleDecl::Import(import)) = m {
if import.src.value == MODULE {
*m = ModuleItem::Stmt(Stmt::Empty(EmptyStmt { span: DUMMY_SP }));
return;
}
if let ModuleItem::ModuleDecl(ModuleDecl::Import(import)) = m
&& import.src.value == MODULE
{
*m = ModuleItem::Stmt(Stmt::Empty(EmptyStmt { span: DUMMY_SP }));
return;
}
m.visit_mut_children_with(self);
......
......@@ -11,19 +11,19 @@ use regex::Regex;
use rustc_hash::FxHashMap;
use serde::Deserialize;
use swc_core::{
atoms::{atom, Atom, Wtf8Atom},
atoms::{Atom, Wtf8Atom, atom},
common::{
DUMMY_SP, FileName, Span, Spanned,
comments::{Comment, CommentKind, Comments},
errors::HANDLER,
util::take::Take,
FileName, Span, Spanned, DUMMY_SP,
},
ecma::{
ast::*,
utils::{prepend_stmts, quote_ident, quote_str, ExprFactory},
utils::{ExprFactory, prepend_stmts, quote_ident, quote_str},
visit::{
noop_visit_mut_type, noop_visit_type, visit_mut_pass, Visit, VisitMut, VisitMutWith,
VisitWith,
Visit, VisitMut, VisitMutWith, VisitWith, noop_visit_mut_type, noop_visit_type,
visit_mut_pass,
},
},
};
......@@ -162,15 +162,13 @@ impl<C: Comments> ReactServerComponents<C> {
/// removes specific directive from the AST.
fn remove_top_level_directive(&mut self, module: &mut Module) {
module.body.retain(|item| {
if let ModuleItem::Stmt(stmt) = item {
if let Some(expr_stmt) = stmt.as_expr() {
if let Expr::Lit(Lit::Str(Str { value, .. })) = &*expr_stmt.expr {
if &**value == "use client" {
// Remove the directive.
return false;
}
}
}
if let ModuleItem::Stmt(stmt) = item
&& let Some(expr_stmt) = stmt.as_expr()
&& let Expr::Lit(Lit::Str(Str { value, .. })) = &*expr_stmt.expr
&& &**value == "use client"
{
// Remove the directive.
return false;
}
true
});
......@@ -454,14 +452,14 @@ fn collect_module_info(
// an exception because they are not valid directives.
Expr::Paren(ParenExpr { expr, .. }) => {
finished_directives = true;
if let Expr::Lit(Lit::Str(Str { value, .. })) = &**expr {
if &**value == "use client" {
report_error(
app_dir,
filepath,
RSCErrorKind::NextRscErrClientDirective(expr_stmt.span),
);
}
if let Expr::Lit(Lit::Str(Str { value, .. })) = &**expr
&& &**value == "use client"
{
report_error(
app_dir,
filepath,
RSCErrorKind::NextRscErrClientDirective(expr_stmt.span),
);
}
}
_ => {
......@@ -825,24 +823,22 @@ impl ReactServerComponentValidator {
let is_error_file = RE.is_match(&self.filepath);
if is_error_file {
if let Some(app_dir) = &self.app_dir {
if let Some(app_dir) = app_dir.to_str() {
if self.filepath.starts_with(app_dir) {
let span = if let Some(first_item) = module.body.first() {
first_item.span()
} else {
module.span
};
if is_error_file
&& let Some(app_dir) = &self.app_dir
&& let Some(app_dir) = app_dir.to_str()
&& self.filepath.starts_with(app_dir)
{
let span = if let Some(first_item) = module.body.first() {
first_item.span()
} else {
module.span
};
report_error(
&self.app_dir,
&self.filepath,
RSCErrorKind::NextRscErrErrorFileServerComponent(span),
);
}
}
}
report_error(
&self.app_dir,
&self.filepath,
RSCErrorKind::NextRscErrErrorFileServerComponent(span),
);
}
}
......
......@@ -3,9 +3,9 @@ use swc_core::{
common::Mark,
ecma::{
ast::*,
atoms::{atom, Atom},
transforms::optimization::simplify::dce::{dce, Config as DCEConfig},
visit::{fold_pass, Fold, FoldWith, VisitMutWith},
atoms::{Atom, atom},
transforms::optimization::simplify::dce::{Config as DCEConfig, dce},
visit::{Fold, FoldWith, VisitMutWith, fold_pass},
},
};
......@@ -63,10 +63,10 @@ impl Fold for ExportShaker {
.decls
.iter()
.filter_map(|var_decl| {
if let Pat::Ident(BindingIdent { id, .. }) = &var_decl.name {
if self.ignore.contains(&id.sym) {
return Some(var_decl.to_owned());
}
if let Pat::Ident(BindingIdent { id, .. }) = &var_decl.name
&& self.ignore.contains(&id.sym)
{
return Some(var_decl.to_owned());
}
None
})
......@@ -87,15 +87,15 @@ impl Fold for ExportShaker {
.filter_map(|spec| {
if let ExportSpecifier::Named(named_spec) = spec {
if let Some(ident) = &named_spec.exported {
if let ModuleExportName::Ident(ident) = ident {
if self.ignore.contains(&ident.sym) {
return Some(ExportSpecifier::Named(named_spec));
}
}
} else if let ModuleExportName::Ident(ident) = &named_spec.orig {
if self.ignore.contains(&ident.sym) {
if let ModuleExportName::Ident(ident) = ident
&& self.ignore.contains(&ident.sym)
{
return Some(ExportSpecifier::Named(named_spec));
}
} else if let ModuleExportName::Ident(ident) = &named_spec.orig
&& self.ignore.contains(&ident.sym)
{
return Some(ExportSpecifier::Named(named_spec));
}
}
None
......
......@@ -9,13 +9,13 @@ use rustc_hash::{FxHashMap, FxHashSet};
use swc_core::{
atoms::Atom,
common::{
DUMMY_SP,
errors::HANDLER,
pass::{Repeat, Repeated},
DUMMY_SP,
},
ecma::{
ast::*,
visit::{fold_pass, noop_fold_type, noop_visit_type, Fold, FoldWith, Visit, VisitWith},
visit::{Fold, FoldWith, Visit, VisitWith, fold_pass, noop_fold_type, noop_visit_type},
},
};
......@@ -347,10 +347,10 @@ impl Visit for Analyzer<'_> {
self.state
.encounter_export(exported_ident, local_ident, export_type);
if let Some(local_ident) = local_ident {
if self.state.should_retain_export_type(export_type) {
self.add_ref(local_ident.to_id());
}
if let Some(local_ident) = local_ident
&& self.state.should_retain_export_type(export_type)
{
self.add_ref(local_ident.to_id());
}
}
}
......@@ -793,13 +793,13 @@ impl Fold for NextSsg {
}
ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(e)) => match &e.decl {
Decl::Fn(f) => {
if let Some(export_type) = self.state.export_type(&f.ident.to_id()) {
if self.state.dropping_export(export_type) {
tracing::trace!(
"Dropping an export specifier because it's an SSR/SSG function"
);
return ModuleItem::Stmt(Stmt::Empty(EmptyStmt { span: DUMMY_SP }));
}
if let Some(export_type) = self.state.export_type(&f.ident.to_id())
&& self.state.dropping_export(export_type)
{
tracing::trace!(
"Dropping an export specifier because it's an SSR/SSG function"
);
return ModuleItem::Stmt(Stmt::Empty(EmptyStmt { span: DUMMY_SP }));
}
}
......@@ -950,20 +950,19 @@ impl Fold for NextSsg {
}
}
Pat::Expr(expr) => {
if let Expr::Member(member_expr) = &**expr {
if let Some(id) = find_member_root_id(member_expr) {
if self.should_remove(&id) {
self.state.should_run_again = true;
tracing::trace!(
"Dropping member expression object `{}{:?}` because it should \
be removed",
id.0,
id.1
);
return Pat::Invalid(Invalid { span: DUMMY_SP });
}
}
if let Expr::Member(member_expr) = &**expr
&& let Some(id) = find_member_root_id(member_expr)
&& self.should_remove(&id)
{
self.state.should_run_again = true;
tracing::trace!(
"Dropping member expression object `{}{:?}` because it should be \
removed",
id.0,
id.1
);
return Pat::Invalid(Invalid { span: DUMMY_SP });
}
}
_ => {}
......@@ -976,32 +975,31 @@ impl Fold for NextSsg {
fn fold_simple_assign_target(&mut self, mut n: SimpleAssignTarget) -> SimpleAssignTarget {
n = n.fold_children_with(self);
if let SimpleAssignTarget::Ident(name) = &n {
if self.should_remove(&name.id.to_id()) {
self.state.should_run_again = true;
tracing::trace!(
"Dropping var `{}{:?}` because it should be removed",
name.id.sym,
name.id.ctxt
);
return SimpleAssignTarget::Invalid(Invalid { span: DUMMY_SP });
}
if let SimpleAssignTarget::Ident(name) = &n
&& self.should_remove(&name.id.to_id())
{
self.state.should_run_again = true;
tracing::trace!(
"Dropping var `{}{:?}` because it should be removed",
name.id.sym,
name.id.ctxt
);
return SimpleAssignTarget::Invalid(Invalid { span: DUMMY_SP });
}
if let SimpleAssignTarget::Member(member_expr) = &n {
if let Some(id) = find_member_root_id(member_expr) {
if self.should_remove(&id) {
self.state.should_run_again = true;
tracing::trace!(
"Dropping member expression object `{}{:?}` because it should be removed",
id.0,
id.1
);
return SimpleAssignTarget::Invalid(Invalid { span: DUMMY_SP });
}
}
if let SimpleAssignTarget::Member(member_expr) = &n
&& let Some(id) = find_member_root_id(member_expr)
&& self.should_remove(&id)
{
self.state.should_run_again = true;
tracing::trace!(
"Dropping member expression object `{}{:?}` because it should be removed",
id.0,
id.1
);
return SimpleAssignTarget::Invalid(Invalid { span: DUMMY_SP });
}
n
......
use rustc_hash::FxHashMap;
use swc_core::{
common::{
BytePos, DUMMY_SP, Mark, Span, Spanned, SyntaxContext,
comments::{Comment, CommentKind, Comments},
source_map::PURE_SP,
util::take::Take,
BytePos, Mark, Span, Spanned, SyntaxContext, DUMMY_SP,
},
ecma::{
ast::*,
utils::{prepend_stmt, private_ident, quote_ident, quote_str},
visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith},
visit::{VisitMut, VisitMutWith, noop_visit_mut_type, visit_mut_pass},
},
quote,
};
......@@ -129,14 +129,12 @@ impl<C: Comments> VisitMut for ImportReplacer<C> {
//
// Only extract names when `await` is present — without await, the
// destructuring targets the Promise, not the module namespace.
if let Some(init) = &decl.init {
if let Some(import_span) = find_awaited_import_call_span(init) {
if let Pat::Object(obj_pat) = &decl.name {
if let Some(names) = extract_export_names_from_pat(obj_pat) {
self.import_export_names.insert(import_span.lo, names);
}
}
}
if let Some(init) = &decl.init
&& let Some(import_span) = find_awaited_import_call_span(init)
&& let Pat::Object(obj_pat) = &decl.name
&& let Some(names) = extract_export_names_from_pat(obj_pat)
{
self.import_export_names.insert(import_span.lo, names);
}
decl.visit_mut_children_with(self);
......@@ -158,24 +156,24 @@ impl<C: Comments> VisitMut for ImportReplacer<C> {
self.has_dynamic_import = true;
// Add /* webpackExports: [...] */ comment if we detected destructuring
if let Some(names) = self.import_export_names.remove(&call_expr.span.lo) {
if let Some(first_arg) = call_expr.args.first() {
let comment_text = if names.is_empty() {
" webpackExports: [] ".to_string()
} else {
let names_json: Vec<String> =
names.iter().map(|n| format!("\"{}\"", n)).collect();
format!(" webpackExports: [{}] ", names_json.join(", "))
};
self.comments.add_leading(
first_arg.span_lo(),
Comment {
span: DUMMY_SP,
kind: CommentKind::Block,
text: comment_text.into(),
},
);
}
if let Some(names) = self.import_export_names.remove(&call_expr.span.lo)
&& let Some(first_arg) = call_expr.args.first()
{
let comment_text = if names.is_empty() {
" webpackExports: [] ".to_string()
} else {
let names_json: Vec<String> =
names.iter().map(|n| format!("\"{}\"", n)).collect();
format!(" webpackExports: [{}] ", names_json.join(", "))
};
self.comments.add_leading(
first_arg.span_lo(),
Comment {
span: DUMMY_SP,
kind: CommentKind::Block,
text: comment_text.into(),
},
);
}
let replacement_expr = quote!(
......
......@@ -2,11 +2,11 @@ use std::sync::Arc;
use swc_core::{
atoms::{Atom, Wtf8Atom},
common::{errors::HANDLER, SourceMap, Span},
common::{SourceMap, Span, errors::HANDLER},
ecma::{
ast::{
op, BinExpr, CallExpr, Callee, CondExpr, Expr, IdentName, IfStmt, ImportDecl, Lit,
MemberExpr, MemberProp, NamedExport, UnaryExpr,
BinExpr, CallExpr, Callee, CondExpr, Expr, IdentName, IfStmt, ImportDecl, Lit,
MemberExpr, MemberProp, NamedExport, UnaryExpr, op,
},
utils::{ExprCtx, ExprExt},
visit::{Visit, VisitWith},
......@@ -272,19 +272,17 @@ Learn more: https://nextjs.org/docs/api-reference/edge-runtime",
self.guarded_symbols.push(ident.sym.clone());
}
Expr::Member(member) => {
if member.prop.is_ident_with("NEXT_RUNTIME") {
if let Expr::Member(obj_member) = &*member.obj {
if obj_member.obj.is_global_ref_to(self.ctx, "process")
&& obj_member.prop.is_ident_with("env")
{
self.guarded_runtime = true;
}
}
if member.prop.is_ident_with("NEXT_RUNTIME")
&& let Expr::Member(obj_member) = &*member.obj
&& obj_member.obj.is_global_ref_to(self.ctx, "process")
&& obj_member.prop.is_ident_with("env")
{
self.guarded_runtime = true;
}
if member.obj.is_global_ref_to(self.ctx, "process") {
if let MemberProp::Ident(prop) = &member.prop {
self.guarded_process_props.push(prop.sym.clone());
}
if member.obj.is_global_ref_to(self.ctx, "process")
&& let MemberProp::Ident(prop) = &member.prop
{
self.guarded_process_props.push(prop.sym.clone());
}
}
Expr::Bin(BinExpr {
......@@ -330,10 +328,10 @@ where
fn visit_call_expr(&mut self, n: &CallExpr) {
n.visit_children_with(self);
if let Callee::Import(_) = &n.callee {
if let Some(Expr::Lit(Lit::Str(s))) = n.args.first().map(|e| &*e.expr) {
self.warn_if_nodejs_module(n.span, &s.value);
}
if let Callee::Import(_) = &n.callee
&& let Some(Expr::Lit(Lit::Str(s))) = n.args.first().map(|e| &*e.expr)
{
self.warn_if_nodejs_module(n.span, &s.value);
}
}
......@@ -371,18 +369,18 @@ where
}
fn visit_expr(&mut self, n: &Expr) {
if let Expr::Ident(ident) = n {
if ident.ctxt == self.ctx.unresolved_ctxt {
if ident.sym == "eval" {
self.emit_dynamic_not_allowed_error(ident.span);
return;
}
if let Expr::Ident(ident) = n
&& ident.ctxt == self.ctx.unresolved_ctxt
{
if ident.sym == "eval" {
self.emit_dynamic_not_allowed_error(ident.span);
return;
}
for api in EDGE_UNSUPPORTED_NODE_APIS {
if self.is_in_middleware_layer() && ident.sym == *api {
self.emit_unsupported_api_error(ident.span, api);
return;
}
for api in EDGE_UNSUPPORTED_NODE_APIS {
if self.is_in_middleware_layer() && ident.sym == *api {
self.emit_unsupported_api_error(ident.span, api);
return;
}
}
}
......@@ -406,11 +404,11 @@ where
}
fn visit_member_expr(&mut self, n: &MemberExpr) {
if n.obj.is_global_ref_to(self.ctx, "process") {
if let MemberProp::Ident(prop) = &n.prop {
self.warn_for_unsupported_process_api(n.span, prop);
return;
}
if n.obj.is_global_ref_to(self.ctx, "process")
&& let MemberProp::Ident(prop) = &n.prop
{
self.warn_for_unsupported_process_api(n.span, prop);
return;
}
n.visit_children_with(self);
......
......@@ -2,12 +2,12 @@ use std::{iter::FromIterator, path::PathBuf};
use next_custom_transforms::transforms::{
disallow_re_export_all_in_page::disallow_re_export_all_in_page,
dynamic::{next_dynamic, NextDynamicMode},
fonts::{next_font_loaders, Config as FontLoaderConfig},
dynamic::{NextDynamicMode, next_dynamic},
fonts::{Config as FontLoaderConfig, next_font_loaders},
next_ssg::next_ssg,
react_server_components::server_components,
server_actions::{self, server_actions, ServerActionsMode},
strip_page_exports::{next_transform_strip_page_exports, ExportFilter},
server_actions::{self, ServerActionsMode, server_actions},
strip_page_exports::{ExportFilter, next_transform_strip_page_exports},
};
use rustc_hash::FxHashSet;
use swc_core::{
......@@ -17,7 +17,7 @@ use swc_core::{
parser::{EsSyntax, Syntax},
transforms::{
base::resolver,
testing::{test_fixture, FixtureTestConfig},
testing::{FixtureTestConfig, test_fixture},
},
},
};
......
......@@ -9,17 +9,17 @@ use next_custom_transforms::transforms::{
cjs_optimizer::cjs_optimizer,
debug_fn_name::debug_fn_name,
debug_instant_stack::debug_instant_stack,
dynamic::{next_dynamic, NextDynamicMode},
fonts::{next_font_loaders, Config as FontLoaderConfig},
dynamic::{NextDynamicMode, next_dynamic},
fonts::{Config as FontLoaderConfig, next_font_loaders},
named_import_transform::named_import_transform,
next_ssg::next_ssg,
optimize_barrel::optimize_barrel,
optimize_server_react::{self, optimize_server_react},
pure::pure_magic,
react_server_components::server_components,
server_actions::{self, server_actions, ServerActionsMode},
shake_exports::{shake_exports, Config as ShakeExportsConfig},
strip_page_exports::{next_transform_strip_page_exports, ExportFilter},
server_actions::{self, ServerActionsMode, server_actions},
shake_exports::{Config as ShakeExportsConfig, shake_exports},
strip_page_exports::{ExportFilter, next_transform_strip_page_exports},
track_dynamic_imports::track_dynamic_imports,
warn_for_edge_runtime::warn_for_edge_runtime,
};
......@@ -27,20 +27,20 @@ use rustc_hash::FxHashSet;
use serde::de::DeserializeOwned;
use swc_core::{
atoms::atom,
common::{comments::SingleThreadedComments, FileName, Mark, SyntaxContext},
common::{FileName, Mark, SyntaxContext, comments::SingleThreadedComments},
ecma::{
ast::Pass,
parser::{EsSyntax, Syntax, TsSyntax},
transforms::{
base::resolver,
react::jsx,
testing::{test_fixture, FixtureTestConfig},
testing::{FixtureTestConfig, test_fixture},
},
utils::ExprCtx,
visit::{visit_mut_pass, visit_pass, Visit},
visit::{Visit, visit_mut_pass, visit_pass},
},
};
use swc_relay::{relay, RelayLanguageConfig};
use swc_relay::{RelayLanguageConfig, relay};
use testing::fixture;
fn syntax() -> Syntax {
......
use std::path::{Path, PathBuf};
use next_custom_transforms::chain_transforms::{custom_before_pass, TransformOptions};
use next_custom_transforms::chain_transforms::{TransformOptions, custom_before_pass};
use serde::de::DeserializeOwned;
use swc_core::{
base::Compiler,
common::{comments::SingleThreadedComments, Mark},
common::{Mark, comments::SingleThreadedComments},
ecma::{
ast::noop_pass,
parser::{Syntax, TsSyntax},
......
......@@ -5,8 +5,8 @@ use once_cell::sync::Lazy;
use rustc_hash::FxHashSet;
use swc_core::{
atoms::Atom,
base::{try_with_handler, Compiler},
common::{comments::SingleThreadedComments, FileName, FilePathMapping, SourceMap, GLOBALS},
base::{Compiler, try_with_handler},
common::{FileName, FilePathMapping, GLOBALS, SourceMap, comments::SingleThreadedComments},
ecma::ast::noop_pass,
};
......
[package]
edition = "2018"
edition = "2024"
name = "wasm"
publish = false
version = "0.0.0"
......
......@@ -2,17 +2,18 @@ use std::{fmt::Debug, sync::Arc};
use anyhow::Context;
use js_sys::JsString;
use next_custom_transforms::chain_transforms::{custom_before_pass, TransformOptions};
use next_custom_transforms::chain_transforms::{TransformOptions, custom_before_pass};
use rustc_hash::FxHashMap;
use swc_core::{
base::{
Compiler,
config::{JsMinifyOptions, ParseOptions},
try_with_handler, Compiler,
try_with_handler,
},
common::{
FileName, FilePathMapping, GLOBALS, Mark, SourceMap,
comments::{Comments, SingleThreadedComments},
errors::ColorConfig,
FileName, FilePathMapping, Mark, SourceMap, GLOBALS,
},
ecma::ast::noop_pass,
};
......
use js_sys::JsString;
use mdxjs::{compile, Options};
use mdxjs::{Options, compile};
use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::future_to_promise;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment