babel-generator
Advanced tools
Comparing version 6.3.26 to 6.4.0
@@ -195,3 +195,4 @@ /* @flow */ | ||
// needs a paren except for `in` expressions: `for (a in b ? a : b;;)` | ||
var parens = this._inForStatementInit && node.operator === "in" && !_node2["default"].needsParens(node, parent); | ||
// and for `ObjectPattern`: `({ f } = { f: 2 };` | ||
var parens = this._inForStatementInit && node.operator === "in" && !_node2["default"].needsParens(node, parent) || t.isObjectPattern(node.left); | ||
@@ -198,0 +199,0 @@ if (parens) { |
@@ -15,3 +15,5 @@ /* @flow */ | ||
exports.DeclareFunction = DeclareFunction; | ||
exports.DeclareInterface = DeclareInterface; | ||
exports.DeclareModule = DeclareModule; | ||
exports.DeclareTypeAlias = DeclareTypeAlias; | ||
exports.DeclareVariable = DeclareVariable; | ||
@@ -30,2 +32,3 @@ exports.ExistentialTypeParam = ExistentialTypeParam; | ||
exports.StringTypeAnnotation = StringTypeAnnotation; | ||
exports.ThisTypeAnnotation = ThisTypeAnnotation; | ||
exports.TupleTypeAnnotation = TupleTypeAnnotation; | ||
@@ -83,2 +86,7 @@ exports.TypeofTypeAnnotation = TypeofTypeAnnotation; | ||
function DeclareInterface(node /*: Object*/) { | ||
this.push("declare "); | ||
this.InterfaceDeclaration(node); | ||
} | ||
function DeclareModule(node /*: Object*/) { | ||
@@ -91,2 +99,7 @@ this.push("declare module "); | ||
function DeclareTypeAlias(node /*: Object*/) { | ||
this.push("declare "); | ||
this.TypeAlias(node); | ||
} | ||
function DeclareVariable(node /*: Object*/) { | ||
@@ -154,2 +167,6 @@ this.push("declare var "); | ||
} | ||
if (node.mixins && node.mixins.length) { | ||
this.push(" mixins "); | ||
this.printJoin(node.mixins, node, { separator: ", " }); | ||
} | ||
this.space(); | ||
@@ -193,2 +210,6 @@ this.print(node.body, node); | ||
function ThisTypeAnnotation() { | ||
this.push("this"); | ||
} | ||
function TupleTypeAnnotation(node /*: Object*/) { | ||
@@ -195,0 +216,0 @@ this.push("["); |
@@ -143,3 +143,6 @@ "use strict"; | ||
if (label) { | ||
this.push(" "); | ||
if (!(this.format.minified && (t.isUnaryExpression(label, { prefix: true }) || t.isUpdateExpression(label, { prefix: true })))) { | ||
this.push(" "); | ||
} | ||
var terminatorState = this.startTerminatorless(); | ||
@@ -195,3 +198,4 @@ this.print(label, node); | ||
this.print(node.param, node); | ||
this.push(") "); | ||
this.push(")"); | ||
this.space(); | ||
this.print(node.body, node); | ||
@@ -198,0 +202,0 @@ } |
@@ -31,3 +31,3 @@ /* @noflow */ | ||
function find(obj, node, parent) { | ||
function find(obj, node, parent, printStack) { | ||
if (!obj) return; | ||
@@ -42,3 +42,3 @@ var result = undefined; | ||
var fn = obj[type]; | ||
result = fn(node, parent); | ||
result = fn(node, parent, printStack); | ||
if (result != null) break; | ||
@@ -105,3 +105,3 @@ } | ||
Node.needsParens = function needsParens(node, parent) { | ||
Node.needsParens = function needsParens(node, parent, printStack) { | ||
if (!parent) return false; | ||
@@ -113,3 +113,3 @@ | ||
return find(parens, node, parent); | ||
return find(parens, node, parent, printStack); | ||
}; | ||
@@ -116,0 +116,0 @@ |
@@ -14,2 +14,3 @@ /* @flow */ | ||
exports.SequenceExpression = SequenceExpression; | ||
exports.AwaitExpression = AwaitExpression; | ||
exports.YieldExpression = YieldExpression; | ||
@@ -69,3 +70,3 @@ exports.ClassExpression = ClassExpression; | ||
function ObjectExpression(node /*: Object*/, parent /*: Object*/) /*: boolean*/ { | ||
function ObjectExpression(node /*: Object*/, parent /*: Object*/, printStack /*: Array<Object>*/) /*: boolean*/ { | ||
if (t.isExpressionStatement(parent)) { | ||
@@ -76,15 +77,3 @@ // ({ foo: "bar" }); | ||
if (t.isMemberExpression(parent) && parent.object === node) { | ||
// ({ foo: "bar" }).foo | ||
return true; | ||
} | ||
if ((t.isBinaryExpression(parent) || t.isLogicalExpression(parent)) && parent.left === node) { | ||
// We'd need to check that the parent's parent is an ExpressionStatement. But this | ||
// code doesn't make any sense to begin with and should be rare. | ||
// `({}) === foo` | ||
return true; | ||
} | ||
return false; | ||
return isFirstInStatement(printStack); | ||
} | ||
@@ -105,3 +94,3 @@ | ||
if (t.isBinary(parent)) { | ||
if (t.isBinary(parent) && !t.isAssignmentExpression(parent)) { | ||
var parentOp = parent.operator; | ||
@@ -163,2 +152,18 @@ var parentPos = PRECEDENCE[parentOp]; | ||
if (t.isSwitchStatement(parent) && parent.discriminant === node) { | ||
return false; | ||
} | ||
if (t.isWhileStatement(parent) && parent.test === node) { | ||
return false; | ||
} | ||
if (t.isIfStatement(parent) && parent.test === node) { | ||
return false; | ||
} | ||
if (t.isForInStatement(parent) && parent.right === node) { | ||
return false; | ||
} | ||
// Otherwise err on the side of overparenthesization, adding | ||
@@ -169,4 +174,8 @@ // explicit exceptions above if this proves overzealous. | ||
function AwaitExpression(node /*: Object*/, parent /*: Object*/) /*: boolean*/ { | ||
return t.isUnaryLike(parent) || t.isCallExpression(parent) || t.isMemberExpression(parent) || t.isNewExpression(parent); | ||
} | ||
function YieldExpression(node /*: Object*/, parent /*: Object*/) /*: boolean*/ { | ||
return t.isBinary(parent) || t.isUnaryLike(parent) || t.isCallExpression(parent) || t.isMemberExpression(parent) || t.isNewExpression(parent) || t.isConditionalExpression(parent) || t.isYieldExpression(parent); | ||
return t.isUnaryLike(parent) || t.isCallExpression(parent) || t.isMemberExpression(parent) || t.isNewExpression(parent); | ||
} | ||
@@ -200,3 +209,3 @@ | ||
function FunctionExpression(node /*: Object*/, parent /*: Object*/) /*: boolean*/ { | ||
function FunctionExpression(node /*: Object*/, parent /*: Object*/, printStack /*: Array<Object>*/) /*: boolean*/ { | ||
// (function () {}); | ||
@@ -212,3 +221,3 @@ if (t.isExpressionStatement(parent)) { | ||
return UnaryLike(node, parent); | ||
return isFirstInStatement(printStack); | ||
} | ||
@@ -234,3 +243,3 @@ | ||
if (t.isBinary(parent)) { | ||
if (t.isBinary(parent) && !t.isAssignmentExpression(parent)) { | ||
return true; | ||
@@ -252,2 +261,26 @@ } | ||
} | ||
} | ||
// Walk up the print stack to deterimine if our node can come first | ||
// in statement. | ||
function isFirstInStatement(printStack /*: Array<Object>*/) /*: boolean*/ { | ||
var i = printStack.length - 1; | ||
var node = printStack[i]; | ||
i--; | ||
var parent = printStack[i]; | ||
while (i > 0) { | ||
if (t.isExpressionStatement(parent, { expression: node })) { | ||
return true; | ||
} | ||
if (t.isCallExpression(parent, { callee: node }) || t.isSequenceExpression(parent) && parent.expressions[0] === node || t.isMemberExpression(parent, { object: node }) || t.isConditional(parent, { test: node }) || t.isBinary(parent, { left: node }) || t.isAssignmentExpression(parent, { left: node })) { | ||
node = parent; | ||
i--; | ||
parent = printStack[i]; | ||
} else { | ||
return false; | ||
} | ||
} | ||
return false; | ||
} |
@@ -46,2 +46,3 @@ "use strict"; | ||
this.printAuxAfterOnNextUserNode = false; | ||
this._printStack = []; | ||
} | ||
@@ -73,6 +74,8 @@ | ||
this._printStack.push(node); | ||
if (node.loc) this.printAuxAfterComment(); | ||
this.printAuxBeforeComment(oldInAux); | ||
var needsParens = _node2["default"].needsParens(node, parent); | ||
var needsParens = _node2["default"].needsParens(node, parent, this._printStack); | ||
if (needsParens) this.push("("); | ||
@@ -100,2 +103,3 @@ | ||
// end | ||
this._printStack.pop(); | ||
this.map.mark(node, "end"); | ||
@@ -102,0 +106,0 @@ if (opts.after) opts.after(); |
{ | ||
"name": "babel-generator", | ||
"version": "6.3.26", | ||
"version": "6.4.0", | ||
"description": "Turns an AST into code.", | ||
@@ -16,3 +16,3 @@ "author": "Sebastian McKenzie <sebmck@gmail.com>", | ||
"babel-runtime": "^5.0.0", | ||
"babel-types": "^6.3.21", | ||
"babel-types": "^6.4.0", | ||
"detect-indent": "^3.0.1", | ||
@@ -27,4 +27,4 @@ "is-integer": "^1.0.4", | ||
"babel-helper-fixtures": "^6.3.13", | ||
"babylon": "^6.3.26" | ||
"babylon": "^6.4.0" | ||
} | ||
} |
@@ -18,5 +18,5 @@ # babel-generator | ||
const code = 'class Example {}'; | ||
const ast = parse(ast); | ||
const ast = parse(code); | ||
const output = generate(ast, { /* options */ }, code); | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
87573
2683
Updatedbabel-types@^6.4.0