diff --git a/bin/6to5/index.js b/bin/6to5/index.js index 045bbe2ac5..5d659da30d 100755 --- a/bin/6to5/index.js +++ b/bin/6to5/index.js @@ -26,6 +26,7 @@ commander.option("-c, --remove-comments", "Remove comments from the compiled cod commander.option("-M, --module-ids", "Insert module id in modules", false); commander.option("-R, --react-compat", "Makes the react transformer produce pre-v0.12 code"); commander.option("--keep-module-id-extensions", "Keep extensions when generating module ids", false); +commander.option("-a, --auxilary-comment [comment]", "Comment text to prepend to all auxilary code"); commander.on("--help", function () { var outKeys = function (title, obj) { @@ -99,6 +100,7 @@ if (errors.length) { exports.opts = { keepModuleIdExtensions: commander.keepModuleIdExtensions, + auxilaryComment: commander.auxilaryComment, sourceMapName: commander.outFile, experimental: commander.experimental, reactCompat: commander.reactCompat, diff --git a/lib/6to5/generation/whitespace.js b/lib/6to5/generation/whitespace.js index e8f299c798..541189f2d6 100644 --- a/lib/6to5/generation/whitespace.js +++ b/lib/6to5/generation/whitespace.js @@ -82,7 +82,7 @@ Whitespace.prototype.getNewlinesAfter = function (node) { } } - if (endToken.type.type === "eof") { + if (endToken && endToken.type.type === "eof") { return 1; } else { var lines = this.getNewlinesBetween(startToken, endToken); @@ -96,6 +96,8 @@ Whitespace.prototype.getNewlinesAfter = function (node) { }; Whitespace.prototype.getNewlinesBetween = function (startToken, endToken) { + if (!endToken || !endToken.loc) return 0; + var start = startToken ? startToken.loc.end.line : 1; var end = endToken.loc.start.line; var lines = 0; diff --git a/lib/6to5/transformation/file.js b/lib/6to5/transformation/file.js index 393da69764..30b8e2918c 100644 --- a/lib/6to5/transformation/file.js +++ b/lib/6to5/transformation/file.js @@ -86,6 +86,7 @@ File.validOptions = [ "experimental", "resolveModuleSource", "runtime", + "auxilaryComment", // these are used by plugins "ignore", @@ -106,6 +107,7 @@ File.prototype.normalizeOptions = function (opts) { defaults(opts, { keepModuleIdExtensions: false, resolveModuleSource: null, + auxilaryComment: "", experimental: false, reactCompat: false, playground: false, @@ -298,6 +300,18 @@ File.prototype.isConsequenceExpressionStatement = function (node) { return t.isExpressionStatement(node) && this.lastStatements.indexOf(node) >= 0; }; +File.prototype.attachAuxilaryComment = function (node) { + var comment = this.opts.auxilaryComment; + if (comment) { + node.leadingComments = node.leadingComments || []; + node.leadingComments.push({ + type: "Line", + value: " " + comment + }); + } + return node; +}; + File.prototype.addHelper = function (name) { if (!includes(File.helpers, name)) { throw new ReferenceError("Unknown helper " + name); diff --git a/lib/6to5/transformation/transformers/internal/declarations.js b/lib/6to5/transformation/transformers/internal/declarations.js index f242831eb4..0e3f3335ba 100644 --- a/lib/6to5/transformation/transformers/internal/declarations.js +++ b/lib/6to5/transformation/transformers/internal/declarations.js @@ -6,7 +6,7 @@ var t = require("../../../types"); exports.secondPass = true; exports.BlockStatement = -exports.Program = function (node) { +exports.Program = function (node, parent, scope, file) { if (!node._declarations) return; var kinds = {}; @@ -19,16 +19,16 @@ exports.Program = function (node) { kind = declar.kind || "var"; var declarNode = t.variableDeclarator(declar.id, declar.init); - if (!declar.init) { + if (declar.init) { + node.body.unshift(file.attachAuxilaryComment(t.variableDeclaration(kind, [declarNode]))); + } else { kinds[kind] = kinds[kind] || []; kinds[kind].push(declarNode); - } else { - node.body.unshift(t.variableDeclaration(kind, [declarNode])); } } for (kind in kinds) { - node.body.unshift(t.variableDeclaration(kind, kinds[kind])); + node.body.unshift(file.attachAuxilaryComment(t.variableDeclaration(kind, kinds[kind]))); } });