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

Improve output of using #15959

Merged
merged 10 commits into from Jan 15, 2024
Merged

Improve output of using #15959

merged 10 commits into from Jan 15, 2024

Conversation

liuxingbaoyu
Copy link
Member

@liuxingbaoyu liuxingbaoyu commented Sep 12, 2023

Q                       A
Fixed Issues? Fixes #1, Fixes #2
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

To be honest I think this may not be an improvement over bundled + minimized size, but it should make the output easier to view.

@babel-bot
Copy link
Collaborator

babel-bot commented Sep 12, 2023

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/56136/

return {
s: [], // stack
e: empty, // error
using: function (value, isAwait) {
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure that this PR is worth doing, but anyway we can improve this helper:

  • use u and d rather than using and dispose
  • s can be a local variable, rather than a property
  • you might split u in u and a ("using" and "await using"), to save on the second parameter

@liuxingbaoyu
Copy link
Member Author

CI errors are funny. I will investigate.

@nicolo-ribaudo
Copy link
Member

We do not support having two helpers with two top-level variables having the same name 😁

@@ -0,0 +1,78 @@
/* @minVersion 7.23.0 */

export default function _usingCtxFactory() {
Copy link
Member

Choose a reason for hiding this comment

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

Could you wrap the old helper rather than the new one, so that the new one remains smaller?

}

function err(e) {
error = error != empty ? new dispose_SuppressedError(e, error) : e;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
error = error != empty ? new dispose_SuppressedError(e, error) : e;
error = error !== empty ? new dispose_SuppressedError(e, error) : e;

If something does throw "[object Object]", error == empty will be true :P

Comment on lines 56 to 59
while ((r = stack.pop())) {
try {
var r,
p = r.d.call(r.v);
Copy link
Member

Choose a reason for hiding this comment

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

The var r here is incredibly confusing, I was initially surprised that it's not undefined :P Unfortunately Terser cannot do this optimization for us so it's fine to do it manually, but I opened terser/terser#1446.

Also, let's rename r to resource and p to disposalResult since we now mangle variables anyway 😉

t.addComment(
replacement.block.body[0],
"leading",
"u: using(obj, isAwait), d: dispose()",
Copy link
Contributor

Choose a reason for hiding this comment

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

We generally do not add comments explaining the transpiled output. Though we could do so under a verbose: boolean flag.

Copy link
Member Author

Choose a reason for hiding this comment

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

I will remove them and open a separate PR if needed in the future.

Copy link
Contributor

@JLHwung JLHwung left a comment

Choose a reason for hiding this comment

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

Can you add a todo item in the make new-version-checklist?
Since we have to update the minVersion data of the usingCtx helper.

@liuxingbaoyu liuxingbaoyu force-pushed the improve-using branch 2 times, most recently from f8a0483 to 0defb46 Compare November 29, 2023 05:59
var empty = {},
stack = [];
function using(isAwait, value) {
if (value != null) {
Copy link
Member Author

Choose a reason for hiding this comment

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

I used v!=null here because it doesn't look like people would use document.all here.
And https://github.com/babel/babel/pull/15959/files#diff-cd9817b5f60ed5ac93a50bd1667f5f8216d36a6d1eb52ade7ad3ebe6bcaaf21cR34

Comment on lines 10 to 20
var _disposeSuppressedError =
typeof SuppressedError === "function"
? // eslint-disable-next-line no-undef
SuppressedError
: (function (suppressed: boolean, error: Error) {
var err = new Error() as SuppressedError;
err.name = "SuppressedError";
err.suppressed = suppressed;
err.error = error;
return err;
} as SuppressedErrorConstructor),
Copy link
Member Author

Choose a reason for hiding this comment

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

I borrowed from ts's implementation because theirs is much simpler, I think the behavior has some differences in the prototype, but I'm not sure who is right and if it is acceptable.

Also sorry for making these changes after the review. :) @JLHwung

ts helper

var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
    return function (env) {
        function fail(e) {
            env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
            env.hasError = true;
        }
        function next() {
            while (env.stack.length) {
                var rec = env.stack.pop();
                try {
                    var result = rec.dispose && rec.dispose.call(rec.value);
                    if (rec.async)
                        return Promise.resolve(result).then(next, function (e) {
                            fail(e);
                            return next();
                        });
                } catch (e) {
                    fail(e);
                }
            }
            if (env.hasError)
                throw env.error;
        }
        return next();
    };
})(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
    var e = new Error(message);
    return e.name = "SuppressedError",
    e.error = error,
    e.suppressed = suppressed,
    e;
});

Copy link
Member Author

Choose a reason for hiding this comment

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

https://github.com/es-shims/SuppressedError/blob/11f592cc4ec88691b20944057b1772e4b62d201c/implementation.js#L13

By looking at it, I think the previous implementation was more spec-compliant, but it seems like ts's behavior is sufficient for use here.
Since we did not assign a value to SuppressedError, users should not use instanceof to compare it.

typeof SuppressedError === "function"
? // eslint-disable-next-line no-undef
SuppressedError
: (function (suppressed: boolean, error: Error) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Oops, I realized that our two parameters were reversed, and it is not boolean. : :)

@liuxingbaoyu
Copy link
Member Author

Since rebasing made this PR more complicated, I removed the new-version-checklist changes and I will update the version of this PR synchronously after the new patch is released.

"using declarations can only be used with objects, functions, null, or undefined.",
);
}
// core-js-pure uses Symbol.for for polyfilling well-known symbols
Copy link
Member Author

Choose a reason for hiding this comment

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

Do we expect users to use the core-js-pure conversion helper? 🤔
image

Copy link
Member

Choose a reason for hiding this comment

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

Not necessarily, but maybe users are importing "things that can be disposed" from libraries that have been compiled with core-js, and that thus use Symbol.for("Symbol.dispose") as their protocol.

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

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

Great! As a follow-up PR, please remove the old transform for Babel 8.

@nicolo-ribaudo nicolo-ribaudo merged commit 218faee into babel:main Jan 15, 2024
47 checks passed
Vylpes pushed a commit to Vylpes/random-bunny that referenced this pull request Apr 10, 2024
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [@babel/traverse](https://babel.dev/docs/en/next/babel-traverse) ([source](https://github.com/babel/babel)) | resolutions | patch | [`7.23.7` -> `7.23.9`](https://renovatebot.com/diffs/npm/@babel%2ftraverse/7.23.7/7.23.9) |

---

### Release Notes

<details>
<summary>babel/babel (@&#8203;babel/traverse)</summary>

### [`v7.23.9`](https://github.com/babel/babel/blob/HEAD/CHANGELOG.md#v7239-2024-01-25)

[Compare Source](babel/babel@v7.23.7...v7.23.9)

##### 🐛 Bug Fix

-   `babel-helper-transform-fixture-test-runner`, `babel-plugin-transform-function-name`, `babel-plugin-transform-modules-systemjs`, `babel-preset-env`
    -   [#&#8203;16225](babel/babel#16225) fix: `systemjs` re-traverses helpers ([@&#8203;liuxingbaoyu](https://github.com/liuxingbaoyu))
-   `babel-helper-create-class-features-plugin`, `babel-plugin-proposal-decorators`
    -   [#&#8203;16226](babel/babel#16226) Improve decorated private method check ([@&#8203;JLHwung](https://github.com/JLHwung))
-   `babel-plugin-proposal-decorators`, `babel-plugin-transform-async-generator-functions`, `babel-plugin-transform-runtime`, `babel-preset-env`
    -   [#&#8203;16224](babel/babel#16224) Properly sort `core-js@3` imports ([@&#8203;nicolo-ribaudo](https://github.com/nicolo-ribaudo))
-   `babel-traverse`
    -   [#&#8203;15383](babel/babel#15383) fix: Don't throw in `getTypeAnnotation` when using TS+inference ([@&#8203;liuxingbaoyu](https://github.com/liuxingbaoyu))
-   Other
    -   [#&#8203;16210](babel/babel#16210) \[eslint] Fix `no-use-before-define` for class ref in fields ([@&#8203;nicolo-ribaudo](https://github.com/nicolo-ribaudo))

##### 🏠 Internal

-   `babel-core`, `babel-parser`, `babel-template`
    -   [#&#8203;16222](babel/babel#16222) Migrate `eslint-parser` to cts ([@&#8203;liuxingbaoyu](https://github.com/liuxingbaoyu))
-   `babel-types`
    -   [#&#8203;16213](babel/babel#16213) Remove `@babel/types` props that are not produced by the parser ([@&#8203;liuxingbaoyu](https://github.com/liuxingbaoyu))

##### 🏃‍♀️ Performance

-   `babel-parser`
    -   [#&#8203;16072](babel/babel#16072) perf: Improve parser performance for typescript ([@&#8203;liuxingbaoyu](https://github.com/liuxingbaoyu))

##### 🔬 Output optimization

-   `babel-helper-create-class-features-plugin`, `babel-plugin-proposal-decorators`, `babel-plugin-proposal-destructuring-private`, `babel-plugin-proposal-pipeline-operator`, `babel-plugin-transform-class-properties`, `babel-plugin-transform-class-static-block`, `babel-plugin-transform-new-target`, `babel-plugin-transform-parameters`, `babel-plugin-transform-private-methods`, `babel-preset-env`
    -   [#&#8203;16218](babel/babel#16218) Improve temporary variables for decorators ([@&#8203;liuxingbaoyu](https://github.com/liuxingbaoyu))
-   `babel-helpers`, `babel-plugin-proposal-explicit-resource-management`, `babel-runtime-corejs2`, `babel-runtime-corejs3`, `babel-runtime`
    -   [#&#8203;15959](babel/babel#15959) Improve output of `using` ([@&#8203;liuxingbaoyu](https://github.com/liuxingbaoyu))

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4wLjAiLCJ1cGRhdGVkSW5WZXIiOiIzNy4wLjAiLCJ0YXJnZXRCcmFuY2giOiJkZXZlbG9wIn0=-->

Reviewed-on: https://gitea.vylpes.xyz/RabbitLabs/random-bunny/pulls/142
Reviewed-by: Vylpes <ethan@vylpes.com>
Co-authored-by: Renovate Bot <renovate@vylpes.com>
Co-committed-by: Renovate Bot <renovate@vylpes.com>
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Apr 16, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Output optimization 🔬 A type of pull request used for our changelog categories Spec: Explicit Resource Management
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants