From dec718f3e3a35ce2c82328090d8b4d0d0e7c13a6 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 29 Dec 2022 18:20:26 +0800 Subject: [PATCH 1/6] Add tests --- .../comment/__snapshots__/jsfmt.spec.js.snap | 115 ++++++++++++++++++ .../js/directives/comment/issue-14080-1.js | 11 ++ .../js/directives/comment/issue-14080.js | 5 + .../js/directives/comment/jsfmt.spec.js | 2 + 4 files changed, 133 insertions(+) create mode 100644 tests/format/js/directives/comment/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/format/js/directives/comment/issue-14080-1.js create mode 100644 tests/format/js/directives/comment/issue-14080.js create mode 100644 tests/format/js/directives/comment/jsfmt.spec.js diff --git a/tests/format/js/directives/comment/__snapshots__/jsfmt.spec.js.snap b/tests/format/js/directives/comment/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000000..575252c57299 --- /dev/null +++ b/tests/format/js/directives/comment/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,115 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`issue-14080.js - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +'use strict' /* comment */; + +function foo() { + 'use strict' /* comment */; +} + +=====================================output===================================== +"use strict" /* comment */ + +function foo() { + "use strict" /* comment */ +} + +================================================================================ +`; + +exports[`issue-14080.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +'use strict' /* comment */; + +function foo() { + 'use strict' /* comment */; +} + +=====================================output===================================== +"use strict" /* comment */; + +function foo() { + "use strict" /* comment */; +} + +================================================================================ +`; + +exports[`issue-14080-1.js - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +'use strict'; + +/* comment */ +[].forEach(); + +function foo() { + 'use strict'; + + /* comment */ + [].forEach(); +} + +=====================================output===================================== +"use strict" + +/* comment */ +;[].forEach() + +function foo() { + "use strict" + + /* comment */ + ;[].forEach() +} + +================================================================================ +`; + +exports[`issue-14080-1.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +'use strict'; + +/* comment */ +[].forEach(); + +function foo() { + 'use strict'; + + /* comment */ + [].forEach(); +} + +=====================================output===================================== +"use strict"; + +/* comment */ +[].forEach(); + +function foo() { + "use strict"; + + /* comment */ + [].forEach(); +} + +================================================================================ +`; diff --git a/tests/format/js/directives/comment/issue-14080-1.js b/tests/format/js/directives/comment/issue-14080-1.js new file mode 100644 index 000000000000..9132b50aed1b --- /dev/null +++ b/tests/format/js/directives/comment/issue-14080-1.js @@ -0,0 +1,11 @@ +'use strict'; + +/* comment */ +[].forEach(); + +function foo() { + 'use strict'; + + /* comment */ + [].forEach(); +} diff --git a/tests/format/js/directives/comment/issue-14080.js b/tests/format/js/directives/comment/issue-14080.js new file mode 100644 index 000000000000..1ccd6c95b3f1 --- /dev/null +++ b/tests/format/js/directives/comment/issue-14080.js @@ -0,0 +1,5 @@ +'use strict' /* comment */; + +function foo() { + 'use strict' /* comment */; +} diff --git a/tests/format/js/directives/comment/jsfmt.spec.js b/tests/format/js/directives/comment/jsfmt.spec.js new file mode 100644 index 000000000000..728e1ab5ce4e --- /dev/null +++ b/tests/format/js/directives/comment/jsfmt.spec.js @@ -0,0 +1,2 @@ +run_spec(__dirname, ["babel", "flow", "typescript"]); +run_spec(__dirname, ["babel", "flow", "typescript"], { semi: false }); From 561609ab850a8f2203094250fdc85db934ec951f Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 29 Dec 2022 18:30:51 +0800 Subject: [PATCH 2/6] Fix --- src/language-js/print/literal.js | 14 +++++++++++++- src/language-js/print/misc.js | 19 +++++++++++++++++++ src/language-js/printer-estree.js | 28 ++-------------------------- 3 files changed, 34 insertions(+), 27 deletions(-) diff --git a/src/language-js/print/literal.js b/src/language-js/print/literal.js index a614bd5f4ba1..252cf4925a50 100644 --- a/src/language-js/print/literal.js +++ b/src/language-js/print/literal.js @@ -1,6 +1,7 @@ "use strict"; const { printString, printNumber } = require("../../common/util.js"); const { replaceTextEndOfLine } = require("../../document/doc-utils.js"); +const { printDirective } = require("./misc.js"); function printLiteral(path, options /*, print*/) { const node = path.getNode(); @@ -41,7 +42,9 @@ function printLiteral(path, options /*, print*/) { } if (typeof value === "string") { - return replaceTextEndOfLine(printString(node.raw, options)); + return isDirective(path) + ? printDirective(node.raw, options) + : replaceTextEndOfLine(printString(node.raw, options)); } return String(value); @@ -49,6 +52,15 @@ function printLiteral(path, options /*, print*/) { } } +function isDirective(path) { + if (path.getName() !== "expression") { + return; + } + + const parent = path.getParentNode(); + return parent.type === "ExpressionStatement" && parent.directive; +} + function printBigInt(raw) { return raw.toLowerCase(); } diff --git a/src/language-js/print/misc.js b/src/language-js/print/misc.js index 3d5a7d0f8194..44ae0ce9c761 100644 --- a/src/language-js/print/misc.js +++ b/src/language-js/print/misc.js @@ -93,6 +93,24 @@ function printRestSpread(path, options, print) { return ["...", print("argument"), printTypeAnnotation(path, options, print)]; } +function printDirective(rawText, options) { + const rawContent = rawText.slice(1, -1); + + // Check for the alternate quote, to determine if we're allowed to swap + // the quotes on a DirectiveLiteral. + if (rawContent.includes('"') || rawContent.includes("'")) { + return rawText; + } + + const enclosingQuote = options.singleQuote ? "'" : '"'; + + // Directives are exact code unit sequences, which means that you can't + // change the escape sequences they use. + // See https://github.com/prettier/prettier/issues/1555 + // and https://tc39.github.io/ecma262/#directive-prologue + return enclosingQuote + rawContent + enclosingQuote; +} + module.exports = { printOptionalToken, printDefiniteToken, @@ -102,4 +120,5 @@ module.exports = { printTypeAnnotation, printRestSpread, adjustClause, + printDirective, }; diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 75e8a71f89c3..6d76502f7926 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -23,7 +23,6 @@ const { isLineComment, isNextLineEmpty, needsHardlineAfterDanglingComment, - rawText, hasIgnoreComment, isCallExpression, isMemberExpression, @@ -47,6 +46,7 @@ const { adjustClause, printRestSpread, printDefiniteToken, + printDirective, } = require("./print/misc.js"); const { printImportDeclaration, @@ -215,11 +215,6 @@ function printPathNoParens(path, options, print, args) { case "EmptyStatement": return ""; case "ExpressionStatement": { - // Detect Flow and TypeScript directives - if (node.directive) { - return [printDirective(node.expression, options), semi]; - } - if ( options.parser === "__vue_event_binding" || options.parser === "__vue_ts_event_binding" @@ -429,7 +424,7 @@ function printPathNoParens(path, options, print, args) { case "Directive": return [print("value"), semi]; // Babel 6 case "DirectiveLiteral": - return printDirective(node, options); + return printDirective(node.extra.raw, options); case "UnaryExpression": parts.push(node.operator); @@ -813,25 +808,6 @@ function printPathNoParens(path, options, print, args) { } } -function printDirective(node, options) { - const raw = rawText(node); - const rawContent = raw.slice(1, -1); - - // Check for the alternate quote, to determine if we're allowed to swap - // the quotes on a DirectiveLiteral. - if (rawContent.includes('"') || rawContent.includes("'")) { - return raw; - } - - const enclosingQuote = options.singleQuote ? "'" : '"'; - - // Directives are exact code unit sequences, which means that you can't - // change the escape sequences they use. - // See https://github.com/prettier/prettier/issues/1555 - // and https://tc39.github.io/ecma262/#directive-prologue - return enclosingQuote + rawContent + enclosingQuote; -} - function canAttachComment(node) { return ( node.type && From 4230e07833f54251d1ab28bf7411bea745805662 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 29 Dec 2022 18:38:49 +0800 Subject: [PATCH 3/6] Use `snippets` to test --- .../comment/__snapshots__/jsfmt.spec.js.snap | 115 ------- .../js/directives/comment/issue-14080-1.js | 11 - .../js/directives/comment/issue-14080.js | 5 - .../js/directives/comment/jsfmt.spec.js | 2 - .../comments/__snapshots__/jsfmt.spec.js.snap | 297 ++++++++++++++++++ .../js/directives/comments/jsfmt.spec.js | 37 +++ 6 files changed, 334 insertions(+), 133 deletions(-) delete mode 100644 tests/format/js/directives/comment/__snapshots__/jsfmt.spec.js.snap delete mode 100644 tests/format/js/directives/comment/issue-14080-1.js delete mode 100644 tests/format/js/directives/comment/issue-14080.js delete mode 100644 tests/format/js/directives/comment/jsfmt.spec.js create mode 100644 tests/format/js/directives/comments/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/format/js/directives/comments/jsfmt.spec.js diff --git a/tests/format/js/directives/comment/__snapshots__/jsfmt.spec.js.snap b/tests/format/js/directives/comment/__snapshots__/jsfmt.spec.js.snap deleted file mode 100644 index 575252c57299..000000000000 --- a/tests/format/js/directives/comment/__snapshots__/jsfmt.spec.js.snap +++ /dev/null @@ -1,115 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`issue-14080.js - {"semi":false} format 1`] = ` -====================================options===================================== -parsers: ["babel", "flow", "typescript"] -printWidth: 80 -semi: false - | printWidth -=====================================input====================================== -'use strict' /* comment */; - -function foo() { - 'use strict' /* comment */; -} - -=====================================output===================================== -"use strict" /* comment */ - -function foo() { - "use strict" /* comment */ -} - -================================================================================ -`; - -exports[`issue-14080.js format 1`] = ` -====================================options===================================== -parsers: ["babel", "flow", "typescript"] -printWidth: 80 - | printWidth -=====================================input====================================== -'use strict' /* comment */; - -function foo() { - 'use strict' /* comment */; -} - -=====================================output===================================== -"use strict" /* comment */; - -function foo() { - "use strict" /* comment */; -} - -================================================================================ -`; - -exports[`issue-14080-1.js - {"semi":false} format 1`] = ` -====================================options===================================== -parsers: ["babel", "flow", "typescript"] -printWidth: 80 -semi: false - | printWidth -=====================================input====================================== -'use strict'; - -/* comment */ -[].forEach(); - -function foo() { - 'use strict'; - - /* comment */ - [].forEach(); -} - -=====================================output===================================== -"use strict" - -/* comment */ -;[].forEach() - -function foo() { - "use strict" - - /* comment */ - ;[].forEach() -} - -================================================================================ -`; - -exports[`issue-14080-1.js format 1`] = ` -====================================options===================================== -parsers: ["babel", "flow", "typescript"] -printWidth: 80 - | printWidth -=====================================input====================================== -'use strict'; - -/* comment */ -[].forEach(); - -function foo() { - 'use strict'; - - /* comment */ - [].forEach(); -} - -=====================================output===================================== -"use strict"; - -/* comment */ -[].forEach(); - -function foo() { - "use strict"; - - /* comment */ - [].forEach(); -} - -================================================================================ -`; diff --git a/tests/format/js/directives/comment/issue-14080-1.js b/tests/format/js/directives/comment/issue-14080-1.js deleted file mode 100644 index 9132b50aed1b..000000000000 --- a/tests/format/js/directives/comment/issue-14080-1.js +++ /dev/null @@ -1,11 +0,0 @@ -'use strict'; - -/* comment */ -[].forEach(); - -function foo() { - 'use strict'; - - /* comment */ - [].forEach(); -} diff --git a/tests/format/js/directives/comment/issue-14080.js b/tests/format/js/directives/comment/issue-14080.js deleted file mode 100644 index 1ccd6c95b3f1..000000000000 --- a/tests/format/js/directives/comment/issue-14080.js +++ /dev/null @@ -1,5 +0,0 @@ -'use strict' /* comment */; - -function foo() { - 'use strict' /* comment */; -} diff --git a/tests/format/js/directives/comment/jsfmt.spec.js b/tests/format/js/directives/comment/jsfmt.spec.js deleted file mode 100644 index 728e1ab5ce4e..000000000000 --- a/tests/format/js/directives/comment/jsfmt.spec.js +++ /dev/null @@ -1,2 +0,0 @@ -run_spec(__dirname, ["babel", "flow", "typescript"]); -run_spec(__dirname, ["babel", "flow", "typescript"], { semi: false }); diff --git a/tests/format/js/directives/comments/__snapshots__/jsfmt.spec.js.snap b/tests/format/js/directives/comments/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000000..2bfb3f3ad837 --- /dev/null +++ b/tests/format/js/directives/comments/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,297 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`snippet: #0 - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +'use strict' /* comment */; +=====================================output===================================== +"use strict" /* comment */ + +================================================================================ +`; + +exports[`snippet: #0 format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +'use strict' /* comment */; +=====================================output===================================== +"use strict" /* comment */; + +================================================================================ +`; + +exports[`snippet: #1 - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +function foo() { + 'use strict' /* comment */; +} +=====================================output===================================== +function foo() { + "use strict" /* comment */ +} + +================================================================================ +`; + +exports[`snippet: #1 format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +function foo() { + 'use strict' /* comment */; +} +=====================================output===================================== +function foo() { + "use strict" /* comment */; +} + +================================================================================ +`; + +exports[`snippet: #2 - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +'use strict' // comment +=====================================output===================================== +"use strict" // comment + +================================================================================ +`; + +exports[`snippet: #2 format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +'use strict' // comment +=====================================output===================================== +"use strict"; // comment + +================================================================================ +`; + +exports[`snippet: #3 - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +function foo() { + 'use strict' // comment +} +=====================================output===================================== +function foo() { + "use strict" // comment +} + +================================================================================ +`; + +exports[`snippet: #3 format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +function foo() { + 'use strict' // comment +} +=====================================output===================================== +function foo() { + "use strict"; // comment +} + +================================================================================ +`; + +exports[`snippet: #4 - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +'use strict'; + +/* comment */ +(function () {})(); +=====================================output===================================== +"use strict" + +/* comment */ +;(function () {})() + +================================================================================ +`; + +exports[`snippet: #4 format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +'use strict'; + +/* comment */ +(function () {})(); +=====================================output===================================== +"use strict"; + +/* comment */ +(function () {})(); + +================================================================================ +`; + +exports[`snippet: #5 - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +function foo() { + 'use strict'; + + /* comment */ + (function () {})(); +} +=====================================output===================================== +function foo() { + "use strict" + + /* comment */ + ;(function () {})() +} + +================================================================================ +`; + +exports[`snippet: #5 format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +function foo() { + 'use strict'; + + /* comment */ + (function () {})(); +} +=====================================output===================================== +function foo() { + "use strict"; + + /* comment */ + (function () {})(); +} + +================================================================================ +`; + +exports[`snippet: #6 - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +'use strict'; + +// comment +(function () {})(); +=====================================output===================================== +"use strict" + +// comment +;(function () {})() + +================================================================================ +`; + +exports[`snippet: #6 format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +'use strict'; + +// comment +(function () {})(); +=====================================output===================================== +"use strict"; + +// comment +(function () {})(); + +================================================================================ +`; + +exports[`snippet: #7 - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +function foo() { + 'use strict'; + + // comment + (function () {})(); +} +=====================================output===================================== +function foo() { + "use strict" + + // comment + ;(function () {})() +} + +================================================================================ +`; + +exports[`snippet: #7 format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +function foo() { + 'use strict'; + + // comment + (function () {})(); +} +=====================================output===================================== +function foo() { + "use strict"; + + // comment + (function () {})(); +} + +================================================================================ +`; diff --git a/tests/format/js/directives/comments/jsfmt.spec.js b/tests/format/js/directives/comments/jsfmt.spec.js new file mode 100644 index 000000000000..7b2daca3d7da --- /dev/null +++ b/tests/format/js/directives/comments/jsfmt.spec.js @@ -0,0 +1,37 @@ +const { outdent } = require("outdent"); +const indent = (text) => + text + .split("\n") + .map((line) => (line ? ` ${line}` : line)) + .join("\n"); + +const snippets = [ + "'use strict' /* comment */;", + outdent` + 'use strict' // comment + `, + outdent` + 'use strict'; + + /* comment */ + (function () {})(); + `, + outdent` + 'use strict'; + + // comment + (function () {})(); + `, +].flatMap((code) => [ + code, + outdent` + function foo() { + ${indent(code)} + } + `, +]); + +run_spec({ dirname: __dirname, snippets }, ["babel", "flow", "typescript"]); +run_spec({ dirname: __dirname, snippets }, ["babel", "flow", "typescript"], { + semi: false, +}); From d2370f3991db662d256064724f6cb18fca67f042 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 29 Dec 2022 18:42:15 +0800 Subject: [PATCH 4/6] More tests --- .../comments/__snapshots__/jsfmt.spec.js.snap | 330 ++++++++++++++++-- .../js/directives/comments/jsfmt.spec.js | 17 +- 2 files changed, 316 insertions(+), 31 deletions(-) diff --git a/tests/format/js/directives/comments/__snapshots__/jsfmt.spec.js.snap b/tests/format/js/directives/comments/__snapshots__/jsfmt.spec.js.snap index 2bfb3f3ad837..62f891b2ff16 100644 --- a/tests/format/js/directives/comments/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/js/directives/comments/__snapshots__/jsfmt.spec.js.snap @@ -7,6 +7,68 @@ printWidth: 80 semi: false | printWidth =====================================input====================================== +/* comment */ 'use strict'; +=====================================output===================================== +/* comment */ "use strict" + +================================================================================ +`; + +exports[`snippet: #0 format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +/* comment */ 'use strict'; +=====================================output===================================== +/* comment */ "use strict"; + +================================================================================ +`; + +exports[`snippet: #1 - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +function foo() { + /* comment */ 'use strict'; +} +=====================================output===================================== +function foo() { + /* comment */ "use strict" +} + +================================================================================ +`; + +exports[`snippet: #1 format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +function foo() { + /* comment */ 'use strict'; +} +=====================================output===================================== +function foo() { + /* comment */ "use strict"; +} + +================================================================================ +`; + +exports[`snippet: #2 - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== 'use strict' /* comment */; =====================================output===================================== "use strict" /* comment */ @@ -14,7 +76,7 @@ semi: false ================================================================================ `; -exports[`snippet: #0 format 1`] = ` +exports[`snippet: #2 format 1`] = ` ====================================options===================================== parsers: ["babel", "flow", "typescript"] printWidth: 80 @@ -27,7 +89,7 @@ printWidth: 80 ================================================================================ `; -exports[`snippet: #1 - {"semi":false} format 1`] = ` +exports[`snippet: #3 - {"semi":false} format 1`] = ` ====================================options===================================== parsers: ["babel", "flow", "typescript"] printWidth: 80 @@ -45,7 +107,7 @@ function foo() { ================================================================================ `; -exports[`snippet: #1 format 1`] = ` +exports[`snippet: #3 format 1`] = ` ====================================options===================================== parsers: ["babel", "flow", "typescript"] printWidth: 80 @@ -62,7 +124,77 @@ function foo() { ================================================================================ `; -exports[`snippet: #2 - {"semi":false} format 1`] = ` +exports[`snippet: #4 - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +// comment +'use strict'; +=====================================output===================================== +// comment +"use strict" + +================================================================================ +`; + +exports[`snippet: #4 format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +// comment +'use strict'; +=====================================output===================================== +// comment +"use strict"; + +================================================================================ +`; + +exports[`snippet: #5 - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +function foo() { + // comment + 'use strict'; +} +=====================================output===================================== +function foo() { + // comment + "use strict" +} + +================================================================================ +`; + +exports[`snippet: #5 format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +function foo() { + // comment + 'use strict'; +} +=====================================output===================================== +function foo() { + // comment + "use strict"; +} + +================================================================================ +`; + +exports[`snippet: #6 - {"semi":false} format 1`] = ` ====================================options===================================== parsers: ["babel", "flow", "typescript"] printWidth: 80 @@ -76,7 +208,7 @@ semi: false ================================================================================ `; -exports[`snippet: #2 format 1`] = ` +exports[`snippet: #6 format 1`] = ` ====================================options===================================== parsers: ["babel", "flow", "typescript"] printWidth: 80 @@ -89,7 +221,7 @@ printWidth: 80 ================================================================================ `; -exports[`snippet: #3 - {"semi":false} format 1`] = ` +exports[`snippet: #7 - {"semi":false} format 1`] = ` ====================================options===================================== parsers: ["babel", "flow", "typescript"] printWidth: 80 @@ -107,7 +239,7 @@ function foo() { ================================================================================ `; -exports[`snippet: #3 format 1`] = ` +exports[`snippet: #7 format 1`] = ` ====================================options===================================== parsers: ["babel", "flow", "typescript"] printWidth: 80 @@ -124,7 +256,7 @@ function foo() { ================================================================================ `; -exports[`snippet: #4 - {"semi":false} format 1`] = ` +exports[`snippet: #8 - {"semi":false} format 1`] = ` ====================================options===================================== parsers: ["babel", "flow", "typescript"] printWidth: 80 @@ -132,38 +264,34 @@ semi: false | printWidth =====================================input====================================== 'use strict'; - /* comment */ (function () {})(); =====================================output===================================== "use strict" - /* comment */ ;(function () {})() ================================================================================ `; -exports[`snippet: #4 format 1`] = ` +exports[`snippet: #8 format 1`] = ` ====================================options===================================== parsers: ["babel", "flow", "typescript"] printWidth: 80 | printWidth =====================================input====================================== 'use strict'; - /* comment */ (function () {})(); =====================================output===================================== "use strict"; - /* comment */ (function () {})(); ================================================================================ `; -exports[`snippet: #5 - {"semi":false} format 1`] = ` +exports[`snippet: #9 - {"semi":false} format 1`] = ` ====================================options===================================== parsers: ["babel", "flow", "typescript"] printWidth: 80 @@ -172,14 +300,12 @@ semi: false =====================================input====================================== function foo() { 'use strict'; - /* comment */ (function () {})(); } =====================================output===================================== function foo() { "use strict" - /* comment */ ;(function () {})() } @@ -187,7 +313,7 @@ function foo() { ================================================================================ `; -exports[`snippet: #5 format 1`] = ` +exports[`snippet: #9 format 1`] = ` ====================================options===================================== parsers: ["babel", "flow", "typescript"] printWidth: 80 @@ -195,14 +321,12 @@ printWidth: 80 =====================================input====================================== function foo() { 'use strict'; - /* comment */ (function () {})(); } =====================================output===================================== function foo() { "use strict"; - /* comment */ (function () {})(); } @@ -210,46 +334,120 @@ function foo() { ================================================================================ `; -exports[`snippet: #6 - {"semi":false} format 1`] = ` +exports[`snippet: #10 - {"semi":false} format 1`] = ` ====================================options===================================== parsers: ["babel", "flow", "typescript"] printWidth: 80 semi: false | printWidth =====================================input====================================== +/* comment */ +'use strict'; +(function () {})(); +=====================================output===================================== +/* comment */ +"use strict" +;(function () {})() + +================================================================================ +`; + +exports[`snippet: #10 format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +/* comment */ 'use strict'; +(function () {})(); +=====================================output===================================== +/* comment */ +"use strict"; +(function () {})(); +================================================================================ +`; + +exports[`snippet: #11 - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +function foo() { + /* comment */ + 'use strict'; + (function () {})(); +} +=====================================output===================================== +function foo() { + /* comment */ + "use strict" + ;(function () {})() +} + +================================================================================ +`; + +exports[`snippet: #11 format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +function foo() { + /* comment */ + 'use strict'; + (function () {})(); +} +=====================================output===================================== +function foo() { + /* comment */ + "use strict"; + (function () {})(); +} + +================================================================================ +`; + +exports[`snippet: #12 - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +'use strict'; // comment (function () {})(); =====================================output===================================== "use strict" - // comment ;(function () {})() ================================================================================ `; -exports[`snippet: #6 format 1`] = ` +exports[`snippet: #12 format 1`] = ` ====================================options===================================== parsers: ["babel", "flow", "typescript"] printWidth: 80 | printWidth =====================================input====================================== 'use strict'; - // comment (function () {})(); =====================================output===================================== "use strict"; - // comment (function () {})(); ================================================================================ `; -exports[`snippet: #7 - {"semi":false} format 1`] = ` +exports[`snippet: #13 - {"semi":false} format 1`] = ` ====================================options===================================== parsers: ["babel", "flow", "typescript"] printWidth: 80 @@ -258,14 +456,12 @@ semi: false =====================================input====================================== function foo() { 'use strict'; - // comment (function () {})(); } =====================================output===================================== function foo() { "use strict" - // comment ;(function () {})() } @@ -273,7 +469,7 @@ function foo() { ================================================================================ `; -exports[`snippet: #7 format 1`] = ` +exports[`snippet: #13 format 1`] = ` ====================================options===================================== parsers: ["babel", "flow", "typescript"] printWidth: 80 @@ -281,15 +477,91 @@ printWidth: 80 =====================================input====================================== function foo() { 'use strict'; - // comment (function () {})(); } =====================================output===================================== function foo() { "use strict"; + // comment + (function () {})(); +} + +================================================================================ +`; + +exports[`snippet: #14 - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +// comment +'use strict'; +(function () {})(); +=====================================output===================================== +// comment +"use strict" +;(function () {})() + +================================================================================ +`; + +exports[`snippet: #14 format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +// comment +'use strict'; +(function () {})(); +=====================================output===================================== +// comment +"use strict"; +(function () {})(); + +================================================================================ +`; + +exports[`snippet: #15 - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +function foo() { + // comment + 'use strict'; + (function () {})(); +} +=====================================output===================================== +function foo() { + // comment + "use strict" + ;(function () {})() +} + +================================================================================ +`; +exports[`snippet: #15 format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +function foo() { // comment + 'use strict'; + (function () {})(); +} +=====================================output===================================== +function foo() { + // comment + "use strict"; (function () {})(); } diff --git a/tests/format/js/directives/comments/jsfmt.spec.js b/tests/format/js/directives/comments/jsfmt.spec.js index 7b2daca3d7da..37cea9d9665b 100644 --- a/tests/format/js/directives/comments/jsfmt.spec.js +++ b/tests/format/js/directives/comments/jsfmt.spec.js @@ -6,20 +6,33 @@ const indent = (text) => .join("\n"); const snippets = [ + "/* comment */ 'use strict';", "'use strict' /* comment */;", + outdent` + // comment + 'use strict'; + `, outdent` 'use strict' // comment `, outdent` 'use strict'; - /* comment */ (function () {})(); `, outdent` + /* comment */ 'use strict'; - + (function () {})(); + `, + outdent` + 'use strict'; + // comment + (function () {})(); + `, + outdent` // comment + 'use strict'; (function () {})(); `, ].flatMap((code) => [ From dfad952f6937e0bf484003057775084eec1bf4cb Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 29 Dec 2022 18:46:55 +0800 Subject: [PATCH 5/6] Add changelog --- changelog_unreleased/javascript/14081.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 changelog_unreleased/javascript/14081.md diff --git a/changelog_unreleased/javascript/14081.md b/changelog_unreleased/javascript/14081.md new file mode 100644 index 000000000000..92a794cbc73d --- /dev/null +++ b/changelog_unreleased/javascript/14081.md @@ -0,0 +1,13 @@ +#### Fix comments after directive (#14081 by @fisker) + + +```jsx +// Input +"use strict" /* comment */; + +// Prettier stable (with other js parsers except `babel`) +Error: Comment "comment" was not printed. Please report this error! + +// Prettier main + +``` From 7e6d8ef1f7e8ec5dfbc76ec3deec0052281612f9 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 29 Dec 2022 19:31:49 +0800 Subject: [PATCH 6/6] Avoid use `.flatMap` --- .../js/directives/comments/jsfmt.spec.js | 81 ++++++++++--------- tests/format/markdown/auto-link/jsfmt.spec.js | 1 + 2 files changed, 44 insertions(+), 38 deletions(-) diff --git a/tests/format/js/directives/comments/jsfmt.spec.js b/tests/format/js/directives/comments/jsfmt.spec.js index 37cea9d9665b..e85b3df3ed9b 100644 --- a/tests/format/js/directives/comments/jsfmt.spec.js +++ b/tests/format/js/directives/comments/jsfmt.spec.js @@ -4,45 +4,50 @@ const indent = (text) => .split("\n") .map((line) => (line ? ` ${line}` : line)) .join("\n"); +// TODO: Remove this when we drop support for Node.js v10 +// eslint-disable-next-line unicorn/prefer-spread +const flat = (array) => [].concat(...array); -const snippets = [ - "/* comment */ 'use strict';", - "'use strict' /* comment */;", - outdent` - // comment - 'use strict'; - `, - outdent` - 'use strict' // comment - `, - outdent` - 'use strict'; - /* comment */ - (function () {})(); - `, - outdent` - /* comment */ - 'use strict'; - (function () {})(); - `, - outdent` - 'use strict'; - // comment - (function () {})(); - `, - outdent` - // comment - 'use strict'; - (function () {})(); - `, -].flatMap((code) => [ - code, - outdent` - function foo() { - ${indent(code)} - } - `, -]); +const snippets = flat( + [ + "/* comment */ 'use strict';", + "'use strict' /* comment */;", + outdent` + // comment + 'use strict'; + `, + outdent` + 'use strict' // comment + `, + outdent` + 'use strict'; + /* comment */ + (function () {})(); + `, + outdent` + /* comment */ + 'use strict'; + (function () {})(); + `, + outdent` + 'use strict'; + // comment + (function () {})(); + `, + outdent` + // comment + 'use strict'; + (function () {})(); + `, + ].map((code) => [ + code, + outdent` + function foo() { + ${indent(code)} + } + `, + ]) +); run_spec({ dirname: __dirname, snippets }, ["babel", "flow", "typescript"]); run_spec({ dirname: __dirname, snippets }, ["babel", "flow", "typescript"], { diff --git a/tests/format/markdown/auto-link/jsfmt.spec.js b/tests/format/markdown/auto-link/jsfmt.spec.js index 176a31f1886b..8f65afa30464 100644 --- a/tests/format/markdown/auto-link/jsfmt.spec.js +++ b/tests/format/markdown/auto-link/jsfmt.spec.js @@ -3,6 +3,7 @@ * @param {Array>} array * @returns {Array} */ +// TODO: Remove this when we drop support for Node.js v10 // eslint-disable-next-line unicorn/prefer-spread const flat = (array) => [].concat(...array);