Prevent inlining JSXAttribute if it has comments (#3132)
parent
38b15f7899
commit
21c6d4f501
|
@ -1717,18 +1717,24 @@ function genericPrintNoParens(path, options, print, args) {
|
||||||
case "JSXExpressionContainer": {
|
case "JSXExpressionContainer": {
|
||||||
const parent = path.getParentNode(0);
|
const parent = path.getParentNode(0);
|
||||||
|
|
||||||
|
const preventInline =
|
||||||
|
parent.type === "JSXAttribute" &&
|
||||||
|
n.expression.comments &&
|
||||||
|
n.expression.comments.length > 0;
|
||||||
|
|
||||||
const shouldInline =
|
const shouldInline =
|
||||||
n.expression.type === "ArrayExpression" ||
|
!preventInline &&
|
||||||
n.expression.type === "ObjectExpression" ||
|
(n.expression.type === "ArrayExpression" ||
|
||||||
n.expression.type === "ArrowFunctionExpression" ||
|
n.expression.type === "ObjectExpression" ||
|
||||||
n.expression.type === "CallExpression" ||
|
n.expression.type === "ArrowFunctionExpression" ||
|
||||||
n.expression.type === "FunctionExpression" ||
|
n.expression.type === "CallExpression" ||
|
||||||
n.expression.type === "JSXEmptyExpression" ||
|
n.expression.type === "FunctionExpression" ||
|
||||||
n.expression.type === "TemplateLiteral" ||
|
n.expression.type === "JSXEmptyExpression" ||
|
||||||
n.expression.type === "TaggedTemplateExpression" ||
|
n.expression.type === "TemplateLiteral" ||
|
||||||
(parent.type === "JSXElement" &&
|
n.expression.type === "TaggedTemplateExpression" ||
|
||||||
(n.expression.type === "ConditionalExpression" ||
|
(parent.type === "JSXElement" &&
|
||||||
isBinaryish(n.expression)));
|
(n.expression.type === "ConditionalExpression" ||
|
||||||
|
isBinaryish(n.expression))));
|
||||||
|
|
||||||
if (shouldInline) {
|
if (shouldInline) {
|
||||||
const printExpression =
|
const printExpression =
|
||||||
|
|
|
@ -1,5 +1,58 @@
|
||||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
// 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`] = `
|
exports[`conditional-expression.js 1`] = `
|
||||||
// There are two ways to print ConditionalExpressions: "normal mode" and
|
// There are two ways to print ConditionalExpressions: "normal mode" and
|
||||||
// "JSX mode". This is normal mode (when breaking):
|
// "JSX mode". This is normal mode (when breaking):
|
||||||
|
|
|
@ -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`
|
||||||
|
}
|
||||||
|
/>;
|
Loading…
Reference in New Issue