Skip to content

Commit 2ebb1b7

Browse files
committed
CLI: Prepare static code with estraverse instead of regular expressions, see #732
1 parent 8aa2126 commit 2ebb1b7

File tree

3 files changed

+57
-13
lines changed

3 files changed

+57
-13
lines changed

cli/targets/static.js

+53-9
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ function beautifyCode(code) {
188188
});
189189
code = escodegen.generate(ast, {
190190
format: {
191-
newline: "\r\n",
191+
newline: "\n",
192192
quotes: "double"
193193
}
194194
});
@@ -202,16 +202,60 @@ function beautifyCode(code) {
202202
return code;
203203
}
204204

205+
var renameVars = {
206+
"Writer": "$Writer",
207+
"Reader": "$Reader",
208+
"util": "$util"
209+
};
210+
205211
function buildFunction(type, functionName, gen, scope) {
206212
var code = gen.str(functionName)
207-
.replace(/this\.ctor/g, " $root" + type.fullName) // types: construct directly instead of using reflected ctor
208-
.replace(/(types\[\d+])(\.values)/g, "$1") // enums: use types[N] instead of reflected types[N].values
209-
.replace(/\b(?!\.)Writer\b/g, "$Writer") // use common aliases instead of binding through an iife
210-
.replace(/\b(?!\.)Reader\b/g, "$Reader") // "
211-
.replace(/\b(?!\.)util\.\b/g, "$util.") // "
212-
.replace(/\b(?!\.)types\[(\d+)\]/g, function($0, $1) {
213-
return "$root" + type.fieldsArray[$1].resolvedType.fullName;
214-
});
213+
.replace(/((?!\.)types\[\d+])(\.values)/g, "$1"); // enums: use types[N] instead of reflected types[N].values
214+
215+
var ast = espree.parse(code);
216+
estraverse.replace(ast, {
217+
enter: function(node, parent) {
218+
// rename vars
219+
if (
220+
node.type === "Identifier" && renameVars[node.name]
221+
&& (
222+
(parent.type === "MemberExpression" && parent.object === node)
223+
|| (parent.type === "BinaryExpression" && parent.right === node)
224+
)
225+
)
226+
return {
227+
"type": "Identifier",
228+
"name": renameVars[node.name]
229+
};
230+
// replace this.ctor with the actual ctor
231+
if (
232+
node.type === "MemberExpression"
233+
&& node.object.type === "ThisExpression"
234+
&& node.property.type === "Identifier" && node.property.name === "ctor"
235+
)
236+
return {
237+
"type": "Identifier",
238+
"name": "$root " + type.fullName
239+
};
240+
// replace types[N] with the field's actual type
241+
if (
242+
node.type === "MemberExpression"
243+
&& node.object.type === "Identifier" && node.object.name === "types"
244+
&& node.property.type === "Literal"
245+
)
246+
return {
247+
"type": "Identifier",
248+
"name": "$root" + type.fieldsArray[node.property.value].resolvedType.fullName
249+
};
250+
return undefined;
251+
}
252+
});
253+
code = escodegen.generate(ast, {
254+
format: {
255+
newline: "\n",
256+
quotes: "double"
257+
}
258+
});
215259

216260
if (config.beautify)
217261
code = beautifyCode(code);

cli/util.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ exports.wrap = function(OUTPUT, options) {
172172
});
173173
if (options.lint !== "")
174174
wrap = "/*" + options.lint + "*/\n" + wrap;
175-
return wrap.replace(/\r?\n/, "\n");
175+
return wrap.replace(/\r?\n/g, "\n");
176176
};
177177

178178
exports.pad = function(str, len, l) {

tests/data/mapbox/vector_tile.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ $root.vector_tile = (function() {
572572
* @type {Object}
573573
* @property {number|Long} [id] Feature id.
574574
* @property {Array.<number>} [tags] Feature tags.
575-
* @property {*} [type] Feature type.
575+
* @property {vector_tile.Tile.GeomType} [type] Feature type.
576576
* @property {Array.<number>} [geometry] Feature geometry.
577577
*/
578578

@@ -893,9 +893,9 @@ $root.vector_tile = (function() {
893893
* @type {Object}
894894
* @property {number} version Layer version.
895895
* @property {string} name Layer name.
896-
* @property {Array.<*>} [features] Layer features.
896+
* @property {Array.<vector_tile.Tile.Feature$Properties>} [features] Layer features.
897897
* @property {Array.<string>} [keys] Layer keys.
898-
* @property {Array.<*>} [values] Layer values.
898+
* @property {Array.<vector_tile.Tile.Value$Properties>} [values] Layer values.
899899
* @property {number} [extent] Layer extent.
900900
*/
901901

0 commit comments

Comments
 (0)