From 2ebb1b781812e77de914cd260e7ab69612ffd99e Mon Sep 17 00:00:00 2001 From: dcodeIO Date: Mon, 27 Mar 2017 23:11:34 +0200 Subject: [PATCH] CLI: Prepare static code with estraverse instead of regular expressions, see #732 --- cli/targets/static.js | 62 +++++++++++++++++++++++++++----- cli/util.js | 2 +- tests/data/mapbox/vector_tile.js | 6 ++-- 3 files changed, 57 insertions(+), 13 deletions(-) diff --git a/cli/targets/static.js b/cli/targets/static.js index e6844f7bc..4514d0b25 100644 --- a/cli/targets/static.js +++ b/cli/targets/static.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); diff --git a/cli/util.js b/cli/util.js index 294e7e0ad..605f4263d 100644 --- a/cli/util.js +++ b/cli/util.js @@ -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) { diff --git a/tests/data/mapbox/vector_tile.js b/tests/data/mapbox/vector_tile.js index 3af769901..df4d86a66 100644 --- a/tests/data/mapbox/vector_tile.js +++ b/tests/data/mapbox/vector_tile.js @@ -572,7 +572,7 @@ $root.vector_tile = (function() { * @type {Object} * @property {number|Long} [id] Feature id. * @property {Array.} [tags] Feature tags. - * @property {*} [type] Feature type. + * @property {vector_tile.Tile.GeomType} [type] Feature type. * @property {Array.} [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.} [features] Layer features. * @property {Array.} [keys] Layer keys. - * @property {Array.<*>} [values] Layer values. + * @property {Array.} [values] Layer values. * @property {number} [extent] Layer extent. */