Print pipeline and nullish-coalescing operators (#3036)

This commit is contained in:
Lucas Azzola authored and GitHub committed 2017-10-16 09:13:25 +11:00
1 parent d90aec1a1d
commit 624d89e7a5
10 files changed
+146 -8

No files matched your search

+1 -1
View File
@@ -334,7 +334,7 @@ FastPath.prototype.needsParens = function(options) {
return true;
}
if (po === "||" && no === "&&") {
if ((po === "||" || po === "??") && no === "&&") {
return true;
}
+3 -1
View File
@@ -26,7 +26,9 @@ function parse(text, parsers, opts) {
"importMeta",
"optionalCatchBinding",
"optionalChaining",
"classPrivateProperties"
"classPrivateProperties",
"pipelineOperator",
"nullishCoalescingOperator"
]
};
+11 -5
View File
@@ -4359,11 +4359,17 @@ function printBinaryishExpressions(
parts.push(path.call(print, "left"));
}
const right = concat([
node.operator,
shouldInlineLogicalExpression(node) ? " " : line,
path.call(print, "right")
]);
const shouldInline = shouldInlineLogicalExpression(node);
const lineBeforeOperator = node.operator === "|>";
const right = shouldInline
? concat([node.operator, " ", path.call(print, "right")])
: concat([
lineBeforeOperator ? softline : "",
node.operator,
lineBeforeOperator ? " " : line,
path.call(print, "right")
]);
// If there's only a single binary expression, we want to create a group
// in order to avoid having a small right part like -1 be on its own line.
+2 -1
View File
@@ -301,7 +301,8 @@ function setLocEnd(node, index) {
const PRECEDENCE = {};
[
["||"],
["|>"],
["||", "??"],
["&&"],
["|"],
["^"],
@@ -0,0 +1,36 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`nullish_coalesing_operator.js 1`] = `
obj.foo ?? "default";
const x = (foo, bar = foo ?? bar) => {};
foo ? bar ?? foo : baz;
foo ?? (bar ?? baz);
foo ?? baz || baz;
(foo && baz) ?? baz;
foo && (baz ?? baz);
foo |> (bar ?? baz);
(foo |> bar) ?? baz;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
obj.foo ?? "default";
const x = (foo, bar = foo ?? bar) => {};
foo ? bar ?? foo : baz;
foo ?? (bar ?? baz);
foo ?? baz || baz;
(foo && baz) ?? baz;
foo && (baz ?? baz);
foo |> bar ?? baz;
(foo |> bar) ?? baz;
`;
+1
View File
@@ -0,0 +1 @@
run_spec(__dirname, { parser: "babylon" });
@@ -0,0 +1,15 @@
obj.foo ?? "default";
const x = (foo, bar = foo ?? bar) => {};
foo ? bar ?? foo : baz;
foo ?? (bar ?? baz);
foo ?? baz || baz;
(foo && baz) ?? baz;
foo && (baz ?? baz);
foo |> (bar ?? baz);
(foo |> bar) ?? baz;
@@ -0,0 +1,52 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`pipeline_operator.js 1`] = `
a |> b |> c;
a |> (b |> c);
(a |> b) || c;
a |> (b || c);
let result = "hello"
|> doubleSay
|> capitalize
|> exclaim;
let newScore = person.score
|> double
|> (_ => add(7, _))
|> (_ => subtract(2, _))
|> (_ => boundScore(0, 100, _));
function createPerson (attrs) {
attrs
|> bounded('age', 1, 100)
|> format('name', /^[a-z]$/i)
|> Person.insertIntoDatabase;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a |> b |> c;
a |> (b |> c);
(a |> b) || c;
a |> b || c;
let result = "hello" |> doubleSay |> capitalize |> exclaim;
let newScore =
person.score
|> double
|> (_ => add(7, _))
|> (_ => subtract(2, _))
|> (_ => boundScore(0, 100, _));
function createPerson(attrs) {
attrs
|> bounded("age", 1, 100)
|> format("name", /^[a-z]$/i)
|> Person.insertIntoDatabase;
}
`;
+1
View File
@@ -0,0 +1 @@
run_spec(__dirname, { parser: "babylon" });
@@ -0,0 +1,24 @@
a |> b |> c;
a |> (b |> c);
(a |> b) || c;
a |> (b || c);
let result = "hello"
|> doubleSay
|> capitalize
|> exclaim;
let newScore = person.score
|> double
|> (_ => add(7, _))
|> (_ => subtract(2, _))
|> (_ => boundScore(0, 100, _));
function createPerson (attrs) {
attrs
|> bounded('age', 1, 100)
|> format('name', /^[a-z]$/i)
|> Person.insertIntoDatabase;
}