Levi
Levi
SDSWC Developers
Created by Levi on 3/8/2024 in #questions
How to test a visitor that uses visit_mut_script?
Let's say you have a visitor, something like this
struct Visitor;

impl VisitMut for Visitor {
fn visit_mut_script(&mut self, script: &mut Script) {
// Do something with script
}
}
struct Visitor;

impl VisitMut for Visitor {
fn visit_mut_script(&mut self, script: &mut Script) {
// Do something with script
}
}
Then you create a unit test, something like
#[test]
fn test_visitor() {
let input = "";
let expected = "";

test_transform(Syntax::default(), |_| as_folder(&mut Visitor), input, expected, true);

// Optionally do some assertions with assert, assert_eq etc on the visitor
}
#[test]
fn test_visitor() {
let input = "";
let expected = "";

test_transform(Syntax::default(), |_| as_folder(&mut Visitor), input, expected, true);

// Optionally do some assertions with assert, assert_eq etc on the visitor
}
The test function won't work because the internal code for test_transform parses the input code as a module and not a script, so visit_mut_script is never called. Is there a different function to use instead of test_transform or is there a way to make test_transform use parse_script instead of parse_module?
4 replies
SDSWC Developers
Created by Levi on 5/13/2023 in #questions
How to compile using your own custom plugin?
I created this discussion (https://github.com/swc-project/swc/discussions/7385) but thought I'd make a post here too to try get a response faster. I don't understand how I'm supposed to apply custom transformations to a given JavaScript file. I'm using the swc compile command (from cargo install swc_cli), but the transformations aren't applied on the produced output. Even using a config like this with a plugin like https://github.com/williamtetlow/swc-plugin-console-prefix doesn't apply the transformations:
{
"jsc": {
"experimental": {
"plugins": [
["swc-plugin-console-prefix", { "prefixPattern": "hello", "ignore": ["info"] }]
]
}
}
}
{
"jsc": {
"experimental": {
"plugins": [
["swc-plugin-console-prefix", { "prefixPattern": "hello", "ignore": ["info"] }]
]
}
}
}
(Input is console.log("world"), output is console.log("world"), expected output is console.log("hello", "world")) The command I'm running is swc compile in.js --out-file out.js --config-file build.swcrc. All files including config are in the directory. Enabling env.debug doesn't produce any helpful output either. The only transformations that get applied are stuff like if I target es5 instead of es2016 (ie const is turned into var) as well as the minify option with mangle etc. Plugin transformations don't seem to work at all. Is there a different command you're supposed to use to apply transformations using plugins? I noticed in the README in the mentioned plugin above it uses npx swc ./test.js but shouldn't you still be able to do the same with the CLI installed with cargo install swc_cli?
1 replies