Skip to content

Commit

Permalink
prettierx: option to import on one line in JS&TS (#265)
Browse files Browse the repository at this point in the history
* add option to skip multi-line import formatting in JS/TS
* use join to support multiple imports on the same line
* keep the test case in tests/import_formatting
* include test on Babel parsers in the test case
* name the option as importFormatting, with choices: auto & oneline
* no `since` specified for the new option (not needed for prettierx)

Co-authored-by: Dmitry Koterov <dmitry.koterov@gmail.com>
Co-authored-by: Christopher J. Brody <chris.brody+brodybits@gmail.com>
  • Loading branch information
brodybits and dimikot committed Jul 21, 2020
1 parent 8eecbe6 commit cb693fd
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 5 deletions.
18 changes: 18 additions & 0 deletions src/language-js/options.js
Expand Up @@ -32,6 +32,24 @@ module.exports = {
oppositeDescription: "Do not print indents at the start of chained calls."
},
parenSpacing: commonOptions.parenSpacing,
importFormatting: {
category: CATEGORY_JAVASCRIPT,
type: "choice",
default: "auto",
description:
"Formatting of import statements, may be `oneline` to avoid confict with" +
' VSCode "Organize Imports" feature.',
choices: [
{
value: "auto",
description: "automatic formatting, like Prettier"
},
{
value: "oneline",
description: "keep import statements on one line"
}
]
},
jsxBracketSameLine: {
since: "0.17.0",
category: CATEGORY_JAVASCRIPT,
Expand Down
13 changes: 8 additions & 5 deletions src/language-js/printer-estree.js
Expand Up @@ -1142,16 +1142,19 @@ function printPathNoParens(path, options, print, args) {
}

if (
grouped.length === 1 &&
standalones.length === 0 &&
n.specifiers &&
!n.specifiers.some(node => node.comments)
// prettierx: importFormatting
options.importFormatting === "oneline" ||
(grouped.length === 1 &&
standalones.length === 0 &&
n.specifiers &&
!n.specifiers.some(node => node.comments))
) {
parts.push(
concat([
"{",
options.bracketSpacing ? " " : "",
concat(grouped),
// prettierx: importFormatting
join(", ", grouped),
options.bracketSpacing ? " " : "",
"}"
])
Expand Down
69 changes: 69 additions & 0 deletions tests/import_formatting/__snapshots__/jsfmt.spec.js.snap
@@ -0,0 +1,69 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`long_import.js 1`] = `
====================================options=====================================
importFormatting: "oneline"
parsers: ["babel", "babel-flow", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
import abc, { Lorem, ipsum, dolor, sit, amet, consectetur, adipiscing, elit, sed, eiusmod, tempor, incididunt, ut, labore, et, magna } from "some-module";
import { Lorem, ipsum, dolor, sit, amet, consectetur, adipiscing, elit, sed, eiusmod, tempor, incididunt, ut, labore, et, magna } from "some-module";
=====================================output=====================================
import abc, { Lorem, ipsum, dolor, sit, amet, consectetur, adipiscing, elit, sed, eiusmod, tempor, incididunt, ut, labore, et, magna } from "some-module";
import { Lorem, ipsum, dolor, sit, amet, consectetur, adipiscing, elit, sed, eiusmod, tempor, incididunt, ut, labore, et, magna } from "some-module";
================================================================================
`;

exports[`long_import.js 2`] = `
====================================options=====================================
importFormatting: "auto"
parsers: ["babel", "babel-flow", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
import abc, { Lorem, ipsum, dolor, sit, amet, consectetur, adipiscing, elit, sed, eiusmod, tempor, incididunt, ut, labore, et, magna } from "some-module";
import { Lorem, ipsum, dolor, sit, amet, consectetur, adipiscing, elit, sed, eiusmod, tempor, incididunt, ut, labore, et, magna } from "some-module";
=====================================output=====================================
import abc, {
Lorem,
ipsum,
dolor,
sit,
amet,
consectetur,
adipiscing,
elit,
sed,
eiusmod,
tempor,
incididunt,
ut,
labore,
et,
magna
} from "some-module";
import {
Lorem,
ipsum,
dolor,
sit,
amet,
consectetur,
adipiscing,
elit,
sed,
eiusmod,
tempor,
incididunt,
ut,
labore,
et,
magna
} from "some-module";
================================================================================
`;
6 changes: 6 additions & 0 deletions tests/import_formatting/jsfmt.spec.js
@@ -0,0 +1,6 @@
run_spec(__dirname, ["babel", "babel-flow", "flow", "typescript"], {
importFormatting: "oneline"
});
run_spec(__dirname, ["babel", "babel-flow", "flow", "typescript"], {
importFormatting: "auto"
});
2 changes: 2 additions & 0 deletions tests/import_formatting/long_import.js
@@ -0,0 +1,2 @@
import abc, { Lorem, ipsum, dolor, sit, amet, consectetur, adipiscing, elit, sed, eiusmod, tempor, incididunt, ut, labore, et, magna } from "some-module";
import { Lorem, ipsum, dolor, sit, amet, consectetur, adipiscing, elit, sed, eiusmod, tempor, incididunt, ut, labore, et, magna } from "some-module";
6 changes: 6 additions & 0 deletions tests_integration/__tests__/__snapshots__/early-exit.js.snap
Expand Up @@ -76,6 +76,9 @@ Format options:
--html-whitespace-sensitivity <css|strict|ignore>
How to handle whitespaces in HTML.
Defaults to css.
--import-formatting <auto|oneline>
Formatting of import statements, may be \`oneline\` to avoid confict with VSCode \\"Organize Imports\\" feature.
Defaults to auto.
--no-indent-chains Do not print indents at the start of chained calls.
--jsx-bracket-same-line Put > on the last line instead of at a new line.
Defaults to false.
Expand Down Expand Up @@ -248,6 +251,9 @@ Format options:
--html-whitespace-sensitivity <css|strict|ignore>
How to handle whitespaces in HTML.
Defaults to css.
--import-formatting <auto|oneline>
Formatting of import statements, may be \`oneline\` to avoid confict with VSCode \\"Organize Imports\\" feature.
Defaults to auto.
--no-indent-chains Do not print indents at the start of chained calls.
--jsx-bracket-same-line Put > on the last line instead of at a new line.
Defaults to false.
Expand Down
18 changes: 18 additions & 0 deletions tests_integration/__tests__/__snapshots__/help-options.js.snap
Expand Up @@ -255,6 +255,24 @@ Default: .prettierignore
exports[`show detailed usage with --help ignore-path (write) 1`] = `Array []`;
exports[`show detailed usage with --help import-formatting (stderr) 1`] = `""`;
exports[`show detailed usage with --help import-formatting (stdout) 1`] = `
"--import-formatting <auto|oneline>
Formatting of import statements, may be \`oneline\` to avoid confict with VSCode \\"Organize Imports\\" feature.
Valid options:
auto automatic formatting, like Prettier
oneline keep import statements on one line
Default: auto
"
`;
exports[`show detailed usage with --help import-formatting (write) 1`] = `Array []`;
exports[`show detailed usage with --help indent-chains (stderr) 1`] = `""`;
exports[`show detailed usage with --help indent-chains (stdout) 1`] = `
Expand Down
18 changes: 18 additions & 0 deletions tests_integration/__tests__/__snapshots__/schema.js.snap
Expand Up @@ -123,6 +123,24 @@ This option cannot be used with --range-start and --range-end.",
},
],
},
"importFormatting": Object {
"default": "auto",
"description": "Formatting of import statements, may be \`oneline\` to avoid confict with VSCode \\"Organize Imports\\" feature.",
"oneOf": Array [
Object {
"description": "automatic formatting, like Prettier",
"enum": Array [
"auto",
],
},
Object {
"description": "keep import statements on one line",
"enum": Array [
"oneline",
],
},
],
},
"indentChains": Object {
"default": true,
"description": "Print indents at the start of chained calls.",
Expand Down
26 changes: 26 additions & 0 deletions tests_integration/__tests__/__snapshots__/support-info.js.snap
Expand Up @@ -139,6 +139,14 @@ Object {
"default": "css",
"type": "choice",
},
"importFormatting": Object {
"choices": Array [
"auto",
"oneline",
],
"default": "auto",
"type": "choice",
},
"indentChains": Object {
"default": true,
"type": "boolean",
Expand Down Expand Up @@ -889,6 +897,24 @@ exports[`CLI --support-info (stdout) 1`] = `
\\"since\\": \\"1.15.0\\",
\\"type\\": \\"choice\\"
},
{
\\"category\\": \\"JavaScript\\",
\\"choices\\": [
{
\\"description\\": \\"automatic formatting, like Prettier\\",
\\"value\\": \\"auto\\"
},
{
\\"description\\": \\"keep import statements on one line\\",
\\"value\\": \\"oneline\\"
}
],
\\"default\\": \\"auto\\",
\\"description\\": \\"Formatting of import statements, may be \`oneline\` to avoid confict with VSCode \\\\\\"Organize Imports\\\\\\" feature.\\",
\\"name\\": \\"importFormatting\\",
\\"pluginDefaults\\": {},
\\"type\\": \\"choice\\"
},
{
\\"category\\": \\"JavaScript\\",
\\"default\\": true,
Expand Down

0 comments on commit cb693fd

Please sign in to comment.