Plugin-php: Prettier crashes on empty declare() or in global scope

Created on 13 Apr 2018  路  10Comments  路  Source: prettier/plugin-php

Hello.

Prettier crashes on declare() without { ... }.
php.net declare

<?php
// these are the same:

// you can use this:
declare(ticks=1) {
    // entire script here
}

// or you can use this:
declare(ticks=1);
// entire script here
?>

Input:

<?php
if ($var) {
    declare(ticks=1);
}

Error:

[error]     SyntaxError: Parse Error : syntax error, unexpected '}' on line 4
[error]     at parser.raiseError (/export/node_modules/php-parser/src/parser.js:290:17)
[error]     at parser.error (/export/node_modules/php-parser/src/parser.js:335:15)
[error]     at parser.read_expr_item (/export/node_modules/php-parser/src/parser/expr.js:420:12)
[error]     at parser.read_expr (/export/node_modules/php-parser/src/parser/expr.js:11:23)
[error]     at parser.read_statement (/export/node_modules/php-parser/src/parser/statement.js:386:21)
[error]     at parser.read_top_statement (/export/node_modules/php-parser/src/parser/statement.js:74:21)
[error]     at parser.read_statement (/export/node_modules/php-parser/src/parser/statement.js:339:28)
[error]     at parser.read_inner_statement (/export/node_modules/php-parser/src/parser/statement.js:186:21)
[error]     at parser.read_inner_statements (/export/node_modules/php-parser/src/parser/statement.js:86:30)
[error]     at parser.read_code_block (/export/node_modules/php-parser/src/parser/statement.js:401:14)

or empty

Input:

<?php
declare();

Error:

[error]     Error: Value undefined is not a valid document
[error]     at assertDoc (/export/node_modules/prettier/src/doc/doc-builders.js:8:11)
[error]     at Array.forEach (<anonymous>)
[error]     at concat (/export/node_modules/prettier/src/doc/doc-builders.js:16:11)
[error]     at printDeclareArguments (/export/node_modules/@prettier/plugin-php/src/printer.js:1298:16)
[error]     at printStatement (/export/node_modules/@prettier/plugin-php/src/printer.js:1322:9)
[error]     at printNode (/export/node_modules/@prettier/plugin-php/src/printer.js:1459:12)
[error]     at Object.genericPrint [as print] (/export/node_modules/@prettier/plugin-php/src/printer.js:69:19)
[error]     at genericPrint$6 (/usr/local/lib/node_modules/prettier/bin-prettier.js:23983:18)
[error]     at comments.printComments.p (/usr/local/lib/node_modules/prettier/bin-prettier.js:23921:14)
[error]     at Object.printComments (/usr/local/lib/node_modules/prettier/bin-prettier.js:8273:19)

Best regards,
Steve

Most helpful comment

@czosel @mgrip what about update parser and add tests ti ensure all good?

All 10 comments

@sseypt declare can be in if? i can't found information about.

@sseypt And can't be empty :confused:

I'm not that familiar with declare(), but it doesn't throw an error.

php -a
Interactive mode enabled

php > if (true) {declare(ticks=1);}
php > if (false) {declare(ticks=1);}
php >

You're right with empty declare() though.

php -a
Interactive mode enabled

php > declare();
PHP Parse error:  syntax error, unexpected ')', expecting identifier (T_STRING) in php shell code on line 1

Parse error: syntax error, unexpected ')', expecting identifier (T_STRING) in php shell code on line 1
php >

I'm sorry for digging out all these edge cases 馃檲

@sseypt Thank you so much for finding these edge cases, we really want to handle all of them 馃槈

I wasn't able to reproduce the issue with declare in global scope though, see #397.

@czosel The error occurs when used in if.

<?php
if ($var) {
    declare(ticks=1);
}

@sseypt Ah, in that case we're tracking it already. Thanks :slightly_smiling_face:

Someone can correct me if I'm wrong but I think this should be an issue on the parser repo right?

Looks as yes.

@mgrip It has actually been fixed in the parser already: https://github.com/glayzzle/php-parser/issues/151

@czosel @mgrip what about update parser and add tests ti ensure all good?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hackel picture hackel  路  3Comments

alexander-akait picture alexander-akait  路  3Comments

mgrip picture mgrip  路  3Comments

sgruetter picture sgruetter  路  5Comments

czosel picture czosel  路  4Comments