diff --git a/CHANGELOG.unreleased.md b/CHANGELOG.unreleased.md index f3a3215d9848..2238ad043886 100644 --- a/CHANGELOG.unreleased.md +++ b/CHANGELOG.unreleased.md @@ -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]) + + + ```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]) + + + ```js // Input new class {}; new function() {} diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index e74a13bd1315..7618450613ce 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -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( @@ -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( @@ -4051,7 +4055,7 @@ function printArgumentsList(path, options, print) { concat([ "(", indent(concat([softline, concat(printedArguments)])), - ifBreak(shouldPrintComma(options, "all") ? "," : ""), + ifBreak(maybeTrailingComma), softline, ")" ]), @@ -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" ); } diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap index dfe76f942040..9198e1dc3efc 100644 --- a/tests/comments/__snapshots__/jsfmt.spec.js.snap +++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap @@ -530,6 +530,18 @@ import(/* Hello */ 'something' /* Hello */) import('something' /* Hello */ + 'else') +import( + /* Hello */ + 'something' + /* Hello */ +) + +wrap( + import(/* Hello */ + 'something' + ) +) + =====================================output===================================== import(/* Hello */ "something"); @@ -539,6 +551,19 @@ import(/* Hello */ "something" /* Hello */); import("something" /* Hello */ + "else"); +import( + /* Hello */ + "something" + /* Hello */ +); + +wrap( + import( + /* Hello */ + "something" + ) +); + ================================================================================ `; diff --git a/tests/comments/dynamic_imports.js b/tests/comments/dynamic_imports.js index 499b0f78d350..548c20f02923 100644 --- a/tests/comments/dynamic_imports.js +++ b/tests/comments/dynamic_imports.js @@ -5,3 +5,15 @@ import('something' /* Hello */) import(/* Hello */ 'something' /* Hello */) import('something' /* Hello */ + 'else') + +import( + /* Hello */ + 'something' + /* Hello */ +) + +wrap( + import(/* Hello */ + 'something' + ) +) diff --git a/tests/import_then/__snapshots__/jsfmt.spec.js.snap b/tests/import_then/__snapshots__/jsfmt.spec.js.snap deleted file mode 100644 index d8f52eff8293..000000000000 --- a/tests/import_then/__snapshots__/jsfmt.spec.js.snap +++ /dev/null @@ -1,44 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`long.js 1`] = ` -====================================options===================================== -parsers: ["flow", "babel"] -printWidth: 80 - | printWidth -=====================================input====================================== -import( - 'myreallylongdynamicallyloadedmodulenamemyreallylongdynamicallyloadedmodulename' -); - -import( - 'myreallylongdynamicallyloadedmodulenamemyreallylongdynamicallyloadedmodulename' -).then(exports => { -}); - -=====================================output===================================== -import("myreallylongdynamicallyloadedmodulenamemyreallylongdynamicallyloadedmodulename"); - -import("myreallylongdynamicallyloadedmodulenamemyreallylongdynamicallyloadedmodulename").then( - exports => {} -); - -================================================================================ -`; - -exports[`then.js 1`] = ` -====================================options===================================== -parsers: ["flow", "babel"] -printWidth: 80 - | printWidth -=====================================input====================================== -const x = import('some-module').then(x => { - // ... -}); - -=====================================output===================================== -const x = import("some-module").then(x => { - // ... -}); - -================================================================================ -`; diff --git a/tests/import_then/jsfmt.spec.js b/tests/import_then/jsfmt.spec.js deleted file mode 100644 index fbfa6501a049..000000000000 --- a/tests/import_then/jsfmt.spec.js +++ /dev/null @@ -1 +0,0 @@ -run_spec(__dirname, ["flow", "babel"]); diff --git a/tests/import_then/long.js b/tests/import_then/long.js deleted file mode 100644 index 72a43bf15274..000000000000 --- a/tests/import_then/long.js +++ /dev/null @@ -1,8 +0,0 @@ -import( - 'myreallylongdynamicallyloadedmodulenamemyreallylongdynamicallyloadedmodulename' -); - -import( - 'myreallylongdynamicallyloadedmodulenamemyreallylongdynamicallyloadedmodulename' -).then(exports => { -}); diff --git a/tests/import_then/then.js b/tests/import_then/then.js deleted file mode 100644 index e5bdbf2f389f..000000000000 --- a/tests/import_then/then.js +++ /dev/null @@ -1,3 +0,0 @@ -const x = import('some-module').then(x => { - // ... -}); diff --git a/tests/trailing_comma/__snapshots__/jsfmt.spec.js.snap b/tests/trailing_comma/__snapshots__/jsfmt.spec.js.snap index 6a4cf9701908..006fbe0594c5 100644 --- a/tests/trailing_comma/__snapshots__/jsfmt.spec.js.snap +++ b/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"] diff --git a/tests/trailing_comma/dynamic-import.js b/tests/trailing_comma/dynamic-import.js new file mode 100644 index 000000000000..b4c11e9a062e --- /dev/null +++ b/tests/trailing_comma/dynamic-import.js @@ -0,0 +1,3 @@ +import( + 'myreallylongdynamicallyloadedmodulenamemyreallylongdynamicallyloadedmodulename' +);