forked from stefanpenner/es3-safe-recast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
128 lines (115 loc) · 2.66 KB
/
index.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
"use strict";
var recast = require('recast');
var esprima = require('esprima');
var Visitor = recast.Visitor;
var types = recast.types;
var namedTypes = types.namedTypes;
var builders = types.builders;
// http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%203rd%20edition,%20December%201999.pdf
var identifierToLiteral = {
// Keyword
break: true,
case: true,
catch: true,
continue: true,
default: true,
delete: true,
do: true,
else: true,
finally: true,
for: true,
function: true,
if: true,
in: true,
instanceof: true,
new: true,
return: true,
switch: true,
this: true,
throw: true,
try: true,
typeof: true,
var: true,
void: true,
while: true,
with: true,
// FutureReservedWords
abstract: true,
boolean: true,
byte: true,
char: true,
class: true,
const: true,
debugger: true,
double: true,
enum: true,
export: true,
extends: true,
final: true,
float: true,
goto: true,
implements: true,
import: true,
int: true,
interface: true,
long: true,
native: true,
package: true,
private: true,
protected: true,
public: true,
short: true,
static: true,
super: true,
synchronized: true,
throws: true,
transient: true,
volatile: true,
// NullLiteral
null: true,
// BooleanLiteral
true: true,
false: true
};
var ES6Safe = Visitor.extend({
visitProperty: function(node) {
if (namedTypes.Identifier.check(node.key) && identifierToLiteral[node.key.name]) {
node.key = builders.literal(node.key.name);
}
return this.genericVisit(node);
},
visitMemberExpression: function(node) {
var property = node.property;
var newNode;
if (namedTypes.Identifier.check(property) && identifierToLiteral[property.name]) {
newNode = builders.memberExpression(node.object, builders.literal(property.name), true);
} else {
newNode = node;
}
return this.genericVisit(newNode);
}
});
module.exports.Visitor = ES6Safe;
module.exports.compile = function(source) {
var ast, code;
if (TEST_REGEX.test(source)) {
ast = recast.parse(source, { esprima: esprima } );
new ES6Safe().visit(ast);
code = recast.print(ast).code;
} else {
code = source;
}
return code;
};
var TEST_REGEX = module.exports.TEST_REGEX = buildTestRegex();
function buildTestRegex() {
var literalsString = Object.keys(identifierToLiteral).join('|');
var memberString = '\\.\\s*(' + literalsString + ')';
var propertyString = '(' + literalsString + ')\\s*\\:';
var regexString = memberString + '|' + propertyString;
return new RegExp(regexString, 'i');
}
module.exports.visit = function(ast) {
new ES6Safe().visit(ast);
return ast;
};