PrettyPrinter: added option `shortListSyntax`
To print [...] instead of list(...)
As this is a separate node, you can already control this by passing in either Array or List as the node type. I don't see a need for a pretty-printer option controlling this.
@nikic list is not array, like as attribute not comment(#). I think that such replacements are not the correct solution.
This is the current representation though:
bin/php-parse '<?php [$x] = $y;'
====> Code <?php [$x] = $y;
==> Node dump:
array(
0: Stmt_Expression(
expr: Expr_Assign(
var: Expr_Array(
items: array(
0: Expr_ArrayItem(
key: null
value: Expr_Variable(
name: x
)
byRef: false
unpack: false
)
)
)
expr: Expr_Variable(
name: y
)
)
)
)
[$x] will get represented as Expr_Array independently of whether it appears on the LHS or RHS. So at least as things are now, replacing the Expr_List node with an Expr_Array node would be the correct thing to do.
@nikic I'm understand you, but this is still not correct and may cause problems at printing
@nikic for example if shortArraySyntax is false, then <?php [$x] = $y; convert to invalid code <?php array($x) = $y; at printing
@nikic for example if
shortArraySyntaxis false, then<?php [$x] = $y;convert to invalid code<?php array($x) = $y;at printing
That's a good point. It may make sense to change the representation then (see also https://github.com/nikic/PHP-Parser/issues/471). A possibility is to invert things and always parse the LHS of assignments into a List rather than Array node, together with a flag that indicates whether list() or [] syntax was used.
@nikic
A possibility is to invert things and always parse the LHS of assignments into a List rather than Array node, together with a flag that indicates whether list() or [] syntax was used.
Yes, can you fix it? I not explored code of parsers yet. Let me know when it's ready so I can adjust my PR
With https://github.com/nikic/PHP-Parser/commit/68fc1ba4cb7b0aef9decc4e9d8f3b61eecdd3883 List is always used to represent destructuring and rendering is decided by kind attribute. The default is determined by phpVersion. I think that should address this use-case?
@nikic "short array destructuring" in PHP documentation called "symmetric array destructuring"
@nikic please, review/merge sended PR's before adding changes - resolve diff not so easy. i.e. you added changes similar to #872 instead to merge PR and after this, change method name.
As I have already explained, I have no interest in supporting a separate option for this. You can already use phpVersion to control the default behavior, and flags on individual nodes to control precise behavior. The shortArraySyntax option exists for historical reasons only.
@nikic i explain why it's not flexible in #890