Skip to content

Commit

Permalink
refactor(transformer): clean up some code
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Apr 12, 2024
1 parent a57f045 commit 664808d
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 57 deletions.
1 change: 1 addition & 0 deletions crates/oxc_transformer/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ impl<'a> TransformCtx<'a> {
}

/// Add an Error
#[allow(unused)]
pub fn error<T: Into<Error>>(&self, error: T) {
self.errors.borrow_mut().push(error.into());
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_transformer/src/decorators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ impl<'a> Decorators<'a> {

// Transformers
impl<'a> Decorators<'a> {
pub fn transform_statement(&mut self, _stmt: &mut Statement<'_>) {}
pub fn transform_statement(&self, _stmt: &mut Statement<'_>) {}
}
34 changes: 9 additions & 25 deletions crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// Core
mod compiler_assumptions;
mod context;
mod options;
// Presets: <https://babel.dev/docs/presets>
mod decorators;
mod react;
Expand All @@ -26,34 +27,17 @@ use oxc_diagnostics::Error;
use oxc_semantic::Semantic;

pub use crate::{
compiler_assumptions::CompilerAssumptions,
decorators::{Decorators, DecoratorsOptions},
react::{React, ReactDisplayName, ReactJsx, ReactJsxSelf, ReactJsxSource, ReactOptions},
typescript::{TypeScript, TypeScriptOptions},
compiler_assumptions::CompilerAssumptions, decorators::DecoratorsOptions,
options::TransformOptions, react::ReactOptions, typescript::TypeScriptOptions,
};

use crate::context::{Ctx, TransformCtx};

#[allow(unused)]
#[derive(Debug, Default, Clone)]
pub struct TransformOptions {
// Core
/// Set assumptions in order to produce smaller output.
/// For more information, check the [assumptions](https://babel.dev/docs/assumptions) documentation page.
pub assumptions: CompilerAssumptions,

// Plugins
/// [proposal-decorators](https://babeljs.io/docs/babel-plugin-proposal-decorators)
pub decorators: DecoratorsOptions,

/// [preset-typescript](https://babeljs.io/docs/babel-preset-typescript)
pub typescript: TypeScriptOptions,

/// [preset-react](https://babeljs.io/docs/babel-preset-react)
pub react: ReactOptions,
}
use crate::{
context::{Ctx, TransformCtx},
decorators::Decorators,
react::React,
typescript::TypeScript,
};

#[allow(unused)]
pub struct Transformer<'a> {
ctx: Ctx<'a>,
// NOTE: all callbacks must run in order.
Expand Down
22 changes: 22 additions & 0 deletions crates/oxc_transformer/src/options.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use crate::{
compiler_assumptions::CompilerAssumptions, decorators::DecoratorsOptions, react::ReactOptions,
typescript::TypeScriptOptions,
};

#[derive(Debug, Default, Clone)]
pub struct TransformOptions {
// Core
/// Set assumptions in order to produce smaller output.
/// For more information, check the [assumptions](https://babel.dev/docs/assumptions) documentation page.
pub assumptions: CompilerAssumptions,

// Plugins
/// [proposal-decorators](https://babeljs.io/docs/babel-plugin-proposal-decorators)
pub decorators: DecoratorsOptions,

/// [preset-typescript](https://babeljs.io/docs/babel-preset-typescript)
pub typescript: TypeScriptOptions,

/// [preset-react](https://babeljs.io/docs/babel-preset-react)
pub react: ReactOptions,
}
9 changes: 3 additions & 6 deletions crates/oxc_transformer/src/react/display_name/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl<'a> ReactDisplayName<'a> {
}

/// `let foo = React.createClass({})`
pub fn transform_variable_declarator(&mut self, declarator: &mut VariableDeclarator<'a>) {
pub fn transform_variable_declarator(&self, declarator: &mut VariableDeclarator<'a>) {
let Some(init_expr) = declarator.init.as_mut() else { return };
let Some(obj_expr) = Self::get_object_from_create_class(init_expr) else {
return;
Expand All @@ -67,7 +67,7 @@ impl<'a> ReactDisplayName<'a> {
}

/// `{foo: React.createClass({})}`
pub fn transform_object_property(&mut self, prop: &mut ObjectProperty<'a>) {
pub fn transform_object_property(&self, prop: &mut ObjectProperty<'a>) {
let Some(obj_expr) = Self::get_object_from_create_class(&mut prop.value) else { return };
let Some(name) = prop.key.static_name() else { return };
let name = self.ctx.ast.new_atom(&name);
Expand All @@ -76,10 +76,7 @@ impl<'a> ReactDisplayName<'a> {

/// `export default React.createClass({})`
/// Uses the current file name as the display name.
pub fn transform_export_default_declaration(
&mut self,
decl: &mut ExportDefaultDeclaration<'a>,
) {
pub fn transform_export_default_declaration(&self, decl: &mut ExportDefaultDeclaration<'a>) {
let ExportDefaultDeclarationKind::Expression(expr) = &mut decl.declaration else { return };
let Some(obj_expr) = Self::get_object_from_create_class(expr) else { return };
let name = self.ctx.ast.new_atom(self.ctx.filename());
Expand Down
11 changes: 4 additions & 7 deletions crates/oxc_transformer/src/react/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl<'a> React<'a> {

// Transforms
impl<'a> React<'a> {
pub fn transform_expression(&mut self, expr: &mut Expression<'a>) {
pub fn transform_expression(&self, expr: &mut Expression<'a>) {
match expr {
Expression::AssignmentExpression(e) => {
self.display_name.transform_assignment_expression(e);
Expand All @@ -65,18 +65,15 @@ impl<'a> React<'a> {
}
}

pub fn transform_variable_declarator(&mut self, declarator: &mut VariableDeclarator<'a>) {
pub fn transform_variable_declarator(&self, declarator: &mut VariableDeclarator<'a>) {
self.display_name.transform_variable_declarator(declarator);
}

pub fn transform_object_property(&mut self, prop: &mut ObjectProperty<'a>) {
pub fn transform_object_property(&self, prop: &mut ObjectProperty<'a>) {
self.display_name.transform_object_property(prop);
}

pub fn transform_export_default_declaration(
&mut self,
decl: &mut ExportDefaultDeclaration<'a>,
) {
pub fn transform_export_default_declaration(&self, decl: &mut ExportDefaultDeclaration<'a>) {
self.display_name.transform_export_default_declaration(decl);
}

Expand Down
26 changes: 8 additions & 18 deletions tasks/transform_conformance/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,28 +165,23 @@ impl TestRunner {
return None;
}
}

if EXCLUDE_TESTS.iter().any(|p| path.to_string_lossy().contains(p)) {
return None;
}

let test_case = TestCaseKind::from_path(path);
if let Some(test_case) = test_case {
if test_case.skip_test_case() {
return None;
}

return Some(test_case);
}
None
TestCaseKind::from_path(path)
.filter(|test_case| !test_case.skip_test_case())
})
.partition(|p| matches!(p, TestCaseKind::Transform(_)));

transform_paths.sort_unstable_by(|a, b| a.path().cmp(b.path()));
exec_paths.sort_unstable_by(|a, b| a.path().cmp(b.path()));

transform_files.insert((*case).to_string(), transform_paths);
exec_files.insert((*case).to_string(), exec_paths);
if !transform_paths.is_empty() {
transform_files.insert((*case).to_string(), transform_paths);
}
if !exec_paths.is_empty() {
exec_files.insert((*case).to_string(), exec_paths);
}
}

(transform_files, exec_files)
Expand All @@ -200,11 +195,6 @@ impl TestRunner {
let mut all_passed_count = 0;

for (case, test_cases) in paths {
// Skip empty test cases, e.g. some cases do not have `exec.js` file.
if test_cases.is_empty() {
continue;
}

let case_root = root.join(&case).join("test/fixtures");
let num_of_tests = test_cases.len();
total += num_of_tests;
Expand Down

0 comments on commit 664808d

Please sign in to comment.