Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80738 views
1
define(
2
["../utils","exports"],
3
function(__dependency1__, __exports__) {
4
"use strict";
5
var isArray = __dependency1__.isArray;
6
7
try {
8
var SourceMap = require('source-map'),
9
SourceNode = SourceMap.SourceNode;
10
} catch (err) {
11
/* istanbul ignore next: tested but not covered in istanbul due to dist build */
12
SourceNode = function(line, column, srcFile, chunks) {
13
this.src = '';
14
if (chunks) {
15
this.add(chunks);
16
}
17
};
18
/* istanbul ignore next */
19
SourceNode.prototype = {
20
add: function(chunks) {
21
if (isArray(chunks)) {
22
chunks = chunks.join('');
23
}
24
this.src += chunks;
25
},
26
prepend: function(chunks) {
27
if (isArray(chunks)) {
28
chunks = chunks.join('');
29
}
30
this.src = chunks + this.src;
31
},
32
toStringWithSourceMap: function() {
33
return {code: this.toString()};
34
},
35
toString: function() {
36
return this.src;
37
}
38
};
39
}
40
41
42
function castChunk(chunk, codeGen, loc) {
43
if (isArray(chunk)) {
44
var ret = [];
45
46
for (var i = 0, len = chunk.length; i < len; i++) {
47
ret.push(codeGen.wrap(chunk[i], loc));
48
}
49
return ret;
50
} else if (typeof chunk === 'boolean' || typeof chunk === 'number') {
51
// Handle primitives that the SourceNode will throw up on
52
return chunk+'';
53
}
54
return chunk;
55
}
56
57
58
function CodeGen(srcFile) {
59
this.srcFile = srcFile;
60
this.source = [];
61
}
62
63
CodeGen.prototype = {
64
prepend: function(source, loc) {
65
this.source.unshift(this.wrap(source, loc));
66
},
67
push: function(source, loc) {
68
this.source.push(this.wrap(source, loc));
69
},
70
71
merge: function() {
72
var source = this.empty();
73
this.each(function(line) {
74
source.add([' ', line, '\n']);
75
});
76
return source;
77
},
78
79
each: function(iter) {
80
for (var i = 0, len = this.source.length; i < len; i++) {
81
iter(this.source[i]);
82
}
83
},
84
85
empty: function(loc) {
86
loc = loc || this.currentLocation || {start:{}};
87
return new SourceNode(loc.start.line, loc.start.column, this.srcFile);
88
},
89
wrap: function(chunk, loc) {
90
if (chunk instanceof SourceNode) {
91
return chunk;
92
}
93
94
loc = loc || this.currentLocation || {start:{}};
95
chunk = castChunk(chunk, this, loc);
96
97
return new SourceNode(loc.start.line, loc.start.column, this.srcFile, chunk);
98
},
99
100
functionCall: function(fn, type, params) {
101
params = this.generateList(params);
102
return this.wrap([fn, type ? '.' + type + '(' : '(', params, ')']);
103
},
104
105
quotedString: function(str) {
106
return '"' + (str + '')
107
.replace(/\\/g, '\\\\')
108
.replace(/"/g, '\\"')
109
.replace(/\n/g, '\\n')
110
.replace(/\r/g, '\\r')
111
.replace(/\u2028/g, '\\u2028') // Per Ecma-262 7.3 + 7.8.4
112
.replace(/\u2029/g, '\\u2029') + '"';
113
},
114
115
objectLiteral: function(obj) {
116
var pairs = [];
117
118
for (var key in obj) {
119
if (obj.hasOwnProperty(key)) {
120
var value = castChunk(obj[key], this);
121
if (value !== 'undefined') {
122
pairs.push([this.quotedString(key), ':', value]);
123
}
124
}
125
}
126
127
var ret = this.generateList(pairs);
128
ret.prepend('{');
129
ret.add('}');
130
return ret;
131
},
132
133
134
generateList: function(entries, loc) {
135
var ret = this.empty(loc);
136
137
for (var i = 0, len = entries.length; i < len; i++) {
138
if (i) {
139
ret.add(',');
140
}
141
142
ret.add(castChunk(entries[i], this, loc));
143
}
144
145
return ret;
146
},
147
148
generateArray: function(entries, loc) {
149
var ret = this.generateList(entries, loc);
150
ret.prepend('[');
151
ret.add(']');
152
153
return ret;
154
}
155
};
156
157
__exports__["default"] = CodeGen;
158
});
159