Skip to content

Commit

Permalink
refactor(transform_conformance): improve report format
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Oct 17, 2023
1 parent 3605f7c commit 052661d
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 73 deletions.
2 changes: 1 addition & 1 deletion crates/oxc_transformer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ oxc_allocator = { workspace = true }
oxc_syntax = { workspace = true }
oxc_semantic = { workspace = true }

serde = { workspace = true }
serde = { workspace = true, features = ["derive"] }

[dev-dependencies]
oxc_parser = { workspace = true }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::options::{TransformOptions, TransformTarget};
///
/// References:
/// * <https://babel.dev/docs/babel-plugin-transform-logical-assignment-operators>
/// * <https://github.com/babel/babel/blob/main/packages/babel-plugin-transform-logical-assignment-operator>
/// * <https://github.com/babel/babel/blob/main/packages/babel-plugin-transform-logical-assignment-operators>
pub struct LogicalAssignmentOperators<'a> {
ast: Rc<AstBuilder<'a>>,
}
Expand Down
12 changes: 6 additions & 6 deletions tasks/transform_conformance/babel.snap.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
Passed: 104/1091

# All Passed:
* babel-plugin-transform-optional-catch-binding
* babel-plugin-transform-shorthand-properties
* babel-plugin-transform-sticky-regex


# babel-plugin-transform-unicode-sets-regex (0/4)
* Failed: basic/basic/input.js
* Failed: basic/string-properties/input.js
Expand Down Expand Up @@ -583,8 +589,6 @@ Passed: 104/1091
* Failed: transparent-expr-wrappers/ts-as-member-expression/input.ts
* Failed: transparent-expr-wrappers/ts-parenthesized-expression-member-call/input.ts

# babel-plugin-transform-optional-catch-binding (All passed)

# babel-plugin-transform-json-strings (2/4)
* Failed: json-strings/directive-line-separator/input.js
* Failed: json-strings/directive-paragraph-separator/input.js
Expand Down Expand Up @@ -729,10 +733,6 @@ Passed: 104/1091
# babel-plugin-transform-exponentiation-operator (3/4)
* Failed: regression/4349/input.js

# babel-plugin-transform-shorthand-properties (All passed)

# babel-plugin-transform-sticky-regex (All passed)

# babel-plugin-transform-unicode-regex (1/4)
* Failed: unicode-regex/basic/input.js
* Failed: unicode-regex/negated-set/input.js
Expand Down
138 changes: 73 additions & 65 deletions tasks/transform_conformance/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,50 @@ fn root() -> PathBuf {
project_root().join("tasks/coverage/babel/packages")
}

const CASES: &[&str] = &[
// ES2024
"babel-plugin-transform-unicode-sets-regex",
// ES2022
"babel-plugin-transform-class-properties",
"babel-plugin-transform-class-static-block",
"babel-plugin-transform-private-methods",
"babel-plugin-transform-private-property-in-object",
// [Syntax] "babel-plugin-transform-syntax-top-level-await",
// ES2021
"babel-plugin-transform-logical-assignment-operators",
"babel-plugin-transform-numeric-separator",
// ES2020
"babel-plugin-transform-export-namespace-from",
"babel-plugin-transform-dynamic-import",
"babel-plugin-transform-export-namespace-from",
"babel-plugin-transform-nullish-coalescing-operator",
"babel-plugin-transform-optional-chaining",
// [Syntax] "babel-plugin-transform-syntax-bigint",
// [Syntax] "babel-plugin-transform-syntax-dynamic-import",
// [Syntax] "babel-plugin-transform-syntax-import-meta",
// ES2019
"babel-plugin-transform-optional-catch-binding",
"babel-plugin-transform-json-strings",
// ES2018
"babel-plugin-transform-async-generator-functions",
"babel-plugin-transform-object-rest-spread",
// [Regex] "babel-plugin-transform-unicode-property-regex",
"babel-plugin-transform-dotall-regex",
// [Regex] "babel-plugin-transform-named-capturing-groups-regex",
// ES2017
"babel-plugin-transform-async-to-generator",
// ES2016
"babel-plugin-transform-exponentiation-operator",
// ES2015
"babel-plugin-transform-shorthand-properties",
"babel-plugin-transform-sticky-regex",
"babel-plugin-transform-unicode-regex",
// TypeScript
"babel-plugin-transform-typescript",
// React
"babel-plugin-transform-react-jsx",
];

impl TestRunner {
pub fn new(options: TestRunnerOptions) -> Self {
Self { options }
Expand All @@ -48,57 +92,12 @@ impl TestRunner {
/// # Panics
pub fn run(self) {
let root = root();

let cases = [
// ES2024
"babel-plugin-transform-unicode-sets-regex",
// ES2022
"babel-plugin-transform-class-properties",
"babel-plugin-transform-class-static-block",
"babel-plugin-transform-private-methods",
"babel-plugin-transform-private-property-in-object",
// [Syntax] "babel-plugin-transform-syntax-top-level-await",
// ES2021
"babel-plugin-transform-logical-assignment-operators",
"babel-plugin-transform-numeric-separator",
// ES2020
"babel-plugin-transform-export-namespace-from",
"babel-plugin-transform-dynamic-import",
"babel-plugin-transform-export-namespace-from",
"babel-plugin-transform-nullish-coalescing-operator",
"babel-plugin-transform-optional-chaining",
// [Syntax] "babel-plugin-transform-syntax-bigint",
// [Syntax] "babel-plugin-transform-syntax-dynamic-import",
// [Syntax] "babel-plugin-transform-syntax-import-meta",
// ES2019
"babel-plugin-transform-optional-catch-binding",
"babel-plugin-transform-json-strings",
// ES2018
"babel-plugin-transform-async-generator-functions",
"babel-plugin-transform-object-rest-spread",
// [Regex] "babel-plugin-transform-unicode-property-regex",
"babel-plugin-transform-dotall-regex",
// [Regex] "babel-plugin-transform-named-capturing-groups-regex",
// ES2017
"babel-plugin-transform-async-to-generator",
// ES2016
"babel-plugin-transform-exponentiation-operator",
// ES2015
"babel-plugin-transform-shorthand-properties",
"babel-plugin-transform-sticky-regex",
"babel-plugin-transform-unicode-regex",
// TypeScript
"babel-plugin-transform-typescript",
// React
"babel-plugin-transform-react-jsx",
];

let mut snapshot = String::new();
let mut total = 0;
let mut all_passed = 0;
let mut all_passed = vec![];
let mut all_passed_count = 0;

// Get all fixtures
for case in cases {
for case in CASES {
let root = root.join(case).join("test/fixtures");
let mut paths = WalkDir::new(&root)
.into_iter()
Expand All @@ -112,35 +111,42 @@ impl TestRunner {
.map(walkdir::DirEntry::into_path)
.collect::<Vec<_>>();
paths.sort_unstable();

let num_of_tests = paths.len();
total += num_of_tests;

// Run the test
let (passed, failed): (Vec<PathBuf>, Vec<PathBuf>) = paths
.into_iter()
.partition(|path| TestCase::new(path).test(self.options.filter.as_deref()));
all_passed += passed.len();
all_passed_count += passed.len();

// Snapshot
snapshot.push_str("# ");
snapshot.push_str(case);
if failed.is_empty() {
snapshot.push_str(" (All passed)\n");
all_passed.push(case);
} else {
snapshot.push_str("# ");
snapshot.push_str(case);
snapshot.push_str(&format!(" ({}/{})\n", passed.len(), num_of_tests));
}
for path in failed {
snapshot.push_str("* Failed: ");
snapshot.push_str(&normalize_path(path.strip_prefix(&root).unwrap()));
for path in failed {
snapshot.push_str("* Failed: ");
snapshot.push_str(&normalize_path(path.strip_prefix(&root).unwrap()));
snapshot.push('\n');
}
snapshot.push('\n');
}
snapshot.push('\n');
}

let snapshot = format!("Passed: {all_passed}/{total}\n\n{snapshot}");
let path = project_root().join("tasks/transform_conformance/babel.snap.md");
let mut file = File::create(path).unwrap();
file.write_all(snapshot.as_bytes()).unwrap();
if self.options.filter.is_none() {
let all_passed =
all_passed.into_iter().map(|s| format!("* {s}")).collect::<Vec<_>>().join("\n");
let snapshot = format!(
"Passed: {all_passed_count}/{total}\n\n# All Passed:\n{all_passed}\n\n\n{snapshot}"
);
let path = project_root().join("tasks/transform_conformance/babel.snap.md");
let mut file = File::create(path).unwrap();
file.write_all(snapshot.as_bytes()).unwrap();
}
}
}

Expand Down Expand Up @@ -187,22 +193,24 @@ impl TestCase {

/// Test conformance by comparing the parsed babel code and transformed code.
fn test(&self, filter: Option<&str>) -> bool {
let filtered = filter.is_some_and(|f| self.path.to_string_lossy().as_ref().contains(f));

let output_path = self.path.parent().unwrap().read_dir().unwrap().find_map(|entry| {
let path = entry.ok()?.path();
let file_stem = path.file_stem()?;
(file_stem == "output").then_some(path)
});

let allocator = Allocator::default();
let source_text = fs::read_to_string(&self.path).unwrap();
let filtered = filter.is_some_and(|f| self.path.to_string_lossy().as_ref().contains(f));
let source_type = SourceType::from_path(&self.path).unwrap();

if filtered {
println!("input_path: {:?}", &self.path);
println!("output_path: {output_path:?}");
println!("input: {source_text}");
}

let allocator = Allocator::default();
let source_type = SourceType::from_path(&self.path).unwrap();

// Get expected code by parsing the source text, so we can get the same code generated result.
let expected = output_path.and_then(|path| fs::read_to_string(path).ok());
let Some(expected) = &expected else { return false };
Expand Down

0 comments on commit 052661d

Please sign in to comment.