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 | 1
1
1
1
1
15
1
1
1
2
3
2
1
16
16
16
16
2
16
16
16
16
16
6
6
6
16
13
13
1
16
16
16
13
13
13
11
6
5
1
5
1
4
13
3
3
1
5
4
4
1
1
1
13
13
96
5
91
13
2
11
78
7
7
5
2
2
71
1
13
13
1
5
5
5
1
1
| var traverse = require("../traverse");
var util = require("../util");
var b = require("ast-types").builders;
var _ = require("lodash");
exports.ClassDeclaration = function (node) {
return b.variableDeclaration("var", [
b.variableDeclarator(node.id, buildClass(node))
]);
};
exports.ClassExpression = function (node) {
return buildClass(node);
};
var getMemberExpressionObject = function (node) {
while (node.type === "MemberExpression") {
node = node.object;
}
return node;
};
var buildClass = function (node) {
var superName = node.superClass;
var className = node.id;
var superClassReference = node.superClass;
if (superName && superName.type === "MemberExpression") {
superClassReference = getMemberExpressionObject(superName);
}
var container = util.template("class", {
CLASS_NAME: className
});
var block = container.callee.body;
var body = block.body;
var returnStatement = body.pop();
if (superName) {
body.push(util.template("inherits", {
SUPER_NAME: superName,
CLASS_NAME: className
}, true));
container.arguments.push(superClassReference);
container.callee.params.push(superClassReference);
}
buildClassBody(body, className, superName, node);
body.push(returnStatement);
return container;
};
var buildClassBody = function (body, className, superName, node) {
var mutatorMap = {};
var classBody = node.body.body;
_.each(classBody, function (node) {
var methodName = node.key.name;
var method = node.value;
replaceInstanceSuperReferences(superName, method);
if (methodName === "constructor") {
if (node.kind === "") {
addConstructor(body[0], method);
} else {
throw util.errorWithNode(node, "unknown kind for constructor method");
}
} else {
if (node.kind === "") {
addInstanceMethod(body, className, methodName, method);
} else {
util.pushMutatorMap(mutatorMap, methodName, node.kind, node);
}
}
});
if (!_.isEmpty(mutatorMap)) {
var protoId = util.template("prototype-identifier", {
CLASS_NAME: className
});
body.push(util.buildDefineProperties(mutatorMap, protoId));
}
};
var superIdentifier = function (superName, node, parent) {
if (parent.property === node) return;
node.name = superName.name || superName.value;
// super(); -> ClassName.call(this);
if (parent.type === "CallExpression" && parent.callee === node) {
node.name += ".call";
parent.arguments.unshift(b.thisExpression());
}
};
var replaceInstanceSuperReferences = function (superName, method) {
superName = superName || b.literal("Function");
traverse(method, function (node, parent) {
if (node.type === "Identifier" && node.name === "super") {
superIdentifier(superName, node, parent);
} else if (node.type === "MemberExpression") {
// no accessing of super properties
if (isAccessingSuperProperties(parent, node)) {
throw util.errorWithNode(node, "cannot access super properties");
} else {
return;
}
} else if (node.type === "CallExpression") {
var callee = node.callee;
if (callee.type !== "MemberExpression") return;
if (callee.object.name !== "super") return;
callee.property.name = "prototype." + callee.property.name + ".call";
node.arguments.unshift(b.thisExpression());
} else {
return;
}
});
};
var isAccessingSuperProperties = function (parent, node) {
var obj = node.object;
return obj.type === "Identifier" && obj.name === "super" &&
parent.object === node;
};
var addConstructor = function (construct, method) {
construct.defaults = method.defaults;
construct.params = method.params;
construct.body = method.body;
};
var addInstanceMethod = function (body, className, methodName, method) {
body.push(util.template("class-method", {
METHOD_NAME: methodName,
CLASS_NAME: className,
FUNCTION: method
}, true));
};
|