node: preserve comments
This commit is contained in:
parent
2568412a50
commit
49241e7a8c
@ -76,7 +76,7 @@ The goal is to produce idiomatic, maintainable Go code as part of a one-off conv
|
|||||||
- [ ] PHPUnit -> Go Test
|
- [ ] PHPUnit -> Go Test
|
||||||
- [ ] Replace Composer/PSR-4 autoloading with Go imports
|
- [ ] Replace Composer/PSR-4 autoloading with Go imports
|
||||||
[X] Variadic function parameters
|
[X] Variadic function parameters
|
||||||
[ ] Preserve comments
|
[X] Preserve comments
|
||||||
[ ] Preserve rough line spacing
|
[ ] Preserve rough line spacing
|
||||||
[ ] Convert wordpress / mediawiki / symfony
|
[ ] Convert wordpress / mediawiki / symfony
|
||||||
- [ ] Option to convert with preset=cli, preset=web, webroot path
|
- [ ] Option to convert with preset=cli, preset=web, webroot path
|
||||||
|
@ -14,6 +14,7 @@ class Bar {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function scalarThrower() {
|
function scalarThrower() {
|
||||||
|
// Comment
|
||||||
throw "str";
|
throw "str";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ for($i = 0; $i < 3; ++$i) {
|
|||||||
foreach($foo2 as $v2) {
|
foreach($foo2 as $v2) {
|
||||||
}
|
}
|
||||||
|
|
||||||
while(true) {
|
while(true /* infinite. */) {
|
||||||
}
|
}
|
||||||
|
|
||||||
do {
|
do {
|
||||||
|
3
main.go
3
main.go
@ -28,6 +28,9 @@ func ConvertFile(filename string) (string, error) {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Enable comments extraction
|
||||||
|
p.WithFreeFloating()
|
||||||
|
|
||||||
p.Parse()
|
p.Parse()
|
||||||
for _, err := range p.GetErrors() {
|
for _, err := range p.GetErrors() {
|
||||||
return "", errors.New(err.String())
|
return "", errors.New(err.String())
|
||||||
|
46
node.go
46
node.go
@ -8,6 +8,7 @@ import (
|
|||||||
//"strconv"
|
//"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/z7zmey/php-parser/freefloating"
|
||||||
"github.com/z7zmey/php-parser/node"
|
"github.com/z7zmey/php-parser/node"
|
||||||
"github.com/z7zmey/php-parser/node/expr"
|
"github.com/z7zmey/php-parser/node/expr"
|
||||||
"github.com/z7zmey/php-parser/node/expr/assign"
|
"github.com/z7zmey/php-parser/node/expr/assign"
|
||||||
@ -43,7 +44,50 @@ type conversionState struct {
|
|||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
func (this *conversionState) convert(n_ node.Node) (string, error) {
|
func (this *conversionState) convert(n node.Node) (string, error) {
|
||||||
|
|
||||||
|
// Get any whitespace/comments attached to this node
|
||||||
|
freePrefix := ""
|
||||||
|
freeSuffix := ""
|
||||||
|
|
||||||
|
if ff := n.GetFreeFloating(); ff != nil && !ff.IsEmpty() {
|
||||||
|
for positionType, elements := range *ff {
|
||||||
|
element:
|
||||||
|
for _, element := range elements {
|
||||||
|
if element.StringType == freefloating.TokenType {
|
||||||
|
// Skip <?php
|
||||||
|
continue element
|
||||||
|
}
|
||||||
|
if element.StringType == freefloating.WhiteSpaceType {
|
||||||
|
// We can't insert arbitrary whitespace
|
||||||
|
// TODO the number of newlines would be fine ONLY IF this is a *stmt
|
||||||
|
continue element
|
||||||
|
}
|
||||||
|
|
||||||
|
switch positionType {
|
||||||
|
default:
|
||||||
|
fallthrough
|
||||||
|
case freefloating.Start:
|
||||||
|
freePrefix += element.Value
|
||||||
|
|
||||||
|
case freefloating.End, freefloating.AltEnd:
|
||||||
|
freeSuffix += element.Value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert the node itself
|
||||||
|
ret, err := this.convertNoFreeFloating(n)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return freePrefix + ret + freeSuffix, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *conversionState) convertNoFreeFloating(n_ node.Node) (string, error) {
|
||||||
|
|
||||||
switch n := n_.(type) {
|
switch n := n_.(type) {
|
||||||
|
|
||||||
//
|
//
|
||||||
|
Loading…
x
Reference in New Issue
Block a user