php-parser
php-parser copied to clipboard
AST type inconsistencies
I encountered the following source discrepancies while moving my AST->PHP writer from any types to strict typing. They are tolerable in JavaScript but cause problems in a type safe TypeScript:
continueandbreakaren't consistent in theirlevel, one uses a plainnumberwhile the other aNumberextended fromNode.emptyextendsExpressionand uses anexpressionproperty but it isn't actually defined as such (empty.jsfails to mention the property, consequently, it isn't generated intotypes.d.ts).issetis the same with thevariablesproperty,printis the same with theexpressionproperty,unsetis the same with thevariablesproperty.namedargumentis lowercase, all other node types have PascalCase names.Stringnormally contains the whole string, with the appropriate quotes, as dictated byisDoubleQuote. However,EncapsedPartcan also contain aStringfragment and that has no quotes of its own. Although I can work around this, the perfectly correct solution would require either anisQuotedproperty forStringor a new node type likeEncapsedStringto be used just here.- Repeated case labels are stored rather problematically, they are empty
Casebranches with no relation to the actual branch they belong to. They should be consolidated into a singleCaseinstead, with atestproperty ofExpression[]instead of a single expression. PropertyandConstantboth are supposed to have anameof simple typestring. In reality, they receive anIdentifier.UseGroupdefines anitemproperty but actually populates one nameditems.- The
typefield ofFunctioncan benull, too.
And something not actually an error but a very needed, very small addition: Node should have a parent property and it should be populated when the AST is generated. It's of paramount importance for any tree manipulation. I can also work around this by using my additional union type and populating it during my own traversal but it would be a very cheap addition to the core.