chris-l / php-unparser Goto Github PK
View Code? Open in Web Editor NEWA JavaScript based code generator, to unparse glayzzle/php-parser compatible AST back to code.
Home Page: https://chris-l.github.io/php-unparser/
A JavaScript based code generator, to unparse glayzzle/php-parser compatible AST back to code.
Home Page: https://chris-l.github.io/php-unparser/
Hi @chris-l
I would like to use your unparser to generate php code for this project : https://github.com/ichiriac/pdoMap and I would like to also support comment blocks.
I've releases php-parser under 0.1.4 and it's now handling comment blocks :
https://github.com/glayzzle/php-parser/blob/master/src/parser/comment.js
But be aware, into the class body, comments decorate properties, constants, and methods : https://github.com/glayzzle/php-parser/blob/master/src/parser/class.js#L135
I use nodejs v6.1.0,The following error occurred
`C:\Users\Administrator\Desktop\php-test>node index.js
C:\Users\Administrator\Desktop\php-test\node_modules\php-unparser\node_translators\index.js:26
throw new Error(
^
Error: Unhandled node type [use]
at CodeGen.process (C:\Users\Administrator\Desktop\php-test\node_modules\php-unparser\node_translators\index.js:26:13)
at C:\Users\Administrator\Desktop\php-test\node_modules\php-unparser\node_translators\helper\body.js:15:30
at Array.map (native)
at module.exports (C:\Users\Administrator\Desktop\php-test\node_modules\php-unparser\node_translators\helper\body.js:11:14)
at CodeGen.module.exports [as program] (C:\Users\Administrator\Desktop\php-test\node_modules\php-unparser\node_translators\program.js:10:7)
at CodeGen.process (C:\Users\Administrator\Desktop\php-test\node_modules\php-unparser\node_translators\index.js:24:29)
at module.exports (C:\Users\Administrator\Desktop\php-test\node_modules\php-unparser\index.js:8:18)
at Object. (C:\Users\Administrator\Desktop\php-test\index.js:6:13)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
`
It looks like there are some breaking changes in the coming php-parser 3.0 that results in php-unparser being unable to use its output to unparse. Specifically I had problems with simple string arrays.
<?
return [
"xzczxc",
"qweqweqwe"
];
Node: xzczxc
Node kind: undefined
{ Error: Bad AST structure
at CodeGen.process (/home/vagrant/apps/translation-scripts/node_modules/php-unparser/node_translators/index.js:42:13)
at /home/vagrant/apps/translation-scripts/node_modules/php-unparser/node_translators/array.js:11:19
at Array.map (<anonymous>)
at CodeGen.module.exports [as array] (/home/vagrant/apps/translation-scripts/node_modules/php-unparser/node_translators/array.js:19:25)
at CodeGen.process (/home/vagrant/apps/translation-scripts/node_modules/php-unparser/node_translators/index.js:32:31)
at /home/vagrant/apps/translation-scripts/node_modules/php-unparser/node_translators/array.js:11:19
at Array.map (<anonymous>)
at CodeGen.module.exports [as array] (/home/vagrant/apps/translation-scripts/node_modules/php-unparser/node_translators/array.js:19:25)
at CodeGen.process (/home/vagrant/apps/translation-scripts/node_modules/php-unparser/node_translators/index.js:32:31)
at CodeGen.module.exports [as return] (/home/vagrant/apps/translation-scripts/node_modules/php-unparser/node_translators/return.js:12:22) node: 'xzczxc' }
According to ASTExplorer, https://astexplorer.net/#/gist/c5098fd3b19f3c0da9de29986aa553d9/578cf3e85d8b5e4aee923e3fa3a24e9de93def19 , the parser worked as expected.
Code here:
/*
aa
aa
aa
cc
*/
CodeGen.prototype.classreference = require("./classreference.js");
CodeGen.prototype.expressionstatement = require("./expressionstatement.js");
CodeGen.prototype.encapsedpart = require("./encapsedpart.js");
Hello!
First off, thank you for your work here. It's saved me many hours of figuring out how to do this myself ๐
I am trying to build a small program to generate PHPUnit tests based off of the class, right now the only thing really preventing me from implementing anything is having the leadingComments supported
"leadingComments": [
{
"kind": "commentblock",
"value": "/**\n * Class " + classObject.name + 'Test' + "\n * @package Tests\\Unit\\Console\\Commands\\ADFRequest\n */"
}
],
My company uses declare(strict_types=1);
and has a comment above it for a file comment and right now the unparser doesn't support any kind of leading comments.
Here is what the parser generates:
{
"kind": "program",
"children": [
{
"kind": "declare",
"leadingComments": [
{
"kind": "commentblock",
"value": "/**\n * Tests the class\n */"
}
],
"children": [],
"what": {
"strict_types": {
"kind": "number",
"value": "1"
}
},
"mode": "none"
},
// Other class item
]
}
And in PHP it would look like this:
<?php
/**
* Tests the class
*/
declare(strict_types=1);
Adding support for the leading comments would be deeply appreciated.
The example from the documentation throws this error:
var unparse = require('php-unparser');
var ast = {
"kind": "program",
"children": [
{
"kind": "echo",
"arguments": [
{
"kind": "string",
"value": "hello world",
"isDoubleQuote": true
}
]
}
],
"errors": []
};
console.log(unparse(ast));
Error: Bad AST structure, expects array
at CodeGen.process (C:\Users\winflop\Desktop\test\php-parse\node_modules\php-unparser\node_translators\index.js:34:11)
at module.exports (C:\Users\winflop\Desktop\test\php-parse\node_modules\php-unparser\index.js:14:18)
Hi @chris-l & wish you a happy new year,
I wish to implement the support of the new version of php-parser based on object nodes. There will be a lot of files changed (almost every file), so how would you like to proceed ?
I could open a new branch named version 0.1.5
for example ?
in NPM version 0.1.3, php-unparser/node_translators/index.js has a process.exit(), which is quite dangerous. Could you change it to exceptions as in github version?
As explained here : #12 (comment)
Tests are actually just testing that the library executes writers, but does not check output.
One thing could make the test more robust, I should do something like this :
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.