Unverified Commit dc0aea54 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Fix SWC test (#45574)

This PR fixes several test cases related to closure handling. Idents
declared in the module scope shouldn't be included in the closure.

NEXT-456

## Bug

- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ]
[e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent 99166f82
...@@ -2,6 +2,7 @@ use next_binding::swc::{ ...@@ -2,6 +2,7 @@ use next_binding::swc::{
core::{ core::{
common::{chain, comments::SingleThreadedComments, FileName, Mark}, common::{chain, comments::SingleThreadedComments, FileName, Mark},
ecma::parser::{EsConfig, Syntax}, ecma::parser::{EsConfig, Syntax},
ecma::transforms::base::resolver,
ecma::transforms::react::jsx, ecma::transforms::react::jsx,
ecma::transforms::testing::{test, test_fixture}, ecma::transforms::testing::{test, test_fixture},
}, },
...@@ -305,10 +306,13 @@ fn server_actions_fixture(input: PathBuf) { ...@@ -305,10 +306,13 @@ fn server_actions_fixture(input: PathBuf) {
test_fixture( test_fixture(
syntax(), syntax(),
&|_tr| { &|_tr| {
server_actions( chain!(
&FileName::Real("/app/item.js".into()), resolver(Mark::new(), Mark::new(), false),
server_actions::Config { is_server: true }, server_actions(
_tr.comments.as_ref().clone(), &FileName::Real("/app/item.js".into()),
server_actions::Config { is_server: true },
_tr.comments.as_ref().clone(),
)
) )
}, },
&input, &input,
......
import deleteFromDb from 'db'
export function Item({ id1, id2 }) { export function Item({ id1, id2 }) {
async function deleteItem() { async function deleteItem() {
"use action"; "use server";
await deleteFromDb(id1); await deleteFromDb(id1);
await deleteFromDb(id2); await deleteFromDb(id2);
} }
......
/* __next_internal_action_entry_do_not_use__ $ACTION_deleteItem */ export function Item({ id1 , id2 }) { /* __next_internal_action_entry_do_not_use__ $ACTION_deleteItem */ import deleteFromDb from 'db'
export function Item({ id1 , id2 }) {
async function deleteItem() { async function deleteItem() {
return $ACTION_deleteItem(deleteItem.$$closure); return $ACTION_deleteItem(deleteItem.$$closure);
} }
deleteItem.$$typeof = Symbol.for("react.action.reference"); deleteItem.$$typeof = Symbol.for("react.server.reference");
deleteItem.$$filepath = "/app/item.js"; deleteItem.$$filepath = "/app/item.js";
deleteItem.$$name = "$ACTION_deleteItem"; deleteItem.$$name = "$ACTION_deleteItem";
deleteItem.$$closure = [ deleteItem.$$closure = [
......
async function myAction(a, b, c) { async function myAction(a, b, c) {
"use action"; "use server";
console.log('a') console.log('a')
} }
......
/* __next_internal_action_entry_do_not_use__ $ACTION_myAction */ async function myAction(a, b, c) { /* __next_internal_action_entry_do_not_use__ $ACTION_myAction */ async function myAction(a, b, c) {
console.log('a'); console.log('a');
} }
myAction.$$typeof = Symbol.for("react.action.reference"); myAction.$$typeof = Symbol.for("react.server.reference");
myAction.$$filepath = "/app/item.js"; myAction.$$filepath = "/app/item.js";
myAction.$$name = "$ACTION_myAction"; myAction.$$name = "$ACTION_myAction";
export const $ACTION_myAction = myAction; export const $ACTION_myAction = myAction;
......
// app/send.ts // app/send.ts
"use action"; "use server";
export async function myAction(a, b, c) { export async function myAction(a, b, c) {
console.log('a') console.log('a')
} }
\ No newline at end of file
...@@ -2,6 +2,6 @@ ...@@ -2,6 +2,6 @@
/* __next_internal_action_entry_do_not_use__ myAction */ export async function myAction(a, b, c) { /* __next_internal_action_entry_do_not_use__ myAction */ export async function myAction(a, b, c) {
console.log('a'); console.log('a');
} }
myAction.$$typeof = Symbol.for("react.action.reference"); myAction.$$typeof = Symbol.for("react.server.reference");
myAction.$$filepath = "/app/item.js"; myAction.$$filepath = "/app/item.js";
myAction.$$name = "myAction"; myAction.$$name = "myAction";
"use action"; "use server";
export async function a() {} export async function a() {}
export async function b() {} export async function b() {}
export async function c() {} export async function c() {}
...@@ -7,6 +7,6 @@ function d() {} ...@@ -7,6 +7,6 @@ function d() {}
function Foo() { function Foo() {
async function e() { async function e() {
"use action" "use server"
} }
} }
/* __next_internal_action_entry_do_not_use__ a,b,c,$ACTION_e */ export async function a() {} /* __next_internal_action_entry_do_not_use__ a,b,c,$ACTION_e */ export async function a() {}
a.$$typeof = Symbol.for("react.action.reference"); a.$$typeof = Symbol.for("react.server.reference");
a.$$filepath = "/app/item.js"; a.$$filepath = "/app/item.js";
a.$$name = "a"; a.$$name = "a";
export async function b() {} export async function b() {}
b.$$typeof = Symbol.for("react.action.reference"); b.$$typeof = Symbol.for("react.server.reference");
b.$$filepath = "/app/item.js"; b.$$filepath = "/app/item.js";
b.$$name = "b"; b.$$name = "b";
export async function c() {} export async function c() {}
c.$$typeof = Symbol.for("react.action.reference"); c.$$typeof = Symbol.for("react.server.reference");
c.$$filepath = "/app/item.js"; c.$$filepath = "/app/item.js";
c.$$name = "c"; c.$$name = "c";
function d() {} function d() {}
...@@ -15,7 +15,7 @@ function Foo() { ...@@ -15,7 +15,7 @@ function Foo() {
async function e() { async function e() {
return $ACTION_e(e.$$closure); return $ACTION_e(e.$$closure);
} }
e.$$typeof = Symbol.for("react.action.reference"); e.$$typeof = Symbol.for("react.server.reference");
e.$$filepath = "/app/item.js"; e.$$filepath = "/app/item.js";
e.$$name = "$ACTION_e"; e.$$name = "$ACTION_e";
e.$$closure = []; e.$$closure = [];
......
import deleteFromDb from 'db'
const v1 = 'v1';
export function Item({ id1, id2 }) {
const v2 = id2;
async function deleteItem() {
"use server";
await deleteFromDb(id1);
await deleteFromDb(v1);
await deleteFromDb(v2);
}
return <Button action={deleteItem}>Delete</Button>;
}
\ No newline at end of file
/* __next_internal_action_entry_do_not_use__ $ACTION_deleteItem */ import deleteFromDb from 'db';
const v1 = 'v1';
export function Item({ id1 , id2 }) {
const v2 = id2;
async function deleteItem() {
return $ACTION_deleteItem(deleteItem.$$closure);
}
deleteItem.$$typeof = Symbol.for("react.server.reference");
deleteItem.$$filepath = "/app/item.js";
deleteItem.$$name = "$ACTION_deleteItem";
deleteItem.$$closure = [
id1,
v2
];
return <Button action={deleteItem}>Delete</Button>;
}
export async function $ACTION_deleteItem(closure) {
await deleteFromDb(closure[0]);
await deleteFromDb(v1);
await deleteFromDb(closure[1]);
}
\ No newline at end of file
import f, { f1, f2 } from 'foo'
const f3 = 1
var f4
let f5
const [f6, [f7, ...f8], {f9}, { f10, f11: [f12], f13: f14, f15: { f16 }, ...f17 }, ...f18] = []
if (true) {
const g19 = 1;
}
function x() {
const f2 = 1;
const g20 = 1;
}
export function y(p, [p1, { p2 }], ...p3) {
const f2 = 1
const f11 = 1
const f19 = 1
if (true) {
const f8 = 1
}
async function action() {
"use server";
const f17 = 1
if (true) {
const f18 = 1
const f19 = 1
}
console.log(
f,
f1,
f2,
f3,
f4,
f5,
f6,
f7,
f8,
f2(f9),
f12,
f11,
f16.x,
f17,
f18,
p,
p1,
p2,
p3,
g19,
g20,
globalThis
)
}
return <Button action={action}>Delete</Button>
}
/* __next_internal_action_entry_do_not_use__ $ACTION_action */ import f, { f1, f2 } from 'foo';
const f3 = 1;
var f4;
let f5;
const [f6, [f7, ...f8], { f9 }, { f10 , f11: [f12] , f13: f14 , f15: { f16 } , ...f17 }, ...f18] = [];
if (true) {
const g191 = 1;
}
function x() {
const f2 = 1;
const g201 = 1;
}
export function y(p, [p1, { p2 }], ...p3) {
const f2 = 1;
const f11 = 1;
const f19 = 1;
if (true) {
const f8 = 1;
}
async function action() {
return $ACTION_action(action.$$closure);
}
action.$$typeof = Symbol.for("react.server.reference");
action.$$filepath = "/app/item.js";
action.$$name = "$ACTION_action";
action.$$closure = [
f2,
f2,
f11,
p,
p1,
p2,
p3
];
return <Button action={action}>Delete</Button>;
}
export async function $ACTION_action(closure) {
const f17 = 1;
if (true) {
const f18 = 1;
const f19 = 1;
}
console.log(f, f1, closure[0], f3, f4, f5, f6, f7, f8, closure[0](f9), f12, closure[2], f16.x, f17, f18, closure[3], closure[4], closure[5], closure[6], g19, g20, globalThis);
}
\ No newline at end of file
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