feat(tasks/coverage): turn on idempotency testing for transformer (#3470)

This commit is contained in:
Dunqing 2024-05-30 11:36:18 +00:00 committed by Dunqing
parent 350cd9158a
commit 574629e5de
4 changed files with 79 additions and 12 deletions

View file

@ -32,8 +32,12 @@ use oxc_span::SourceType;
use oxc_traverse::{traverse_mut, Traverse, TraverseCtx}; use oxc_traverse::{traverse_mut, Traverse, TraverseCtx};
pub use crate::{ pub use crate::{
compiler_assumptions::CompilerAssumptions, env::EnvOptions, es2015::ES2015Options, compiler_assumptions::CompilerAssumptions,
options::BabelOptions, options::TransformOptions, react::ReactOptions, env::EnvOptions,
es2015::{ArrowFunctionsOptions, ES2015Options},
options::BabelOptions,
options::TransformOptions,
react::ReactOptions,
typescript::TypeScriptOptions, typescript::TypeScriptOptions,
}; };

View file

@ -371,6 +371,9 @@ pub trait Case: Sized + Sync + Send + UnwindSafe {
writer.write_all(error.as_bytes())?; writer.write_all(error.as_bytes())?;
} }
TestResult::Mismatch(ast_string, expected_ast_string) => { TestResult::Mismatch(ast_string, expected_ast_string) => {
writer.write_all(
format!("Mismatch: {:?}\n", normalize_path(self.path())).as_bytes(),
)?;
if args.diff { if args.diff {
self.print_diff(writer, ast_string.as_str(), expected_ast_string.as_str())?; self.print_diff(writer, ast_string.as_str(), expected_ast_string.as_str())?;
println!("Mismatch: {:?}", normalize_path(self.path())); println!("Mismatch: {:?}", normalize_path(self.path()));

View file

@ -1,9 +1,13 @@
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use oxc_allocator::Allocator; use oxc_allocator::Allocator;
use oxc_codegen::{Codegen, CodegenOptions};
use oxc_parser::Parser; use oxc_parser::Parser;
use oxc_span::SourceType; use oxc_span::SourceType;
use oxc_transformer::{TransformOptions, Transformer}; use oxc_transformer::{
ArrowFunctionsOptions, ES2015Options, ReactOptions, TransformOptions, Transformer,
TypeScriptOptions,
};
use crate::{ use crate::{
babel::BabelCase, babel::BabelCase,
@ -14,16 +18,62 @@ use crate::{
}; };
/// Runs the transformer and make sure it doesn't crash. /// Runs the transformer and make sure it doesn't crash.
/// TODO: add codegen to turn on idempotency test.
fn get_result(source_text: &str, source_type: SourceType, source_path: &Path) -> TestResult { fn get_result(source_text: &str, source_type: SourceType, source_path: &Path) -> TestResult {
let allocator = Allocator::default(); let allocator = Allocator::default();
let ret = Parser::new(&allocator, source_text, source_type).parse(); let filename = source_path.file_name().unwrap().to_string_lossy();
let mut program = ret.program; let options = TransformOptions {
let options = TransformOptions::default(); typescript: TypeScriptOptions::default(),
let _ = es2015: ES2015Options { arrow_function: Some(ArrowFunctionsOptions::default()) },
Transformer::new(&allocator, source_path, source_type, source_text, &ret.trivias, options) react: ReactOptions {
.build(&mut program); jsx_plugin: true,
TestResult::Passed jsx_self_plugin: true,
jsx_source_plugin: true,
..Default::default()
},
..Default::default()
};
let parse_result1 = Parser::new(&allocator, source_text, source_type).parse();
let mut program = parse_result1.program;
let _ = Transformer::new(
&allocator,
source_path,
source_type,
source_text,
&parse_result1.trivias,
options.clone(),
)
.build(&mut program);
let source_text1 =
Codegen::<false>::new(&filename, source_text, CodegenOptions::default(), None)
.build(&program)
.source_text;
let parse_result2 = Parser::new(&allocator, &source_text1, source_type).parse();
let mut program = parse_result2.program;
let _ = Transformer::new(
&allocator,
source_path,
source_type,
&source_text1,
&parse_result2.trivias,
options,
)
.build(&mut program);
let source_text2 =
Codegen::<false>::new(&filename, &source_text1, CodegenOptions::default(), None)
.build(&program)
.source_text;
let result = source_text1 == source_text2;
if result {
TestResult::Passed
} else {
TestResult::Mismatch(source_text1.clone(), source_text2)
}
} }
pub struct TransformerTest262Case { pub struct TransformerTest262Case {

View file

@ -2,4 +2,14 @@ commit: 64d2eeea
transformer_typescript Summary: transformer_typescript Summary:
AST Parsed : 5243/5243 (100.00%) AST Parsed : 5243/5243 (100.00%)
Positive Passed: 5243/5243 (100.00%) Positive Passed: 5233/5243 (99.81%)
Mismatch: "compiler/elidedEmbeddedStatementsReplacedWithSemicolon.ts"
Mismatch: "compiler/jsxComplexSignatureHasApplicabilityError.tsx"
Mismatch: "compiler/jsxEmptyExpressionNotCountedAsChild.tsx"
Mismatch: "compiler/sourceMapValidationClasses.ts"
Mismatch: "compiler/styledComponentsInstantiaionLimitNotReached.ts"
Mismatch: "compiler/tsxReactPropsInferenceSucceedsOnIntersections.tsx"
Mismatch: "compiler/typeAliasDeclarationEmit3.ts"
Mismatch: "conformance/classes/constructorDeclarations/superCalls/emitStatementsBeforeSuperCall.ts"
Mismatch: "conformance/classes/constructorDeclarations/superCalls/emitStatementsBeforeSuperCallWithDefineFields.ts"
Mismatch: "conformance/statements/VariableStatements/usingDeclarations/usingDeclarations.11.ts"