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

[helpers TS conversion] object #16505

Merged
merged 13 commits into from
May 17, 2024
Merged

Conversation

SukkaW
Copy link
Contributor

@SukkaW SukkaW commented May 17, 2024

Q                       A
Fixed Issues? Part of #16500
Patch: Bug Fix? Yes
Major: Breaking Change? No
Minor: New Feature? No
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes? No
License MIT

Migrate all Object related Babel helpers (extends, defaults, objectWithoutProperties, objectSpread, objectSpread2) to TypeScript with reasonable types (and a little TypeScript exercise~). The PR is made in a way that prevents yarn gulp generate-runtime-helpers from introducing changes.

@babel-bot
Copy link
Collaborator

babel-bot commented May 17, 2024

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

Comment on lines +7 to +9
var keys: string[] = Object.getOwnPropertyNames(defaults), i = 0;
i < keys.length;
i++
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I hoist the keys to the scope of the _defaults, the generated size will be increased by 3 ~ 5 bytes.

Comment on lines +30 to +31
// @ts-expect-error -- intentionally omitting the argument
Object.assign.bind(/* undefined */)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If undefined is explicitly given, the generated size will increase by about 6 bytes. So I omit the undefined and add a ts-expect-error.

Comment on lines +3 to +7
export default function _objectDestructuringEmpty<T>(
obj: T | null | undefined,
): asserts obj is T {
if (obj == null) throw new TypeError("Cannot destructure " + obj);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

_objectDestructuringEmpty is basically a nullthrow with a custom error message, so asserts is used.

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.

This looks incredibly high quality, thank you! 😄

I left two minor comments about import paths, but it overall looks pretty good.

/* @onlyBabel7 */

// @ts-expect-error Migrate in another PR
import defineProperty from "defineProperty.ts";
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
import defineProperty from "defineProperty.ts";
import defineProperty from "./defineProperty.ts";

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, I missed that part! Fixing now.

@@ -1,26 +1,47 @@
/* @minVersion 7.5.0 */

// @ts-expect-error Migrate in another PR
import defineProperty from "defineProperty";
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
import defineProperty from "defineProperty";
import defineProperty from "./defineProperty.ts";

(this need to match the final path, so that TS will stop complaining once defineProperty is migrated and it will tell us to remove the @ts-expect-error)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And I missed this one as well, fixing.

Copy link
Member

@liuxingbaoyu liuxingbaoyu left a comment

Choose a reason for hiding this comment

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

Thanks! This is really great!

@SukkaW
Copy link
Contributor Author

SukkaW commented May 17, 2024

@nicolo-ribaudo @liuxingbaoyu I've applied the code review suggestions in 321f41a (#16505).

object: object,
enumerableOnly?: boolean | undefined,
): PropertyKey[] {
var keys: PropertyKey[] = Object.keys(object);
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be narrowed down to (string | symbol)[]. The PropertyKey is supposed to type the valid non-computed key syntax in the object literal, which also contains number, however, Object.keys() will always return string.

Copy link
Member

Choose a reason for hiding this comment

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

Actually, Object.keys only returns strings and not symbols. TypeScript's build-in definition of Object.keys is fine.

Copy link
Member

Choose a reason for hiding this comment

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

Oh I see, we later push symbols.

@nicolo-ribaudo nicolo-ribaudo changed the title Migrate Object helpers (#16500) [helpers TS conversion] object May 17, 2024
@@ -204,7 +204,7 @@ const helpers: Record<string, Helper> = {
// size: 252, gzip size: 188
extends: helper(
"7.0.0-beta.0",
"export default function _extends(){return _extends=Object.assign?Object.assign.bind():function(n){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var r in t)({}).hasOwnProperty.call(t,r)&&(n[r]=t[r])}return n},_extends.apply(this,arguments)}",
"export default function _extends(){return _extends=Object.assign?Object.assign.bind():function(n){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var r in t)({}).hasOwnProperty.call(t,r)&&(n[r]=t[r])}return n},_extends.apply(null,arguments)}",
Copy link
Member

Choose a reason for hiding this comment

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

I changed this to null so that we don't have to add a this: any parameter to the function, which doesn't actually use this.

@nicolo-ribaudo nicolo-ribaudo merged commit 740b7ce into babel:main May 17, 2024
51 checks passed
@nicolo-ribaudo
Copy link
Member

Thanks, and sorry for the many extra commits :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants