Conversation
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/11676/ |
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/11350/ |
| } | ||
| var tIsString = isString(t); | ||
| var tIsArray = Array.isArray(t); | ||
| if ( |
There was a problem hiding this comment.
I think this is too complicated for a proposal that doesn't have defined semantics yet.
There was a problem hiding this comment.
This notation can be used for slice operations on primitives like String and any object that provides indexed access using [[Get]] like Array and TypedArray.
It is used to limit the slicee to string, array and typed array. Surely we can debate about whether we should loose the control here and allow any fancy objects as long as they implement indexed access.
There was a problem hiding this comment.
That matches array-likes, too, eg { length: 1, 0: 'test' }. Let TypeScript handle type errors.
There was a problem hiding this comment.
That matches array-likes, too, eg { length: 1, 0: 'test' }.
Yes, array-like objects is supported.
const obj = { 0: 'a', 1: 'b', 2: 'c', 3: 'd', length: 4 };
obj[1:3];
// → ['b', 'c']But we should reject the following Proxy exotic object
new Proxy({} , {
get(target, prop) {
if (prop === "length") { return 1 }
if (prop == 0) { return prop }
return undefined
}
});There was a problem hiding this comment.
any object that provides indexed access using [[Get]]
I think that this means that proxies should be allowed. For example, when using a properly defined proxy, [].slice works:
var obj = new Proxy({} , {
get(target, prop) {
if (prop === "length") { return 1 }
if (prop == 0) { return 3 }
},
has(target, prop) {
return prop == 0;
}
});
[].slice.call(obj); // [3]cc @gsathya What should it do here?
|
|
||
| var o; | ||
| if (tIsString) { | ||
| o = ""; |
There was a problem hiding this comment.
Nit: We can use the array path for this, and join at the end.
There was a problem hiding this comment.
My major concern on implementation is memory footprint and performance. We could shorten the helper if we convert string to array, but that means given a string str with length N, str[::M] will require N byte memory while the optimal should be N/M.
| if ( | ||
| !expr || | ||
| expr.type === "NumericLiteral" || | ||
| expr.type === "Identifier" || |
There was a problem hiding this comment.
It seems super weird that Identifier is allowed, but normal expressions are not. 😞
There was a problem hiding this comment.
Actually I like this design choice. If nested general expression is supported, we can expect pretty nested code and one would easily get confused with ternary operator. Think of
a[
foo : (
get_bar_from_qux()
) : (
get_qux_from_quux()
)
]Surely we can open an issue on the proposal to discuss this choice.
| return this.finishNode(node, "MemberExpression"); | ||
| } | ||
|
|
||
| parseMaybeSliceExpression( |
There was a problem hiding this comment.
I like this way much better.
| @@ -0,0 +1 @@ | |||
| a[b:c] | |||
There was a problem hiding this comment.
Can you add more tests?
a[b:c];
a[b:c:d]
a[b:];
a[b::];
a[:c];
a[::d];
a[b::d];
a[:];
a[::];| expr.operator === "-" && | ||
| (expr.argument.type === "NumericLiteral" || | ||
| expr.argument.type === "Identifier")) | ||
| ) { |
There was a problem hiding this comment.
We also need to check that expr.extra && expr.extra.parenthesized is false.
There was a problem hiding this comment.
Correct, dogfooding another stage 3 proposal. 😝
bcad23f to
31bf50f
Compare
|
|
||
| parseSliceArgument(): ?N.Expression { | ||
| let expr = null; | ||
| this.eat(tt.colon); |
There was a problem hiding this comment.
Shouldn't this be this.expect, or be inside an if (this.eat(tt.colon))?
Also please add a test where this expectation is not respected, e.g. foo[1:2;3]
There was a problem hiding this comment.
Shouldn't this be this.expect
No. Because the colon step part is optional.
In the case of foo[1:2;3], parseExpression will throw unexpected token error since ; could not start an expression.
However, I think you idea of putting parseExpression inside tt.colon check is more reasonable. Once tt.colon is not matched, we should expect ] since it's the end of slice expression. I have added testcase for foo[1:2;3].
There was a problem hiding this comment.
In the case of
foo[1:2;3],parseExpressionwill throw unexpected token error since;could not start an expression.
What about foo[1:2 3]?
There was a problem hiding this comment.
It would throw "unexpected token, expected ]" now since parseExpression is not guarded by tt.colon check.
0bd7232 to
f5a378a
Compare
e18eab3 to
b2dc6b2
Compare
# Conflicts: # packages/babel-parser/test/fixtures/experimental/slice-notation/invalid-arg-bigint-literal/input.js # packages/babel-parser/test/fixtures/experimental/slice-notation/invalid-arg-bigint-literal/options.json # packages/babel-parser/test/fixtures/experimental/slice-notation/invalid-arg-binary-expression/input.js # packages/babel-parser/test/fixtures/experimental/slice-notation/invalid-arg-binary-expression/options.json # packages/babel-parser/test/fixtures/experimental/slice-notation/invalid-arg-unary-expression-plus/input.js # packages/babel-parser/test/fixtures/experimental/slice-notation/invalid-arg-unary-expression-plus/options.json
4ac40b2 to
ab61d86
Compare
|
Abandoning this PR due to #10596 (comment) |
This PR has been split into the following PRs:
They should now be reviewed individually instead of here.