diff --git a/changelog_unreleased/html/19304.md b/changelog_unreleased/html/19304.md new file mode 100644 index 000000000..aa83a9385 --- /dev/null +++ b/changelog_unreleased/html/19304.md @@ -0,0 +1,16 @@ +#### Keep an inline comment attached to the preceding node (#19304 by @kovsu) + + +```html + +

x

+ + +

+ x +

+ + +

x

+``` diff --git a/src/language-html/print/children.js b/src/language-html/print/children.js index 9658f2303..05cadf7fb 100644 --- a/src/language-html/print/children.js +++ b/src/language-html/print/children.js @@ -126,6 +126,18 @@ function printBetweenLine(prevNode, nextNode) { return ""; } + /** + *
{{ x }}
+ * ~ + */ + if ( + nextNode.kind === "comment" && + nextNode.isLeadingSpaceSensitive && + !nextNode.hasLeadingSpaces + ) { + return softline; + } + if ( !nextNode.isLeadingSpaceSensitive || preferHardlineAsLeadingSpaces(nextNode) || diff --git a/tests/format/angular/angular/__snapshots__/format.test.js.snap b/tests/format/angular/angular/__snapshots__/format.test.js.snap index 60eab7b43..14e52ad65 100644 --- a/tests/format/angular/angular/__snapshots__/format.test.js.snap +++ b/tests/format/angular/angular/__snapshots__/format.test.js.snap @@ -3018,10 +3018,8 @@ parsers: ["angular"] }} {{ a }}{{ b }}{{ a }}{{ b }} {{ aNormalValue | aPipe }}: @@ -3745,10 +3743,8 @@ trailingComma: "es5" }} {{ a }}{{ b }}{{ a }}{{ b }} {{ aNormalValue | aPipe }}: @@ -3919,10 +3915,8 @@ trailingComma: "none" }} {{ a }}{{ b }}{{ a }}{{ b }} {{ aNormalValue | aPipe }}: diff --git a/tests/format/html/comments/__snapshots__/format.test.js.snap b/tests/format/html/comments/__snapshots__/format.test.js.snap index 054e74bdc..5e5e8c6ab 100644 --- a/tests/format/html/comments/__snapshots__/format.test.js.snap +++ b/tests/format/html/comments/__snapshots__/format.test.js.snap @@ -166,6 +166,146 @@ parsers: ["html"] ================================================================================ `; +exports[`comment-after-element.html - {"htmlWhitespaceSensitivity":"ignore"} format 1`] = ` +====================================options===================================== +htmlWhitespaceSensitivity: "ignore" +parsers: ["html"] + printWidth: 80 (default) | +=====================================input====================================== +

x

+

x

+

hello

+

text

+ +=====================================output===================================== +

+ x + +

+

+ x + +

+

+ hello + +

+

+ text + +

+ +================================================================================ +`; + +exports[`comment-after-element.html - {"htmlWhitespaceSensitivity":"strict"} format 1`] = ` +====================================options===================================== +htmlWhitespaceSensitivity: "strict" +parsers: ["html"] + printWidth: 80 (default) | +=====================================input====================================== +

x

+

x

+

hello

+

text

+ +=====================================output===================================== +

x

+

x +

+

hello

+

text

+ +================================================================================ +`; + +exports[`comment-after-element.html - {"printWidth":"Infinity"} format 1`] = ` +====================================options===================================== +parsers: ["html"] +printWidth: Infinity +=====================================input====================================== +

x

+

x

+

hello

+

text

+ +=====================================output===================================== +

x

+

+ x + +

+

hello

+

text

+ +================================================================================ +`; + +exports[`comment-after-element.html - {"printWidth":1} format 1`] = ` +====================================options===================================== +parsers: ["html"] +printWidth: 1 + | printWidth: 1 +=====================================input====================================== +

x

+

x

+

hello

+

text

+ +=====================================output===================================== +

+ x +

+

+ x + +

+

+ hello +

+

+ text +

+ +================================================================================ +`; + +exports[`comment-after-element.html format 1`] = ` +====================================options===================================== +parsers: ["html"] + printWidth: 80 (default) | +=====================================input====================================== +

x

+

x

+

hello

+

text

+ +=====================================output===================================== +

x

+

+ x + +

+

+ hello +

+

text

+ +================================================================================ +`; + exports[`conditional.html - {"htmlWhitespaceSensitivity":"ignore"} format 1`] = ` ====================================options===================================== htmlWhitespaceSensitivity: "ignore" @@ -1626,26 +1766,20 @@ See console log: --> abab abab abab @@ -1764,26 +1898,20 @@ See console log: --> abab abab abab @@ -2054,26 +2182,20 @@ See console log: --> abab abab abab diff --git a/tests/format/html/comments/comment-after-element.html b/tests/format/html/comments/comment-after-element.html new file mode 100644 index 000000000..bd7bd6a34 --- /dev/null +++ b/tests/format/html/comments/comment-after-element.html @@ -0,0 +1,4 @@ +

x

+

x

+

hello

+

text

diff --git a/tests/format/html/prettier_ignore/__snapshots__/format.test.js.snap b/tests/format/html/prettier_ignore/__snapshots__/format.test.js.snap index ab41a2149..f2666c59d 100644 --- a/tests/format/html/prettier_ignore/__snapshots__/format.test.js.snap +++ b/tests/format/html/prettier_ignore/__snapshots__/format.test.js.snap @@ -12,8 +12,7 @@ parsers: ["html"] =====================================output===================================== 123456 - + ================================================================================ `; diff --git a/tests/format/vue/interpolation/__snapshots__/format.test.js.snap b/tests/format/vue/interpolation/__snapshots__/format.test.js.snap index 87bf5437b..1923c8a0e 100644 --- a/tests/format/vue/interpolation/__snapshots__/format.test.js.snap +++ b/tests/format/vue/interpolation/__snapshots__/format.test.js.snap @@ -40,6 +40,41 @@ parsers: ["vue"] ================================================================================ `; +exports[`comment-after-interpolation.vue format 1`] = ` +====================================options===================================== +parsers: ["vue"] + printWidth: 80 (default) | +=====================================input====================================== + + +=====================================output===================================== + + +================================================================================ +`; + exports[`parenthesized.vue format 1`] = ` ====================================options===================================== parsers: ["vue"] diff --git a/tests/format/vue/interpolation/comment-after-interpolation.vue b/tests/format/vue/interpolation/comment-after-interpolation.vue new file mode 100644 index 000000000..b91679b66 --- /dev/null +++ b/tests/format/vue/interpolation/comment-after-interpolation.vue @@ -0,0 +1,9 @@ +