-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreact.go
195 lines (185 loc) · 4.77 KB
/
react.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package jsx
import (
"bytes"
"fmt"
"strings"
"github.com/robertkrimen/otto/ast"
"github.com/robertkrimen/otto/file"
"github.com/robertkrimen/otto/parser"
)
// React implements Visitor.
// Should be called with Walk(NewReact(...), node).
type React struct {
fset *file.FileSet
errList parser.ErrorList
file *file.File
result bytes.Buffer
last file.Idx
}
// NewReact returns a ready-to-use React object.
func NewReact(fset *file.FileSet, errList parser.ErrorList, file *file.File) *React {
return &React{fset: fset, errList: errList, file: file}
}
// String returns the Javascript version of what the React visitor processed so far.
func (v *React) String() string {
return v.result.String()
}
// TODO: refactor this and expose only something like the current `v.str()` to spare
// others from reimplementing the whole visitor when all they need is to provide a
// string given an *ElementNode.
func (v *React) Enter(node ast.Node) ast.Visitor {
switch n := node.(type) {
case *ast.Program:
for _, stmt := range n.Body {
ast.Walk(v, stmt)
}
src := v.file.Source()
// print the rest
if int(v.last) < len(src) {
v.result.WriteString(src[v.last-1:])
}
return nil
case *ast.BadExpression:
for _, err := range v.errList {
pos := v.fset.Position(n.From)
// This is the hack to "identify" JSX code within Javascript.
if pos.Column == err.Position.Column-1 && pos.Line == err.Position.Line && strings.Contains(err.Message, "Unexpected token <") {
src := v.file.Source()
// Print everything from last time up until `<`, not included.
v.result.WriteString(src[v.last : n.From-1])
p := Parser{lexer: lex(src[n.From-1 : n.To-1])}
if err := p.Parse(); err != nil {
panic(err)
}
// Print the JSX code
v.result.WriteString(v.str(p.root))
v.last = n.From + file.Idx(p.lastPos)
return v
}
}
case *ast.AssignExpression:
i, ok := n.Left.(*ast.Identifier)
if !ok {
break
}
v.ensureDisplayName(i.Name, n.Right)
case *ast.VariableExpression:
v.ensureDisplayName(n.Name, n.Initializer)
}
return v
}
func (r *React) str(n *ElementNode) string {
var buf bytes.Buffer
buf.WriteString("React.createElement(")
if n.Name[0] >= 'a' && n.Name[0] <= 'z' {
buf.WriteByte('"')
buf.WriteString(n.Name)
buf.WriteByte('"')
} else if n.Name[0] >= 'A' && n.Name[0] <= 'Z' {
buf.WriteString(n.Name)
} else {
panic("unexpected name: " + n.Name)
}
buf.WriteString(", ")
if len(n.SpreadAttrs) > 0 {
buf.WriteString("React.__spread({}, ")
for _, attr := range n.SpreadAttrs {
buf.WriteString(string(attr))
buf.WriteString(", ")
}
if len(n.Attrs) > 0 {
buf.WriteString("{")
for k, v := range n.Attrs {
buf.WriteString(k)
switch v.Typ {
case JsAttr:
buf.WriteString(": ")
buf.WriteString(v.Payload)
case HtmlAttr:
buf.WriteString(`:"`)
buf.WriteString(v.Payload)
buf.WriteString(`", `)
default:
panic(fmt.Errorf("unexpected type for %q: %v", v.Payload, v.Typ))
}
}
buf.WriteString("}")
}
buf.WriteString(")")
} else {
if len(n.Attrs) == 0 {
buf.WriteString("null")
} else {
buf.WriteString("{")
for k, v := range n.Attrs {
buf.WriteString(k)
buf.WriteString(": ")
switch v.Typ {
case JsAttr:
buf.WriteString(v.Payload)
case HtmlAttr:
buf.WriteString(`"`)
buf.WriteString(v.Payload)
buf.WriteString(`", `)
default:
panic("unexpected attr value type: " + v.Payload)
}
}
buf.WriteString("}")
}
}
if len(n.Children) > 0 {
buf.WriteString(", ")
for _, child := range n.Children {
switch x := child.(type) {
case *ElementNode:
buf.WriteString(r.str(x))
case TextNode:
buf.WriteString(`"`)
buf.WriteString(string(x))
buf.WriteString(`"`)
case JsNode:
y, err := String(string(x))
if err != nil {
panic(err)
}
buf.WriteString(y)
}
buf.WriteString(", ")
}
buf.Truncate(buf.Len() - 2) // to remove the last `, `
}
buf.WriteString(")")
return buf.String()
}
// Ensures that if `init` is a React.createClass() call, its first argument has a `displayName` key.
// If not, it will add it and set its value to `name`.
func (r *React) ensureDisplayName(name string, init ast.Expression) {
c, ok := init.(*ast.CallExpression)
if !ok {
return
}
d, ok := c.Callee.(*ast.DotExpression)
if !ok || d.Identifier.Name != "createClass" {
return
}
i, ok := d.Left.(*ast.Identifier)
if !ok || i.Name != "React" {
return
}
o, ok := c.ArgumentList[0].(*ast.ObjectLiteral)
if !ok {
return
}
for _, val := range o.Value {
if val.Key == "displayName" {
return
}
}
s := r.file.Source()[r.last:o.LeftBrace]
r.result.WriteString(s)
r.last += file.Idx(len(s))
r.result.WriteString(`displayName: "` + name + `",`)
}
func (v *React) Exit(node ast.Node) {
}