Permalink
Browse files

CLI: Prepare static code with estraverse instead of regular expressio…

…ns, see #732
  • Loading branch information...
dcodeIO committed Mar 27, 2017
1 parent 8aa2126 commit 2ebb1b781812e77de914cd260e7ab69612ffd99e
Showing with 57 additions and 13 deletions.
  1. +53 −9 cli/targets/static.js
  2. +1 −1 cli/util.js
  3. +3 −3 tests/data/mapbox/vector_tile.js
@@ -188,7 +188,7 @@ function beautifyCode(code) {
});
code = escodegen.generate(ast, {
format: {
newline: "\r\n",
newline: "\n",
quotes: "double"
}
});
@@ -202,16 +202,60 @@ function beautifyCode(code) {
return code;
}
var renameVars = {
"Writer": "$Writer",
"Reader": "$Reader",
"util": "$util"
};
function buildFunction(type, functionName, gen, scope) {
var code = gen.str(functionName)
.replace(/this\.ctor/g, " $root" + type.fullName) // types: construct directly instead of using reflected ctor
.replace(/(types\[\d+])(\.values)/g, "$1") // enums: use types[N] instead of reflected types[N].values
.replace(/\b(?!\.)Writer\b/g, "$Writer") // use common aliases instead of binding through an iife
.replace(/\b(?!\.)Reader\b/g, "$Reader") // "
.replace(/\b(?!\.)util\.\b/g, "$util.") // "
.replace(/\b(?!\.)types\[(\d+)\]/g, function($0, $1) {
return "$root" + type.fieldsArray[$1].resolvedType.fullName;
});
.replace(/((?!\.)types\[\d+])(\.values)/g, "$1"); // enums: use types[N] instead of reflected types[N].values
var ast = espree.parse(code);
estraverse.replace(ast, {
enter: function(node, parent) {
// rename vars
if (
node.type === "Identifier" && renameVars[node.name]
&& (
(parent.type === "MemberExpression" && parent.object === node)
|| (parent.type === "BinaryExpression" && parent.right === node)
)
)
return {
"type": "Identifier",
"name": renameVars[node.name]
};
// replace this.ctor with the actual ctor
if (
node.type === "MemberExpression"
&& node.object.type === "ThisExpression"
&& node.property.type === "Identifier" && node.property.name === "ctor"
)
return {
"type": "Identifier",
"name": "$root " + type.fullName
};
// replace types[N] with the field's actual type
if (
node.type === "MemberExpression"
&& node.object.type === "Identifier" && node.object.name === "types"
&& node.property.type === "Literal"
)
return {
"type": "Identifier",
"name": "$root" + type.fieldsArray[node.property.value].resolvedType.fullName
};
return undefined;
}
});
code = escodegen.generate(ast, {
format: {
newline: "\n",
quotes: "double"
}
});
if (config.beautify)
code = beautifyCode(code);
@@ -172,7 +172,7 @@ exports.wrap = function(OUTPUT, options) {
});
if (options.lint !== "")
wrap = "/*" + options.lint + "*/\n" + wrap;
return wrap.replace(/\r?\n/, "\n");
return wrap.replace(/\r?\n/g, "\n");
};
exports.pad = function(str, len, l) {
@@ -572,7 +572,7 @@ $root.vector_tile = (function() {
* @type {Object}
* @property {number|Long} [id] Feature id.
* @property {Array.<number>} [tags] Feature tags.
* @property {*} [type] Feature type.
* @property {vector_tile.Tile.GeomType} [type] Feature type.
* @property {Array.<number>} [geometry] Feature geometry.
*/
@@ -893,9 +893,9 @@ $root.vector_tile = (function() {
* @type {Object}
* @property {number} version Layer version.
* @property {string} name Layer name.
* @property {Array.<*>} [features] Layer features.
* @property {Array.<vector_tile.Tile.Feature$Properties>} [features] Layer features.
* @property {Array.<string>} [keys] Layer keys.
* @property {Array.<*>} [values] Layer values.
* @property {Array.<vector_tile.Tile.Value$Properties>} [values] Layer values.
* @property {number} [extent] Layer extent.
*/

0 comments on commit 2ebb1b7

Please sign in to comment.