diff --git a/changelog_unreleased/handlebars/pr-8677.md b/changelog_unreleased/handlebars/pr-8677.md new file mode 100644 index 000000000..4ee4e3445 --- /dev/null +++ b/changelog_unreleased/handlebars/pr-8677.md @@ -0,0 +1,16 @@ +#### Format only class names in attributes ([#8677](https://github.com/prettier/prettier/pull/8677) by [@dcyriller](https://github.com/dcyriller) + + +```hbs +{{!-- Input --}} +
+
+ +{{!-- Prettier stable --}} +
+
+ +{{!-- Prettier master --}} +
+
+``` diff --git a/src/language-handlebars/printer-glimmer.js b/src/language-handlebars/printer-glimmer.js index 68235f09d..adcdb1b15 100644 --- a/src/language-handlebars/printer-glimmer.js +++ b/src/language-handlebars/printer-glimmer.js @@ -216,13 +216,17 @@ function print(path, options, print) { } } - let leadingSpace = ""; - let trailingSpace = ""; - - // preserve a space inside of an attribute node where whitespace present, - // when next to mustache statement. const inAttrNode = path.stack.includes("attributes"); if (inAttrNode) { + // TODO: format style and srcset attributes + // and cleanup concat that is not necessary + if (!isInAttributeOfName(path, "class")) { + return concat([n.chars]); + } + + let leadingSpace = ""; + let trailingSpace = ""; + if (isParentOfSomeType(path, ["ConcatStatement"])) { if (isPreviousNodeOfSomeType(path, ["MustacheStatement"])) { leadingSpace = " "; @@ -231,30 +235,42 @@ function print(path, options, print) { trailingSpace = " "; } } - } else { - if ( - trailingLineBreaksCount === 0 && - isNextNodeOfSomeType(path, ["MustacheStatement"]) - ) { - trailingSpace = " "; - } - if ( - leadingLineBreaksCount === 0 && - isPreviousNodeOfSomeType(path, ["MustacheStatement"]) - ) { - leadingSpace = " "; - } + return concat([ + ...generateHardlines(leadingLineBreaksCount, maxLineBreaksToPreserve), + n.chars.replace(/^\s+/g, leadingSpace).replace(/\s+$/, trailingSpace), + ...generateHardlines( + trailingLineBreaksCount, + maxLineBreaksToPreserve + ), + ]); + } - if (isFirstElement) { - leadingLineBreaksCount = 0; - leadingSpace = ""; - } + let leadingSpace = ""; + let trailingSpace = ""; - if (isLastElement) { - trailingLineBreaksCount = 0; - trailingSpace = ""; - } + if ( + trailingLineBreaksCount === 0 && + isNextNodeOfSomeType(path, ["MustacheStatement"]) + ) { + trailingSpace = " "; + } + + if ( + leadingLineBreaksCount === 0 && + isPreviousNodeOfSomeType(path, ["MustacheStatement"]) + ) { + leadingSpace = " "; + } + + if (isFirstElement) { + leadingLineBreaksCount = 0; + leadingSpace = ""; + } + + if (isLastElement) { + trailingLineBreaksCount = 0; + trailingSpace = ""; } let text = n.chars; @@ -502,6 +518,15 @@ function printInverse(path, print) { /* TextNode print helpers */ +function isInAttributeOfName(path, type) { + return ( + (isParentOfSomeType(path, ["AttrNode"]) && + path.getParentNode().name.toLowerCase() === type) || + (isParentOfSomeType(path, ["ConcatStatement"]) && + path.getParentNode(1).name.toLowerCase() === type) + ); +} + function countNewLines(string) { /* istanbul ignore next */ string = typeof string === "string" ? string : ""; diff --git a/tests/handlebars/attr-node/__snapshots__/jsfmt.spec.js.snap b/tests/handlebars/attr-node/__snapshots__/jsfmt.spec.js.snap index 785413ece..61614f980 100644 --- a/tests/handlebars/attr-node/__snapshots__/jsfmt.spec.js.snap +++ b/tests/handlebars/attr-node/__snapshots__/jsfmt.spec.js.snap @@ -1,5 +1,160 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`attribute.hbs - {"singleQuote":true} format 1`] = ` +====================================options===================================== +parsers: ["glimmer"] +printWidth: 80 +singleQuote: true + | printWidth +=====================================input====================================== +
+ +
+ +
+ +
+ +=====================================output===================================== +
+ +
+ +
+ +
+================================================================================ +`; + +exports[`attribute.hbs format 1`] = ` +====================================options===================================== +parsers: ["glimmer"] +printWidth: 80 + | printWidth +=====================================input====================================== +
+ +
+ +
+ +
+ +=====================================output===================================== +
+ +
+ +
+ +
+================================================================================ +`; + exports[`brackets.hbs - {"singleQuote":true} format 1`] = ` ====================================options===================================== parsers: ["glimmer"] @@ -38,3 +193,138 @@ printWidth: 80
================================================================================ `; + +exports[`class-name.hbs - {"singleQuote":true} format 1`] = ` +====================================options===================================== +parsers: ["glimmer"] +printWidth: 80 +singleQuote: true + | printWidth +=====================================input====================================== +
+
+
+

String

+

String

+

String

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

+ String +

+

+ String +

+

+ String +

+================================================================================ +`; + +exports[`class-name.hbs format 1`] = ` +====================================options===================================== +parsers: ["glimmer"] +printWidth: 80 + | printWidth +=====================================input====================================== +
+
+
+

String

+

String

+

String

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

+ String +

+

+ String +

+

+ String +

+================================================================================ +`; diff --git a/tests/handlebars/attr-node/attribute.hbs b/tests/handlebars/attr-node/attribute.hbs new file mode 100644 index 000000000..509a295f6 --- /dev/null +++ b/tests/handlebars/attr-node/attribute.hbs @@ -0,0 +1,33 @@ +
+ +
+ +
+ +
diff --git a/tests/handlebars/attr-node/class-name.hbs b/tests/handlebars/attr-node/class-name.hbs new file mode 100644 index 000000000..c14f0fbe9 --- /dev/null +++ b/tests/handlebars/attr-node/class-name.hbs @@ -0,0 +1,24 @@ +
+
+
+

String

+

String

+

String