oxc-project / oxc

⚓ A collection of JavaScript tools written in Rust.
https://oxc.rs
MIT License
12.42k stars 456 forks source link

codegen, transformer: print pure annotation comments for jsx #6072

Open Boshen opened 1 month ago

Boshen commented 1 month ago

Example of babel output:

  return /*#__PURE__*/_jsx(DndContext, {
    sensors: sensors,
    onDragEnd: onDragEnd,
    modifiers: modifiers,
    children: /*#__PURE__*/_jsx(SortableContext, {
      disabled: !draggable,
      items: properties,
      children: children(filteredProperties)
    })
  });

We lack /*#__PURE__*/.

Dunqing commented 1 month ago

FYI

https://github.com/evanw/esbuild/blob/332727499e62315cff4ecaff9fa8b86336555e46/internal/js_ast/js_ast.go#L613-L621esbuild decides whether to print the PURE annotation comments in printer by marking CanBeUnwrappedIfUnused during the parser phase.

IWANABETHATGUY commented 1 day ago

consider the pure comments may attach to a newly generated AstNode so the original Span based comment lookup may invalid in this scenario, after reading https://github.com/oxc-project/backlog/issues/140, I think we could attach the PURE comment to AstNodeAddress rather than original span

overlookmotel commented 22 hours ago

Babel inserts /*#__PURE__*/ comments on generated nodes in class properties transform. We're currently unable to do this (though maybe it's not important).

// Input
class C {
  #x = 1;
}

// Output
var _x = /*#__PURE__*/ new WeakMap();
class C {
  constructor() {
    _classPrivateFieldInitSpec(this, _x, 1);
  }
}

Babel REPL

IWANABETHATGUY commented 22 hours ago

Babel inserts /*#__PURE__*/ comments on generated nodes in class properties transform. We're currently unable to do this (though maybe it's not important).

// Input
class C {
  #x = 1;
}

// Output
var _x = /*#__PURE__*/ new WeakMap();
class C {
  constructor() {
    _classPrivateFieldInitSpec(this, _x, 1);
  }
}

Babel REPL

It depends, from a transformer view it does not matter, but from a bundler view it does matter since it may generate large bundle compare using babel.