From d0a33ab933edb24d0d7703c554c3e4e3fe378a89 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sat, 8 Nov 2014 12:00:12 +1100 Subject: [PATCH] add inherits option to util.template --- lib/6to5/transformation/modules/common.js | 20 +++++- lib/6to5/util.js | 86 ++++++++++++++--------- 2 files changed, 72 insertions(+), 34 deletions(-) diff --git a/lib/6to5/transformation/modules/common.js b/lib/6to5/transformation/modules/common.js index 08a2adcb03..cceb8fd833 100644 --- a/lib/6to5/transformation/modules/common.js +++ b/lib/6to5/transformation/modules/common.js @@ -10,6 +10,7 @@ function CommonJSFormatter(file) { CommonJSFormatter.prototype.import = function (node, nodes) { // import "foo"; nodes.push(util.template("require", { + inherits: node, MODULE_NAME: node.source.raw }, true)); }; @@ -28,6 +29,8 @@ CommonJSFormatter.prototype.importSpecifier = function (specifier, node, nodes) if (specifier.type !== "ImportBatchSpecifier") templateName += "-key"; nodes.push(util.template(templateName, { + inherits: node.specifiers.length === 1 && node, + VARIABLE_NAME: variableName, MODULE_NAME: node.source.raw, KEY: specifier.id @@ -42,12 +45,14 @@ CommonJSFormatter.prototype.export = function (node, nodes) { if (t.isClass(ref) || t.isFunction(ref)) { if (ref.id) { - nodes.push(ref); + nodes.push(t.toStatement(ref)); ref = ref.id; } } nodes.push(util.template("exports-default", { + inherits: node, + VALUE: ref }, true)); } else { @@ -58,6 +63,8 @@ CommonJSFormatter.prototype.export = function (node, nodes) { if (decl.init) { decl.init = util.template("exports-assign", { + inherits: node, + VALUE: decl.init, KEY: decl.id }); @@ -66,6 +73,8 @@ CommonJSFormatter.prototype.export = function (node, nodes) { nodes.push(declar); } else { assign = util.template("exports-assign", { + inherits: node, + VALUE: declar.id, KEY: declar.id }, true); @@ -83,15 +92,22 @@ CommonJSFormatter.prototype.export = function (node, nodes) { CommonJSFormatter.prototype._exportSpecifier = function (getRef, specifier, node, nodes) { var variableName = t.getSpecifierName(specifier); + var inherits = false; + if (node.specifiers.length === 1) inherits = node; + if (node.source) { if (t.isExportBatchSpecifier(specifier)) { // export * from "foo"; nodes.push(util.template("exports-wildcard", { + inherits: inherits, + OBJECT: getRef() }, true)); } else { // export { foo } from "test"; nodes.push(util.template("exports-assign-key", { + inherits: inherits, + VARIABLE_NAME: variableName.name, OBJECT: getRef(), KEY: specifier.id @@ -100,6 +116,8 @@ CommonJSFormatter.prototype._exportSpecifier = function (getRef, specifier, node } else { // export { foo }; nodes.push(util.template("exports-assign", { + inherits: inherits, + VALUE: specifier.id, KEY: variableName }, true)); diff --git a/lib/6to5/util.js b/lib/6to5/util.js index 36ca58e3d3..796c4df99b 100644 --- a/lib/6to5/util.js +++ b/lib/6to5/util.js @@ -113,26 +113,36 @@ exports.template = function (name, nodes, keepExpression) { template = _.cloneDeep(template); - if (!_.isEmpty(nodes)) { - traverse(template, function (node) { - if (t.isIdentifier(node) && _.has(nodes, node.name)) { - var newNode = nodes[node.name]; - if (_.isString(newNode)) { - node.name = newNode; - } else { - return newNode; + var inherits = false; + if (nodes) { + inherits = nodes.inherits; + delete nodes.inherits; + + if (!_.isEmpty(nodes)) { + traverse(template, function (node) { + if (t.isIdentifier(node) && _.has(nodes, node.name)) { + var newNode = nodes[node.name]; + if (_.isString(newNode)) { + node.name = newNode; + } else { + return newNode; + } } - } - }); + }); + } } var node = template.body[0]; if (!keepExpression && t.isExpressionStatement(node)) { - return node.expression; - } else { - return node; + node = node.expression; } + + if (inherits) { + node = t.inherits(node, inherits); + } + + return node; }; exports.codeFrame = function (lines, lineNumber, colNumber) { @@ -227,27 +237,37 @@ exports.parseNoProperties = function (loc, code) { return traverse.removeProperties(ast); }; -try { - exports.templates = require("../../templates.json"); -} catch (err) { - if (err.code !== "MODULE_NOT_FOUND") throw err; +var loadTemplates = function () { + try { + return require("../../templates.json"); + } catch (err) { + if (err.code !== "MODULE_NOT_FOUND") throw err; - exports.templates = {}; + var templates = {}; - var templatesLoc = __dirname + "/templates"; - if (!fs.existsSync(templatesLoc)) { - throw new Error("no templates directory - this is most likely the result" + - " of a broken `npm publish`. Please report to " + - "https://githut.com/sebmck/6to5/issues"); + var templatesLoc = __dirname + "/templates"; + if (!fs.existsSync(templatesLoc)) { + throw new Error("no templates directory - this is most likely the " + + "result of a broken `npm publish`. Please report to " + + "https://githut.com/sebmck/6to5/issues"); + } + + _.each(fs.readdirSync(templatesLoc), function (name) { + if (name[0] === ".") return; + + var key = path.basename(name, path.extname(name)); + var loc = templatesLoc + "/" + name; + var code = fs.readFileSync(loc, "utf8"); + + templates[key] = exports.parseNoProperties(loc, code); + }); + + return templates; } +}; - _.each(fs.readdirSync(templatesLoc), function (name) { - if (name[0] === ".") return; - - var key = path.basename(name, path.extname(name)); - var loc = templatesLoc + "/" + name; - var code = fs.readFileSync(loc, "utf8"); - - exports.templates[key] = exports.parseNoProperties(loc, code); - }); -} +Object.defineProperty(exports, "templates", { + get: function () { + return exports.templates = loadTemplates(); + } +});