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

feat(coverage): transformer idempotency test #3691

Merged
merged 1 commit into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
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
7 changes: 3 additions & 4 deletions crates/oxc_transformer/examples/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ fn main() {
let ret = Parser::new(&allocator, &source_text, source_type).parse();

if !ret.errors.is_empty() {
println!("Parser Errors:");
for error in ret.errors {
let error = error.with_source_code(source_text.clone());
println!("{error:?}");
}
return;
}

println!("Original:\n");
Expand All @@ -46,16 +46,15 @@ fn main() {
},
..Default::default()
};
Transformer::new(
let _ = Transformer::new(
&allocator,
path,
source_type,
&source_text,
ret.trivias.clone(),
transform_options,
)
.build(&mut program)
.unwrap();
.build(&mut program);

let printed = Codegen::<false>::new("", &source_text, ret.trivias, CodegenOptions::default())
.build(&program)
Expand Down
113 changes: 44 additions & 69 deletions tasks/coverage/src/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
typescript::TypeScriptCase,
};

/// Runs the transformer and make sure it doesn't crash.
/// Idempotency test
fn get_result(
source_text: &str,
source_type: SourceType,
Expand All @@ -27,76 +27,51 @@ fn get_result(
let allocator = Allocator::default();
let filename = source_path.file_name().unwrap().to_string_lossy();
let options = options.unwrap_or_else(get_default_transformer_options);
let parse_result1 = Parser::new(&allocator, source_text, source_type).parse();
let mut program = parse_result1.program;
let transform_result1 = Transformer::new(
&allocator,
source_path,
source_type,
source_text,
parse_result1.trivias.clone(),
options.clone(),
)
.build(&mut program);

let ts_source_text1 = Codegen::<false>::new(
&filename,
source_text,
parse_result1.trivias.clone(),
CodegenOptions::default(),
)
.build(&program)
.source_text;

let source_text1 = Codegen::<false>::new(
&filename,
source_text,
parse_result1.trivias.clone(),
CodegenOptions::default(),
)
.build(&program)
.source_text;

if transform_result1.is_ok() && ts_source_text1 != source_text1 {
return TestResult::Mismatch(ts_source_text1.clone(), source_text1.clone());
}

let parse_result2 = Parser::new(&allocator, &ts_source_text1, source_type).parse();
let mut program = parse_result2.program;

let transform_result2 = Transformer::new(
&allocator,
source_path,
source_type,
&source_text1,
parse_result2.trivias.clone(),
options,
)
.build(&mut program);

let source_text2 = Codegen::<false>::new(
&filename,
&source_text1,
parse_result2.trivias,
CodegenOptions::default(),
)
.build(&program)
.source_text;

if source_text1 == source_text2
|| transform_result1.is_err_and(|err| {
// If error messages are the same, we consider it as a pass.
transform_result2
.map_err(|err| err.iter().map(ToString::to_string).collect::<Vec<_>>().join("\n"))
.is_err_and(|err_message| {
err.iter().map(ToString::to_string).collect::<Vec<_>>().join("\n")
== err_message
})
})
{

// First pass
let transformed1 = {
let mut ret1 = Parser::new(&allocator, source_text, source_type).parse();
let _ = Transformer::new(
&allocator,
source_path,
source_type,
source_text,
ret1.trivias.clone(),
options.clone(),
)
.build(&mut ret1.program);
Codegen::<false>::new(
&filename,
source_text,
ret1.trivias.clone(),
CodegenOptions::default(),
)
.build(&ret1.program)
.source_text
};

// Second pass with only JavaScript parsing
let transformed2 = {
let source_type = SourceType::default().with_module(source_type.is_module());
let mut ret2 = Parser::new(&allocator, &transformed1, source_type).parse();
let _ = Transformer::new(
&allocator,
source_path,
source_type,
&transformed1,
ret2.trivias.clone(),
options,
)
.build(&mut ret2.program);
Codegen::<false>::new(&filename, source_text, ret2.trivias, CodegenOptions::default())
.build(&ret2.program)
.source_text
};

if transformed1 == transformed2 {
TestResult::Passed
} else {
TestResult::Mismatch(source_text1.clone(), source_text2)
TestResult::Mismatch(transformed1, transformed2)
}
}

Expand Down
3 changes: 2 additions & 1 deletion tasks/coverage/transformer_babel.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ commit: 12619ffe

transformer_babel Summary:
AST Parsed : 2101/2101 (100.00%)
Positive Passed: 2101/2101 (100.00%)
Positive Passed: 2100/2101 (99.95%)
Mismatch: "typescript/class/accessor/input.ts"
16 changes: 15 additions & 1 deletion tasks/coverage/transformer_typescript.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,19 @@ commit: d8086f14

transformer_typescript Summary:
AST Parsed : 5283/5283 (100.00%)
Positive Passed: 5282/5283 (99.98%)
Positive Passed: 5268/5283 (99.72%)
Mismatch: "compiler/elidedEmbeddedStatementsReplacedWithSemicolon.ts"
Mismatch: "compiler/esDecoratorsClassFieldsCrash.ts"
Mismatch: "compiler/useBeforeDeclaration_classDecorators.2.ts"
Mismatch: "conformance/classes/propertyMemberDeclarations/autoAccessor10.ts"
Mismatch: "conformance/classes/propertyMemberDeclarations/autoAccessor2.ts"
Mismatch: "conformance/classes/propertyMemberDeclarations/autoAccessor9.ts"
Mismatch: "conformance/classes/propertyMemberDeclarations/autoAccessorAllowedModifiers.ts"
Mismatch: "conformance/classes/propertyMemberDeclarations/staticAutoAccessors.ts"
Mismatch: "conformance/classes/propertyMemberDeclarations/staticAutoAccessorsWithDecorators.ts"
Mismatch: "conformance/decorators/legacyDecorators-contextualTypes.ts"
Mismatch: "conformance/esDecorators/classDeclaration/classThisReference/esDecorators-classDeclaration-classThisReference.ts"
Mismatch: "conformance/esDecorators/classDeclaration/fields/esDecorators-classDeclaration-fields-staticAccessor.ts"
Mismatch: "conformance/esDecorators/classDeclaration/fields/esDecorators-classDeclaration-fields-staticPrivateAccessor.ts"
Mismatch: "conformance/esDecorators/classExpression/esDecorators-classExpression-commentPreservation.ts"
Mismatch: "conformance/esDecorators/esDecorators-contextualTypes.ts"