proposals icon indicating copy to clipboard operation
proposals copied to clipboard

[Feature request] Implement slice-notation

Open gengjiawen opened this issue 7 years ago • 9 comments
trafficstars

The proposal specific is at https://github.com/gsathya/proposal-slice-notation.

gengjiawen avatar Mar 15 '18 01:03 gengjiawen

This proposal is now stage 1 and we can expect an implementation in Babel soon.

xtuc avatar Mar 24 '18 15:03 xtuc

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]

caub avatar Nov 03 '18 10:11 caub

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

caub avatar Aug 17 '19 10:08 caub

Here's a (simplified) SliceExpression transform written with acorn

Awesome! I would take it as a reference to draft a babel plugin later.

JLHwung avatar Aug 17 '19 18:08 JLHwung

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

caub avatar Aug 17 '19 18:08 caub

@JLHwung r u implemented? How to configure babel to use it?

ghost avatar Sep 10 '20 00:09 ghost

@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 avatar Sep 10 '20 00:09 JLHwung

@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]) {}.

ghost avatar Sep 10 '20 00:09 ghost

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.

JLHwung avatar Sep 10 '20 16:09 JLHwung