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

feat(ast)!: add pure field to Function, CallExpression, and NewExpression #9207

Conversation

overlookmotel
Copy link
Contributor

@overlookmotel overlookmotel commented Feb 18, 2025

Add pure field to Function, CallExpression, and NewExpression.

These fields are marked #[builder(default)] so it doesn't alter the signatures of main AstBuilder methods for these types.

Codegen also generates new builder methods including these fields e.g. AstBuilder::call_expression_with_pure. This is why I've chosen the field name pure, rather than no_side_effects - call_expression_with_no_side_effects would be a misleading name as this method allows creating a CallExpression which does have side effects.

Where a type has multiple default fields, oxc_ast_tools only generates a single extra method for creating a node with all the default fields specified. So the unfortunate side-effect of adding pure field to Function is that AstBuilder::function_with_scope_id becomes AstBuilder::function_with_scope_id_and_pure, which broke lots of stuff in transformer.

Making codegen create multiple methods for all combinations of default fields would be time-consuming, so instead I have taken the shortcut of manually adding a function_with_scope_id method which has same signature as the old one in ast_builder_impl.rs. This makes the transformer code work as before, without modification.

Copy link
Contributor Author

overlookmotel commented Feb 18, 2025


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.

This stack of pull requests is managed by Graphite. Learn more about stacking.

Copy link

codspeed-hq bot commented Feb 18, 2025

CodSpeed Performance Report

Merging #9207 will not alter performance

Comparing 02-18-feat_ast_add_pure_field_to_function_callexpression_and_newexpression_ (abb15e0) with main (71155cf)

Summary

✅ 33 untouched benchmarks

@overlookmotel
Copy link
Contributor Author

overlookmotel commented Feb 18, 2025

@Boshen feel free to change the field name if you want. For the reasons explained above, I think the name pure has advantages over no_side_effects, but you may disagree. I've left this PR as draft for now, in case you want to change it.

@overlookmotel overlookmotel requested a review from Boshen February 18, 2025 13:25
@Boshen Boshen changed the base branch from 02-18-docs_ast_docs_for_astbuilder__with__methods_list_extra_field_names_not_field_types to graphite-base/9207 February 18, 2025 14:51
@Boshen Boshen force-pushed the 02-18-feat_ast_add_pure_field_to_function_callexpression_and_newexpression_ branch from dd4c663 to 9b9b8fd Compare February 18, 2025 15:05
@Boshen Boshen force-pushed the graphite-base/9207 branch from 8a76d33 to 70726e9 Compare February 18, 2025 15:05
@Boshen Boshen changed the base branch from graphite-base/9207 to main February 18, 2025 15:06
@Boshen Boshen force-pushed the 02-18-feat_ast_add_pure_field_to_function_callexpression_and_newexpression_ branch from 9b9b8fd to c426034 Compare February 18, 2025 15:06
@overlookmotel overlookmotel marked this pull request as ready for review February 18, 2025 19:38
@overlookmotel overlookmotel marked this pull request as draft February 18, 2025 19:38
@Boshen Boshen force-pushed the 02-18-feat_ast_add_pure_field_to_function_callexpression_and_newexpression_ branch from c426034 to a435f86 Compare February 25, 2025 09:56
@Boshen Boshen marked this pull request as ready for review February 26, 2025 13:40
@graphite-app graphite-app bot added the 0-merge Merge with Graphite Merge Queue label Feb 26, 2025
Copy link

graphite-app bot commented Feb 26, 2025

Merge activity

…wExpression` (#9207)

Add `pure` field to `Function`, `CallExpression`, and `NewExpression`.

These fields are marked `#[builder(default)]` so it doesn't alter the signatures of main `AstBuilder` methods for these types.

Codegen also generates new builder methods including these fields e.g. `AstBuilder::call_expression_with_pure`. This is why I've chosen the field name `pure`, rather than `no_side_effects` - `call_expression_with_no_side_effects` would be a misleading name as this method allows creating a `CallExpression` which *does* have side effects.

Where a type has multiple default fields, `oxc_ast_tools` only generates a single extra method for creating a node with *all* the default fields specified. So the unfortunate side-effect of adding `pure` field to `Function` is that `AstBuilder::function_with_scope_id` becomes `AstBuilder::function_with_scope_id_and_pure`, which broke lots of stuff in transformer.

Making codegen create multiple methods for all combinations of default fields would be time-consuming, so instead I have taken the shortcut of manually adding a `function_with_scope_id` method which has same signature as the old one in [ast_builder_impl.rs](https://github.com/oxc-project/oxc/blob/dd4c6630b92e3d573e5fc75147d713458543b2b0/crates/oxc_ast/src/ast_builder_impl.rs#L293-L333). This makes the transformer code work as before, without modification.
@graphite-app graphite-app bot force-pushed the 02-18-feat_ast_add_pure_field_to_function_callexpression_and_newexpression_ branch from a435f86 to abb15e0 Compare February 26, 2025 13:41
@graphite-app graphite-app bot merged commit abb15e0 into main Feb 26, 2025
26 checks passed
@graphite-app graphite-app bot deleted the 02-18-feat_ast_add_pure_field_to_function_callexpression_and_newexpression_ branch February 26, 2025 13:52
This was referenced Mar 3, 2025
Boshen added a commit that referenced this pull request Mar 4, 2025
## [0.54.0] - 2025-03-04

- 098f652 codegen: [**BREAKING**] Add `CommentAnnotation` to avoid
parsing comments again (#9506) (Boshen)

- a8d1d48 parser,codegen: [**BREAKING**] Parse and
print`#__NO_SIDE_EFFECTS__` (#9496) (Boshen)

- 355a4db napi/parser: [**BREAKING**] Remove `parse_without_return` API
(#9455) (Boshen)

- a5cde10 visit_ast: [**BREAKING**] Add `oxc_visit_ast` crate (#9428)
(Boshen)

- abb15e0 ast: [**BREAKING**] Add `pure` field to `Function`,
`CallExpression`, and `NewExpression` (#9207) (overlookmotel)

### Features

- a9f0d2b ast_tools: Generate field offset assertions for more types
(#9449) (overlookmotel)
- 64f4a82 ecmascript: Handle pure call expression in chain expressions
(#9480) (sapphi-red)
- 32139d2 ecmascript: Support `/* @__PURE__ */` in may_have_side_effects
(#9409) (sapphi-red)
- f5453f6 minifier: Flatten spread args in new expressions (#9512)
(sapphi-red)
- f8073f3 minifier: Support if with side effects in dead code
elimination (#9502) (sapphi-red)
- 10eb8f7 minifier: Use `remove_unused_expression` in
`try_fold_sequence_expression` in `remove_dead_code` (#9467)
(sapphi-red)
- 70916db minifier: Remove unused expression in for init and update
(#9466) (sapphi-red)
- 26fde56 minifier: Inline simple IIFEs in `remove_unused_expression`
(#9465) (sapphi-red)
- ec2193e minifier: Support extracting arguments in pure calls in
`remove_unused_expression` (#9463) (sapphi-red)
- 007051c minifier: Compress `a != null && a.b()` to `a?.b()` in
`remove_unused_expression` (#9459) (sapphi-red)
- 50fce20 minifier: Support binary expression in
`remove_unused_expression` (#9456) (sapphi-red)
- ed9ede3 minifier: Support conditional expression in
`remove_unused_expression` (#9432) (sapphi-red)
- 3520538 minifier: Support object expression in
`remove_unused_expression` (#9430) (sapphi-red)
- fb8a93d minifier: Improve array expression handling in
`remove_unused_expression` (#9427) (sapphi-red)
- ff477cd minifier: Support template literals in
`remove_unused_expression` (#9426) (sapphi-red)
- 0d26113 minifier: Compress `/* @__PURE__ */ a() ? b : b` to `b`
(#9410) (sapphi-red)
- 68c77c8 napi/parser: Return semantic errors (#9460) (Boshen)
- 7d7f16c parser: Apply pure to rhs of binary expression (#9492)
(Boshen)
- 2a08b14 parser: Support V8 intrinsics (#9379) (injuly)
- 9b7017c parser,codegen: Pure annotations (#9351) (Boshen)
- aa06932 transformer: Support `ModuleRunnerTransform` (#9371) (Dunqing)

### Bug Fixes

- 75f06ad codegen: Do not print comments when only `annotation_comments`
is enabled (#9518) (Dunqing)
- f5bbd5d ecmascript: Fix toString for negative numbers (#9508)
(sapphi-red)
- d2cd975 ecmascript: Fix may_have_side_effects for `${a() === b}`
(#9478) (sapphi-red)
- 584d847 ecmascript: Objects passed to template literals may have side
effects (#9425) (sapphi-red)
- b371fdc estree/ast: Camel-case `typeAnnotation` field (#9420)
(overlookmotel)
- 1fff993 minifier: Correctly remove dead code in `try` with `finally`
(#9503) (sapphi-red)
- 0b96ebe minifier: Don't inline IIFE with parameters (#9477)
(sapphi-red)
- 071c84c minifier: Skip `try_fold_stmt_in_boolean_context` on
`ExpressionStatement` (#9458) (sapphi-red)
- 306284d minifier: Call mark_current_function_as_changed in
remove_unused_expression (#9457) (sapphi-red)
- 9c6ae9f parser: `@__NO_SIDE_EFFECTS` only affects const variable decl
(#9517) (Boshen)
- b7d5513 parser: Parse `@__NO_SIDE_EFFECTS__` between `export default`
and `async function` (#9514) (Boshen)
- 01de74c parser: Correctly fail parsing when parsing `foo.bar?.`
(#9499) (camc314)
- 58defe3 parser: Mark expression as pure in chain expression (#9479)
(sapphi-red)
- 2a03689 parser: Mark expressions on the left side of logical and
conditional expressions as pure (#9414) (sapphi-red)
- 7fde233 transformer/legacy-decorator: Incorrect check for potentially
non-existent ident (#9491) (Dunqing)
- b4f050c transformer/legacy-decorator: Metadata incorrectly wrapped by
decorateParam (#9490) (Dunqing)
- 5f67fa9 transformer/optional-chaning: Keep `this` when transforming
`this.f?.()` (#9505) (camc314)

### Performance

- 10e4431 ast/estree: Skip escaping identifiers (#9398) (overlookmotel)
- b0a0a82 ast/estree: Reduce overhead serializing static strings (#9396)
(overlookmotel)
- 27a8e50 isolated_declarations: Reserve sufficient capacity in
statements `Vec` (#9394) (overlookmotel)

### Refactor

- 8916335 ast/estree: Convert enums with converters on variants (#9438)
(overlookmotel)
- dcff40c ast_tools: Generate layout assertions in multiple crates
(#9448) (overlookmotel)
- 19c4835 codegen: Simplify printing comments between arguments in
call-like expressions (#9501) (Dunqing)
- c880481 isolated_declarations: Rename var (#9390) (overlookmotel)
- 37e41f0 isolated_declarations: Use aliases `ArenaBox` / `ArenaVec`
(#9389) (overlookmotel)
- bbb450c minifier: Move `a != null && b` -> `a ?? b` compression to
`remove_unused_expression` (#9468) (sapphi-red)
- b93774c minifier: Move `try_fold_iife` to `remove_unused_expression`
(#9464) (sapphi-red)
- 96a9719 minifier: Use `may_have_side_effects` in
`remove_unused_expression` (#9413) (sapphi-red)

### Testing

- c187b11 ecmascript: Add comments and tests for cases where
`ToPropertyKey` throws an error (#9429) (sapphi-red)
- d129055 napi: Add tests for worker threads (#9408) (Boshen)
- 48d51e3 napi: Add tests for `hashbang` field (#9386) (overlookmotel)

Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0-merge Merge with Graphite Merge Queue A-ast Area - AST C-enhancement Category - New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant