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
|
||||
- [ ] Replace Composer/PSR-4 autoloading with Go imports
|
||||
[X] Variadic function parameters
|
||||
[ ] Preserve comments
|
||||
[X] Preserve comments
|
||||
[ ] Preserve rough line spacing
|
||||
[ ] Convert wordpress / mediawiki / symfony
|
||||
- [ ] Option to convert with preset=cli, preset=web, webroot path
|
||||
|
@ -14,6 +14,7 @@ class Bar {
|
||||
}
|
||||
|
||||
function scalarThrower() {
|
||||
// Comment
|
||||
throw "str";
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ for($i = 0; $i < 3; ++$i) {
|
||||
foreach($foo2 as $v2) {
|
||||
}
|
||||
|
||||
while(true) {
|
||||
while(true /* infinite. */) {
|
||||
}
|
||||
|
||||
do {
|
||||
|
3
main.go
3
main.go
@ -28,6 +28,9 @@ func ConvertFile(filename string) (string, error) {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Enable comments extraction
|
||||
p.WithFreeFloating()
|
||||
|
||||
p.Parse()
|
||||
for _, err := range p.GetErrors() {
|
||||
return "", errors.New(err.String())
|
||||
|
46
node.go
46
node.go
@ -8,6 +8,7 @@ import (
|
||||
//"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/z7zmey/php-parser/freefloating"
|
||||
"github.com/z7zmey/php-parser/node"
|
||||
"github.com/z7zmey/php-parser/node/expr"
|
||||
"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) {
|
||||
|
||||
//
|
||||
|
Loading…
Reference in New Issue
Block a user