1
0
mirror of https://github.com/robertkrimen/otto synced 2025-10-12 20:27:30 +08:00

fix(ast): walk missing nodes (#471)

Fix ast.Walk missing out:
* DotExpressions.Identifier
* LabelledStatement.Label

Fixes #299
This commit is contained in:
Steven Hartland 2022-11-29 11:09:56 +00:00 committed by GitHub
parent 8d121dcad1
commit 93fb47cccc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 22 deletions

View File

@ -93,6 +93,7 @@ func Walk(v Visitor, n Node) {
case *DotExpression:
if n != nil {
Walk(v, n.Left)
Walk(v, n.Identifier)
}
case *EmptyExpression:
case *EmptyStatement:
@ -134,6 +135,7 @@ func Walk(v Visitor, n Node) {
}
case *LabelledStatement:
if n != nil {
Walk(v, n.Label)
Walk(v, n.Statement)
}
case *NewExpression:

View File

@ -1,18 +1,20 @@
package ast_test
import (
"log"
"testing"
"github.com/robertkrimen/otto/ast"
"github.com/robertkrimen/otto/file"
"github.com/robertkrimen/otto/parser"
"github.com/stretchr/testify/require"
)
type walker struct {
stack []ast.Node
source string
shift file.Idx
stack []ast.Node
source string
shift file.Idx
seen map[ast.Node]struct{}
duplicate int
}
// push and pop below are to prove the symmetry of Enter/Exit calls
@ -38,18 +40,25 @@ func (w *walker) pop(n ast.Node) {
func (w *walker) Enter(n ast.Node) ast.Visitor {
w.push(n)
if _, ok := w.seen[n]; ok {
// Skip items we've already seen which occurs due to declarations.
w.duplicate++
return w
}
w.seen[n] = struct{}{}
if id, ok := n.(*ast.Identifier); ok && id != nil {
idx := n.Idx0() + w.shift - 1
s := w.source[:idx] + "new_" + w.source[idx:]
s := w.source[:idx] + "IDENT_" + w.source[idx:]
w.source = s
w.shift += 4
w.shift += 6
}
if v, ok := n.(*ast.VariableExpression); ok && v != nil {
idx := n.Idx0() + w.shift - 1
s := w.source[:idx] + "varnew_" + w.source[idx:]
s := w.source[:idx] + "VAR_" + w.source[idx:]
w.source = s
w.shift += 7
w.shift += 4
}
return w
@ -60,23 +69,27 @@ func (w *walker) Exit(n ast.Node) {
}
func TestVisitorRewrite(t *testing.T) {
source := `var b = function() {test(); try {} catch(e) {} var test = "test(); var test = 1"} // test`
source := `var b = function() {
test();
try {} catch(e) {}
var test = "test(); var test = 1"
} // test`
program, err := parser.ParseFile(nil, "", source, 0)
if err != nil {
log.Fatal(err)
require.NoError(t, err)
w := &walker{
source: source,
seen: make(map[ast.Node]struct{}),
}
w := &walker{source: source}
ast.Walk(w, program)
xformed := `var varnew_b = function() {new_test(); try {} catch(new_e) {} var varnew_test = "test(); var test = 1"} // test`
xformed := `var VAR_b = function() {
IDENT_test();
try {} catch(IDENT_e) {}
var VAR_test = "test(); var test = 1"
} // test`
if w.source != xformed {
t.Errorf("source is `%s` not `%s`", w.source, xformed)
}
if len(w.stack) != 0 {
t.Errorf("stack should be empty, but is length: %d", len(w.stack))
}
require.Equal(t, xformed, w.source)
require.Len(t, w.stack, 0)
require.Equal(t, w.duplicate, 0)
}