Skip to content

Commit

Permalink
feat(transform_conformance): move Formatter to codegen (#986)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen authored Oct 12, 2023
1 parent 809f050 commit ce79bc1
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 60 deletions.
6 changes: 4 additions & 2 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions crates/oxc_minifier/src/compressor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use oxc_syntax::{
NumberBase,
};

use self::prepass::Prepass;
pub use self::prepass::Prepass;

#[allow(clippy::struct_excessive_bools)]
#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -85,7 +85,7 @@ impl<'a> Compressor<'a> {
}

pub fn build(mut self, program: &mut Program<'a>) {
self.prepass.visit_program(program);
self.prepass.build(program);
self.visit_program(program);
}

Expand Down
4 changes: 4 additions & 0 deletions crates/oxc_minifier/src/compressor/prepass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ impl<'a> Prepass<'a> {
Self { ast: AstBuilder::new(allocator) }
}

pub fn build(&mut self, program: &mut Program<'a>) {
self.visit_program(program);
}

fn strip_parenthesized_expression(&self, expr: &mut Expression<'a>) {
if let Expression::ParenthesizedExpression(paren_expr) = expr {
*expr = self.ast.move_expression(&mut paren_expr.expression);
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_minifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use oxc_codegen::Codegen;
use oxc_parser::Parser;
use oxc_span::SourceType;

pub use crate::compressor::Prepass;
pub use crate::{
compressor::{CompressOptions, Compressor},
mangler::ManglerBuilder,
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_transformer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ oxc_semantic = { workspace = true }

[dev-dependencies]
oxc_parser = { workspace = true }
oxc_formatter = { workspace = true }
oxc_codegen = { workspace = true }
oxc_minifier = { workspace = true }
10 changes: 6 additions & 4 deletions crates/oxc_transformer/examples/transformer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::{env, path::Path};

use oxc_allocator::Allocator;
use oxc_formatter::{Formatter, FormatterOptions};
use oxc_codegen::{Codegen, CodegenOptions};
use oxc_minifier::Prepass;
use oxc_parser::Parser;
use oxc_semantic::SemanticBuilder;
use oxc_span::SourceType;
Expand All @@ -28,19 +29,20 @@ fn main() {
return;
}

let formatter_options = FormatterOptions::default();
let printed = Formatter::new(source_text.len(), formatter_options.clone()).build(&ret.program);
let codegen_options = CodegenOptions;
let printed = Codegen::<false>::new(source_text.len(), codegen_options).build(&ret.program);
println!("Original:\n");
println!("{printed}");

let program = allocator.alloc(ret.program);
Prepass::new(&allocator).build(program);
let _ = SemanticBuilder::new(&source_text, source_type).build(program);
let transform_options = TransformOptions {
target: TransformTarget::ES2015,
react: Some(TransformReactOptions::default()),
};
Transformer::new(&allocator, source_type, transform_options).build(program);
let printed = Formatter::new(source_text.len(), formatter_options).build(program);
let printed = Codegen::<false>::new(source_text.len(), codegen_options).build(program);
println!("Transformed:\n");
println!("{printed}");
}
3 changes: 2 additions & 1 deletion tasks/transform_conformance/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ doctest = false
oxc_span = { workspace = true }
oxc_allocator = { workspace = true }
oxc_parser = { workspace = true }
oxc_formatter = { workspace = true }
oxc_codegen = { workspace = true }
oxc_minifier = { workspace = true }
oxc_transformer = { workspace = true }
oxc_tasks_common = { workspace = true }

Expand Down
91 changes: 44 additions & 47 deletions tasks/transform_conformance/babel.snap.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Passed: 97/1091
Passed: 54/1091

# babel-plugin-transform-unicode-sets-regex
* Failed: basic/basic/input.js
Expand Down Expand Up @@ -275,6 +275,10 @@ Passed: 97/1091
* Passed: public-loose/arrow-this-without-transform/input.js

# babel-plugin-transform-class-static-block
* Failed: class-static-block/class-binding/input.js
* Failed: class-static-block/class-declaration/input.js
* Failed: class-static-block/in-class-heritage/input.js
* Failed: class-static-block/multiple-static-initializers/input.js
* Failed: class-static-block/name-conflict/input.js
* Failed: class-static-block/new-target/input.js
* Failed: class-static-block/preserve-comments/input.js
Expand All @@ -292,10 +296,6 @@ Passed: 97/1091
* Failed: integration-loose/name-conflict/input.js
* Failed: integration-loose/preserve-comments/input.js
* Failed: integration-loose/super-static-block/input.js
* Passed: class-static-block/class-binding/input.js
* Passed: class-static-block/class-declaration/input.js
* Passed: class-static-block/in-class-heritage/input.js
* Passed: class-static-block/multiple-static-initializers/input.js
* Passed: integration-loose/.new-target/input.js

# babel-plugin-transform-private-methods
Expand Down Expand Up @@ -498,16 +498,15 @@ Passed: 97/1091
* Failed: to-native-fields/static-shadowed-binding/input.js

# babel-plugin-transform-logical-assignment-operators
* Failed: logical-assignment/anonymous-functions-transform/input.js
* Failed: logical-assignment/arrow-functions-transform/input.js
* Failed: logical-assignment/general-semantics/input.js
* Failed: logical-assignment/named-functions-transform/input.js
* Failed: logical-assignment/null-coalescing/input.js
* Failed: logical-assignment/null-coalescing-without-other/input.js
* Passed: logical-assignment/anonymous-functions-transform/input.js
* Passed: logical-assignment/arrow-functions-transform/input.js
* Passed: logical-assignment/named-functions-transform/input.js

# babel-plugin-transform-numeric-separator
[All passed]
* Passed: removal/bigint/input.js
* Failed: removal/bigint/input.js
* Passed: used-with-transform-es2015-literals/input.js

# babel-plugin-transform-export-namespace-from
Expand Down Expand Up @@ -595,6 +594,7 @@ Passed: 97/1091
* Failed: general/unary/input.js
* Failed: loose/cast-to-boolean/input.js
* Failed: regression/10959-transform-optional-chaining/input.ts
* Failed: regression/10959-transform-ts/input.ts
* Failed: regression/10959-transform-ts-and-optional-chaining/input.ts
* Failed: regression/7642/input.js
* Failed: transparent-expr-wrappers/ts-as-call-context/input.ts
Expand All @@ -603,14 +603,12 @@ Passed: 97/1091
* Failed: transparent-expr-wrappers/ts-as-in-conditional/input.ts
* Failed: transparent-expr-wrappers/ts-as-member-expression/input.ts
* Failed: transparent-expr-wrappers/ts-parenthesized-expression-member-call/input.ts
* Passed: regression/10959-transform-ts/input.ts

# babel-plugin-transform-optional-catch-binding
[All passed]
* Passed: optional-catch-bindings/try-catch-block-binding/input.js
* Passed: optional-catch-bindings/try-catch-block-no-binding/input.js
* Passed: optional-catch-bindings/try-catch-finally-binding/input.js
* Passed: optional-catch-bindings/try-catch-finally-no-binding/input.js
* Failed: optional-catch-bindings/try-catch-block-binding/input.js
* Failed: optional-catch-bindings/try-catch-block-no-binding/input.js
* Failed: optional-catch-bindings/try-catch-finally-binding/input.js
* Failed: optional-catch-bindings/try-catch-finally-no-binding/input.js

# babel-plugin-transform-json-strings
* Failed: json-strings/directive-line-separator/input.js
Expand Down Expand Up @@ -757,23 +755,22 @@ Passed: 97/1091

# babel-plugin-transform-exponentiation-operator
* Failed: exponentiation-operator/assignment/input.js
* Failed: exponentiation-operator/binary/input.js
* Failed: regression/4349/input.js
* Failed: regression/4403/input.js
* Passed: exponentiation-operator/binary/input.js

# babel-plugin-transform-shorthand-properties
* Failed: shorthand-properties/method-plain/input.js
* Failed: shorthand-properties/method-type-annotations/input.js
* Failed: shorthand-properties/proto/input.js
* Failed: shorthand-properties/shorthand-comments/input.js
* Passed: shorthand-properties/method-plain/input.js
* Passed: shorthand-properties/proto/input.js
* Passed: shorthand-properties/shorthand-mixed/input.js
* Passed: shorthand-properties/shorthand-multiple/input.js
* Passed: shorthand-properties/shorthand-single/input.js
* Failed: shorthand-properties/shorthand-mixed/input.js
* Failed: shorthand-properties/shorthand-multiple/input.js
* Failed: shorthand-properties/shorthand-single/input.js

# babel-plugin-transform-sticky-regex
[All passed]
* Passed: sticky-regex/basic/input.js
* Passed: sticky-regex/ignore-non-sticky/input.js
* Failed: sticky-regex/basic/input.js
* Failed: sticky-regex/ignore-non-sticky/input.js

# babel-plugin-transform-unicode-regex
* Failed: unicode-regex/basic/input.js
Expand All @@ -782,6 +779,10 @@ Passed: 97/1091
* Failed: unicode-regex/slash/input.js

# babel-plugin-transform-typescript
* Failed: cast/as-expression/input.ts
* Failed: cast/multiple-assert-and-assign/input.ts
* Failed: cast/non-null-assertion/input.ts
* Failed: cast/type-assertion/input.ts
* Failed: class/abstract-class-decorated/input.ts
* Failed: class/abstract-class-decorated-method/input.ts
* Failed: class/abstract-class-decorated-parameter/input.ts
Expand All @@ -791,8 +792,10 @@ Passed: 97/1091
* Failed: class/declare/input.ts
* Failed: class/declare-babel-7/input.ts
* Failed: class/decorated-declare-properties/input.ts
* Failed: class/field-not-initialized/input.ts
* Failed: class/field-not-initialized-babel-7/input.ts
* Failed: class/methods/input.ts
* Failed: class/override/input.ts
* Failed: class/parameter-properties/input.ts
* Failed: class/parameter-properties-late-super/input.ts
* Failed: class/parameter-properties-with-class/input.ts
Expand All @@ -803,6 +806,8 @@ Passed: 97/1091
* Failed: class/private-method-override-transform-private/input.ts
* Failed: class/properties/input.ts
* Failed: class/properties-babel-7/input.ts
* Failed: class/uninitialized-definite/input.ts
* Failed: class/uninitialized-definite-babel-7/input.ts
* Failed: class/uninitialized-definite-with-declare-disabled-babel-7/input.ts
* Failed: declarations/erased/input.ts
* Failed: declarations/export-declare-enum/input.ts
Expand All @@ -826,8 +831,11 @@ Passed: 97/1091
* Failed: exports/declare-shadowed/input.ts
* Failed: exports/declared-types/input.ts
* Failed: exports/default-function/input.ts
* Failed: exports/default-literal/input.ts
* Failed: exports/export-const-enums/input.ts
* Failed: exports/export-context-variables/input.ts
* Failed: exports/export-from/input.ts
* Failed: exports/export-globals/input.ts
* Failed: exports/export-type/input.ts
* Failed: exports/export-type-from/input.ts
* Failed: exports/export-type-star-from/input.ts
Expand All @@ -838,6 +846,7 @@ Passed: 97/1091
* Failed: exports/issue-9916-1/input.ts
* Failed: exports/issue-9916-2/input.ts
* Failed: exports/issue-9916-3/input.ts
* Failed: exports/simple/input.ts
* Failed: exports/type-only-export-specifier-1/input.ts
* Failed: exports/type-only-export-specifier-2/input.ts
* Failed: exports/type-only-export-specifier-3/input.ts
Expand Down Expand Up @@ -892,6 +901,9 @@ Passed: 97/1091
* Failed: imports/type-only-import-specifier-2/input.ts
* Failed: imports/type-only-import-specifier-3/input.ts
* Failed: imports/type-only-import-specifier-4/input.ts
* Failed: lvalues/as-expression/input.ts
* Failed: lvalues/non-null/input.ts
* Failed: lvalues/type-assertion/input.ts
* Failed: namespace/alias/input.ts
* Failed: namespace/canonical/input.ts
* Failed: namespace/clobber-class/input.ts
Expand Down Expand Up @@ -925,48 +937,33 @@ Passed: 97/1091
* Failed: regression/10162/input.ts
* Failed: regression/10338/input.ts
* Failed: regression/11061/input.mjs
* Failed: type-arguments/call/input.ts
* Failed: type-arguments/expr/input.ts
* Failed: type-arguments/new/input.ts
* Failed: type-arguments/optional-call/input.ts
* Failed: type-arguments/tagged-template/input.ts
* Failed: type-arguments/tsx/input.ts
* Failed: type-arguments/tsx-babel-7/input.ts
* Failed: variable-declaration/exclamation/input.ts
* Failed: variable-declaration/non-null-in-optional-chain/input.ts
* Failed: variable-declaration/types-erased/input.ts
* Passed: cast/as-expression/input.ts
* Passed: cast/multiple-assert-and-assign/input.ts
* Passed: cast/non-null-assertion/input.ts
* Passed: cast/type-assertion/input.ts
* Passed: catch-clause/param-type/input.ts
* Passed: class/abstract-allowDeclareFields-false/input.ts
* Passed: class/abstract-allowDeclareFields-true/input.ts
* Passed: class/declare-not-enabled-babel-7/input.ts
* Passed: class/export-default-abstract/input.ts
* Passed: class/field-not-initialized/input.ts
* Passed: class/head/input.ts
* Passed: class/index-signature/input.ts
* Passed: class/override/input.ts
* Passed: class/transform-properties-declare-wrong-order/input.ts
* Passed: class/uninitialized-definite/input.ts
* Passed: class/uninitialized-definite-babel-7/input.ts
* Passed: declarations/const-enum/input.ts
* Passed: exports/default-literal/input.ts
* Passed: exports/export-context-variables/input.ts
* Passed: exports/export-globals/input.ts
* Passed: exports/export=/input.ts
* Passed: exports/simple/input.ts
* Passed: function/parameters/input.ts
* Passed: function/tuple-parameter/input.ts
* Passed: imports/import=-module/input.ts
* Passed: lvalues/as-expression/input.ts
* Passed: lvalues/non-null/input.ts
* Passed: lvalues/type-assertion/input.ts
* Passed: namespace/ambient-module-nested/input.ts
* Passed: namespace/ambient-module-nested-exported/input.ts
* Passed: namespace/mutable-fail/input.ts
* Passed: namespace/namespace-flag/input.ts
* Passed: type-arguments/call/input.ts
* Passed: type-arguments/expr/input.ts
* Passed: type-arguments/new/input.ts
* Passed: type-arguments/optional-call/input.ts
* Passed: type-arguments/tagged-template/input.ts
* Passed: variable-declaration/exclamation/input.ts

# babel-plugin-transform-react-jsx
* Failed: autoImport/after-polyfills/input.mjs
Expand All @@ -979,6 +976,7 @@ Passed: 97/1091
* Failed: autoImport/complicated-scope-script/input.js
* Failed: autoImport/import-source/input.js
* Failed: autoImport/import-source-pragma/input.js
* Failed: autoImport/no-jsx/input.js
* Failed: autoImport/react-defined/input.js
* Failed: pure/false-default-pragma-automatic-runtime/input.js
* Failed: pure/false-default-pragma-classic-runtime/input.js
Expand Down Expand Up @@ -1115,7 +1113,6 @@ Passed: 97/1091
* Failed: sourcemaps/JSXText/input.js
* Failed: spread-transform/transform-to-babel-extend/input.js
* Failed: spread-transform/transform-to-object-assign/input.js
* Passed: autoImport/no-jsx/input.js
* Passed: pure/false-pragma-comment-automatic-runtime/input.js
* Passed: pure/false-pragma-option-automatic-runtime/input.js
* Passed: pure/true-pragma-comment-automatic-runtime/input.js
Expand Down
9 changes: 6 additions & 3 deletions tasks/transform_conformance/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use std::{
use walkdir::WalkDir;

use oxc_allocator::Allocator;
use oxc_formatter::{Formatter, FormatterOptions};
use oxc_codegen::{Codegen, CodegenOptions};
use oxc_minifier::Prepass;
use oxc_parser::Parser;
use oxc_span::{SourceType, VALID_EXTENSIONS};
use oxc_tasks_common::{normalize_path, project_root};
Expand Down Expand Up @@ -140,11 +141,13 @@ fn babel_test(input_path: &Path, options: &BabelOptions) -> bool {
target: TransformTarget::ES5,
react: Some(TransformReactOptions::default()),
};

let program = allocator.alloc(ret.program);

Transformer::new(&allocator, source_type, transform_options).build(program);
Prepass::new(&allocator).build(program);
let transformed = Codegen::<false>::new(source_text.len(), CodegenOptions).build(program);

let formatter_options = FormatterOptions::default();
let transformed = Formatter::new(source_text.len(), formatter_options).build(program);
let trim_transformed = remove_whitespace(&transformed);
let trim_expected = remove_whitespace(expected);
let passed = trim_transformed == trim_expected;
Expand Down

0 comments on commit ce79bc1

Please sign in to comment.