-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
194 lines (169 loc) · 4.73 KB
/
main.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"path/filepath"
"regexp"
"strings"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
identifierRe := `[A-Za-z0-9_]+`
pathFlag := flag.String("path", "ERROR", "Path of the file to scan")
flag.Parse()
if *pathFlag == "ERROR" {
panic("Forgot --path <foo.js> ?")
}
// Make all paths absolute
path, err := filepath.Abs(*pathFlag)
check(err)
// fmt.Println("reading", path)
dat, err := ioutil.ReadFile(path)
check(err)
js := string(dat);
// Look for a class and the ctor params
re := regexp.MustCompile(`function (` + identifierRe + `)\((.*)\) \{`)
result := re.FindStringSubmatch(js);
clazz := ""
ctorParams := ""
if len(result) > 0 {
clazz = result[1];
ctorParams = result[2]
}
if clazz == "" {
// fmt.Printf("No class found")
fmt.Printf(js)
return
}
//fmt.Printf("class: %s\n", clazz)
//fmt.Printf("ctorParams: %s\n", ctorParams)
// is there a superclass?
re = regexp.MustCompile(`extends {(` + identifierRe + `)}`)
result = re.FindStringSubmatch(js)
sup := ""
if len(result) > 0 {
sup = result[1];
}
//fmt.Printf("superclass: %s\n", sup)
if sup == "" {
// replace "function clazz(ctorParams)" with
// class clazz {
// constructor(ctorParams)
js = strings.Replace(js,
"function " + clazz + "(" + ctorParams + ")",
"class " + clazz + " {\n" +
" constructor(" + ctorParams + ")",
1)
} else {
// take care of a bunch of superclass stuff...
// replace "function clazz(ctorParams)" with
// class clazz extends sup{
// constructor(ctorParams)
js = strings.Replace(js,
"function " + clazz + "(" + ctorParams + ")",
"class " + clazz + " extends " + sup + " {\n" +
" constructor(" + ctorParams + ")",
1)
// replace calls to superclass ctor
// first group is the set of params after "this"
re = regexp.MustCompile(sup + `\.call\(this\,?(.*)\);`)
result = re.FindStringSubmatch(js)
if len(result) > 0 {
args := strings.TrimSpace(result[1])
js = strings.Replace(js,
result[0],
"super(" + args + ");",
1)
}
// delete prototype pointer and constructor fix:
// Foo.prototype = new Bar();
// Foo.prototype.constructor = Foo;
re = regexp.MustCompile(clazz + `\.prototype *= +new ` + sup + `\(.*\)\;\n`)
js = re.ReplaceAllString(js, "")
re = regexp.MustCompile(clazz + `\.prototype.constructor +=.*;\n`)
js = re.ReplaceAllString(js, "")
// replace superclass method calls
// Bar.prototype.beep.call(this, superblah);
// ==>
// super.beep(superblah);
for {
re = regexp.MustCompile(sup + `\.prototype\.(` + identifierRe + `)\.call\(this\,?(.*)\);`)
result = re.FindStringSubmatch(js)
if len(result) > 0 {
meth := strings.TrimSpace(result[1])
args := strings.TrimSpace(result[2])
js = strings.Replace(js,
result[0],
"super." + meth + "(" + args + ");",
1)
} else {
break
}
}
}
// replace "Foo.prototype.bar = function(blah)"
// with "bar(blah)"
for {
re = regexp.MustCompile(clazz + `\.prototype\.(` + identifierRe + `) *= *function.*\((.*)\)`)
result = re.FindStringSubmatch(js)
if len(result) > 0 {
meth := strings.TrimSpace(result[1])
args := result[2]
js = strings.Replace(js,
result[0],
meth + "(" + args + ")",
1)
} else {
break
}
}
// replace "Foo.meth = function(params)"
// with "static meth(params)"
for {
re = regexp.MustCompile(`\n` + clazz + `\.(` + identifierRe + `) += +function\((.*)\)`)
result = re.FindStringSubmatch(js)
if len(result) > 0 {
meth := result[1]
args := result[2]
js = strings.Replace(js,
result[0],
"\nstatic " + meth + "(" + args + ")",
1)
} else {
break
}
}
// replace "Foo.SOMETHING"
// with "static SOMETHING"
for {
re = regexp.MustCompile(`\n` + clazz + `\.(` + identifierRe + `)`)
result = re.FindStringSubmatch(js)
if len(result) > 0 {
something := result[1]
js = strings.Replace(js,
result[0],
"\nstatic " + something,
1)
} else {
break
}
}
// Remove the semicolons. Some of these are wrong to remove, but most are right.
re = regexp.MustCompile(`\n}\;`)
js = re.ReplaceAllString(js, "\n}")
// Remove " * @constructor "
re = regexp.MustCompile(`\n.*\@constructor.*\n`)
js = re.ReplaceAllString(js, "\n")
// Remove " * @extends {...} "
re = regexp.MustCompile(`\n.*\@extends.*\n`)
js = re.ReplaceAllString(js, "\n")
// Remove empty JSDoc
js = strings.ReplaceAll(js, "/**\n */\n", "")
// ... and we're done!
fmt.Printf(js + "\n}\n") // close "class {"
}