use acorns preserveParens

This commit is contained in:
Sebastian McKenzie
2014-11-09 12:06:56 +11:00
parent b84cb1828b
commit 0b86a2fef8
7 changed files with 62 additions and 50 deletions

View File

@@ -17,6 +17,12 @@ exports.UnaryExpression = function (node, print) {
print(node.argument);
};
exports.ParenthesizedExpression = function (node, print) {
this.push("(");
print(node.expression);
this.push(")");
};
exports.UpdateExpression = function (node, print) {
if (node.prefix) {
this.push(node.operator);
@@ -35,7 +41,7 @@ exports.ConditionalExpression = function (node, print) {
print(node.alternate);
};
exports.NewExpression = function (node, print, parent) {
exports.NewExpression = function (node, print) {
this.push("new ");
print(node.callee);
this.push("(");

View File

@@ -25,7 +25,7 @@ var multiple = function (node, file) {
});
container._aliasFunction = true;
var block = container.callee.body;
var block = container.callee.expression.body;
var body = block.body;
var returnStatement = body.pop();

View File

@@ -40,7 +40,7 @@ var buildClass = function (node, file, scope) {
CLASS_NAME: className
});
var block = container.callee.body;
var block = container.callee.expression.body;
var body = block.body;
var constructor = body[0].declarations[0].init;
@@ -52,7 +52,7 @@ var buildClass = function (node, file, scope) {
body.push(t.expressionStatement(t.callExpression(file.addDeclaration("extends"), [className, superName])));
container.arguments.push(superClassArgument);
container.callee.params.push(superClassCallee);
container.callee.expression.params.push(superClassCallee);
}
buildClassBody({
@@ -180,8 +180,7 @@ var superIdentifier = function (superName, methodNode, node, parent) {
};
var replaceInstanceSuperReferences = function (superName, methodNode) {
var methodName = methodNode.key;
var method = methodNode.value;
var method = methodNode.value;
superName = superName || t.identifier("Function");

View File

@@ -26,7 +26,7 @@ exports.ObjectExpression = function (node, parent, file) {
OBJECT: node
});
var containerCallee = container.callee;
var containerCallee = container.callee.expression;
var containerBody = containerCallee.body.body;
containerCallee._aliasFunction = "arrows";

View File

@@ -28,7 +28,10 @@ exports.TemplateLiteral = function (node) {
nodes.push(t.literal(elem.value.raw));
var expr = node.expressions.shift();
if (expr) nodes.push(expr);
if (expr) {
if (t.isBinary(expr)) expr = t.parenthesizedExpression(expr);
nodes.push(expr);
}
});
if (nodes.length > 1) {

View File

@@ -41,6 +41,7 @@
"NewExpression": ["callee", "arguments"],
"ObjectExpression": ["properties"],
"ObjectPattern": ["properties"],
"ParenthesizedExpression": ["expression"],
"Program": ["body"],
"Property": ["key", "value"],
"ReturnStatement": ["argument"],

View File

@@ -1,3 +1,5 @@
require("./patch");
var estraverse = require("estraverse");
var traverse = require("./traverse");
var acorn = require("acorn-6to5");
@@ -7,8 +9,6 @@ var fs = require("fs");
var t = require("./types");
var _ = require("lodash");
_.extend(estraverse.VisitorKeys, t.VISITOR_KEYS);
exports.inherits = util.inherits;
exports.resolve = function (loc) {
@@ -136,6 +136,8 @@ exports.template = function (name, nodes, keepExpression) {
if (!keepExpression && t.isExpressionStatement(node)) {
node = node.expression;
if (t.isParenthesizedExpression(node)) node = node.expression;
}
if (inherits) {
@@ -190,23 +192,24 @@ exports.parse = function (opts, code, callback) {
var tokens = [];
var ast = acorn.parse(code, {
ecmaVersion: Infinity,
strictMode: true,
onComment: comments,
locations: true,
onToken: tokens,
ranges: true
preserveParens: true,
ecmaVersion: Infinity,
strictMode: true,
onComment: comments,
locations: true,
onToken: tokens,
ranges: true
});
estraverse.attachComments(ast, comments, tokens);
ast = {
type: "File",
program: ast
};
type: "File",
program: ast,
ast.comments = comments;
ast.tokens = tokens;
comments: comments,
tokens: tokens,
};
if (callback) {
return callback(ast);
@@ -238,36 +241,36 @@ exports.parseNoProperties = function (loc, code) {
};
var loadTemplates = function () {
try {
return require("../../templates.json");
} catch (err) {
if (err.code !== "MODULE_NOT_FOUND") throw err;
var 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");
}
_.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;
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;
};
Object.defineProperty(exports, "templates", {
get: function () {
return exports.templates = loadTemplates();
}
});
try {
return require("../../templates.json");
} catch (err) {
if (err.code !== "MODULE_NOT_FOUND") throw err;
Object.defineProperty(exports, "templates", {
get: function () {
return exports.templates = loadTemplates();
}
});
}