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