Skip to content
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

Add more tests of chain expressions. #783

Merged
merged 2 commits into from
Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions lib/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,10 @@ function genericPrintNoParens(path: any, options: any, print: any) {
parts.push(path.call(print, "object"));

const property = path.call(print, "property");
const optional = n.type === "OptionalMemberExpression" && n.optional;

// Like n.optional, except with defaults applied, so optional
// defaults to true for OptionalMemberExpression nodes.
const optional = types.getFieldValue(n, "optional");

if (n.computed) {
parts.push(optional ? "?.[" : "[", property, "]");
Expand All @@ -310,17 +313,6 @@ function genericPrintNoParens(path: any, options: any, print: any) {

case "ChainExpression":
return path.call(print, "expression");

case "ChainElement": {
parts.push(path.call(print, "object"));

const property = path.call(print, "property");
const optional = n.optional;

parts.push(optional ? "?." : ".", property);

return concat(parts);
}

case "MetaProperty":
return concat([
Expand Down Expand Up @@ -677,10 +669,10 @@ function genericPrintNoParens(path: any, options: any, print: any) {
parts.push(path.call(print, "typeArguments"));
}

if (
n.type === "OptionalCallExpression" &&
n.callee.type !== "OptionalMemberExpression"
) {
// Like n.optional, but defaults to true for OptionalCallExpression
// nodes that are missing an n.optional property (unusual),
// according to the OptionalCallExpression definition in ast-types.
if (types.getFieldValue(n, "optional")) {
parts.push("?.");
}

Expand Down Expand Up @@ -1541,6 +1533,7 @@ function genericPrintNoParens(path: any, options: any, print: any) {
case "TSHasOptionalTypeParameterInstantiation":
case "TSHasOptionalTypeParameters":
case "TSHasOptionalTypeAnnotation":
case "ChainElement": // Supertype of MemberExpression and CallExpression
throw new Error("unprintable type: " + JSON.stringify(n.type));

case "CommentBlock": // Babel block comment.
Expand Down
30 changes: 30 additions & 0 deletions test/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1692,6 +1692,36 @@ describe("printer", function () {
assert.strictEqual(recast.print(node).code, "foo?.bar");
});

it("reprints various optional member/call expressions", function () {
const parser = require("../parsers/babel");

function check(code: string) {
const ast = recast.parse(code, { parser });
const exprStmt = ast.program.body[0];
n.ExpressionStatement.assert(exprStmt);
const expr = exprStmt.expression;
const output = recast.prettyPrint(expr, { tabWidth: 2 }).code;
assert.strictEqual(code, output);
}

check("a.b");
check("a?.b");
check("a?.b.c");
check("a.b?.c");
check("a?.b?.c");
check("a?.(b)");
check("a?.b(c)");
check("a?.b?.(c)");
check("a.b?.(c)");
check("a.b?.(c)?.d");
check("a.b?.(c)?.d(e)");
check("a.b?.(c)?.d?.(e)");
check("a?.b?.(c).d?.(e)");
check("a?.b?.(c)?.d?.(e)");
check("(a?.b)?.(c)?.d?.(e)");
check("(a?.b?.(c)?.d)?.(e)");
});

it("prints numbers in bases other than 10 without converting them", function () {
const code = [
"let decimal = 6;",
Expand Down