JavaScript: Format ThrowStatement like ReturnStatement (#7070)
This commit is contained in:
1 parent
ca0ff0f280
commit
2eca6193d7
8 files changed
+323
-43
No files matched your search
@@ -0,0 +1,24 @@
|
||||
# Format `throw` like `return` ([#7070](https://github.com/prettier/prettier/pull/7070) by [@sosukesuzuki](https://github.com/sosukesuzuki))
|
||||
|
||||
<!-- prettier-ignore -->
|
||||
```js
|
||||
// Input
|
||||
function foo() {
|
||||
throw this.hasPlugin("dynamicImports") && this.lookahead().type === tt.parenLeft.right;
|
||||
}
|
||||
|
||||
// Prettier stable
|
||||
function foo() {
|
||||
throw this.hasPlugin("dynamicImports") &&
|
||||
this.lookahead().type === tt.parenLeft.right;
|
||||
}
|
||||
|
||||
|
||||
// Prettier master
|
||||
function foo() {
|
||||
throw (
|
||||
this.hasPlugin("dynamicImports") &&
|
||||
this.lookahead().type === tt.parenLeft.right
|
||||
);
|
||||
}
|
||||
```
|
||||
@@ -759,6 +759,7 @@ function needsParens(path, options) {
|
||||
parent.type !== "OptionalCallExpression" &&
|
||||
parent.type !== "Property" &&
|
||||
parent.type !== "ReturnStatement" &&
|
||||
parent.type !== "ThrowStatement" &&
|
||||
parent.type !== "TypeCastExpression" &&
|
||||
parent.type !== "VariableDeclarator")
|
||||
);
|
||||
|
||||
@@ -597,6 +597,7 @@ function printPathNoParens(path, options, print, args) {
|
||||
// indented accordingly. We should indent sub-expressions where the first case isn't indented.
|
||||
const shouldNotIndent =
|
||||
parent.type === "ReturnStatement" ||
|
||||
parent.type === "ThrowStatement" ||
|
||||
(parent.type === "JSXExpressionContainer" &&
|
||||
parentParent.type === "JSXAttribute") ||
|
||||
(n.type !== "NGPipeExpression" &&
|
||||
@@ -608,6 +609,7 @@ function printPathNoParens(path, options, print, args) {
|
||||
(n !== parent.body && parent.type === "ForStatement") ||
|
||||
(parent.type === "ConditionalExpression" &&
|
||||
parentParent.type !== "ReturnStatement" &&
|
||||
parentParent.type !== "ThrowStatement" &&
|
||||
parentParent.type !== "CallExpression" &&
|
||||
parentParent.type !== "OptionalCallExpression") ||
|
||||
parent.type === "TemplateLiteral";
|
||||
@@ -1121,48 +1123,10 @@ function printPathNoParens(path, options, print, args) {
|
||||
return concat(parts);
|
||||
}
|
||||
case "ReturnStatement":
|
||||
parts.push("return");
|
||||
|
||||
if (n.argument) {
|
||||
if (returnArgumentHasLeadingComment(options, n.argument)) {
|
||||
parts.push(
|
||||
concat([
|
||||
" (",
|
||||
indent(concat([hardline, path.call(print, "argument")])),
|
||||
hardline,
|
||||
")"
|
||||
])
|
||||
);
|
||||
} else if (
|
||||
n.argument.type === "LogicalExpression" ||
|
||||
n.argument.type === "BinaryExpression" ||
|
||||
n.argument.type === "SequenceExpression"
|
||||
) {
|
||||
parts.push(
|
||||
group(
|
||||
concat([
|
||||
ifBreak(" (", " "),
|
||||
indent(concat([softline, path.call(print, "argument")])),
|
||||
softline,
|
||||
ifBreak(")")
|
||||
])
|
||||
)
|
||||
);
|
||||
} else {
|
||||
parts.push(" ", path.call(print, "argument"));
|
||||
}
|
||||
}
|
||||
|
||||
if (hasDanglingComments(n)) {
|
||||
parts.push(
|
||||
" ",
|
||||
comments.printDanglingComments(path, options, /* sameIndent */ true)
|
||||
);
|
||||
}
|
||||
|
||||
parts.push(semi);
|
||||
|
||||
return concat(parts);
|
||||
return concat([
|
||||
"return",
|
||||
printReturnAndThrowArgument(path, options, print)
|
||||
]);
|
||||
case "NewExpression":
|
||||
case "OptionalCallExpression":
|
||||
case "CallExpression": {
|
||||
@@ -2030,7 +1994,10 @@ function printPathNoParens(path, options, print, args) {
|
||||
|
||||
return concat(["catch ", path.call(print, "body")]);
|
||||
case "ThrowStatement":
|
||||
return concat(["throw ", path.call(print, "argument"), semi]);
|
||||
return concat([
|
||||
"throw",
|
||||
printReturnAndThrowArgument(path, options, print)
|
||||
]);
|
||||
// Note: ignoring n.lexical because it has no printing consequences.
|
||||
case "SwitchStatement":
|
||||
return concat([
|
||||
@@ -5957,6 +5924,53 @@ function printArrayItems(path, options, printPath, print) {
|
||||
return concat(printedElements);
|
||||
}
|
||||
|
||||
function printReturnAndThrowArgument(path, options, print) {
|
||||
const node = path.getValue();
|
||||
const semi = options.semi ? ";" : "";
|
||||
const parts = [];
|
||||
|
||||
if (node.argument) {
|
||||
if (returnArgumentHasLeadingComment(options, node.argument)) {
|
||||
parts.push(
|
||||
concat([
|
||||
" (",
|
||||
indent(concat([hardline, path.call(print, "argument")])),
|
||||
hardline,
|
||||
")"
|
||||
])
|
||||
);
|
||||
} else if (
|
||||
node.argument.type === "LogicalExpression" ||
|
||||
node.argument.type === "BinaryExpression" ||
|
||||
node.argument.type === "SequenceExpression"
|
||||
) {
|
||||
parts.push(
|
||||
group(
|
||||
concat([
|
||||
ifBreak(" (", " "),
|
||||
indent(concat([softline, path.call(print, "argument")])),
|
||||
softline,
|
||||
ifBreak(")")
|
||||
])
|
||||
)
|
||||
);
|
||||
} else {
|
||||
parts.push(" ", path.call(print, "argument"));
|
||||
}
|
||||
}
|
||||
|
||||
if (hasDanglingComments(node)) {
|
||||
parts.push(
|
||||
" ",
|
||||
comments.printDanglingComments(path, options, /* sameIndent */ true)
|
||||
);
|
||||
}
|
||||
|
||||
parts.push(semi);
|
||||
|
||||
return concat(parts);
|
||||
}
|
||||
|
||||
function willPrintOwnComments(path /*, options */) {
|
||||
const node = path.getValue();
|
||||
const parent = path.getParentNode();
|
||||
|
||||
@@ -0,0 +1,173 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`binaryish.js 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["flow", "typescript", "babel"]
|
||||
printWidth: 80
|
||||
| printWidth
|
||||
=====================================input======================================
|
||||
function f() {
|
||||
throw (
|
||||
property.isIdentifier() &&
|
||||
FUNCTIONS[property.node.name] &&
|
||||
(object.isIdentifier(JEST_GLOBAL) ||
|
||||
(callee.isMemberExpression() && shouldHoistExpression(object))) &&
|
||||
FUNCTIONS[property.node.name](expr.get('arguments'))
|
||||
);
|
||||
|
||||
throw (
|
||||
chalk.bold(
|
||||
'No tests found related to files changed since last commit.\\n',
|
||||
) +
|
||||
chalk.dim(
|
||||
patternInfo.watch ?
|
||||
'Press \`a\` to run all tests, or run Jest with \`--watchAll\`.' :
|
||||
'Run Jest without \`-o\` to run all tests.',
|
||||
)
|
||||
);
|
||||
|
||||
throw !filePath.includes(coverageDirectory) &&
|
||||
!filePath.endsWith(\`.\${SNAPSHOT_EXTENSION}\`);
|
||||
}
|
||||
|
||||
=====================================output=====================================
|
||||
function f() {
|
||||
throw (
|
||||
property.isIdentifier() &&
|
||||
FUNCTIONS[property.node.name] &&
|
||||
(object.isIdentifier(JEST_GLOBAL) ||
|
||||
(callee.isMemberExpression() && shouldHoistExpression(object))) &&
|
||||
FUNCTIONS[property.node.name](expr.get("arguments"))
|
||||
);
|
||||
|
||||
throw (
|
||||
chalk.bold("No tests found related to files changed since last commit.\\n") +
|
||||
chalk.dim(
|
||||
patternInfo.watch
|
||||
? "Press \`a\` to run all tests, or run Jest with \`--watchAll\`."
|
||||
: "Run Jest without \`-o\` to run all tests."
|
||||
)
|
||||
);
|
||||
|
||||
throw (
|
||||
!filePath.includes(coverageDirectory) &&
|
||||
!filePath.endsWith(\`.\${SNAPSHOT_EXTENSION}\`)
|
||||
);
|
||||
}
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`comment.js 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["flow", "typescript", "babel"]
|
||||
printWidth: 80
|
||||
| printWidth
|
||||
=====================================input======================================
|
||||
function x() {
|
||||
throw func2
|
||||
//comment
|
||||
.bar();
|
||||
}
|
||||
|
||||
function f() {
|
||||
throw (
|
||||
foo
|
||||
// comment
|
||||
.bar()
|
||||
);
|
||||
}
|
||||
|
||||
fn(function f() {
|
||||
throw (
|
||||
foo
|
||||
// comment
|
||||
.bar()
|
||||
);
|
||||
});
|
||||
|
||||
=====================================output=====================================
|
||||
function x() {
|
||||
throw (
|
||||
func2
|
||||
//comment
|
||||
.bar()
|
||||
);
|
||||
}
|
||||
|
||||
function f() {
|
||||
throw (
|
||||
foo
|
||||
// comment
|
||||
.bar()
|
||||
);
|
||||
}
|
||||
|
||||
fn(function f() {
|
||||
throw (
|
||||
foo
|
||||
// comment
|
||||
.bar()
|
||||
);
|
||||
});
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`jsx.js 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["flow", "typescript", "babel"]
|
||||
printWidth: 80
|
||||
| printWidth
|
||||
=====================================input======================================
|
||||
function foo() {
|
||||
throw <Bar />;
|
||||
}
|
||||
|
||||
function foo() {
|
||||
throw <Bar>baz</Bar>;
|
||||
}
|
||||
|
||||
function foo() {
|
||||
throw <Bar baz={baz} />;
|
||||
}
|
||||
|
||||
function foo() {
|
||||
throw <Bar baz={baz}>foo</Bar>;
|
||||
}
|
||||
|
||||
function foo() {
|
||||
throw <></>;
|
||||
}
|
||||
|
||||
function foo() {
|
||||
throw <>foo</>;
|
||||
}
|
||||
|
||||
=====================================output=====================================
|
||||
function foo() {
|
||||
throw <Bar />;
|
||||
}
|
||||
|
||||
function foo() {
|
||||
throw <Bar>baz</Bar>;
|
||||
}
|
||||
|
||||
function foo() {
|
||||
throw <Bar baz={baz} />;
|
||||
}
|
||||
|
||||
function foo() {
|
||||
throw <Bar baz={baz}>foo</Bar>;
|
||||
}
|
||||
|
||||
function foo() {
|
||||
throw <></>;
|
||||
}
|
||||
|
||||
function foo() {
|
||||
throw <>foo</>;
|
||||
}
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
@@ -0,0 +1,23 @@
|
||||
function f() {
|
||||
throw (
|
||||
property.isIdentifier() &&
|
||||
FUNCTIONS[property.node.name] &&
|
||||
(object.isIdentifier(JEST_GLOBAL) ||
|
||||
(callee.isMemberExpression() && shouldHoistExpression(object))) &&
|
||||
FUNCTIONS[property.node.name](expr.get('arguments'))
|
||||
);
|
||||
|
||||
throw (
|
||||
chalk.bold(
|
||||
'No tests found related to files changed since last commit.\n',
|
||||
) +
|
||||
chalk.dim(
|
||||
patternInfo.watch ?
|
||||
'Press `a` to run all tests, or run Jest with `--watchAll`.' :
|
||||
'Run Jest without `-o` to run all tests.',
|
||||
)
|
||||
);
|
||||
|
||||
throw !filePath.includes(coverageDirectory) &&
|
||||
!filePath.endsWith(`.${SNAPSHOT_EXTENSION}`);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
function x() {
|
||||
throw func2
|
||||
//comment
|
||||
.bar();
|
||||
}
|
||||
|
||||
function f() {
|
||||
throw (
|
||||
foo
|
||||
// comment
|
||||
.bar()
|
||||
);
|
||||
}
|
||||
|
||||
fn(function f() {
|
||||
throw (
|
||||
foo
|
||||
// comment
|
||||
.bar()
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1 @@
|
||||
run_spec(__dirname, ["flow", "typescript", "babel"]);
|
||||
@@ -0,0 +1,23 @@
|
||||
function foo() {
|
||||
throw <Bar />;
|
||||
}
|
||||
|
||||
function foo() {
|
||||
throw <Bar>baz</Bar>;
|
||||
}
|
||||
|
||||
function foo() {
|
||||
throw <Bar baz={baz} />;
|
||||
}
|
||||
|
||||
function foo() {
|
||||
throw <Bar baz={baz}>foo</Bar>;
|
||||
}
|
||||
|
||||
function foo() {
|
||||
throw <></>;
|
||||
}
|
||||
|
||||
function foo() {
|
||||
throw <>foo</>;
|
||||
}
|
||||
Reference in new issue
Block a user