-
-
Notifications
You must be signed in to change notification settings - Fork 454
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transformer): add
EnvOptions::from_browerslist_query
API
- Loading branch information
Showing
8 changed files
with
95 additions
and
45 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,16 +1,39 @@ | ||
mod es_target; | ||
mod plugins; | ||
mod targets; | ||
|
||
use std::path::Path; | ||
|
||
use oxc_allocator::Allocator; | ||
use oxc_codegen::{CodeGenerator, CodegenOptions}; | ||
use oxc_parser::Parser; | ||
use oxc_semantic::SemanticBuilder; | ||
use oxc_span::SourceType; | ||
use oxc_transformer::{TransformOptions, Transformer}; | ||
|
||
pub fn run(source_text: &str, source_type: SourceType) -> String { | ||
pub fn codegen(source_text: &str, source_type: SourceType) -> String { | ||
let allocator = Allocator::default(); | ||
let ret = Parser::new(&allocator, source_text, source_type).parse(); | ||
CodeGenerator::new() | ||
.with_options(CodegenOptions { single_quote: true, ..CodegenOptions::default() }) | ||
.build(&ret.program) | ||
.code | ||
} | ||
|
||
pub(crate) fn test(source_text: &str, options: TransformOptions) -> String { | ||
let source_type = SourceType::default(); | ||
let allocator = Allocator::default(); | ||
let ret = Parser::new(&allocator, source_text, source_type).parse(); | ||
let mut program = ret.program; | ||
let (symbols, scopes) = | ||
SemanticBuilder::new().build(&program).semantic.into_symbol_table_and_scope_tree(); | ||
Transformer::new(&allocator, Path::new(""), options).build_with_symbols_and_scopes( | ||
symbols, | ||
scopes, | ||
&mut program, | ||
); | ||
CodeGenerator::new() | ||
.with_options(CodegenOptions { single_quote: true, ..CodegenOptions::default() }) | ||
.build(&program) | ||
.code | ||
} |
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,36 @@ | ||
use crate::{codegen, test}; | ||
use oxc_span::SourceType; | ||
use oxc_transformer::{ESTarget, EnvOptions, TransformOptions}; | ||
|
||
#[test] | ||
fn targets() { | ||
let cases = [ | ||
("() => {}"), | ||
("a ** b"), | ||
// ("async function foo() {}"), | ||
("({ ...x })"), | ||
("try {} catch {}"), | ||
("a ?? b"), | ||
("a ||= b"), | ||
// ("class foo { static {} }"), | ||
]; | ||
|
||
// Test no transformation for default targets. | ||
for case in cases { | ||
let options = TransformOptions { | ||
env: EnvOptions::from_browerslist_query("defaults").unwrap(), | ||
..TransformOptions::default() | ||
}; | ||
assert_eq!(codegen(case, SourceType::mjs()), test(case, options)); | ||
} | ||
|
||
// Test transformation for very low targets. | ||
for case in cases { | ||
let options = TransformOptions::from(ESTarget::ES5); | ||
let options_node = TransformOptions { | ||
env: EnvOptions::from_browerslist_query("node 0.10").unwrap(), | ||
..TransformOptions::default() | ||
}; | ||
assert_eq!(test(case, options), test(case, options_node)); | ||
} | ||
} |