-
-
Notifications
You must be signed in to change notification settings - Fork 466
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(codegen): print shorthand for all
{ x }
variants
closes #4340
- Loading branch information
Showing
9 changed files
with
154 additions
and
36 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
mod tester; | ||
|
||
use std::fmt::Write; | ||
|
||
use tester::mangle; | ||
|
||
#[test] | ||
fn mangler() { | ||
let cases = [ | ||
"function foo(a) {a}", | ||
"function foo(a) { let _ = { x } }", | ||
"function foo(a) { let { x } = y }", | ||
"var x; function foo(a) { ({ x } = y) }", | ||
]; | ||
|
||
let snapshot = cases.into_iter().fold(String::new(), |mut w, case| { | ||
write!(w, "{case}\n{}\n", mangle(case)).unwrap(); | ||
w | ||
}); | ||
|
||
insta::with_settings!({ prepend_module_to_snapshot => false, omit_expression => true }, { | ||
insta::assert_snapshot!("mangler", snapshot); | ||
}); | ||
} |
23 changes: 23 additions & 0 deletions
23
crates/oxc_mangler/tests/integration/snapshots/mangler.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
source: crates/oxc_mangler/tests/integration/main.rs | ||
--- | ||
function foo(a) {a} | ||
function a(b) { | ||
b; | ||
} | ||
|
||
function foo(a) { let _ = { x } } | ||
function a(b) { | ||
let c = {x}; | ||
} | ||
|
||
function foo(a) { let { x } = y } | ||
function a(b) { | ||
let { x: c } = y; | ||
} | ||
|
||
var x; function foo(a) { ({ x } = y) } | ||
var a; | ||
function b(c) { | ||
({x: a} = y); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use oxc_allocator::Allocator; | ||
use oxc_codegen::CodeGenerator; | ||
use oxc_mangler::ManglerBuilder; | ||
use oxc_parser::Parser; | ||
use oxc_span::SourceType; | ||
|
||
pub fn mangle(source_text: &str) -> String { | ||
let allocator = Allocator::default(); | ||
let source_type = SourceType::default().with_module(true); | ||
let ret = Parser::new(&allocator, source_text, source_type).parse(); | ||
let program = ret.program; | ||
let mangler = ManglerBuilder::default().build(&program); | ||
CodeGenerator::new().with_mangler(Some(mangler)).build(&program).source_text | ||
} |