Prevent inlining JSXAttribute if it has comments (#3132)

master
Lucas Duailibe 2017-11-01 13:56:04 -02:00 committed by Stephen Scott
parent 38b15f7899
commit 21c6d4f501
3 changed files with 94 additions and 11 deletions

View File

@ -1717,18 +1717,24 @@ function genericPrintNoParens(path, options, print, args) {
case "JSXExpressionContainer": {
const parent = path.getParentNode(0);
const preventInline =
parent.type === "JSXAttribute" &&
n.expression.comments &&
n.expression.comments.length > 0;
const shouldInline =
n.expression.type === "ArrayExpression" ||
n.expression.type === "ObjectExpression" ||
n.expression.type === "ArrowFunctionExpression" ||
n.expression.type === "CallExpression" ||
n.expression.type === "FunctionExpression" ||
n.expression.type === "JSXEmptyExpression" ||
n.expression.type === "TemplateLiteral" ||
n.expression.type === "TaggedTemplateExpression" ||
(parent.type === "JSXElement" &&
(n.expression.type === "ConditionalExpression" ||
isBinaryish(n.expression)));
!preventInline &&
(n.expression.type === "ArrayExpression" ||
n.expression.type === "ObjectExpression" ||
n.expression.type === "ArrowFunctionExpression" ||
n.expression.type === "CallExpression" ||
n.expression.type === "FunctionExpression" ||
n.expression.type === "JSXEmptyExpression" ||
n.expression.type === "TemplateLiteral" ||
n.expression.type === "TaggedTemplateExpression" ||
(parent.type === "JSXElement" &&
(n.expression.type === "ConditionalExpression" ||
isBinaryish(n.expression))));
if (shouldInline) {
const printExpression =

View File

@ -1,5 +1,58 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`attr-comments.js 1`] = `
<Component
propFn={
// comment
function(arg) {
fn(arg);
}
}
propArrowFn={
// comment
arg => fn(arg)
}
propArray={
// comment
[el1, el2]
}
propObj={
// comment
{ key: val }
}
propTemplate={
// comment
\`text\`
}
/>;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<Component
propFn={
// comment
function(arg) {
fn(arg);
}
}
propArrowFn={
// comment
arg => fn(arg)
}
propArray={
// comment
[el1, el2]
}
propObj={
// comment
{ key: val }
}
propTemplate={
// comment
\`text\`
}
/>;
`;
exports[`conditional-expression.js 1`] = `
// There are two ways to print ConditionalExpressions: "normal mode" and
// "JSX mode". This is normal mode (when breaking):

View File

@ -0,0 +1,24 @@
<Component
propFn={
// comment
function(arg) {
fn(arg);
}
}
propArrowFn={
// comment
arg => fn(arg)
}
propArray={
// comment
[el1, el2]
}
propObj={
// comment
{ key: val }
}
propTemplate={
// comment
`text`
}
/>;