-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Print parens for extra.parenthesized. Fix bugs
- Loading branch information
1 parent
83a8f83
commit 4540166
Showing
7 changed files
with
141 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import assert from "assert"; | ||
import * as babylon from "@babel/parser"; | ||
import { parse as recastParse } from "../lib/parser"; | ||
import { Printer } from "../lib/printer"; | ||
import * as types from "ast-types"; | ||
|
||
const printer = new Printer(); | ||
const { namedTypes: n } = types; | ||
|
||
function parseExpression(expr: any) { | ||
let ast: any = babylon.parseExpression(expr); | ||
return n.ExpressionStatement.check(ast) ? ast.expression : ast; | ||
} | ||
|
||
const parse = (expr: string) => recastParse(expr, { | ||
parser: babylon, | ||
}); | ||
|
||
function check(expr: string) { | ||
const ast = parse(expr); | ||
|
||
const reprinted = printer.print(ast).code; | ||
assert.strictEqual(reprinted, expr); | ||
|
||
const expressionAst = parseExpression(expr); | ||
const generic = printer.printGenerically(expressionAst).code; | ||
types.astNodesAreEquivalent.assert(expressionAst, parseExpression(generic)); | ||
} | ||
|
||
describe("babylon parens", function () { | ||
it("AwaitExpression", function () { | ||
check("async () => ({...(await obj)})"); | ||
check("(async function* () { yield await foo })"); | ||
}); | ||
|
||
it("YieldExpression", function () { | ||
check("(function* () { return {...(yield obj)}})"); | ||
}); | ||
|
||
it("decorative parens", function () { | ||
const ast = parse("1"); | ||
const expr = ast.program.body[0].expression; | ||
|
||
expr.extra.parenthesized = true; | ||
|
||
assert.strictEqual(printer.print(ast).code, '(1)'); | ||
}); | ||
|
||
it("decorative parens which are also necessary", function () { | ||
const ast = parse("(1).foo"); | ||
const expr = ast.program.body[0].expression; | ||
|
||
expr.object.extra.parenthesized = false; | ||
|
||
assert.strictEqual(printer.print(ast).code, "(1).foo"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.