Skip to content

Parentheses are missing when BinaryExpression child is nullish expression #890

@xyy94813

Description

@xyy94813

Test Case:

const b = recast.types.builders;

const ast = recast.parse('');
const add = ast.program.body[0];
ast.program.body[0] = b.variableDeclaration(
    'let', 
    [b.variableDeclarator(
        b.identifier('a'), 
        b.binaryExpression(
            "+", 
            b.logicalExpression(
                "??",
                b.optionalMemberExpression(
                    b.identifier('c'), 
                    b.identifier('b')
                ),
                b.numericLiteral(1)
            ),
            b.numericLiteral(1)
        )
    )]
);

console.log(recast.print(ast).code);

Now:

let a = c?.b ?? 1 + 1;

Except:

let a = (c?.b ?? 1) + 1;

It work well if logic expression operator is '||' or '&&'。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions