Skip to content

Importing a class with \Trait\ in the namespace throws unexpected 'Trait' (T_TRAIT) #834

@robin-vendredi

Description

@robin-vendredi

I stumbled upon an issue while using @prettier/plugin-php and I believe it's a bug coming from the php-parser lib - please let me know if I'm mistaken. Thank you for your work maintaining php-parser.

What I did

Starting from a fresh install of 3.0.3, importing any namespace containing \Trait\ in the path results in a unexpected 'Trait' (T_TRAIT) error.

Code to reproduce:

var parser = require('php-parser');

var eval = parser.parseEval('use App\\Trait\\Foo;');

console.log( 'Eval parse:', eval );

throws

/code/phpparser/node_modules/php-parser/src/parser.js:345
    throw err;
    ^

SyntaxError: Parse Error : syntax error, unexpected 'Trait' (T_TRAIT), expecting ';' on line 1
    at Parser.raiseError (/code/phpparser/node_modules/php-parser/src/parser.js:337:17)
    at Parser.error (/code/phpparser/node_modules/php-parser/src/parser.js:384:15)
    at Parser.expect (/code/phpparser/node_modules/php-parser/src/parser.js:546:10)
    at Parser.read_use_statement (/code/phpparser/node_modules/php-parser/src/parser/namespace.js:127:10)
    at Parser.read_top_statement (/code/phpparser/node_modules/php-parser/src/parser/statement.js:53:21)
    at Parser.read_start (/code/phpparser/node_modules/php-parser/src/parser/main.js:18:19)
    at Parser.parse (/code/phpparser/node_modules/php-parser/src/parser.js:290:22)
    at Engine.parseEval (/code/phpparser/node_modules/php-parser/src/index.js:146:22)
    at Function.Engine.parseEval (/code/phpparser/node_modules/php-parser/src/index.js:134:15)
    at Object.<anonymous> (/code/phpparser/test.js:4:19) {
  lineNumber: 1,
  fileName: 'eval',
  columnNumber: 8
}

Changing the path to use App\\TraitX\\Foo; or anything else in the \Trait\ section of the namespace fixes the error.

What I expected

Being able to have Trait in my namespace without the parser throwing an error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions