Last active
December 26, 2017 21:21
-
-
Save ereyes01/f9dc3335e553221c07b53d59b1568b01 to your computer and use it in GitHub Desktop.
Testing ES6 and Otto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"os" | |
"path/filepath" | |
"strings" | |
"github.com/PuerkitoBio/goquery" | |
"github.com/robertkrimen/otto/ast" | |
"github.com/robertkrimen/otto/parser" | |
) | |
var ( | |
filename string | |
dirname string | |
) | |
func init() { | |
flag.StringVar(&filename, "src", "", "path to source file") | |
flag.StringVar(&dirname, "dir", "", "path to directory of source files") | |
} | |
func dirSrcBlocks(dirname string) []string { | |
var blocks []string | |
err := filepath.Walk(dirname, func(path string, info os.FileInfo, err error) error { | |
if err != nil { | |
return err | |
} | |
if info.IsDir() { | |
return nil | |
} | |
if !strings.HasSuffix(path, ".vue") && !strings.HasSuffix(path, ".html") && !strings.HasSuffix(path, ".js") { | |
return nil | |
} | |
fileBlocks := getSrcBlocks(path) | |
blocks = append(blocks, fileBlocks...) | |
return nil | |
}) | |
if err != nil { | |
log.Fatalln("walk dir:", err) | |
} | |
return blocks | |
} | |
func getSrcBlocks(filename string) []string { | |
if !strings.HasSuffix(filename, ".vue") && !strings.HasSuffix(filename, ".html") { | |
sourceBytes, err := ioutil.ReadFile(filename) | |
if err != nil { | |
log.Fatalln("read JS source file:", err) | |
} | |
return []string{string(sourceBytes)} | |
} | |
file, err := os.Open(filename) | |
if err != nil { | |
log.Fatalln("open html/vue file:", err) | |
} | |
defer file.Close() | |
doc, err := goquery.NewDocumentFromReader(file) | |
if err != nil { | |
log.Fatalln("parse html:", err) | |
} | |
var blocks []string | |
doc.Find("script").EachWithBreak(func(i int, s *goquery.Selection) bool { | |
blocks = append(blocks, s.Text()) | |
return true | |
}) | |
return blocks | |
} | |
type walker []ast.Node // stack of nodes | |
func (w *walker) push(n ast.Node) { | |
(*w) = append((*w), n) | |
fmt.Println("ENTER:", n) | |
} | |
func (w *walker) pop(n ast.Node) { | |
size := len(*w) | |
if size <= 0 { | |
panic("pop of empty stack") | |
} | |
if (*w)[size-1] != n { | |
panic("pop of unexpected node, doesn't match current top of stack") | |
} | |
fmt.Println("LEAVE:", (*w)[size-1]) | |
(*w)[size-1] = nil | |
*w = (*w)[:size-1] | |
} | |
func (w *walker) Enter(n ast.Node) ast.Visitor { | |
w.push(n) | |
// TODO: print new ES6 nodes as we pass by them | |
return w | |
} | |
func (w *walker) Exit(n ast.Node) { | |
w.pop(n) | |
} | |
func main() { | |
flag.Parse() | |
var blocks []string | |
if filename != "" { | |
blocks = getSrcBlocks(filename) | |
} else if dirname != "" { | |
blocks = dirSrcBlocks(dirname) | |
} else { | |
log.Fatalln("Must supply one of ``-src'' or ``-dir''") | |
} | |
for _, block := range blocks { | |
program, err := parser.ParseFile(nil, filename, block, parser.IgnoreRegExpErrors) | |
if err != nil { | |
log.Fatalln("parse source block:", err) | |
} | |
ast.Walk(new(walker), program) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment