From 094dfa5604cba67c31c84f657fe912e3b431dd85 Mon Sep 17 00:00:00 2001 From: Wenzhe Wang Date: Sun, 29 Oct 2023 13:39:32 +0800 Subject: [PATCH] feat: support filter exec snap (#1084) Co-authored-by: Boshen --- crates/oxc_codegen/src/gen.rs | 21 +++++++++++++++++++- tasks/coverage/codegen_typescript.snap | 4 +--- tasks/transform_conformance/src/lib.rs | 8 +++++--- tasks/transform_conformance/src/test_case.rs | 18 +++++++++++++---- 4 files changed, 40 insertions(+), 11 deletions(-) diff --git a/crates/oxc_codegen/src/gen.rs b/crates/oxc_codegen/src/gen.rs index 565f91180..3eb95bf4c 100644 --- a/crates/oxc_codegen/src/gen.rs +++ b/crates/oxc_codegen/src/gen.rs @@ -2128,7 +2128,26 @@ impl<'a, const MINIFY: bool> Gen for Vec<'a, Decorator<'a>> { impl<'a, const MINIFY: bool> Gen for Decorator<'a> { fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) { + fn need_wrap(expr: &Expression) -> bool { + match expr { + // "@foo" + Expression::Identifier(_) => false, + Expression::MemberExpression(member_expr) => { + // "@foo.bar" + // "@(foo['bar'])" + matches!(&**member_expr, MemberExpression::ComputedMemberExpression(_)) + } + Expression::CallExpression(call_expr) => need_wrap(&call_expr.callee), + // "@(foo + bar)" + // "@(() => {})" + _ => true, + } + } + p.print(b'@'); - self.expression.gen_expr(p, Precedence::Assign, Context::default()); + let wrap = need_wrap(&self.expression); + p.wrap(wrap, |p| { + self.expression.gen_expr(p, Precedence::Assign, Context::default()); + }); } } diff --git a/tasks/coverage/codegen_typescript.snap b/tasks/coverage/codegen_typescript.snap index 91708a119..a43e98418 100644 --- a/tasks/coverage/codegen_typescript.snap +++ b/tasks/coverage/codegen_typescript.snap @@ -1,6 +1,6 @@ codegen_typescript Summary: AST Parsed : 5063/5063 (100.00%) -Positive Passed: 5041/5063 (99.57%) +Positive Passed: 5043/5063 (99.60%) Expect to Parse: "compiler/binopAssignmentShouldHaveType.ts" Expect to Parse: "compiler/castExpressionParentheses.ts" Expect to Parse: "compiler/elidedEmbeddedStatementsReplacedWithSemicolon.ts" @@ -13,8 +13,6 @@ Expect to Parse: "compiler/localClassesInLoop_ES6.ts" Expect to Parse: "compiler/typeAliasDeclarationEmit3.ts" Expect to Parse: "conformance/constEnums/constEnum4.ts" Expect to Parse: "conformance/decorators/legacyDecorators-contextualTypes.ts" -Expect to Parse: "conformance/es6/yieldExpressions/generatorTypeCheck59.ts" -Expect to Parse: "conformance/es6/yieldExpressions/generatorTypeCheck61.ts" Expect to Parse: "conformance/esDecorators/esDecorators-contextualTypes.ts" Expect to Parse: "conformance/expressions/asOperator/asOpEmitParens.ts" Expect to Parse: "conformance/expressions/elementAccess/letIdentifierInElementAccess01.ts" diff --git a/tasks/transform_conformance/src/lib.rs b/tasks/transform_conformance/src/lib.rs index 3f94f7f1b..a45b0b2c6 100644 --- a/tasks/transform_conformance/src/lib.rs +++ b/tasks/transform_conformance/src/lib.rs @@ -220,7 +220,7 @@ impl TestRunnerEnv { ) } - fn run_test(path: &Path) -> bool { + fn get_test_result(path: &Path) -> String { let output = Command::new("bun") .current_dir(path.parent().unwrap()) .args(["test", path.file_name().unwrap().to_string_lossy().as_ref()]) @@ -228,8 +228,10 @@ impl TestRunnerEnv { .expect("Try install bun: https://bun.sh/docs/installation"); let content = if output.stderr.is_empty() { &output.stdout } else { &output.stderr }; - let content = String::from_utf8_lossy(content); + String::from_utf8_lossy(content).to_string() + } - content.contains("1 pass") + fn run_test(path: &Path) -> bool { + Self::get_test_result(path).contains("1 pass") } } diff --git a/tasks/transform_conformance/src/test_case.rs b/tasks/transform_conformance/src/test_case.rs index 5adfe965b..e55e11d46 100644 --- a/tasks/transform_conformance/src/test_case.rs +++ b/tasks/transform_conformance/src/test_case.rs @@ -29,8 +29,7 @@ impl TestCaseKind { pub fn test(&self, filter: Option<&str>) -> bool { match self { Self::Transform(test_case) => test_case.test(filter), - // not support filter yet. - Self::Exec(test_case) => test_case.test(None), + Self::Exec(test_case) => test_case.test(filter), } } @@ -267,9 +266,20 @@ impl TestCase for ExecTestCase { Self { path, options } } - fn test(&self, _filter: Option<&str>) -> bool { + fn test(&self, filter: Option<&str>) -> bool { + let filtered = filter.is_some_and(|f| self.path.to_string_lossy().as_ref().contains(f)); + let result = self.transform(&self.path); let target_path = self.write_to_test_files(&result); - Self::run_test(&target_path) + let passed = Self::run_test(&target_path); + if filtered { + println!("input_path: {:?}", &self.path); + println!("target_path: {:?}", &target_path); + println!("Input:\n{}\n", fs::read_to_string(&self.path).unwrap()); + println!("Transformed:\n{result}\n"); + println!("Test Result:\n{}\n", TestRunnerEnv::get_test_result(&target_path)); + } + + passed } }