add auxilary comment option to prepend to helpers - #777

This commit is contained in:
Sebastian McKenzie
2015-02-14 16:53:41 +11:00
parent 6ad16302cd
commit 0e1743738e
4 changed files with 24 additions and 6 deletions

View File

@@ -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;

View File

@@ -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);

View File

@@ -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])));
}
});