Break long imports (#590)

Fixes #576
master
Christopher Chedeau 2017-02-03 13:19:14 -08:00 committed by James Long
parent e623668183
commit 0924e7f3dc
3 changed files with 40 additions and 6 deletions

View File

@ -417,14 +417,15 @@ function genericPrintNoParens(path, options, print) {
case "ImportDeclaration":
parts.push("import ");
const fromParts = [];
if (n.importKind && n.importKind !== "value") {
parts.push(n.importKind + " ");
}
var standalones = [];
var grouped = [];
if (n.specifiers && n.specifiers.length > 0) {
var standalones = [];
var grouped = [];
path.each(
function(specifierPath) {
var value = specifierPath.getValue();
@ -468,12 +469,28 @@ function genericPrintNoParens(path, options, print) {
);
}
parts.push(" from ");
fromParts.push(grouped.length === 0 ? line : " ", "from ");
}
parts.push(path.call(print, "source"), ";");
fromParts.push(path.call(print, "source"), ";");
return concat(parts);
// If there's a very long import, break the following way:
//
// import veryLong
// from 'verylong'
//
// In case there are grouped elements, they will already break the way
// we want and this break would take precedence instead.
if (grouped.length === 0) {
return group(
concat([
concat(parts),
indent(options.tabWidth, concat(fromParts))
])
);
}
return concat([concat(parts), concat(fromParts)]);
case "Import": {
return "import";

View File

@ -52,6 +52,22 @@ import {fitsIn, oneLine} from \".\";
"
`;
exports[`test long-line.js 1`] = `
"import someCoolUtilWithARatherLongName from \'../../../../utils/someCoolUtilWithARatherLongName\';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import someCoolUtilWithARatherLongName
from \"../../../../utils/someCoolUtilWithARatherLongName\";
"
`;
exports[`test long-line.js 2`] = `
"import someCoolUtilWithARatherLongName from \'../../../../utils/someCoolUtilWithARatherLongName\';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import someCoolUtilWithARatherLongName
from \"../../../../utils/someCoolUtilWithARatherLongName\";
"
`;
exports[`test multiple_standalones.js 1`] = `
"import a, * as b from \'a\';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -0,0 +1 @@
import someCoolUtilWithARatherLongName from '../../../../utils/someCoolUtilWithARatherLongName';