PHP-Parser icon indicating copy to clipboard operation
PHP-Parser copied to clipboard

PrettyPrinter: added option `shortListSyntax`

Open WinterSilence opened this issue 3 years ago • 7 comments

To print [...] instead of list(...)

WinterSilence avatar Jul 20 '22 16:07 WinterSilence

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 avatar Jul 23 '22 16:07 nikic

@nikic list is not array, like as attribute not comment(#). I think that such replacements are not the correct solution.

WinterSilence avatar Aug 07 '22 07:08 WinterSilence

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 avatar Aug 07 '22 15:08 nikic

@nikic I'm understand you, but this is still not correct and may cause problems at printing

WinterSilence avatar Aug 07 '22 15:08 WinterSilence

@nikic for example if shortArraySyntax is false, then <?php [$x] = $y; convert to invalid code <?php array($x) = $y; at printing

WinterSilence avatar Aug 07 '22 15:08 WinterSilence

@nikic for example if shortArraySyntax is 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 avatar Aug 07 '22 16:08 nikic

@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

WinterSilence avatar Aug 07 '22 17:08 WinterSilence

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 avatar Aug 28 '22 16:08 nikic

@nikic "short array destructuring" in PHP documentation called "symmetric array destructuring"

WinterSilence avatar Aug 28 '22 17:08 WinterSilence

@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.

WinterSilence avatar Aug 28 '22 17:08 WinterSilence

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 avatar Sep 10 '22 07:09 nikic

@nikic i explain why it's not flexible in #890

WinterSilence avatar Sep 10 '22 08:09 WinterSilence