8000 add tests · coder-xiaotian/swc-useclient@0371baa · GitHub
[go: up one dir, main page]

Skip to content

Commit 0371baa

Browse files
add tests
1 parent 3576523 commit 0371baa

File tree

11 files changed

+86
-55
lines changed

11 files changed

+86
-55
lines changed

transform/src/lib.rs

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,47 @@
1-
use swc_core:: {
2-
ecma::{
3-
visit::{VisitMut},
4-
ast:: {
5-
Module,
6-
ExprStmt,
7-
Str,
8-
ModuleItem,
9-
Stmt
10-
},
11-
},
12-
common::{
13-
Span,
14-
BytePos,
15-
SyntaxContext,
16-
},
17-
};
181
use serde::Deserialize;
2+
use swc_core::{
3+
common::{BytePos, Span, SyntaxContext},
4+
ecma::{
5+
ast::{ExprStmt, Module, ModuleItem, Stmt, Str},
6+
visit::VisitMut,
7+
},
8+
};
199

2010
#[derive(Debug, Default, Clone, Deserialize)]
2111
#[serde(rename_all = "camelCase", deny_unknown_fields)]
22-
pub struct Config {
12+
pub struct Config {
2313
#[serde(default)]
24-
pub include: Vec<String>
14+
pub include: Vec<String>,
2515
}
2616
pub struct TransformVisitor {
27-
pub filepath: String,
28-
pub include: Vec<String>
17+
pub filepath: String,
18+
pub include: Vec<String>,
2919
}
3020

3121
// #[swc_trace]
3222
impl VisitMut for TransformVisitor {
33-
fn visit_mut_module(&mut self, n: &mut Module) {
34-
for path in &self.include {
35-
if self.filepath.contains(path) {
36-
let str = Str {
37-
span: Span {
38-
lo: BytePos(0),
39-
hi: BytePos(12),
40-
ctxt: SyntaxContext::from_u32(0)
41-
},
42-
value: "use client".into(),
43-
raw: Option::Some("\"use client\"".into())
44-
};
45-
let e = ExprStmt {
46-
span: Span {
47-
lo: BytePos(0),
48-
hi: BytePos(12),
49-
ctxt: SyntaxContext::from_u32(0)
50-
},
51-
expr: Box::new(str.into())
52-
};
53-
n.body.insert(0, ModuleItem::Stmt(Stmt::Expr(e)));
23+
fn visit_mut_module(&mut self, n: &mut Module) {
24+
for path in &self.include {
25+
if self.filepath.contains(path) {
26+
let str = Str {
27+
span: Span {
28+
lo: BytePos(0),
29+
hi: BytePos(12),
30+
ctxt: SyntaxContext::from_u32(0),
31+
},
32+
value: "use client".into(),
33+
raw: Option::Some("\"use client\"".into()),
34+
};
35+
let e = ExprStmt {
36+
span: Span {
37+
lo: BytePos(0),
38+
hi: BytePos(12),
39+
ctxt: SyntaxContext::from_u32(0),
40+
},
41+
expr: Box::new(str.into()),
42+
};
43+
n.body.insert(0, ModuleItem::Stmt(Stmt::Expr(e)));
44+
}
5445
}
55-
}
56-
}
57-
}
46+
}
47+
}

transform/tests/fixture.rs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
use std::path::PathBuf;
2-
3-
use use_client::TransformVisitor;
1+
use serde::Deserialize;
2+
use std::{fs::read_to_string, path::PathBuf};
43
use swc_core::{
4+
ecma::transforms::testing::{test_fixture, FixtureTestConfig},
55
ecma::{
6+
parser::{EsConfig, Syntax},
67
visit::as_folder,
7-
parser::{EsConfig, Syntax}},
8-
ecma::transforms::testing::{test_fixture, FixtureTestConfig},
8+
},
99
};
1010
use testing::fixture;
11+
use use_client::TransformVisitor;
1112

1213
fn syntax() -> Syntax {
1314
Syntax::Es(EsConfig {
@@ -16,15 +17,30 @@ fn syntax() -> Syntax {
1617
})
1718
}
1819

19-
#[fixture("tests/fixture/input.js")]
20+
#[derive(Debug, Default, Clone, Deserialize)]
21+
#[serde(rename_all = "camelCase", deny_unknown_fields)]
22+
pub struct TestConfig {
23+
#[serde(default)]
24+
pub include: Vec<String>,
25+
#[serde(default)]
26+
pub filepath: String,
27+
}
28+
29+
#[fixture("tests/fixtures/**/input.js")]
2030
fn use_client_fixture(input: PathBuf) {
21-
let output = input.parent().unwrap().join("output.js");
31+
let dir = input.parent().unwrap();
32+
let output = dir.join("output.js");
33+
let config = read_to_string(dir.join("config.json")).expect("failed to read config.json");
34+
println!("---- Config -----\n{}", config);
35+
let config: TestConfig = serde_json::from_str(&config).unwrap();
36+
2237
test_fixture(
2338
syntax(),
2439
&|_tr| {
25-
as_folder(TransformVisitor {
26-
filepath: "@mui".into()
27-
})
40+
as_folder(TransformVisitor {
41+
filepath: config.filepath.clone(),
42+
include: config.include.to_vec(),
43+
})
2844
},
2945
&input,
3046
&output,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"filepath": "@mui/material/Button",
3+
"include": ["@mui"]
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"use client";
2+
function Test() {
3+
return <div>test</div>;
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"filepath": "@mui/material/Button",
3+
"include": ["@mui/material"]
4+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function Test() {
2+
return <div>test</div>
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"filepath": "@mui/material/Button",
3+
"include": []
4+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function Test() {
2+
return <div>test</div>
3+
}

0 commit comments

Comments
 (0)
0