Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(testing): Use program as entry #7221

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions crates/swc_ecma_transforms_testing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,20 +154,20 @@ impl<'a> Tester<'a> {
name: &str,
syntax: Syntax,
src: &str,
) -> Result<Module, ()> {
) -> Result<Program, ()> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change necessary? I think we can alternatively change only the argument passed to fold_**

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to ensure that what fold_with is a program, so the return value has been changed to Program.

let program = program
.fold_with(&mut tr)
.fold_with(&mut as_folder(Normalizer));

convert back to Module might be difficult, as it is possible for it to be parsed as a script.
do you have some good ways to improve it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean you want to use parse_program?
If so, your code is the correct direction

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I want to use visit_mut_program.
If I get Module instead of Program, the visit_mut_program will be skipped.

See https://github.com/swc-project/swc/pull/7202/files#r1159484231

let fm = self
.cm
.new_source_file(FileName::Real(name.into()), src.into());

let module = {
let program = {
let mut p = Parser::new_from(Lexer::new(
syntax,
EsVersion::latest(),
StringInput::from(&*fm),
Some(&self.comments),
));
let res = p
.parse_module()
.parse_program()
.map_err(|e| e.into_diagnostic(self.handler).emit());

for e in p.take_errors() {
Expand All @@ -177,14 +177,14 @@ impl<'a> Tester<'a> {
res?
};

let module = module
let program = program
.fold_with(&mut tr)
.fold_with(&mut as_folder(Normalizer));

Ok(module)
Ok(program)
}

pub fn print(&mut self, module: &Module, comments: &Rc<SingleThreadedComments>) -> String {
pub fn print(&mut self, program: &Program, comments: &Rc<SingleThreadedComments>) -> String {
let mut buf = vec![];
{
let mut emitter = Emitter {
Expand All @@ -200,7 +200,7 @@ impl<'a> Tester<'a> {
};

// println!("Emitting: {:?}", module);
emitter.emit_module(module).unwrap();
emitter.emit_program(program).unwrap();
}

let s = String::from_utf8_lossy(&buf);
Expand Down Expand Up @@ -381,27 +381,27 @@ where
Tester::run(|tester| {
let tr = make_tr(tr, tester);

let module = tester.apply_transform(tr, "input.js", syntax, input)?;
let program = tester.apply_transform(tr, "input.js", syntax, input)?;

match ::std::env::var("PRINT_HYGIENE") {
Ok(ref s) if s == "1" => {
let hygiene_src = tester.print(
&module.clone().fold_with(&mut HygieneVisualizer),
&program.clone().fold_with(&mut HygieneVisualizer),
&tester.comments.clone(),
);
println!("----- Hygiene -----\n{}", hygiene_src);
}
_ => {}
}

let mut module = module
let mut program = program
.fold_with(&mut hygiene::hygiene())
.fold_with(&mut fixer::fixer(Some(&tester.comments)));

let src_without_helpers = tester.print(&module, &tester.comments.clone());
module = module.fold_with(&mut inject_helpers(Mark::fresh(Mark::root())));
let src_without_helpers = tester.print(&program, &tester.comments.clone());
program = program.fold_with(&mut inject_helpers(Mark::fresh(Mark::root())));

let transformed_src = tester.print(&module, &tester.comments.clone());
let transformed_src = tester.print(&program, &tester.comments.clone());

println!(
"\t>>>>> Orig <<<<<\n{}\n\t>>>>> Code <<<<<\n{}",
Expand Down Expand Up @@ -429,7 +429,7 @@ where
Tester::run(|tester| {
let tr = make_tr(tr, tester);

let module = tester.apply_transform(
let program = tester.apply_transform(
tr,
"input.js",
syntax,
Expand All @@ -443,22 +443,22 @@ where
match ::std::env::var("PRINT_HYGIENE") {
Ok(ref s) if s == "1" => {
let hygiene_src = tester.print(
&module.clone().fold_with(&mut HygieneVisualizer),
&program.clone().fold_with(&mut HygieneVisualizer),
&tester.comments.clone(),
);
println!("----- Hygiene -----\n{}", hygiene_src);
}
_ => {}
}

let mut module = module
let mut program = program
.fold_with(&mut hygiene::hygiene())
.fold_with(&mut fixer::fixer(Some(&tester.comments)));

let src_without_helpers = tester.print(&module, &tester.comments.clone());
module = module.fold_with(&mut inject_helpers(Mark::fresh(Mark::root())));
let src_without_helpers = tester.print(&program, &tester.comments.clone());
program = program.fold_with(&mut inject_helpers(Mark::fresh(Mark::root())));

let src = tester.print(&module, &tester.comments.clone());
let src = tester.print(&program, &tester.comments.clone());

println!(
"\t>>>>> {} <<<<<\n{}\n\t>>>>> {} <<<<<\n{}",
Expand Down Expand Up @@ -699,9 +699,9 @@ pub fn test_fixture<P>(
let expected = expected.unwrap_or_default();

let expected_src = Tester::run(|tester| {
let expected_module = tester.apply_transform(noop(), "expected.js", syntax, &expected)?;
let expected_program = tester.apply_transform(noop(), "expected.js", syntax, &expected)?;

let expected_src = tester.print(&expected_module, &tester.comments.clone());
let expected_src = tester.print(&expected_program, &tester.comments.clone());

println!(
"----- {} -----\n{}",
Expand Down Expand Up @@ -747,7 +747,7 @@ pub fn test_fixture<P>(
.fold_with(&mut crate::fixer::fixer(Some(&tester.comments)));

let actual_src = {
let module = &actual;
let program = &actual;
let comments: &Rc<SingleThreadedComments> = &tester.comments.clone();
let mut buf = vec![];
{
Expand All @@ -764,7 +764,7 @@ pub fn test_fixture<P>(
};

// println!("Emitting: {:?}", module);
emitter.emit_module(module).unwrap();
emitter.emit_program(program).unwrap();
}

if let Some(src_map) = &mut src_map {
Expand Down