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): add transformer to prevent crashes #2970

Merged
merged 1 commit into from
Apr 14, 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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tasks/coverage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ oxc_prettier = { workspace = true }
oxc_span = { workspace = true }
oxc_tasks_common = { workspace = true }
oxc_sourcemap = { workspace = true }
oxc_transformer = { workspace = true }

serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
Expand Down
26 changes: 21 additions & 5 deletions tasks/coverage/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
// Core
mod runtime;
mod suite;
// Suites
mod babel;
mod misc;
mod test262;
mod typescript;
// Tools
mod codegen;
mod minifier;
mod misc;
mod prettier;
mod runtime;
mod sourcemap;
mod suite;
mod test262;
mod typescript;
mod transformer;

use std::{fs, path::PathBuf, process::Command, time::Duration};

Expand All @@ -24,6 +28,10 @@ use crate::{
prettier::{PrettierBabelCase, PrettierMiscCase, PrettierTest262Case, PrettierTypeScriptCase},
suite::Suite,
test262::{Test262Case, Test262Suite},
transformer::{
TransformerBabelCase, TransformerMiscCase, TransformerTest262Case,
TransformerTypeScriptCase,
},
typescript::{TypeScriptCase, TypeScriptSuite},
};

Expand All @@ -50,6 +58,7 @@ impl AppArgs {
self.run_parser();
self.run_codegen();
self.run_prettier();
self.run_transformer();
// self.run_codegen_runtime();
self.run_minifier();
}
Expand All @@ -76,6 +85,13 @@ impl AppArgs {
MiscSuite::<PrettierMiscCase>::new().run("prettier_misc", self);
}

pub fn run_transformer(&self) {
Test262Suite::<TransformerTest262Case>::new().run("transformer_test262", self);
BabelSuite::<TransformerBabelCase>::new().run("transformer_babel", self);
TypeScriptSuite::<TransformerTypeScriptCase>::new().run("transformer_typescript", self);
MiscSuite::<TransformerMiscCase>::new().run("transformer_misc", self);
}

/// # Panics
pub fn run_codegen_runtime(&self) {
// Run runtime.js to test codegen runtime
Expand Down
1 change: 1 addition & 0 deletions tasks/coverage/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ fn main() {
"codegen" => args.run_codegen(),
"codegen-runtime" => args.run_codegen_runtime(),
"prettier" => args.run_prettier(),
"transformer" => args.run_transformer(),
"minifier" => args.run_minifier(),
"v8_test262_status" => args.run_sync_v8_test262_status(),
_ => args.run_all(),
Expand Down
163 changes: 163 additions & 0 deletions tasks/coverage/src/transformer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
use std::path::{Path, PathBuf};

use oxc_allocator::Allocator;
use oxc_parser::Parser;
use oxc_semantic::SemanticBuilder;
use oxc_span::SourceType;
use oxc_transformer::{TransformOptions, Transformer};

use crate::{
babel::BabelCase,
misc::MiscCase,
suite::{Case, TestResult},
test262::{Test262Case, TestFlag},
typescript::TypeScriptCase,
};

/// 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 {
let allocator = Allocator::default();

let parser_ret = Parser::new(&allocator, source_text, source_type).parse();

let semantic_ret = SemanticBuilder::new(source_text, source_type)
.with_trivias(parser_ret.trivias)
.with_check_syntax_error(true)
.build(&parser_ret.program);

let mut program = parser_ret.program;
let options = TransformOptions::default();
let _ = Transformer::new(&allocator, source_path, semantic_ret.semantic, options)
.build(&mut program);
TestResult::Passed
}

pub struct TransformerTest262Case {
base: Test262Case,
}

impl Case for TransformerTest262Case {
fn new(path: PathBuf, code: String) -> Self {
Self { base: Test262Case::new(path, code) }
}

fn code(&self) -> &str {
self.base.code()
}

fn path(&self) -> &Path {
self.base.path()
}

fn test_result(&self) -> &TestResult {
self.base.test_result()
}

fn skip_test_case(&self) -> bool {
self.base.should_fail()
}

fn run(&mut self) {
let source_text = self.base.code();
let is_module = self.base.meta().flags.contains(&TestFlag::Module);
let source_type = SourceType::default().with_module(is_module);
let result = get_result(source_text, source_type, self.path());
self.base.set_result(result);
}
}

pub struct TransformerBabelCase {
base: BabelCase,
}

impl Case for TransformerBabelCase {
fn new(path: PathBuf, code: String) -> Self {
Self { base: BabelCase::new(path, code) }
}

fn code(&self) -> &str {
self.base.code()
}

fn path(&self) -> &Path {
self.base.path()
}

fn test_result(&self) -> &TestResult {
self.base.test_result()
}

fn skip_test_case(&self) -> bool {
self.base.skip_test_case() || self.base.should_fail()
}

fn run(&mut self) {
let source_text = self.base.code();
let source_type = self.base.source_type();
let result = get_result(source_text, source_type, self.path());
self.base.set_result(result);
}
}

pub struct TransformerTypeScriptCase {
base: TypeScriptCase,
}

impl Case for TransformerTypeScriptCase {
fn new(path: PathBuf, code: String) -> Self {
Self { base: TypeScriptCase::new(path, code) }
}

fn code(&self) -> &str {
self.base.code()
}

fn path(&self) -> &Path {
self.base.path()
}

fn test_result(&self) -> &TestResult {
self.base.test_result()
}

fn skip_test_case(&self) -> bool {
self.base.skip_test_case() || self.base.should_fail()
}

fn run(&mut self) {
let result = get_result(self.base.code(), self.base.source_type(), self.path());
self.base.set_result(result);
}
}

pub struct TransformerMiscCase {
base: MiscCase,
}

impl Case for TransformerMiscCase {
fn new(path: PathBuf, code: String) -> Self {
Self { base: MiscCase::new(path, code) }
}

fn code(&self) -> &str {
self.base.code()
}

fn path(&self) -> &Path {
self.base.path()
}

fn test_result(&self) -> &TestResult {
self.base.test_result()
}

fn skip_test_case(&self) -> bool {
self.base.skip_test_case() || self.base.should_fail()
}

fn run(&mut self) {
let result = get_result(self.base.code(), self.base.source_type(), self.path());
self.base.set_result(result);
}
}
3 changes: 3 additions & 0 deletions tasks/coverage/transformer_babel.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
transformer_babel Summary:
AST Parsed : 2097/2097 (100.00%)
Positive Passed: 2097/2097 (100.00%)
3 changes: 3 additions & 0 deletions tasks/coverage/transformer_misc.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
transformer_misc Summary:
AST Parsed : 15/15 (100.00%)
Positive Passed: 15/15 (100.00%)
3 changes: 3 additions & 0 deletions tasks/coverage/transformer_test262.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
transformer_test262 Summary:
AST Parsed : 45836/45836 (100.00%)
Positive Passed: 45836/45836 (100.00%)
3 changes: 3 additions & 0 deletions tasks/coverage/transformer_typescript.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
transformer_typescript Summary:
AST Parsed : 5243/5243 (100.00%)
Positive Passed: 5243/5243 (100.00%)
Loading