Open gengjiawen opened 6 years ago
This proposal is now stage 1 and we can expect an implementation in Babel soon.
The main proposal is simple to implement
I'll try to also implement partly a Range literal https://github.com/tc39/proposal-slice-notation/issues/19#issuecomment-421453934 this is in my opinion the second big benefit in this proposal, to replace things like Array.from({length: n}, (_, i) => i)
with [...0:n]
Here's a (simplified) SliceExpression transform written with acorn https://github.com/caub/jellobot/blob/slice-expression/src/plugins/js-eval/processSliceExpression.js
I'm sure anyone familiar with babel can port it
Here's a (simplified) SliceExpression transform written with acorn
Awesome! I would take it as a reference to draft a babel plugin later.
Thanks @JLHwung, if you're experienced with babel, it seems so :), that's great news, looking forward to see and learn how it'd be done with babel
keep in mind this proposal is quite early and unfinished, what I did is a simplified (and also 'augmented' with the way SliceExpression are handled out of property accessors as an iterator, you're free to not do that part at all)
If we want to better handle arrays, strings and array-like objects, with also the step argument, we could look into https://github.com/tc39/proposal-slice-notation/issues/1, as we will have to touch to Object.prototype
@JLHwung r u implemented? How to configure babel to use it?
@xxxzsx It's not yet ready and I don't expect it will be ready soon. The proposal has seen strong push back on the July meeting. Before the proposal gains stronger motivation support, it will not be prioritized in Babel.
@JLHwung can you make a simple plugin personally for me based on the work you’ve already done, absolutely not necessary if it doesn’t meet specs, preferably with third argument support [::x]
? I'll be very grateful.
And it would be super cool if separately, without an array, this construction would turn into a generator, like in other languages. Use case: for (const x of [5:7]) {}
.
And it would be super cool if separately, without an array, this construction would turn into a generator, like in other languages.
It is not covered in slice notation proposal but Number.range: https://github.com/tc39/proposal-Number.range
third argument support
[::x]
The third argument step
is removed in current proposal. I don't think I will work on that in the near future. You can fork @babel/parser
and experiment with that based on the work I have done. If you can then provide more feedback to the proposal, that will be great.
The proposal specific is at https://github.com/gsathya/proposal-slice-notation.