Skip to content

Commit

Permalink
refactor(transform_conformance): add driver (#4969)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Aug 19, 2024
1 parent 37b9b0e commit 4fdf26d
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 197 deletions.
9 changes: 1 addition & 8 deletions Cargo.lock

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

8 changes: 6 additions & 2 deletions crates/oxc/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub trait CompilerInterface {
}

fn compress_options(&self) -> Option<CompressOptions> {
Some(CompressOptions::all_true())
None
}

fn codegen_options(&self) -> Option<CodegenOptions> {
Expand All @@ -69,6 +69,10 @@ pub trait CompilerInterface {
false
}

fn check_semantic_error(&self) -> bool {
true
}

fn after_parse(&mut self, _parser_return: &mut ParserReturn) -> ControlFlow<()> {
ControlFlow::Continue(())
}
Expand Down Expand Up @@ -172,7 +176,7 @@ pub trait CompilerInterface {
source_path: &Path,
) -> SemanticBuilderReturn<'a> {
SemanticBuilder::new(source_text, source_type)
.with_check_syntax_error(true)
.with_check_syntax_error(self.check_semantic_error())
.build_module_record(source_path.to_path_buf(), program)
.build(program)
}
Expand Down
9 changes: 1 addition & 8 deletions tasks/transform_conformance/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,8 @@ test = false
doctest = false

[dependencies]
oxc_ast = { workspace = true }
oxc_span = { workspace = true }
oxc_allocator = { workspace = true }
oxc_parser = { workspace = true }
oxc_codegen = { workspace = true }
oxc_semantic = { workspace = true }
oxc_transformer = { workspace = true }
oxc = { workspace = true, features = ["full"] }
oxc_tasks_common = { workspace = true }
oxc_diagnostics = { workspace = true }

walkdir = { workspace = true }
pico-args = { workspace = true }
Expand Down
25 changes: 2 additions & 23 deletions tasks/transform_conformance/babel.snap.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
commit: 12619ffe

Passed: 466/953
Passed: 487/953

# All Passed:
* babel-plugin-transform-optional-catch-binding
Expand Down Expand Up @@ -465,37 +465,16 @@ Passed: 466/953
* opts/optimizeConstEnums/input.ts
* opts/rewriteImportExtensions/input.ts

# babel-plugin-transform-typescript (109/151)
# babel-plugin-transform-typescript (130/151)
* class/accessor-allowDeclareFields-false/input.ts
* class/accessor-allowDeclareFields-true/input.ts
* enum/mix-references/input.ts
* enum/ts5.0-const-foldable/input.ts
* exports/declared-types/input.ts
* exports/export-import=/input.ts
* exports/interface/input.ts
* imports/elide-type-referenced-in-imports-equal-no/input.ts
* imports/import=-module/input.ts
* imports/only-remove-type-imports/input.ts
* imports/type-only-export-specifier-2/input.ts
* imports/type-only-import-specifier-4/input.ts
* namespace/alias/input.ts
* namespace/clobber-class/input.ts
* namespace/clobber-enum/input.ts
* namespace/clobber-export/input.ts
* namespace/contentious-names/input.ts
* namespace/declare/input.ts
* namespace/declare-global-nested-namespace/input.ts
* namespace/empty-removed/input.ts
* namespace/export/input.ts
* namespace/module-nested/input.ts
* namespace/module-nested-export/input.ts
* namespace/multiple/input.ts
* namespace/mutable-fail/input.ts
* namespace/nested/input.ts
* namespace/nested-namespace/input.ts
* namespace/nested-shorthand/input.ts
* namespace/same-name/input.ts
* namespace/undeclared/input.ts
* optimize-const-enums/custom-values/input.ts
* optimize-const-enums/custom-values-exported/input.ts
* optimize-const-enums/declare/input.ts
Expand Down
8 changes: 2 additions & 6 deletions tasks/transform_conformance/oxc.snap.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
commit: 12619ffe

Passed: 28/35
Passed: 31/35

# All Passed:
* babel-plugin-transform-optional-catch-binding
* babel-plugin-transform-typescript


# babel-plugin-transform-typescript (4/7)
* computed-constant-value/input.ts
* enum-member-reference/input.ts
* export-elimination/input.ts

# babel-plugin-transform-react-jsx (23/27)
* refresh/can-handle-implicit-arrow-returns/input.jsx
* refresh/registers-identifiers-used-in-jsx-at-definition-site/input.jsx
Expand Down
94 changes: 94 additions & 0 deletions tasks/transform_conformance/src/driver.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
use std::{mem, ops::ControlFlow, path::Path};

use oxc::{
ast::ast::Program,
diagnostics::OxcDiagnostic,
semantic::{post_transform_checker::PostTransformChecker, SemanticBuilderReturn},
span::SourceType,
transformer::{TransformOptions, TransformerReturn},
CompilerInterface,
};

pub struct Driver {
options: TransformOptions,
printed: String,
errors: Vec<OxcDiagnostic>,
check_semantic: bool,
checker: PostTransformChecker,
}

impl CompilerInterface for Driver {
fn transform_options(&self) -> Option<TransformOptions> {
Some(self.options.clone())
}

fn check_semantic_error(&self) -> bool {
false
}

fn handle_errors(&mut self, errors: Vec<OxcDiagnostic>) {
self.errors.extend(errors);
}

fn after_codegen(&mut self, printed: String) {
self.printed = printed;
}

fn after_semantic(
&mut self,
program: &mut Program<'_>,
_semantic_return: &mut SemanticBuilderReturn,
) -> ControlFlow<()> {
if self.check_semantic {
if let Some(errors) = self.checker.before_transform(program) {
self.errors.extend(errors);
return ControlFlow::Break(());
}
}
ControlFlow::Continue(())
}

fn after_transform(
&mut self,
program: &mut Program<'_>,
transformer_return: &mut TransformerReturn,
) -> ControlFlow<()> {
if self.check_semantic {
if let Some(errors) = self.checker.after_transform(
&transformer_return.symbols,
&transformer_return.scopes,
program,
) {
self.errors.extend(errors);
return ControlFlow::Break(());
}
}
ControlFlow::Continue(())
}
}

impl Driver {
pub fn new(options: TransformOptions) -> Self {
Self {
options,
printed: String::new(),
errors: vec![],
check_semantic: false,
checker: PostTransformChecker::default(),
}
}

pub fn execute(
&mut self,
source_text: &str,
source_type: SourceType,
source_path: &Path,
) -> Result<String, Vec<OxcDiagnostic>> {
self.compile(source_text, source_type, source_path);
if self.errors.is_empty() {
Ok(mem::take(&mut self.printed))
} else {
Err(mem::take(&mut self.errors))
}
}
}
2 changes: 1 addition & 1 deletion tasks/transform_conformance/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use oxc_tasks_common::{normalize_path, project_root, Snapshot};
use test_case::TestCaseKind;
use walkdir::WalkDir;

mod semantic;
mod driver;
mod test_case;

#[test]
Expand Down
86 changes: 0 additions & 86 deletions tasks/transform_conformance/src/semantic.rs

This file was deleted.

Loading

0 comments on commit 4fdf26d

Please sign in to comment.