Skip to content

Commit

Permalink
feat(codegen): align more esbuild implementations (#4510)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Jul 28, 2024
1 parent 35654e6 commit 7446e98
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 38 deletions.
13 changes: 10 additions & 3 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1731,8 +1731,8 @@ impl<'a, const MINIFY: bool> GenExpr<MINIFY> for UnaryExpression<'a> {
impl<'a, const MINIFY: bool> GenExpr<MINIFY> for BinaryExpression<'a> {
fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, precedence: Precedence, ctx: Context) {
let mut ctx = ctx;
let wrap_in = self.operator == BinaryOperator::In && ctx.intersects(Context::FORBID_IN);
let wrap = precedence >= self.precedence() || wrap_in;
let wrap = precedence >= self.precedence()
|| (self.operator == BinaryOperator::In && ctx.intersects(Context::FORBID_IN));
if wrap {
ctx &= Context::FORBID_IN.not();
}
Expand Down Expand Up @@ -2333,7 +2333,14 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for JSXOpeningElement<'a> {
p.print_char(b'<');
self.name.gen(p, ctx);
for attr in &self.attributes {
p.print_hard_space();
match attr {
JSXAttributeItem::Attribute(_) => {
p.print_hard_space();
}
JSXAttributeItem::SpreadAttribute(_) => {
p.print_soft_space();
}
}
attr.gen(p, ctx);
}
if self.self_closing {
Expand Down
46 changes: 22 additions & 24 deletions crates/oxc_codegen/tests/integration/esbuild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ fn test_template() {

#[test]
fn test_object() {
// test("let x = {'(':')'}", "let x = { \"(\": \");\" };\n");
test("let x = {'(':')'}", "let x = { \"(\": \")\" };\n");
test("({})", "({});\n");
test("({}.x)", "({}).x;\n");
test("({} = {})", "({} = {});\n");
Expand Down Expand Up @@ -601,11 +601,10 @@ fn test_private_identifiers() {
}

#[test]
#[ignore]
fn test_decorators() {
// example := "class Foo {\n@w\nw; @x x; @a1\n@b1@b2\n@c1@c2@c3\ny = @y1 @y2 class {}; @a1\n@b1@b2\n@c1@c2@c3 z =\n@z1\n@z2\nclass {}}"
// test( example, "class Foo {\n @w\n w;\n @x x;\n @a1\n @b1 @b2\n @c1 @c2 @c3\n "+
// "y = @y1 @y2 class {\n };\n @a1\n @b1 @b2\n @c1 @c2 @c3 z = @z1 @z2 class {\n };\n}\n");
let source = "class Foo {\n@w\nw; @x x; @a1\n@b1@b2\n@c1@c2@c3\ny = @y1 @y2 class {}; @a1\n@b1@b2\n@c1@c2@c3 z =\n@z1\n@z2\nclass {}}";
let expect = "class Foo {\n\t@w w;\n\t@x x;\n\t@a1 @b1 @b2 @c1 @c2 @c3 y = @y1 @y2 class {};\n\t@a1 @b1 @b2 @c1 @c2 @c3 z = @z1 @z2 class {};\n}\n";
test(source, expect);
// test_minify( example, "class Foo{@w w;@x x;@a1@b1@b2@c1@c2@c3 y=@y1@y2 class{};@a1@b1@b2@c1@c2@c3 z=@z1@z2 class{}}");
}

Expand Down Expand Up @@ -878,26 +877,25 @@ fn test_ascii_only() {
}

#[test]
#[ignore]
fn test_jsx() {
test("<a/>", "<a />;\n");
test("<A/>", "<A />;\n");
test("<a.b/>", "<a.b />;\n");
test("<A.B/>", "<A.B />;\n");
test("<a-b/>", "<a-b />;\n");
test("<a:b/>", "<a:b />;\n");
test("<a></a>", "<a />;\n");
test("<a b></a>", "<a b />;\n");

test("<a b={true}></a>", "<a b={true} />;\n");
test("<a b='x'></a>", "<a b='x' />;\n");
test("<a b=\"x\"></a>", "<a b=\"x\" />;\n");
test("<a b={'x'}></a>", "<a b={\"x\"} />;\n");
test("<a b={`'`}></a>", "<a b={`'`} />;\n");
test("<a b={`\"`}></a>", "<a b={`\"`} />;\n");
test("<a b={`'\"`}></a>", "<a b={`'\"`} />;\n");
test("<a b=\"&quot;\"></a>", "<a b=\"&quot;\" />;\n");
test("<a b=\"&amp;\"></a>", "<a b=\"&amp;\" />;\n");
// test("<a></a>", "<a />;\n");
// test("<a b></a>", "<a b />;\n");

// test("<a b={true}></a>", "<a b={true} />;\n");
// test("<a b='x'></a>", "<a b='x' />;\n");
// test("<a b=\"x\"></a>", "<a b=\"x\" />;\n");
// test("<a b={'x'}></a>", "<a b={\"x\"} />;\n");
// test("<a b={`'`}></a>", "<a b={`'`} />;\n");
// test("<a b={`\"`}></a>", "<a b={`\"`} />;\n");
// test("<a b={`'\"`}></a>", "<a b={`'\"`} />;\n");
// test("<a b=\"&quot;\"></a>", "<a b=\"&quot;\" />;\n");
// test("<a b=\"&amp;\"></a>", "<a b=\"&amp;\" />;\n");

test("<a>x</a>", "<a>x</a>;\n");
test("<a>x\ny</a>", "<a>x\ny</a>;\n");
Expand Down Expand Up @@ -955,14 +953,13 @@ fn test_jsx() {
}

#[test]
#[ignore]
fn test_jsx_single_line() {
test("<x/>", "<x />;\n");
test("<x y/>", "<x y />;\n");
test("<x\n/>", "<x />;\n");
test("<x\ny/>", "<x\n y\n/>;\n");
test("<x y\n/>", "<x\n y\n/>;\n");
test("<x\n{...y}/>", "<x\n {...y}\n/>;\n");
// test("<x\ny/>", "<x\n\ty\n/>;\n");
// test("<x y\n/>", "<x\n\ty\n/>;\n");
// test("<x\n{...y}/>", "<x\n\t{...y}\n/>;\n");

test_minify("<x/>", "<x/>;");
test_minify("<x y/>", "<x y/>;");
Expand Down Expand Up @@ -1075,8 +1072,9 @@ fn test_binary_operator_visitor() {
// testMangle(t, "x = (0, /*a*/ (0, /*b*/ (0, /*c*/ 1 == 2) + 3) * 4)", "x = /*a*/\n/*b*/\n(/*c*/\n!1 + 3) * 4;\n");

// Make sure deeply-nested ASTs don't cause a stack overflow
// x := "x = f()" + strings.Repeat(" || f()", 10_000) + ";\n"
// test( x, x)
// FIXME:
// let x = format!("x = f(){};\n", " || f()".repeat(2)); // TODO: change this to 10_000
// test(&x, &x);
}

// See: https://github.com/tc39/proposal-explicit-resource-management
Expand Down
1 change: 0 additions & 1 deletion crates/oxc_parser/src/js/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ impl<'a> ParserImpl<'a> {
}

fn parse_rest_binding(&mut self) -> Result<BindingRestElement<'a>> {
// self.eat_decorators()?;
let elem = self.parse_rest_element()?;
if self.at(Kind::Comma) {
if matches!(self.peek_kind(), Kind::RCurly | Kind::RBrack) {
Expand Down
13 changes: 3 additions & 10 deletions crates/oxc_parser/src/js/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,15 +434,8 @@ impl<'a> ParserImpl<'a> {
) -> Result<ClassElement<'a>> {
let type_annotation =
if self.ts_enabled() { self.parse_ts_type_annotation()? } else { None };
let value = if self.eat(Kind::Eq) {
// let current_flags = self.scope.current_flags();
// self.scope.set_current_flags(self.scope.current_flags());
let expr = self.parse_expr()?;
// self.scope.set_current_flags(current_flags);
Some(expr)
} else {
None
};
let decorators = self.consume_decorators();
let value = if self.eat(Kind::Eq) { Some(self.parse_expr()?) } else { None };
self.asi()?;

let r#type = if r#abstract {
Expand All @@ -464,7 +457,7 @@ impl<'a> ParserImpl<'a> {
accessibility,
optional,
definite,
decorators: self.consume_decorators(),
decorators,
};
Ok(ClassElement::PropertyDefinition(self.ast.alloc(property_definition)))
}
Expand Down

0 comments on commit 7446e98

Please sign in to comment.