Conversation
|
|
||
| function setOptionalTransformed(node) { | ||
| t.assertMemberExpression(node); // Dev | ||
| node._optionalTransformed = true; |
There was a problem hiding this comment.
I'd prefer to add nodes to a WeakSet instead of putting extra props in it
| @@ -0,0 +1,160 @@ | |||
| export default function ({ types: t }) { | |||
| const nilIdentifier = t.identifier("undefined"); | |||
There was a problem hiding this comment.
t.unaryExpression('void', t.numericLiteral(0)) would be safer here; avoids any potential trouble with shadowing.
There was a problem hiding this comment.
Yes, and will avoid a custom undefined value.
| ); | ||
|
|
||
| // FIXME(sven): if will be a ConditionalExpression for childs, only top level will be ifStatement | ||
| const remplacement = t.ifStatement(isChainNil, t.blockStatement([t.expressionStatement(path.node)])); |
| return; | ||
| } | ||
|
|
||
| if (!isNodeOptional(argument) || isOptionalTransformed(argument)) { |
There was a problem hiding this comment.
What happens on, say, delete a.b?.c? the argument is not directly optional.
| nilIdentifier, | ||
| ); | ||
|
|
||
| const remplacement = t.ifStatement(isChainNil, t.blockStatement([t.expressionStatement(path.node)])); |
| nilIdentifier, | ||
| ), | ||
| nilIdentifier, | ||
| ); |
There was a problem hiding this comment.
This looks like it will skip deleting keys that exist but are set to undefined.
var o = { foo: undefined }
assertTrue(Object.keys(o).includes('foo'))
delete o?.foo
assertFalse(Object.keys(o).includes('foo'))| state.optionalTemp, | ||
| object, | ||
| property, | ||
| t.callExpression(t.identifier("Function"), []), |
There was a problem hiding this comment.
Can you do this under CSP? Or does it only block invoking Function if it actually receives an argument?
There was a problem hiding this comment.
In the worst case it can be replaced with t.functionExpression(null, [], t.blockStatement([])). This also avoids risk of shadowing, though, at the cost of 2 extra bytes when minified.
|
Alright closing this in favor of #5813! |
Parser PR babel/babylon#545
Supported transformations:
TODO
optionalChainingBabylon plugin)delete