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(transformer): remove span of define value #7811

Merged
merged 12 commits into from
Dec 13, 2024

Conversation

hi-ogawa
Copy link
Contributor

Removing span seems to work, but I'm not sure traversing all the time is okay.

Copy link

graphite-app bot commented Dec 12, 2024

How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

@github-actions github-actions bot added the A-transformer Area - Transformer / Transpiler label Dec 12, 2024
@hi-ogawa hi-ogawa changed the title fix: remove span of define value fix(transformer): remove span of define value Dec 12, 2024
@github-actions github-actions bot added the C-bug Category - Bug label Dec 12, 2024
@hi-ogawa hi-ogawa marked this pull request as ready for review December 12, 2024 02:13
Copy link

codspeed-hq bot commented Dec 12, 2024

CodSpeed Performance Report

Merging #7811 will not alter performance

Comparing hi-ogawa:fix-define-span (6149889) with main (65b109a)

Summary

✅ 29 untouched benchmarks

@Boshen Boshen requested review from Boshen and removed request for overlookmotel and Dunqing December 12, 2024 02:24
@Boshen Boshen self-assigned this Dec 12, 2024
@overlookmotel
Copy link
Contributor

overlookmotel commented Dec 12, 2024

I don't think this will generalize well. As I understand it, the replacement expression can be anything, and this will not remove all the spans from e.g. (() => { return 123; })().

To solve this fully, we either need:

  1. Option to parser to produce empty spans for all nodes.
  2. VisitMut implementation which removes span from all nodes. This could be generated using ast_tools codegen.

The 1st option would be faster for this particular use case, but we'd need to make sure it doesn't produce a noticeable slowdown for parsing in "standard mode". We could make it a generic param ParserImpl<const EMPTY_SPANS: bool> but that'd produce a large binary size increase.

So probably 2nd option is better. The AST for replacement expression is generally small, so traversing it will not be too costly.

@hi-ogawa
Copy link
Contributor Author

As I understand it, the replacement expression can be anything, and this will not remove all the spans from e.g. (() => { return 123; })().

I was wondering about that too. Esbuild doesn't allow arbitrary expression (probably it supports only json value, identifier, member expression https://esbuild.github.io/api/#define), but oxc can do different.

For the usage I'm aware of, I thought this could be a reasonable fix to start with, but if better fix is desired, I would be happy to try ast_tools approach as I'm interested in it (and I'm scared to touch parser code 😅). Please let me know!

@overlookmotel
Copy link
Contributor

#7816 adds a visit_span method to Visit and VisitMut. This would make implementing RemoveSpans trivial:

impl<'a> VisitMut<'a> for RemoveSpans {
  fn visit_span(&mut self, span: &mut Span) {
    *span = SPAN;
  }
}

Only problem is that #7816 may cause a perf regression. I hope it won't as visit_span is a no-op by default, but let's see what benchmarks say.

Note: I suggest calling the visitor RemoveSpans (plural), not RemoveSpan, because this will remove all spans, not just one.

@overlookmotel
Copy link
Contributor

Looks like #7816 has no perf impact. If Boshen is happy with it, it will solve the problem in this PR.

Boshen pushed a commit that referenced this pull request Dec 12, 2024
Add `Visit::visit_span` and `VisitMut::visit_span` methods, to facilitate #7811.

Both are no-ops by default, and marked `#[inline]`, so this produces no performance impact.
Copy link
Contributor

@overlookmotel overlookmotel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a test where replacement contains statements e.g. (() => { return 123; })()?

@overlookmotel overlookmotel merged commit 5b7e1ad into oxc-project:main Dec 13, 2024
27 checks passed
Boshen pushed a commit that referenced this pull request Dec 13, 2024
Follow-on after #7811. Remove the panicking test. It *does* currently
cause a panic if replacement contains scopes (e.g. `() => 123`) but
that's a bug. So we shouldn't have a test saying that it *should* panic.
Boshen added a commit that referenced this pull request Dec 13, 2024
## [0.41.0] - 2024-12-13

- fb325dc ast: [**BREAKING**] `span` field must be the first element
(#7821) (Boshen)

- 96a26d3 ast: [**BREAKING**] Rename `is_strict` methods to
`has_use_strict_directive` (#7783) (overlookmotel)

### Features

- 8991f33 ast: Add `visit_span` to `Visit` and `VisitMut` (#7816)
(overlookmotel)
- f7900ab ast: Add `ArrowFunctionExpression::has_use_strict_directive`
method (#7784) (overlookmotel)
- e727ae9 transformer/class-properties: Transform super member
expressions that are inside static prop initializer (#7815) (Dunqing)

### Bug Fixes

- 7610dc1 parser: Parse `import source from 'mod'` (#7833) (Boshen)
- 9479e2b semantic: Missing references when `export {}` references a
type-only binding and a normal (#7812) (Yunfei He)
- 7a83230 semantic: Missing reference when `export default` references a
type alias binding (#7813) (Dunqing)
- 4a3bca8 semantic: Fix identifying strict mode arrow functions (#7785)
(overlookmotel)
- 5b7e1ad transformer: Remove span of define value (#7811) (Hiroshi
Ogawa)
- 14896cb transformer/class-properties: Create temp vars in correct
scope (#7824) (overlookmotel)
- 25bb6da transformer/class-properties: Fix `ScopeId`s in instance prop
initializers (#7823) (overlookmotel)
- 65b109a transformer/class-properties: No `raw` for generated
`StringLiteral` (#7825) (overlookmotel)
- 2964a61 transformer/class-properties: Unwrap failed when private field
expression doesn't contain optional expression in `ChainExpression`
(#7798) (Dunqing)
- 6fa6785 transformer/class-properties: Panic when the callee or member
is `ParenthesisExpression` or TS-syntax expressions. (#7795) (Dunqing)
- bb22c67 transformer/class-properties: Fix `ScopeId`s in static prop
initializers (#7791) (overlookmotel)
- caa57f1 transformer/class-properties: Fix scope flags in static prop
initializers (#7786) (overlookmotel)

### Performance

- 4448b63 codegen: Faster writing indentation (#7820) (overlookmotel)
- afaaffa codegen: Fast path for `options.print_comments()` (#7806)
(Boshen)

### Refactor

- 0f367e5 semantic: Improve the logic of resolving references to be
cleaner (#7829) (Dunqing)
- 5710950 semantic: Move export-related reference flags logic to visit
functions (#7828) (Dunqing)
- b290ebd transformer: Handle `<CWD>` in test runner (#7799) (Dunqing)
- e70deb9 transformer/class-properties: Locate instance props insertion
location in separate step (#7819) (overlookmotel)
- afc5f1e transformer/class-properties: De-deduplicate code (#7805)
(overlookmotel)
- 47a91d2 transformer/class-properties: Shorten code (#7804)
(overlookmotel)
- 54ef2b9 transformer/class-properties: Rename
`debug_assert_expr_is_not_parenthesis_or_typescript_syntax` (#7803)
(overlookmotel)
- 3cdc47c transformer/class-properties: `#[inline(always)]` on
`assert_expr_neither_parenthesis_nor_typescript_syntax` (#7802)
(overlookmotel)

### Testing

- d72c888 transformer/replace-global-defines: Remove panicking test
(#7838) (overlookmotel)

Co-authored-by: Boshen <[email protected]>
@hi-ogawa hi-ogawa deleted the fix-define-span branch December 14, 2024 00:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-transformer Area - Transformer / Transpiler C-bug Category - Bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

define generates incorrect sourcemap
3 participants