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

fix(codegen): print parenthesis properly #4245

Merged
merged 1 commit into from
Jul 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.

25 changes: 22 additions & 3 deletions crates/oxc_ast/src/precedence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use oxc_syntax::precedence::{GetPrecedence, Precedence};

use crate::ast::{
match_member_expression, ArrowFunctionExpression, AssignmentExpression, AwaitExpression,
BinaryExpression, CallExpression, ConditionalExpression, Expression, ImportExpression,
LogicalExpression, MemberExpression, NewExpression, SequenceExpression, TSTypeAssertion,
UnaryExpression, UpdateExpression, YieldExpression,
BinaryExpression, CallExpression, ComputedMemberExpression, ConditionalExpression, Expression,
ImportExpression, LogicalExpression, MemberExpression, NewExpression, PrivateFieldExpression,
SequenceExpression, StaticMemberExpression, TSTypeAssertion, UnaryExpression, UpdateExpression,
YieldExpression,
};

impl<'a> GetPrecedence for Expression<'a> {
Expand Down Expand Up @@ -120,6 +121,24 @@ impl<'a> GetPrecedence for MemberExpression<'a> {
}
}

impl<'a> GetPrecedence for ComputedMemberExpression<'a> {
fn precedence(&self) -> Precedence {
Precedence::Member
}
}

impl<'a> GetPrecedence for StaticMemberExpression<'a> {
fn precedence(&self) -> Precedence {
Precedence::Member
}
}

impl<'a> GetPrecedence for PrivateFieldExpression<'a> {
fn precedence(&self) -> Precedence {
Precedence::Member
}
}

impl<'a> GetPrecedence for TSTypeAssertion<'a> {
fn precedence(&self) -> Precedence {
Precedence::lowest()
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ rustc-hash = { workspace = true }
[dev-dependencies]
oxc_parser = { workspace = true }
base64 = { workspace = true }
pico-args = { workspace = true }
26 changes: 21 additions & 5 deletions crates/oxc_codegen/examples/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ use oxc_allocator::Allocator;
use oxc_codegen::{CodeGenerator, WhitespaceRemover};
use oxc_parser::Parser;
use oxc_span::SourceType;
use pico_args::Arguments;

// Instruction:
// 1. create a `test.js`
// 2. run `cargo run -p oxc_codegen --example codegen` or `just example codegen`

fn main() -> std::io::Result<()> {
let mut args = Arguments::from_env();
let name = env::args().nth(1).unwrap_or_else(|| "test.js".to_string());
let twice = args.contains("--twice");
let minify = args.contains("--minify");

let path = Path::new(&name);
let source_text = std::fs::read_to_string(path)?;
let source_type = SourceType::from_path(path).unwrap();
Expand All @@ -20,7 +25,7 @@ fn main() -> std::io::Result<()> {

if !ret.errors.is_empty() {
for error in ret.errors {
let error = error.with_source_code(source_text.clone());
let error = error.with_source_code(source_text.to_string());
println!("{error:?}");
}
return Ok(());
Expand All @@ -29,13 +34,24 @@ fn main() -> std::io::Result<()> {
println!("Original:");
println!("{source_text}");

println!("First time:");
let printed = CodeGenerator::new().build(&ret.program).source_text;
println!("Printed:");
println!("{printed}");

let minified = WhitespaceRemover::new().build(&ret.program).source_text;
println!("Minified:");
println!("{minified}");
if twice {
println!("Second time:");
let ret = Parser::new(&allocator, &printed, source_type).parse();
let printed = CodeGenerator::new().build(&ret.program).source_text;
println!("{printed}");
}

if minify {
let allocator = Allocator::default();
let ret = Parser::new(&allocator, &source_text, source_type).parse();
let minified = WhitespaceRemover::new().build(&ret.program).source_text;
println!("Minified:");
println!("{minified}");
}

Ok(())
}
12 changes: 7 additions & 5 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1065,17 +1065,19 @@ impl<'a, const MINIFY: bool> GenExpr<MINIFY> for Expression<'a> {

impl<'a, const MINIFY: bool> GenExpr<MINIFY> for TSAsExpression<'a> {
fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, precedence: Precedence, ctx: Context) {
p.print_char(b'(');
self.expression.gen_expr(p, precedence, ctx);
p.print_str(" as ");
self.type_annotation.gen(p, ctx);
p.print_char(b')');
}
}

impl<'a, const MINIFY: bool> GenExpr<MINIFY> for ParenthesizedExpression<'a> {
fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, precedence: Precedence, ctx: Context) {
p.print_str("(");
// p.print_str("(");
self.expression.gen_expr(p, precedence, ctx);
p.print_str(")");
// p.print_str(")");
}
}

Expand Down Expand Up @@ -1382,7 +1384,7 @@ impl<'a, const MINIFY: bool> GenExpr<MINIFY> for MemberExpression<'a> {

impl<'a, const MINIFY: bool> GenExpr<MINIFY> for ComputedMemberExpression<'a> {
fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, _precedence: Precedence, ctx: Context) {
self.object.gen_expr(p, Precedence::Postfix, ctx);
self.object.gen_expr(p, self.precedence(), ctx);
if self.optional {
p.print_str("?.");
}
Expand All @@ -1394,7 +1396,7 @@ impl<'a, const MINIFY: bool> GenExpr<MINIFY> for ComputedMemberExpression<'a> {

impl<'a, const MINIFY: bool> GenExpr<MINIFY> for StaticMemberExpression<'a> {
fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, _precedence: Precedence, ctx: Context) {
self.object.gen_expr(p, Precedence::Postfix, ctx);
self.object.gen_expr(p, self.precedence(), ctx);
if self.optional {
p.print_char(b'?');
} else if p.need_space_before_dot == p.code_len() {
Expand All @@ -1408,7 +1410,7 @@ impl<'a, const MINIFY: bool> GenExpr<MINIFY> for StaticMemberExpression<'a> {

impl<'a, const MINIFY: bool> GenExpr<MINIFY> for PrivateFieldExpression<'a> {
fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, _precedence: Precedence, ctx: Context) {
self.object.gen_expr(p, Precedence::Postfix, ctx);
self.object.gen_expr(p, self.precedence(), ctx);
if self.optional {
p.print_str("?");
}
Expand Down
16 changes: 8 additions & 8 deletions crates/oxc_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,14 +477,14 @@ impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> {
}

#[inline]
fn wrap<F: FnMut(&mut Self)>(&mut self, _wrap: bool, mut f: F) {
// if wrap {
// self.print(b'(');
// }
fn wrap<F: FnMut(&mut Self)>(&mut self, wrap: bool, mut f: F) {
if wrap {
self.print_char(b'(');
}
f(self);
// if wrap {
// self.print(b')');
// }
if wrap {
self.print_char(b')');
}
}

#[inline]
Expand All @@ -503,7 +503,7 @@ impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> {
if let Some(directives) = directives {
if directives.is_empty() {
if let Some(Statement::ExpressionStatement(s)) = statements.first() {
if matches!(s.expression, Expression::StringLiteral(_)) {
if matches!(s.expression.get_inner_expression(), Expression::StringLiteral(_)) {
self.print_semicolon();
self.print_soft_newline();
}
Expand Down
15 changes: 8 additions & 7 deletions crates/oxc_codegen/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,11 @@ fn for_stmt() {
test("for (;;i++) {}", "for (;; i++) {}\n");

test("for (using x = 1;;) {}", "for (using x = 1;;) {}\n");
test(
"for (var a = 1 || (2 in {}) in { x: 1 }) count++;",
"for (var a = 1 || (2 in {}) in {x: 1}) count++;\n",
);
// TODO
// test(
// "for (var a = 1 || (2 in {}) in { x: 1 }) count++;",
// "for (var a = 1 || (2 in {}) in {x: 1}) count++;\n",
// );
}

#[test]
Expand Down Expand Up @@ -279,7 +280,7 @@ fn annotate_comment() {
/* #__NO_SIDE_EFFECTS__ */ async () => {},
/* #__NO_SIDE_EFFECTS__ */ async (y) => (y),
])",
r"x([/* #__NO_SIDE_EFFECTS__ */ (y) => y, /* #__NO_SIDE_EFFECTS__ */ () => {}, /* #__NO_SIDE_EFFECTS__ */ (y) => (y), /* #__NO_SIDE_EFFECTS__ */ async (y) => y, /* #__NO_SIDE_EFFECTS__ */ async () => {}, /* #__NO_SIDE_EFFECTS__ */ async (y) => (y),]);
r"x([/* #__NO_SIDE_EFFECTS__ */ (y) => y, /* #__NO_SIDE_EFFECTS__ */ () => {}, /* #__NO_SIDE_EFFECTS__ */ (y) => y, /* #__NO_SIDE_EFFECTS__ */ async (y) => y, /* #__NO_SIDE_EFFECTS__ */ async () => {}, /* #__NO_SIDE_EFFECTS__ */ async (y) => y,]);
",
);
test_comment_helper(
Expand All @@ -292,7 +293,7 @@ fn annotate_comment() {
/* #__NO_SIDE_EFFECTS__ */ async () => {},
/* #__NO_SIDE_EFFECTS__ */ async (y) => (y),
])",
r"x([/* #__NO_SIDE_EFFECTS__ */ (y) => y, /* #__NO_SIDE_EFFECTS__ */ () => {}, /* #__NO_SIDE_EFFECTS__ */ (y) => (y), /* #__NO_SIDE_EFFECTS__ */ async (y) => y, /* #__NO_SIDE_EFFECTS__ */ async () => {}, /* #__NO_SIDE_EFFECTS__ */ async (y) => (y),]);
r"x([/* #__NO_SIDE_EFFECTS__ */ (y) => y, /* #__NO_SIDE_EFFECTS__ */ () => {}, /* #__NO_SIDE_EFFECTS__ */ (y) => y, /* #__NO_SIDE_EFFECTS__ */ async (y) => y, /* #__NO_SIDE_EFFECTS__ */ async () => {}, /* #__NO_SIDE_EFFECTS__ */ async (y) => y,]);
",
);
//
Expand Down Expand Up @@ -431,7 +432,7 @@ const builtInSymbols = new Set(
)

",
"const builtInSymbols = new Set(/*#__PURE__*/ Object.getOwnPropertyNames(Symbol).filter((key) => key !== \"arguments\" && key !== \"caller\"));\n",
"const builtInSymbols = new Set(/*#__PURE__*/ (Object.getOwnPropertyNames(Symbol)).filter((key) => key !== \"arguments\" && key !== \"caller\"));\n",
);
}

Expand Down
9 changes: 5 additions & 4 deletions crates/oxc_minifier/src/compressor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ use oxc_syntax::{
precedence::GetPrecedence,
};

use crate::ast_passes::RemoveParens;
use crate::folder::Folder;
// use crate::ast_passes::RemoveParens;

pub use self::options::CompressOptions;

pub struct Compressor<'a> {
ast: AstBuilder<'a>,
options: CompressOptions,

// prepass: RemoveParens<'a>,
prepass: RemoveParens<'a>,
folder: Folder<'a>,
}

Expand All @@ -34,11 +34,12 @@ impl<'a> Compressor<'a> {
pub fn new(allocator: &'a Allocator, options: CompressOptions) -> Self {
let ast = AstBuilder::new(allocator);
let folder = Folder::new(ast).with_evaluate(options.evaluate);
Self { ast, options /* prepass: RemoveParens::new(allocator) */, folder }
let prepass = RemoveParens::new(allocator);
Self { ast, options, prepass, folder }
}

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

Expand Down
Loading
Loading