From aeed7840e2e6f8975cc820780ffecc6fcad08728 Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Mon, 22 May 2017 06:40:19 -0700 Subject: [PATCH] Do not inline arrow when argument has a leading comment (#1660) Fixes #1614 --- src/printer.js | 13 ++--- tests/arrows/__snapshots__/jsfmt.spec.js.snap | 51 +++++++++++++++++++ tests/arrows/comment.js | 23 +++++++++ 3 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 tests/arrows/comment.js diff --git a/src/printer.js b/src/printer.js index 5508c9398..7b233d445 100644 --- a/src/printer.js +++ b/src/printer.js @@ -392,12 +392,13 @@ function genericPrintNoParens(path, options, print, args) { // We want to always keep these types of nodes on the same line // as the arrow. if ( - n.body.type === "ArrayExpression" || - n.body.type === "ObjectExpression" || - n.body.type === "JSXElement" || - n.body.type === "BlockStatement" || - isTemplateOnItsOwnLine(n.body, options.originalText) || - n.body.type === "ArrowFunctionExpression" + !hasLeadingOwnLineComment(options.originalText, n.body) && + (n.body.type === "ArrayExpression" || + n.body.type === "ObjectExpression" || + n.body.type === "JSXElement" || + n.body.type === "BlockStatement" || + isTemplateOnItsOwnLine(n.body, options.originalText) || + n.body.type === "ArrowFunctionExpression") ) { return group(collapsed); } diff --git a/tests/arrows/__snapshots__/jsfmt.spec.js.snap b/tests/arrows/__snapshots__/jsfmt.spec.js.snap index 1c5b41267..5e325c154 100644 --- a/tests/arrows/__snapshots__/jsfmt.spec.js.snap +++ b/tests/arrows/__snapshots__/jsfmt.spec.js.snap @@ -239,6 +239,57 @@ jest.mock( `; +exports[`comment.js 1`] = ` +/** + * Curried function that ends with a BEM CSS Selector + * + * @param {String} block - the BEM Block you'd like to select. + * @returns {Function} + */ +export const bem = block => + /** + * @param {String} [element] - the BEM Element within that block; if undefined, selects the block itself. + * @returns {Function} + */ + element => + /** + * @param {?String} [modifier] - the BEM Modifier for the Block or Element; if undefined, selects the Block or Element unmodified. + * @returns {String} + */ + modifier => + [ + ".", + css(block), + element ? \`__\${css(element)}\` : "", + modifier ? \`--\${css(modifier)}\` : "" + ].join(""); +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +/** + * Curried function that ends with a BEM CSS Selector + * + * @param {String} block - the BEM Block you'd like to select. + * @returns {Function} + */ +export const bem = block => + /** + * @param {String} [element] - the BEM Element within that block; if undefined, selects the block itself. + * @returns {Function} + */ + element => + /** + * @param {?String} [modifier] - the BEM Modifier for the Block or Element; if undefined, selects the Block or Element unmodified. + * @returns {String} + */ + modifier => + [ + ".", + css(block), + element ? \`__\${css(element)}\` : "", + modifier ? \`--\${css(modifier)}\` : "" + ].join(""); + +`; + exports[`currying.js 1`] = ` const fn = b => c => d => { return 3; diff --git a/tests/arrows/comment.js b/tests/arrows/comment.js new file mode 100644 index 000000000..b7c0724d5 --- /dev/null +++ b/tests/arrows/comment.js @@ -0,0 +1,23 @@ +/** + * Curried function that ends with a BEM CSS Selector + * + * @param {String} block - the BEM Block you'd like to select. + * @returns {Function} + */ +export const bem = block => + /** + * @param {String} [element] - the BEM Element within that block; if undefined, selects the block itself. + * @returns {Function} + */ + element => + /** + * @param {?String} [modifier] - the BEM Modifier for the Block or Element; if undefined, selects the Block or Element unmodified. + * @returns {String} + */ + modifier => + [ + ".", + css(block), + element ? `__${css(element)}` : "", + modifier ? `--${css(modifier)}` : "" + ].join("");