Skip to content

Commit

Permalink
CLI: Prepare static code with estraverse instead of regular expressio…
Browse files Browse the repository at this point in the history
…ns, see #732
  • Loading branch information
dcodeIO committed Mar 27, 2017
1 parent 8aa2126 commit 2ebb1b7
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 13 deletions.
62 changes: 53 additions & 9 deletions cli/targets/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ function beautifyCode(code) {
});
code = escodegen.generate(ast, {
format: {
newline: "\r\n",
newline: "\n",
quotes: "double"
}
});
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion cli/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions tests/data/mapbox/vector_tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/

Expand Down Expand Up @@ -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.
*/

Expand Down

0 comments on commit 2ebb1b7

Please sign in to comment.