Navigation Menu

Skip to content

Commit

Permalink
fix multiline dynamic import comments (#6025)
Browse files Browse the repository at this point in the history
  • Loading branch information
noahsug authored and vjeux committed Apr 2, 2019
1 parent 1e471a0 commit 0b07e10
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 62 deletions.
34 changes: 33 additions & 1 deletion CHANGELOG.unreleased.md
Expand Up @@ -42,9 +42,41 @@ Examples:
-->

- JavaScript: Add parentheses for immediately-constructed fn/class ([#5996] by [@bakkot])
- JavaScript: Fix multiline dynamic import comments ([#6025] by [@noahsug])

<!-- prettier-ignore -->
```js
// Input
import(
/* Hello */
'something'
/* Hello */
)
import(
'myreallylongdynamicallyloadedmodulenamemyreallylongdynamicallyloadedmodulename'
)

// Output (Prettier stable)
import(/* Hello */
"something");
/* Hello */
import('myreallylongdynamicallyloadedmodulenamemyreallylongdynamicallyloadedmodulename');

// Output (Prettier master)
import(
/* Hello */
'something'
/* Hello */
)
import(
'myreallylongdynamicallyloadedmodulenamemyreallylongdynamicallyloadedmodulename'
);
```

- JavaScript: Add parentheses for immediately-constructed fn/class ([#5996] by [@bakkot])

<!-- prettier-ignore -->
```js
// Input
new class {};
new function() {}
Expand Down
13 changes: 8 additions & 5 deletions src/language-js/printer-estree.js
Expand Up @@ -1165,7 +1165,6 @@ function printPathNoParens(path, options, print, args) {
(!isNew &&
n.callee.type === "Identifier" &&
(n.callee.name === "require" || n.callee.name === "define")) ||
n.callee.type === "Import" ||
// Template literals as single arguments
(n.arguments.length === 1 &&
isTemplateOnItsOwnLine(
Expand Down Expand Up @@ -3953,7 +3952,12 @@ function printArgumentsList(path, options, print) {
return concat(parts);
}, "arguments");

const maybeTrailingComma = shouldPrintComma(options, "all") ? "," : "";
const maybeTrailingComma =
// Dynamic imports cannot have trailing commas
!(node.callee && node.callee.type === "Import") &&
shouldPrintComma(options, "all")
? ","
: "";

function allArgsBrokenOut() {
return group(
Expand Down Expand Up @@ -4051,7 +4055,7 @@ function printArgumentsList(path, options, print) {
concat([
"(",
indent(concat([softline, concat(printedArguments)])),
ifBreak(shouldPrintComma(options, "all") ? "," : ""),
ifBreak(maybeTrailingComma),
softline,
")"
]),
Expand Down Expand Up @@ -6435,8 +6439,7 @@ function canAttachComment(node) {
node.type !== "Block" &&
node.type !== "EmptyStatement" &&
node.type !== "TemplateElement" &&
node.type !== "Import" &&
!(node.callee && node.callee.type === "Import")
node.type !== "Import"
);
}

Expand Down
25 changes: 25 additions & 0 deletions tests/comments/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -530,6 +530,18 @@ import(/* Hello */ 'something' /* Hello */)
import('something' /* Hello */ + 'else')
import(
/* Hello */
'something'
/* Hello */
)
wrap(
import(/* Hello */
'something'
)
)
=====================================output=====================================
import(/* Hello */ "something");
Expand All @@ -539,6 +551,19 @@ import(/* Hello */ "something" /* Hello */);
import("something" /* Hello */ + "else");
import(
/* Hello */
"something"
/* Hello */
);
wrap(
import(
/* Hello */
"something"
)
);
================================================================================
`;
Expand Down
12 changes: 12 additions & 0 deletions tests/comments/dynamic_imports.js
Expand Up @@ -5,3 +5,15 @@ import('something' /* Hello */)
import(/* Hello */ 'something' /* Hello */)

import('something' /* Hello */ + 'else')

import(
/* Hello */
'something'
/* Hello */
)

wrap(
import(/* Hello */
'something'
)
)
44 changes: 0 additions & 44 deletions tests/import_then/__snapshots__/jsfmt.spec.js.snap

This file was deleted.

1 change: 0 additions & 1 deletion tests/import_then/jsfmt.spec.js

This file was deleted.

8 changes: 0 additions & 8 deletions tests/import_then/long.js

This file was deleted.

3 changes: 0 additions & 3 deletions tests/import_then/then.js

This file was deleted.

56 changes: 56 additions & 0 deletions tests/trailing_comma/__snapshots__/jsfmt.spec.js.snap
@@ -1,5 +1,61 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`dynamic-import.js 1`] = `
====================================options=====================================
parsers: ["flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
import(
'myreallylongdynamicallyloadedmodulenamemyreallylongdynamicallyloadedmodulename'
);
=====================================output=====================================
import(
"myreallylongdynamicallyloadedmodulenamemyreallylongdynamicallyloadedmodulename"
);
================================================================================
`;

exports[`dynamic-import.js 2`] = `
====================================options=====================================
parsers: ["flow", "typescript"]
printWidth: 80
trailingComma: "all"
| printWidth
=====================================input======================================
import(
'myreallylongdynamicallyloadedmodulenamemyreallylongdynamicallyloadedmodulename'
);
=====================================output=====================================
import(
"myreallylongdynamicallyloadedmodulenamemyreallylongdynamicallyloadedmodulename"
);
================================================================================
`;

exports[`dynamic-import.js 3`] = `
====================================options=====================================
parsers: ["flow", "typescript"]
printWidth: 80
trailingComma: "es5"
| printWidth
=====================================input======================================
import(
'myreallylongdynamicallyloadedmodulenamemyreallylongdynamicallyloadedmodulename'
);
=====================================output=====================================
import(
"myreallylongdynamicallyloadedmodulenamemyreallylongdynamicallyloadedmodulename"
);
================================================================================
`;

exports[`es5.js 1`] = `
====================================options=====================================
parsers: ["flow", "typescript"]
Expand Down
3 changes: 3 additions & 0 deletions tests/trailing_comma/dynamic-import.js
@@ -0,0 +1,3 @@
import(
'myreallylongdynamicallyloadedmodulenamemyreallylongdynamicallyloadedmodulename'
);

0 comments on commit 0b07e10

Please sign in to comment.