-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathes6arrow.js
executable file
·183 lines (162 loc) · 8.22 KB
/
es6arrow.js
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
/**
* This is a small Javascript parser that replaces ES6 arrow functions and ES6-style methods
* with the corresponding ES5 compatible code.
* Note: This might not always work, it has been tested with only the .tag files in this project.
*/
(function () {
var pairs = { '{':'}', '(':')', '[':']', '"':'"', "'":"'" };
// matches "(param1, param2, ..) => " or "param => "
var arrowRE = /(\(\s*\w*(?:\s*,\s*\w+)*\s*\)|\w+)\s*=>\s*/gim;
// matches "<8char_word> <func_name> (params) {"
// Why 8? We need to find out if there's a "function" befor the method name
// ("function".length === 8)
var methodRE = /((\w{8})?\s+)(\w+)\s*(\(\s*\w*(?:\s*,\s*\w+)*\s*\))\s*\{/gim;
// if we find one of these without their opening counterparts we probably hit
// the next character after the end of the expression.
var expressionSeparators = ',;})]';
/**
* Finds the closing pair of the opening character at position 'pos' in the string 'code'.
* Returns the index or -1 if it's not found.
* Correctly matches parenthesis pairs and skips characters inside a string/regex.
*/
function findMatch(code, pos) {
var stack = []; // the stack of closing parenthesis to match in the remaining string
var inString = false; // contains the quote character if we're inside a quoted string
do {
var c = code.charAt(pos++);
if (inString) { // we're inside a string
if (c === '\\') pos++; else // skip escaped chars
if (c === inString) inString = false; // found the end of the string
} else {
if (c === stack[stack.length-1]) { // found a matching close paren.
stack.pop();
} else if (c === '"' || c === "'") { // a string starts here
inString = c;
} else if (pairs[c]) { // found an open parenthesis
stack.push(pairs[c]);
} else
// could be a regex
// make sure it's not a line comment: "//"
if (c === '/' && code.charAt(pos) !== '/' && code.charAt(pos-2) !== '/') {
for (var end = pos+1; end < code.length; end++) {
var c2 = code.charAt(end);
if (c2 === '\r' || c2 === '\n') break; // can't be regex
// ending "/" of the regex, if it's not escaped
if (c2 === '/' && code.charAt(end-1) !== '\\') {
try {
// let's try to compile the regex
var rx = new RegExp(code.substring(pos, end));
// no errors, we can skip the whole block
pos = end+1;
break;
} catch (err) {}
}
}
}
}
// exit when the stack is empty and we're not in a string => found the matching pair
// or when we run out of characters
} while (pos < code.length && (inString || stack.length > 0));
return inString === false && stack.length === 0 ? pos-1 : -1;
}
function processJScode(js, url) {
var finalEdits = [];
var match;
console.log('[es6arrow] Processing ' + url);
/* Find ES6-style method blocks
* <pre><code>
* replace this:
* myMethod(param1, param1) {
* doStuff();
* }
* </code></pre>
* with this:
* <pre><code>
* this.myMethod=(function(param1, param2) {
* doStuff();
* }).bind(this);
* </code></pre>
*/
while ((match = methodRE.exec(js))) {
if (match[2] !== 'function' && !/if|while|for|switch|catch|function/.test(match[3])) {
// methodRE matches the opening "{", we find the closing one now
var endPos = findMatch(js, match.index + match[0].length -1);
if (endPos >= 0) {
var namePos = match.index + match[1].length; // function name start pos
finalEdits.push(
{ ins: 'this.', idx: namePos },
{ ins: '=(function', idx: namePos + match[3].length /* function name end pos */ },
{ ins: ').bind(this);', idx: endPos+1 /* function block end pos */ }
);
} else {
console.log('[es6arrow] Potential syntax error at pos ' + namePos + ' around "'+match[3]+'"');
}
}
}
// Find ES6 arrow functions and replace them with ES5 compatible code
while ((match = arrowRE.exec(js))) {
var edits = [];
var params = match[1];
var paramsEndPos = match.index + match[1].length;
var valuePos = match.index + match[0].length;
if (params.charAt(0) === '(') {
params = params.substr(1, params.length-2);
edits.push({ idx: match.index, ins: '(function' }, { idx: paramsEndPos, skip:valuePos-paramsEndPos });
} else {
edits.push({ idx: match.index, ins: '(function(' }, { idx: paramsEndPos, skip:valuePos-paramsEndPos, ins: ')' });
}
params = !params ? [] : params.split(',').map(function (s) { return s.trim(); });
var ok = false;
var valueEnd;
if (js.charAt(valuePos) === '{') {
valueEnd = findMatch(js, valuePos);
if (valueEnd >= 0) {
edits.push({ idx: valueEnd + 1, ins: ').bind(this)' });
ok = true;
}
} else {
for (valueEnd = valuePos; valueEnd < js.length; valueEnd++) {
var c = js.charAt(valueEnd);
if (expressionSeparators.indexOf(c) >= 0) {
ok = true;
//try to "compile"
try {
Function.apply(undefined, params.concat('return ' + js.substring(valuePos, valueEnd)));
} catch (err) {
ok = false;
}
if (ok) break;
} else if (pairs[c]) {
valueEnd = findMatch(js, valueEnd);
if (valueEnd === -1) break; // ok === false
}
}
if (ok) {
// "(<params>) => <some_expression>" -> "function(<params>){return <some_expression>}.bind(this)"
edits.push({ idx: valuePos, ins: '{return '}, { idx: valueEnd, ins: ';}).bind(this)' });
}
}
if (ok) finalEdits.push.apply(finalEdits, edits); else console.log('[es6arrow] Lambda replacement failed: ', match[0], ' at ', match.index, ' in ', url);
}
if (finalEdits.length > 0) {
finalEdits.sort(function (a, b) { return a.idx - b.idx; });
var processedOffset = 0;
var results = [];
finalEdits.forEach(function (edit) {
results.push(js.substring(processedOffset, edit.idx));
processedOffset = edit.idx + (edit.skip || 0);
if (edit.ins) results.push(edit.ins);
});
results.push(js.substr(processedOffset));
return results.join('');
} else {
return js;
}
}
var processor = function (code, options, url) {
var code2 = processJScode(code, url);
return code2;
};
if (typeof module === 'object') module.exports = processor;
if (typeof riot === 'object') riot.parsers.js.es6arrow = processor;
})();