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(transformer): add object-spread plugin #3133

Merged

Conversation

magic-akari
Copy link
Contributor

No description provided.

@github-actions github-actions bot added the A-transformer Area - Transformer / Transpiler label Apr 29, 2024
Copy link
Contributor Author

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

Join @magic-akari and the rest of your teammates on Graphite Graphite

Copy link

codspeed-hq bot commented Apr 29, 2024

CodSpeed Performance Report

Merging #3133 will not alter performance

Comparing 04-29-feat_transformer_add_object-rest-spread_plugin (08dc0ad) with main (5c4c001)

Summary

✅ 29 untouched benchmarks

@github-actions github-actions bot added the A-ast Area - AST label May 3, 2024
@magic-akari magic-akari force-pushed the 04-29-feat_transformer_add_object-rest-spread_plugin branch from 3e6e05f to 401ddc6 Compare May 6, 2024 09:00
@magic-akari magic-akari changed the title feat(transformer): add object-rest-spread plugin feat(transformer): add object-spread plugin May 6, 2024
@magic-akari magic-akari force-pushed the 04-29-feat_transformer_add_object-rest-spread_plugin branch from 616869c to 418f3c2 Compare May 6, 2024 11:46
@magic-akari
Copy link
Contributor Author

module_imports.rs may need to be improved to enable testing, but I do not want to make changes to it in this PR.

@magic-akari magic-akari marked this pull request as ready for review May 6, 2024 12:06
@Boshen Boshen marked this pull request as draft June 12, 2024 08:25
@overlookmotel
Copy link
Contributor

Hi @magic-akari. Just wondered how complete this is and whether we should try to finish it off?

Transformer has changed a bit since April - it now uses Traverse rather than VisitMut - but I doubt updating it for that change would be difficult, as they're largely the same.

@magic-akari
Copy link
Contributor Author

Hi @magic-akari. Just wondered how complete this is and whether we should try to finish it off?

It's up to you; I'm not in a rush.

Transformer has changed a bit since April - it now uses Traverse rather than VisitMut - but I doubt updating it for that change would be difficult, as they're largely the same.

Wow, I am very willing to learn new methods.

@magic-akari
Copy link
Contributor Author

I am currently refactoring this pull request.

@overlookmotel
Copy link
Contributor

overlookmotel commented Aug 20, 2024

Hi @magic-akari. Sorry I didn't reply before. My Github notifications is like a snowdrift!

In case you've not noticed it, we recently adopted a "style guide" for writing transformers: https://github.com/oxc-project/oxc/blob/main/crates/oxc_transformer/README.md

Most of the existing transformers have not yet been brought in line with the style guide, but this one has, so it's a good model to follow.

If you have any trouble figuring out Traverse, please feel free to ping me.

@magic-akari magic-akari force-pushed the 04-29-feat_transformer_add_object-rest-spread_plugin branch 3 times, most recently from a31f907 to f7bef44 Compare August 21, 2024 05:22
@magic-akari
Copy link
Contributor Author

❯ cargo run -p oxc_transformer --example transformer tasks/coverage/babel/packages/babel-parser/test/fixtures/es2018/object-rest-spread/5/input.js
    Finished `dev` profile [unoptimized] target(s) in 0.15s
     Running `target/debug/examples/transformer tasks/coverage/babel/packages/babel-parser/test/fixtures/es2018/object-rest-spread/5/input.js`
Original:

z = {x, ...y}

Transformed:

import { default as _objectSpread } from "@babel/helpers/lib/helpers/objectSpread2.js";
z = _objectSpread({ x }, y);

@magic-akari magic-akari marked this pull request as ready for review August 21, 2024 05:41
@magic-akari magic-akari marked this pull request as draft August 21, 2024 06:06
@magic-akari magic-akari marked this pull request as ready for review August 21, 2024 06:34
@magic-akari magic-akari force-pushed the 04-29-feat_transformer_add_object-rest-spread_plugin branch from 6fd8452 to 42530ac Compare August 21, 2024 13:04
@magic-akari magic-akari requested review from Dunqing and Boshen August 21, 2024 13:08
@Dunqing
Copy link
Member

Dunqing commented Aug 21, 2024

Oh, this plugin used @babel/helpers, we may hold this until #4753 has a conclusion.

@Dunqing
Copy link
Member

Dunqing commented Aug 23, 2024

https://github.com/babel/babel/blob/f42e24d6451d333052900e1ae073e4bc5d484cca/packages/babel-plugin-transform-object-rest-spread/test/fixtures/assumption-pureGetters/spread-single-call/output.js#L1-L7

In Babel's test cases, babelHelpers.objectSpread2 is used instead of the imported helpers. I should make a special return when running the tests. For example,

fn get_extend_object_callee(&mut self, ctx: &mut TraverseCtx<'a>) -> Expression<'a> {
if self.options.set_spread_properties {
Self::object_assign(ctx)
} else {
let ident = if self.ctx.source_type.is_module() {
self.add_import_statement(
"_objectSpread",
"@babel/helpers/lib/helpers/objectSpread2.js".into(),
ctx,
)
} else {
self.add_require_statement(
"_objectSpread",
"@babel/helpers/lib/helpers/objectSpread2.js".into(),
false,
ctx,
)
};
ctx.ast.expression_from_identifier_reference(ident.create_read_reference(ctx))
}
}

         Expression::from(ctx.ast.member_expression_static(SPAN, object, property, false))
     }
 
+    #[cfg(feature = "external_helper")]
+    fn babel_external_helper(ctx: &mut TraverseCtx<'a>) -> Expression<'a> {
+        let ident_object =
+            ctx.create_reference_id(SPAN, ctx.ast.atom("babelHelpers"), None, ReferenceFlags::Read);
+        let object = ctx.ast.expression_from_identifier_reference(ident_object);
+        let property = ctx.ast.identifier_name(SPAN, "_objectSpread");
+
+        Expression::from(ctx.ast.member_expression_static(SPAN, object, property, false))
+    }
+
     fn get_extend_object_callee(&mut self, ctx: &mut TraverseCtx<'a>) -> Expression<'a> {
+        if cfg!(feature = "external_helper") {
+            return Self::babel_external_helper(ctx);
+        }
+
         if self.options.set_spread_properties {
             Self::object_assign(ctx)
         } else {

What do you think?

We're not sure how we're going to solve #4753. So let's make it as simple as possible, so we'll just return babel_external_helper

@magic-akari magic-akari requested a review from Dunqing August 23, 2024 09:49
crates/oxc_transformer/src/es2018/mod.rs Outdated Show resolved Hide resolved
crates/oxc_transformer/src/es2018/mod.rs Outdated Show resolved Hide resolved
@overlookmotel
Copy link
Contributor

Can I just check: Are we running the conformance tests from this Babel plugin? If so, they're all passing - in that case, bravo! With the introduction of the transform semantic checker, that's no mean feat.

@magic-akari
Copy link
Contributor Author

Can I just check: Are we running the conformance tests from this Babel plugin? If so, they're all passing - in that case, bravo! With the introduction of the transform semantic checker, that's no mean feat.

I'm not sure about the workings of conformance testing.
However, I think that achieving a 100% pass is something wrong.
This is because there's an unimplemented part of the plugin.
It implies that some parts of the test code may not be working as expected.

@overlookmotel
Copy link
Contributor

I'm not sure about the workings of conformance testing.

Me neither! @Dunqing Can you advise?

@magic-akari
Copy link
Contributor Author

I've caught it. It's been additionally disabled here

"transform-modules-commonjs",
"transform-object-rest-spread",
"transform-optional-chaining",

@overlookmotel
Copy link
Contributor

OK great. Now how do we disable just the rest-related tests? Or maybe we don't? @Dunqing?

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.

I've made a bunch of comments. I hope they're useful.

As I said in one of them, please don't feel the need to address everything immediately. The priority is to get the tests passing first.

crates/oxc_transformer/src/es2018/mod.rs Show resolved Hide resolved
crates/oxc_transformer/src/es2018/object_spread.rs Outdated Show resolved Hide resolved
crates/oxc_transformer/src/es2018/object_spread.rs Outdated Show resolved Hide resolved
crates/oxc_transformer/src/es2018/object_spread.rs Outdated Show resolved Hide resolved
crates/oxc_transformer/src/es2018/object_spread.rs Outdated Show resolved Hide resolved
crates/oxc_transformer/src/lib.rs Outdated Show resolved Hide resolved
@magic-akari magic-akari force-pushed the 04-29-feat_transformer_add_object-rest-spread_plugin branch from 7f8916f to 453d83d Compare August 23, 2024 16:08
@Dunqing
Copy link
Member

Dunqing commented Aug 24, 2024

OK great. Now how do we disable just the rest-related tests? Or maybe we don't? @Dunqing?

These disabled plugins are used in other plugins. i.e. when we develop the transform-typescript plugin, and the transform-object-rest-spread plugin is not supported yet, so we don't care about tests where transform-typescript and transform-object-rest-spread are both enabled.

Now, we can remove transform-object-rest-spread because we are doing support this plugin right now

@overlookmotel
Copy link
Contributor

overlookmotel commented Aug 24, 2024

I'm not sure if I was clear. This PR adds support for the spread part of the transform-object-rest-spread but the rest part is left as a TODO. So should we:

  1. Enable all the tests for this plugin, and the rest-related tests will show as failing in snapshot?
  2. Disable the rest-related tests, so only the spread-related tests run (and should all pass)?

@Dunqing
Copy link
Member

Dunqing commented Aug 24, 2024

  1. Enable all the tests for this plugin, and the rest-related tests will show as failing in snapshot?

Yes

  1. Disable the rest-related tests, so only the spread-related tests run (and should all pass)?

This depends on whether the spread part is fully completed or not

@magic-akari magic-akari force-pushed the 04-29-feat_transformer_add_object-rest-spread_plugin branch from 0e2bb39 to 7e411ec Compare August 26, 2024 08:41
@Dunqing Dunqing added this to the Transformer Milestone 2 milestone Aug 26, 2024
@Dunqing Dunqing requested a review from overlookmotel August 28, 2024 01:44
@Dunqing
Copy link
Member

Dunqing commented Aug 28, 2024

This PR is now large, and there will always be conflicts. I think we should merge first and then iterate for improvement. @overlookmotel What do you think?

@magic-akari magic-akari force-pushed the 04-29-feat_transformer_add_object-rest-spread_plugin branch from 7e411ec to ea64b88 Compare August 28, 2024 03:30
@overlookmotel overlookmotel dismissed Dunqing’s stale review August 28, 2024 15:21

Merge conflicts now resolved.

@overlookmotel
Copy link
Contributor

overlookmotel commented Aug 28, 2024

This PR is now large, and there will always be conflicts. I think we should merge first and then iterate for improvement.

Agreed. I've merged latest main. If CI passes, I'll merge it.

@overlookmotel overlookmotel added 0-merge Merge with Graphite Merge Queue and removed A-ast Area - AST labels Aug 28, 2024
@overlookmotel
Copy link
Contributor

Thanks @magic-akari for your work on this. Sorry it's taken a while to get merged.

Copy link

graphite-app bot commented Aug 28, 2024

Merge activity

  • Aug 28, 11:56 AM EDT: The merge label 'merge' was detected. This PR will be added to the Graphite merge queue once it meets the requirements.
  • Aug 28, 11:56 AM EDT: overlookmotel added this pull request to the Graphite merge queue.
  • Aug 28, 12:01 PM EDT: overlookmotel merged this pull request with the Graphite merge queue.

@overlookmotel overlookmotel force-pushed the 04-29-feat_transformer_add_object-rest-spread_plugin branch from 3bb287d to 08dc0ad Compare August 28, 2024 15:57
@graphite-app graphite-app bot merged commit 08dc0ad into main Aug 28, 2024
25 checks passed
@graphite-app graphite-app bot deleted the 04-29-feat_transformer_add_object-rest-spread_plugin branch August 28, 2024 16:01
@oxc-bot oxc-bot mentioned this pull request Sep 3, 2024
Boshen added a commit that referenced this pull request Sep 3, 2024
## [0.26.0] - 2024-09-03

- 1aa49af ast: [**BREAKING**] Remove
`JSXMemberExpressionObject::Identifier` variant (#5358) (Dunqing)

- 01cc2ce semantic: [**BREAKING**] Add `ScopeTree:get_child_ids` API
behind a runtime flag (#5403) (Boshen)

- b1d0075 napi/transform: [**BREAKING**] Align output API `sourceText`
-> `code` with babel (#5398) (Boshen)

- 32f7300 ast: [**BREAKING**] Add `JSXElementName::IdentifierReference`
and `JSXMemberExpressionObject::IdentifierReference` (#5223) (Dunqing)

- 23e8456 traverse: [**BREAKING**] `TraverseCtx::ancestor` with level 0
= equivalent to `parent` (#5294) (overlookmotel)

- 582ce9e traverse: [**BREAKING**] `TraverseCtx::ancestor` return
`Ancestor::None` if out of bounds (#5286) (overlookmotel)

- 234a24c ast: [**BREAKING**] Merge `UsingDeclaration` into
`VariableDeclaration` (#5270) (Kevin Deng 三咲智子)

- c100826 semantic: [**BREAKING**] Always create a scope for `for`
statements (#5110) (overlookmotel)

- d304d6f semantic: [**BREAKING**] Always create a scope for
`CatchClause` (#5109) (overlookmotel)

### Features

- 180b1a1 ast: Add `Function::name()` (#5361) (DonIsaac)
- 5505749 ast: Add `accessibility` field to `AccessorProperty` (#5290)
(Dunqing)
- 49cd5db ast,parser: Add `definite` flag to `AccessorProperty` node
(#5182) (DonIsaac)
- c2fa725 ast,parser: Parse `TSTypeAnnotations` on `AccessorProperty`
(#5179) (DonIsaac)
- 292d162 codegen: Print missing fields for `AccessorProperty` (#5291)
(Dunqing)
- 72740b3 isolated_declaration: Support sourcemap option (#5170)
(dalaoshu)
- f81e8a1 linter: Add `oxc/no-async-endpoint-handlers` (#5364)
(DonIsaac)
- 9c22ce9 linter: Add hyperlinks to diagnostic messages (#5318)
(DonIsaac)
- d22bd20 module_lexer: Distinguish for types-only imports and exports
(#5184) (Kevin Deng 三咲智子)
- 7dfd51a parser: Report class properties that are both definite and
optional (#5181) (DonIsaac)
- a563968 parser: Report errors on optional accessor properties (#5180)
(DonIsaac)
- 46b641b regular_expression: Validate max quantifier value (#5218)
(leaysgur)
- be4642f semantic: Transform checker check child scope IDs (#5410)
(overlookmotel)
- 6e969f9 semantic: Add `ScopeTree::delete_root_unresolved_reference`
(#5305) (overlookmotel)
- 1b20ceb span: Add `CompactStr::to_compact_string` method (#5385)
(Boshen)
- 5a137f0 span/source-type: Add SourceType factory methods (#5242)
(DonIsaac)
- f5e05db span/source-type: Impl `Display` and `Error` for
`UnknownExtension` (#5240) (DonIsaac)
- d04857b transformer: Support `Targets::from_query` method (#5336)
(Dunqing)
- 3d4a64c transformer: Make `Targets` public (#5335) (Dunqing)
- 0eb7602 transformer: Support `TransformOptions::from_preset_env` API
(#5323) (Dunqing)
- 08dc0ad transformer: Add `object-spread` plugin (#3133) (magic-akari)
- 01c0c3e transformer: Add remaining options to transformer options
(#5169) (Boshen)
- 056c667 transformer/arrow-functions: The output that uses `this`
inside blocks doesn't match Babel (#5188) (Dunqing)
- 0abfc50 transformer/typescript: Support `rewrite_import_extensions`
option (#5399) (Dunqing)

### Bug Fixes

- 8ebc23f ast: Serialize `TSParenthesizedType` with camelCase (#5199)
(Kevin Deng 三咲智子)
- 5c4c001 codegen: Print `export @decorator declare abstract class Foo`
correctly (#5303) (Boshen)
- 7b1546b codegen: Do not print comments when `--minify` (Boshen)
- ff7fa98 diagnostics: Improve "file is too long to fit on the screen"
(#5120) (Boshen)
- 8a17807 parser: Treat JSX element tags starting with `_` or `$` as
`IdentifierReference`s (#5343) (overlookmotel)
- d4c06ef parser: Revert "check for `@flow` with recoverable errors as
well" (#5297) (overlookmotel)
- e1d8b92 parser: Check for `@flow` with recoverable errors as well
(Boshen)
- e6fd52e parser: Change unterminated regex error to be non-recoverable
(#5285) (Boshen)
- 1686920 parser: Span for invalid regex flags (#5225) (leaysgur)
- cffce11 regular_expression: Prevent panic on too large number (#5282)
(leaysgur)
- 293413f semantic: Implicit return `UpdateExpression` in
`ArrowFunctionExpression` does not as read reference (#5161) (Dunqing)
- a6bb3b1 span/source-type: Consider `.cjs` and `.cts` files as
`ModuleKind::Script` (#5239) (DonIsaac)
- 35f03db transformer: `ArrowfunctionExpression`'s expression is true
but has more than one body statement (#5366) (Dunqing)
- 8d6b05c transformer: Class property with typescript value should not
be removed (#5298) (Boshen)
- 47e69a8 transformer-optional-catch-binding: The `unused` binding is
not in the correct scope (#5066) (Dunqing)
- 94ff94c transformer/arrow-functions: Reaches `unreachable` when
`<this.foo>` is inside an arrow function (#5356) (Dunqing)
- f8bb022 transformer/arrow-functions: Remove
`SymbolFlags::ArrowFunction` (#5190) (Dunqing)
- d9ba5ad transformer/arrow-functions: Correct scope for `_this` (#5189)
(Dunqing)
- 3acb3f6 transformer/react: Mismatch output caused by incorrect
transformation ordering (#5255) (Dunqing)
- 5754c89 transformer/typescript: Remove accessibility from
`AccessorProperty` (#5292) (Dunqing)
- d594818 traverse: `insert_scope_below` update child scopes records
(#5409) (overlookmotel)
- 25d6e20 traverse: Add missing visitors to `ChildScopeCollector`
(#5118) (overlookmotel)

### Performance

- 292f217 ast: Optimize `JSXIdentifier::is_reference` (#5344)
(overlookmotel)
- 12a7607 codegen: Inline `Codegen::print_list` (#5221) (overlookmotel)
- fb847bd codegen: Slightly faster `print_list` (#5192) (Boshen)
- a1523c6 transformer: Remove an allocation from arrow functions
transform (#5412) (overlookmotel)

### Documentation

- 8334bd4 transformer: Add documentation for `Targets::get_targets`
(#5337) (Dunqing)
- d51a954 transformer: Add documentation for arrow-functions plugin
(#5186) (Dunqing)

### Refactor

- c2d8c9e ast: Reduce allocations in
`AstBuilder::move_assignment_target` (#5367) (overlookmotel)
- 946c867 ast: Box `TSThisParameter` (#5325) (overlookmotel)
- 960e1d5 ast: Rename `IdentifierReference::new_with_reference_id`
(#5157) (overlookmotel)
- f63b568 ast: Remove `#[non_exhaustive]` attr from `AstBuilder` (#5130)
(overlookmotel)
- d4c3778 codegen: Rename vars (#5222) (overlookmotel)
- 543cad6 codegen: Remove some pub APIs (Boshen)
- cd63336 diagnostic: Change how diagnostic codes are rendered (#5317)
(DonIsaac)
- d236554 parser: Move `JSXIdentifier` conversion code into parser
(#5345) (overlookmotel)
- bc59dd2 parser: Improve example for `byte_search!` macro usage (#5234)
(overlookmotel)
- a3ddfdd parser: Improve lexer pointer maths (#5233) (overlookmotel)
- 3ae94b8 semantic: Change `build_module_record` to accept &Path instead
of PathBuf (Boshen)
- 05d25e2 semantic: Combine add scope methods (#5262) (overlookmotel)
- fdedc0f semantic: Transform checker: rename `SemanticData` to
`Scoping` (#5261) (overlookmotel)
- 1086109 semantic: Transform checker do not output spans in errors
(#5260) (overlookmotel)
- af5713e semantic: Transform checker continue checks if missing IDs
(#5259) (overlookmotel)
- 943454f semantic: Update transform checker for no conditional scopes
(#5252) (overlookmotel)
- 892a7fa semantic: Replace `ref` syntax (#5253) (overlookmotel)
- cbb4725 semantic: Add comment to transform checker (#5250)
(overlookmotel)
- a17cf33 semantic: Remove `ScopeTree::child_ids` (#5232) (Boshen)
- d5a4940 semantic: Rewrite handling of label statement errors (#5138)
(Boshen)
- 94f60e7 span/source-type: Make `SourceType` factories `const` (#5241)
(DonIsaac)
- 0de844d transformer: Remove unnecessary code from JSX transform
(#5339) (overlookmotel)
- 5136f01 transformer: Remove unnecessary type annotation (#5131)
(overlookmotel)
- 260c9d2 transformer/es2015: Move all entry points to implementation of
Traverse trait (#5187) (Dunqing)
- 1645115 transformer/object-reset-spread: Make plugin initialization
unconditional (#5319) (Dunqing)
- d2666fe transformer/object-rest-spread: Move plugin-relates files to
`object_rest_spread` mod (#5320) (Dunqing)
- 7e2a7af transformer/react: Remove `CalculateSignatureKey`
implementation from refresh (#5289) (Dunqing)
- b43a394 traverse: Correct code comments (#5293) (overlookmotel)
- d71f0ed traverse: Inline all passthrough methods (#5279)
(overlookmotel)
- 188ce07 traverse: Improve safety via type system (#5277)
(overlookmotel)
- 0f4a8b3 traverse: Add debug asserts for safety invariants (#5272)
(overlookmotel)
- 341e42a traverse: Make `Ancestor` an owned type (#5269)
(overlookmotel)
- eba5033 traverse: Codegen `ChildScopeCollector` (#5119)
(overlookmotel)
- f771d7c traverse: Remove unnecessary imports (#5116) (overlookmotel)
- c6590ae traverse: Move generated files into separate folder (#5115)
(overlookmotel)
- fc2e9ad traverse: Remove support for `#[scope(if(...))]` attr (#5114)
(overlookmotel)
- 1ba11a3 traverse: Refactor `ChildScopeCollector` (#5112)
(overlookmotel)
- 40e2f6e traverse: Remove unnecessary branch in `ChildScopeCollector`
(#5111) (overlookmotel)
- b39c0d6 wasm: Add `source_type` for parser, replace class options with
plain object (#5217) (Kevin Deng 三咲智子)

### Testing

- 7009177 parser: Fix incorrect flow error test (Boshen)
- be7b8c6 semantic: Add `JSXIdentifierReference`-related tests (#5224)
(Dunqing)

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-transformer Area - Transformer / Transpiler
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants