ast-types
Advanced tools
Comparing version 0.3.27 to 0.3.28
@@ -100,3 +100,16 @@ var assert = require("assert"); | ||
NPp.needsParens = function() { | ||
/** | ||
* Determine whether this.node needs to be wrapped in parentheses in order | ||
* for a parser to reproduce the same local AST structure. | ||
* | ||
* For instance, in the expression `(1 + 2) * 3`, the BinaryExpression | ||
* whose operator is "+" needs parentheses, because `1 + 2 * 3` would | ||
* parse differently. | ||
* | ||
* If assumeExpressionContext === true, we don't worry about edge cases | ||
* like an anonymous FunctionExpression appearing lexically first in its | ||
* enclosing statement and thus needing parentheses to avoid being parsed | ||
* as a FunctionDeclaration with a missing name. | ||
*/ | ||
NPp.needsParens = function(assumeExpressionContext) { | ||
if (!this.parent) | ||
@@ -219,10 +232,7 @@ return false; | ||
if (n.FunctionExpression.check(node) && | ||
if (assumeExpressionContext !== true && | ||
!this.canBeFirstInStatement() && | ||
this.firstInStatement()) | ||
return true; | ||
if (n.ObjectExpression.check(node) && | ||
this.firstInStatement()) | ||
return true; | ||
return false; | ||
@@ -271,2 +281,8 @@ }; | ||
NPp.canBeFirstInStatement = function() { | ||
var node = this.node; | ||
return !n.FunctionExpression.check(node) | ||
&& !n.ObjectExpression.check(node); | ||
}; | ||
NPp.firstInStatement = function() { | ||
@@ -273,0 +289,0 @@ return firstInStatement(this); |
@@ -21,3 +21,3 @@ { | ||
], | ||
"version": "0.3.27", | ||
"version": "0.3.28", | ||
"homepage": "http://github.com/benjamn/ast-types", | ||
@@ -24,0 +24,0 @@ "repository": { |
@@ -492,2 +492,20 @@ var assert = require("assert"); | ||
}); | ||
it("should support .needsParens(true)", function() { | ||
var programPath = new NodePath(parse("(function(){})")); | ||
var funExpPath = programPath.get("body", 0, "expression"); | ||
n.FunctionExpression.assert(funExpPath.value); | ||
assert.strictEqual(funExpPath.needsParens(), true); | ||
assert.strictEqual(funExpPath.canBeFirstInStatement(), false); | ||
assert.strictEqual(funExpPath.firstInStatement(), true); | ||
assert.strictEqual(funExpPath.needsParens(true), false); | ||
programPath = new NodePath(parse("({ foo: 42 })")); | ||
var objLitPath = programPath.get("body", 0, "expression"); | ||
n.ObjectExpression.assert(objLitPath.value); | ||
assert.strictEqual(objLitPath.needsParens(), true); | ||
assert.strictEqual(objLitPath.canBeFirstInStatement(), false); | ||
assert.strictEqual(objLitPath.firstInStatement(), true); | ||
assert.strictEqual(objLitPath.needsParens(true), false); | ||
}); | ||
}); | ||
@@ -494,0 +512,0 @@ |
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
433322
12053