-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
read file from memory + recover if fail; add mangler test
- Loading branch information
Showing
7 changed files
with
157 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,23 @@ | ||
use anyhow::Result; | ||
|
||
use monitor_oxc::{ | ||
codegen::CodegenRunner, isolated_declarations::test_isolated_declarations, | ||
transform::TransformRunner, | ||
mangler::ManglerRunner, transform::TransformRunner, NodeModulesRunner, | ||
}; | ||
|
||
fn main() { | ||
CodegenRunner::new().run(); | ||
TransformRunner::new().run(); | ||
fn main() -> Result<()> { | ||
let node_modules_runner = NodeModulesRunner::new(); | ||
let result = run(&node_modules_runner); | ||
if result.is_err() { | ||
node_modules_runner.recover(); | ||
} | ||
result | ||
} | ||
|
||
fn run(node_modules_runner: &NodeModulesRunner) -> Result<()> { | ||
CodegenRunner.run(node_modules_runner)?; | ||
TransformRunner.run(node_modules_runner)?; | ||
ManglerRunner.run(node_modules_runner)?; | ||
test_isolated_declarations(); | ||
Ok(()) | ||
} |
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,67 @@ | ||
use std::{fs, path::Path}; | ||
|
||
use anyhow::Result; | ||
|
||
use oxc::{ | ||
allocator::Allocator, | ||
codegen::{CodeGenerator, CommentOptions}, | ||
mangler::ManglerBuilder, | ||
parser::{Parser, ParserReturn}, | ||
span::SourceType, | ||
}; | ||
|
||
use crate::{NodeModulesRunner, Source}; | ||
|
||
pub struct ManglerRunner; | ||
|
||
impl ManglerRunner { | ||
pub fn run(self, runner: &NodeModulesRunner) -> Result<()> { | ||
println!("Running Mangler"); | ||
for source in &runner.files { | ||
self.test(source)?; | ||
} | ||
NodeModulesRunner::run_runtime_test() | ||
} | ||
|
||
fn test(&self, source: &Source) -> Result<()> { | ||
let Source { path, source_type, source_text } = source; | ||
let source_text2 = self.mangle(path, source_text, *source_type); | ||
|
||
// Idempotency test | ||
let source_text3 = self.mangle(path, &source_text2, *source_type); | ||
if source_text2 != source_text3 { | ||
NodeModulesRunner::print_diff(&source_text2, &source_text3); | ||
anyhow::bail!("Mangler idempotency test failed: {path:?}"); | ||
} | ||
|
||
// Write js files for runtime test | ||
if source_type.is_javascript() { | ||
fs::write(path, source_text3).unwrap(); | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn mangle(&self, path: &Path, source_text: &str, source_type: SourceType) -> String { | ||
let allocator = Allocator::default(); | ||
let ParserReturn { program, errors, trivias, .. } = | ||
Parser::new(&allocator, source_text, source_type) | ||
.allow_return_outside_function(true) | ||
.parse(); | ||
if !errors.is_empty() { | ||
for error in errors { | ||
println!("{:?}", error.with_source_code(source_text.to_string())); | ||
} | ||
panic!("Expect no parse errors: {path:?}"); | ||
} | ||
let mangler = ManglerBuilder.build(&program); | ||
CodeGenerator::new() | ||
.enable_comment( | ||
source_text, | ||
trivias, | ||
CommentOptions { preserve_annotate_comments: true }, | ||
) | ||
.with_mangler(Some(mangler)) | ||
.build(&program) | ||
.source_text | ||
} | ||
} |
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