Skip to content

Commit

Permalink
Print parens for nodes with extra.parenthesized
Browse files Browse the repository at this point in the history
  • Loading branch information
conartist6 committed May 25, 2020
1 parent 1d920ae commit 17b85de
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/fast-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ FPp.needsParens = function (assumeExpressionContext) {
return false;
}

if (parent.type === 'ParenthesizedExpression') {
if (parent.type === 'ParenthesizedExpression' || (node.extra && node.extra.parenthesized)) {
return false;
}

Expand Down
16 changes: 2 additions & 14 deletions lib/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,14 @@ function genericPrint(path: any, config: any, options: any, printPath: any) {
return linesWithoutParens;
}

let shouldAddParens = false;
let shouldAddParens = node.extra ? node.extra.parenthesized : false;
const decoratorsLines = printDecorators(path, printPath);

if (decoratorsLines.isEmpty()) {
// Nodes with decorators can't have parentheses, so we can avoid
// computing path.needsParens() except in this case.
if (!options.avoidRootParens) {
shouldAddParens = path.needsParens();
shouldAddParens = shouldAddParens || path.needsParens();
}
} else {
parts.push(decoratorsLines);
Expand Down Expand Up @@ -1969,11 +1969,8 @@ function genericPrintNoParens(path: any, options: any, print: any) {
return concat([path.call(print, 'left'), '.', path.call(print, 'right')]);

case 'TSAsExpression': {
const withParens = n.extra && n.extra.parenthesized === true;
if (withParens) parts.push('(');
const expression = path.call(print, 'expression');
parts.push(expression, fromString(' as '), path.call(print, 'typeAnnotation'));
if (withParens) parts.push(')');

return concat(parts);
}
Expand Down Expand Up @@ -2099,17 +2096,8 @@ function genericPrintNoParens(path: any, options: any, print: any) {
}

case 'TSTypeAssertion': {
const withParens = n.extra && n.extra.parenthesized === true;
if (withParens) {
parts.push('(');
}

parts.push('<', path.call(print, 'typeAnnotation'), '> ', path.call(print, 'expression'));

if (withParens) {
parts.push(')');
}

return concat(parts);
}

Expand Down
18 changes: 18 additions & 0 deletions test/parens-babylon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,22 @@ describe('babylon parens', function () {
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');
});
});

0 comments on commit 17b85de

Please sign in to comment.