302 lines
7.3 KiB
JavaScript
302 lines
7.3 KiB
JavaScript
module.exports = function (ast, opts, code) {
|
|
var gen = new CodeGenerator(ast, opts, code);
|
|
return gen.generate();
|
|
};
|
|
|
|
module.exports.CodeGenerator = CodeGenerator;
|
|
|
|
var Whitespace = require("./whitespace");
|
|
var SourceMap = require("./source-map");
|
|
var Position = require("./position");
|
|
var Buffer = require("./buffer");
|
|
var util = require("../util");
|
|
var n = require("./node");
|
|
var t = require("../types");
|
|
var _ = require("lodash");
|
|
|
|
function CodeGenerator(ast, opts, code) {
|
|
opts = opts || {};
|
|
|
|
this.comments = ast.comments || [];
|
|
this.tokens = ast.tokens || [];
|
|
this.format = CodeGenerator.normaliseOptions(opts);
|
|
this.ast = ast;
|
|
|
|
this.whitespace = new Whitespace(this.tokens, this.comments);
|
|
this.position = new Position;
|
|
this.map = new SourceMap(this.position, opts, code);
|
|
this.buffer = new Buffer(this.position, this.format);
|
|
}
|
|
|
|
_.each(Buffer.prototype, function (fn, key) {
|
|
CodeGenerator.prototype[key] = function () {
|
|
return fn.apply(this.buffer, arguments);
|
|
};
|
|
});
|
|
|
|
CodeGenerator.normaliseOptions = function (opts) {
|
|
opts = opts.format || {};
|
|
|
|
opts = _.merge({
|
|
parentheses: true,
|
|
semicolons: true,
|
|
comments: true,
|
|
compact: false,
|
|
indent: {
|
|
adjustMultilineComment: true,
|
|
style: " ",
|
|
base: 0
|
|
}
|
|
}, opts);
|
|
|
|
return opts;
|
|
};
|
|
|
|
CodeGenerator.generators = {
|
|
arrayComprehensions: require("./generators/array-comprehensions"),
|
|
templateLiterals: require("./generators/template-literals"),
|
|
expressions: require("./generators/expressions"),
|
|
statements: require("./generators/statements"),
|
|
classes: require("./generators/classes"),
|
|
methods: require("./generators/methods"),
|
|
modules: require("./generators/modules"),
|
|
types: require("./generators/types"),
|
|
base: require("./generators/base"),
|
|
jsx: require("./generators/jsx")
|
|
};
|
|
|
|
_.each(CodeGenerator.generators, function (generator) {
|
|
_.extend(CodeGenerator.prototype, generator);
|
|
});
|
|
|
|
CodeGenerator.prototype.generate = function () {
|
|
var ast = this.ast;
|
|
|
|
this.print(ast);
|
|
|
|
return {
|
|
map: this.map.get(),
|
|
ast: ast,
|
|
code: this.buffer.get()
|
|
};
|
|
};
|
|
|
|
CodeGenerator.prototype.buildPrint = function (parent) {
|
|
var self = this;
|
|
|
|
var print = function (node, opts) {
|
|
return self.print(node, parent, opts);
|
|
};
|
|
|
|
print.sequence = function (nodes, opts) {
|
|
opts = opts || {};
|
|
opts.statement = true;
|
|
return self.printJoin(print, nodes, opts);
|
|
};
|
|
|
|
print.join = function (nodes, opts) {
|
|
return self.printJoin(print, nodes, opts);
|
|
};
|
|
|
|
print.block = function (node) {
|
|
return self.printBlock(print, node);
|
|
};
|
|
|
|
print.indentOnComments = function (node) {
|
|
return self.printAndIndentOnComments(print, node);
|
|
};
|
|
|
|
return print;
|
|
};
|
|
|
|
CodeGenerator.prototype.print = function (node, parent, opts) {
|
|
if (!node) return "";
|
|
|
|
var self = this;
|
|
opts = opts || {};
|
|
|
|
var newline = function (leading) {
|
|
var ignoreDuplicates = false;
|
|
|
|
if (!opts.statement && !n.isUserWhitespacable(node, parent)) {
|
|
return;
|
|
}
|
|
|
|
var lines = 0;
|
|
|
|
if (node.start != null) {
|
|
// user node
|
|
if (leading) {
|
|
lines = self.whitespace.getNewlinesBefore(node);
|
|
} else {
|
|
lines = self.whitespace.getNewlinesAfter(node);
|
|
}
|
|
} else {
|
|
// generated node
|
|
if (!leading) lines++; // always include at least a single line after
|
|
|
|
var needs = n.needsWhitespaceAfter;
|
|
if (leading) needs = n.needsWhitespaceBefore;
|
|
lines += needs(node, parent);
|
|
}
|
|
|
|
self.newline(lines, ignoreDuplicates);
|
|
};
|
|
|
|
if (this[node.type]) {
|
|
this.printLeadingComments(node, parent);
|
|
|
|
newline(true);
|
|
|
|
if (opts.before) opts.before();
|
|
this.map.mark(node, "start");
|
|
|
|
// only compute if this node needs parens if our parent has been changed
|
|
// since acorn would've wrapped us in a ParanthesizedExpression
|
|
var needsParens = parent !== node._parent && n.needsParens(node, parent);
|
|
if (needsParens) this.push("(");
|
|
|
|
this[node.type](node, this.buildPrint(node), parent);
|
|
|
|
if (needsParens) this.push(")");
|
|
|
|
this.map.mark(node, "end");
|
|
if (opts.after) opts.after();
|
|
|
|
newline(false);
|
|
|
|
this.printTrailingComments(node, parent);
|
|
} else {
|
|
throw new ReferenceError("unknown node " + node.type + " " + JSON.stringify(node));
|
|
}
|
|
};
|
|
|
|
CodeGenerator.prototype.printJoin = function (print, nodes, opts) {
|
|
opts = opts || {};
|
|
|
|
var self = this;
|
|
var len = nodes.length;
|
|
|
|
if (opts.indent) self.indent();
|
|
|
|
_.each(nodes, function (node, i) {
|
|
print(node, {
|
|
statement: opts.statement,
|
|
after: function () {
|
|
if (opts.iterator) {
|
|
opts.iterator(node, i);
|
|
}
|
|
|
|
if (opts.separator && i < len - 1) {
|
|
self.push(opts.separator);
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
if (opts.indent) self.dedent();
|
|
};
|
|
|
|
CodeGenerator.prototype.printAndIndentOnComments = function (print, node) {
|
|
var indent = !!node.leadingComments;
|
|
if (indent) this.indent();
|
|
print(node);
|
|
if (indent) this.dedent();
|
|
};
|
|
|
|
CodeGenerator.prototype.printBlock = function (print, node) {
|
|
if (t.isEmptyStatement(node)) {
|
|
this.semicolon();
|
|
} else {
|
|
this.push(" ");
|
|
print(node);
|
|
}
|
|
};
|
|
|
|
CodeGenerator.prototype.generateComment = function (comment) {
|
|
var val = comment.value;
|
|
if (comment.type === "Line") {
|
|
val = "//" + val;
|
|
} else {
|
|
val = "/*" + val + "*/";
|
|
}
|
|
return val;
|
|
};
|
|
|
|
CodeGenerator.prototype.printTrailingComments = function (node, parent) {
|
|
this._printComments(this.getComments("trailingComments", node, parent));
|
|
};
|
|
|
|
CodeGenerator.prototype.printLeadingComments = function (node, parent) {
|
|
this._printComments(this.getComments("leadingComments", node, parent));
|
|
};
|
|
|
|
CodeGenerator.prototype.getComments = function (key, node, parent) {
|
|
if (t.isExpressionStatement(parent)) {
|
|
return [];
|
|
}
|
|
|
|
var comments = [];
|
|
var nodes = [node];
|
|
var self = this;
|
|
|
|
if (t.isExpressionStatement(node)) {
|
|
nodes.push(node.argument);
|
|
}
|
|
|
|
_.each(nodes, function (node) {
|
|
comments = comments.concat(self._getComments(key, node));
|
|
});
|
|
|
|
return comments;
|
|
};
|
|
|
|
CodeGenerator.prototype._getComments = function (key, node) {
|
|
return (node && node[key]) || [];
|
|
};
|
|
|
|
CodeGenerator.prototype._printComments = function (comments) {
|
|
if (this.format.compact) return;
|
|
if (!this.format.comments) return;
|
|
if (!comments || !comments.length) return;
|
|
|
|
var self = this;
|
|
|
|
_.each(comments, function (comment) {
|
|
// whitespace before
|
|
self.newline(self.whitespace.getNewlinesBefore(comment));
|
|
|
|
var column = self.position.column;
|
|
var val = self.generateComment(comment);
|
|
|
|
if (column && !self.isLast(["\n", " ", "[", "{"])) {
|
|
self._push(" ");
|
|
column++;
|
|
}
|
|
|
|
//
|
|
|
|
if (comment.type === "Block" && self.format.indent.adjustMultilineComment) {
|
|
var offset = comment.loc.start.column;
|
|
if (offset) {
|
|
var newlineRegex = new RegExp("\\n\\s{1," + offset + "}", "g");
|
|
val = val.replace(newlineRegex, "\n");
|
|
}
|
|
|
|
var indent = Math.max(self.indentSize(), column);
|
|
val = val.replace(/\n/g, "\n" + util.repeat(indent));
|
|
}
|
|
|
|
if (column === 0) {
|
|
val = self.getIndent() + val;
|
|
}
|
|
|
|
//
|
|
|
|
self._push(val);
|
|
|
|
// whitespace after
|
|
self.newline(self.whitespace.getNewlinesAfter(comment));
|
|
});
|
|
};
|