Skip to content

[helpers ts conversion] Arrays#16518

Merged
nicolo-ribaudo merged 25 commits intobabel:mainfrom
blakewilson:array-types
May 22, 2024
Merged

[helpers ts conversion] Arrays#16518
nicolo-ribaudo merged 25 commits intobabel:mainfrom
blakewilson:array-types

Conversation

@blakewilson
Copy link
Copy Markdown
Contributor

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

Adds proper types for Array helper methods. Additional comments are provided as inline comments.

@babel-bot
Copy link
Copy Markdown
Collaborator

babel-bot commented May 20, 2024

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

@blakewilson blakewilson marked this pull request as ready for review May 20, 2024 15:01
@blakewilson
Copy link
Copy Markdown
Contributor Author

There are some tests failing from the recent suggestions/changes. I'll get those fixed!

@nicolo-ribaudo
Copy link
Copy Markdown
Member

Oh well, it looks like one of my suggestions was wrong if it changes tests behavior 😅

@blakewilson
Copy link
Copy Markdown
Contributor Author

blakewilson commented May 20, 2024

Oh well, it looks like one of my suggestions was wrong if it changes tests behavior 😅

@nicolo-ribaudo Yeah I thought it was only an invalid snapshot but it looks like actual tests were failing. I've reverted. Looks like everything is green now.

/* @minVersion 7.0.0-beta.0 */

export default function _iterableToArrayLimitLoose<T>(
arr: Array<T>,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
arr: Array<T>,
arr: Iterable<T>,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ahh, right. Now I recall why I did this. It's because we are accessing the .length property below on arr, which only exists on Array. See: #16518 (comment)

var _arr: T[] = [];
var step;
iterator = iterator.call(arr);
while (arr.length < i && !(step = iterator.next()).done) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The function argument arr accepts Array<T> here as we are accessing the .length property, which doesn't exist on normal Iterators.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I suppose this would include String as well. I'll update the arr arg to accept Array<T> | String

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ughhh I'm quite confident that this is a bug, and we should be using _arr instead of arr. Can you mark arr as Iterable<T>, and add a @ts-expect-error here? I'll open an issue to track it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Oh whaat, this helper (and slicedToArrayLoose) have never been used. They have been introduced in ae7d536, but there has never been any code actually loading them.

It doesn't matter how they are converted to TS at this point -- could you instead open a separate PR to delete them? 😅

Copy link
Copy Markdown
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.

iterableToArrayLimitLoose.ts needs to be removed in a separate PR. Its types are a bit wrong, but so is them implementation so it's fine.

Copy link
Copy Markdown
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!

@nicolo-ribaudo nicolo-ribaudo merged commit 960dacf into babel:main May 22, 2024
@blakewilson blakewilson deleted the array-types branch May 22, 2024 14:38
@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 Aug 22, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 22, 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants