-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathjsx.go
More file actions
41 lines (34 loc) · 1.06 KB
/
jsx.go
File metadata and controls
41 lines (34 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Jsx transpiles React JSX code to Javascript.
//
// This package also provides primitives to write your own visitors
package jsx
import (
"fmt"
"github.com/robertkrimen/otto/ast"
"github.com/robertkrimen/otto/file"
"github.com/robertkrimen/otto/parser"
)
// File takes the name of a JSX file as input and returns the Javascript version of the content.
func File(filename string) (string, error) {
fset := new(file.FileSet)
program, err := parser.ParseFile(fset, filename, nil, 0)
return eval(fset, program, err)
}
// String takes JSX source code as input and returns Javascript.
func String(src string) (string, error) {
fset := new(file.FileSet)
program, err := parser.ParseFile(fset, "", src, 0)
return eval(fset, program, err)
}
func eval(fset *file.FileSet, program *ast.Program, err error) (string, error) {
if err == nil {
return program.File.Source(), nil
}
errList, ok := err.(parser.ErrorList)
if !ok {
return "", fmt.Errorf("unexpected error: %v", err)
}
r := NewReact(fset, errList, program.File)
ast.Walk(r, program)
return r.String(), nil
}