-
Notifications
You must be signed in to change notification settings - Fork 350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix missing parens in reprinted output #1068
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -324,9 +324,6 @@ FPp.needsParens = function (assumeExpressionContext) { | |
} | ||
|
||
const parent = this.getParentNode(); | ||
if (!parent) { | ||
return false; | ||
} | ||
|
||
const name = this.getName(); | ||
|
||
|
@@ -347,13 +344,16 @@ FPp.needsParens = function (assumeExpressionContext) { | |
return false; | ||
} | ||
|
||
if ( | ||
parent.type === "ParenthesizedExpression" || | ||
(node.extra && node.extra.parenthesized) | ||
) { | ||
if (parent && parent.type === "ParenthesizedExpression") { | ||
return false; | ||
} | ||
|
||
if (node.extra && node.extra.parenthesized) { | ||
return true; | ||
} | ||
Comment on lines
+351
to
+353
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem with short circuiting to |
||
|
||
if (!parent) return false; | ||
|
||
switch (node.type) { | ||
case "UnaryExpression": | ||
case "SpreadElement": | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { parser } from "./babel"; | ||
import getBabelOptions, { Overrides } from "./_babel_options"; | ||
|
||
export { parser }; | ||
|
||
export function parse(source: string, options?: Overrides) { | ||
const babelOptions = getBabelOptions(options); | ||
babelOptions.plugins.push("jsx", "typescript"); | ||
return parser.parse(source, babelOptions); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import assert from "assert"; | ||
// the babel parser denotes decorative parens with extra.parenthesized | ||
import * as babylon from "@babel/parser"; | ||
import { parse as recastParse } from "../lib/parser"; | ||
import { Printer } from "../lib/printer"; | ||
import * as parser from "../parsers/babel-ts"; | ||
import * as types from "ast-types"; | ||
|
||
const printer = new Printer(); | ||
|
@@ -12,23 +14,20 @@ function parseExpression(expr: any) { | |
return n.ExpressionStatement.check(ast) ? ast.expression : ast; | ||
} | ||
|
||
const parse = (expr: string) => | ||
recastParse(expr, { | ||
parser: babylon, | ||
}); | ||
const parse = (expr: string) => recastParse(expr, { parser }); | ||
|
||
function check(expr: string) { | ||
const ast = parse(expr); | ||
|
||
const reprinted = printer.print(ast).code; | ||
assert.strictEqual(reprinted, expr); | ||
assert.strictEqual(expr, reprinted); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. expected is first |
||
|
||
const expressionAst = parseExpression(expr); | ||
const generic = printer.printGenerically(expressionAst).code; | ||
types.astNodesAreEquivalent.assert(expressionAst, parseExpression(generic)); | ||
} | ||
|
||
describe("babylon parens", function () { | ||
describe("parens from node.extra.parenthesized", function () { | ||
it("AwaitExpression", function () { | ||
check("async () => ({...(await obj)})"); | ||
check("(async function* () { yield await foo })"); | ||
|
@@ -55,4 +54,20 @@ describe("babylon parens", function () { | |
|
||
assert.strictEqual(printer.print(ast).code, "(1).foo"); | ||
}); | ||
|
||
it("prints top level parens for an expression ast", function () { | ||
check("(() => {})()"); | ||
check("(function () {} ())"); | ||
}); | ||
|
||
describe("reprinter", function () { | ||
it("preserves necessary parens", function () { | ||
const ast = parse("() => ({ prop: true })"); | ||
const expr = ast.program.body[0].expression; | ||
|
||
expr.body.properties = []; | ||
|
||
assert.strictEqual(printer.print(ast).code, "() => ({})"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven't yet been able to verify that this test fails if the behavior regresses. |
||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import "./identity"; | |
import "./jsx"; | ||
import "./lines"; | ||
import "./mapping"; | ||
import "./parens-extra"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. d'oh |
||
import "./parens"; | ||
import "./parser"; | ||
import "./patcher"; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We delay this check so that we can reprint
(function () {} ())
, which otherwise will always have its outermost parens stripped since it has no parent.