Skip to content

Commit 8fd58d4

Browse files
authored
Merge pull request #455 from ember-tooling/issue-453-glint-comment-merge
Don't merge lines if a comment breaks them up
2 parents 06e1ee2 + 005fe0c commit 8fd58d4

8 files changed

Lines changed: 72 additions & 8 deletions

File tree

src/printers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function getVisitorKeys(node: NodeType, nonTraversableKeys: Set<string>) {
7070
function print(
7171
path: AstPath<NodeType>,
7272
options: PluginOptions<NodeType>,
73-
print: (path: AstPath<NodeType>) => AST.builders.Doc,
73+
print: Parameters<Printer<NodeType>['print']>[2],
7474
args?: unknown,
7575
) {
7676
const { node } = path;

src/printers/ambiguity.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function fixPreviousPrint(
3636
previousTemplatePrinted: AST.builders.Doc[],
3737
path: AstPath<NodeType>,
3838
options: PluginOptions,
39-
print: (path: AstPath<NodeType>) => AST.builders.Doc,
39+
print: Parameters<Printer<NodeType>['print']>[2],
4040
args: unknown,
4141
): void {
4242
const printedSemiFalse = printer.print(

src/printers/print.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export async function printTemplateContent(
5151
) => Promise<AST.builders.Doc>,
5252
options: PluginOptions,
5353
): Promise<AST.builders.Doc> {
54-
return await textToDoc(text.trim(), {
54+
return textToDoc(text.trim(), {
5555
...options,
5656
parser: 'glimmer',
5757
singleQuote: options.templateSingleQuote ?? options.singleQuote,
@@ -70,9 +70,33 @@ export async function printTemplateContent(
7070
export function printTemplateTag(
7171
content: AST.builders.Doc,
7272
): AST.builders.Doc[] {
73-
const contents = flattenDoc(content);
73+
const strings: string[] = [];
7474

75-
const useHardline = contents.some(
75+
// Single pass: collect strings for the hardline/softline decision and
76+
// simultaneously force any group that contains an HBS comment
77+
// ({{! ... }} or {{!-- ... --}}) to always expand. This prevents the
78+
// Glimmer printer from collapsing a component tag onto one line when a
79+
// Glint annotation comment appears between its attributes.
80+
const processedContent = AST.utils.mapDoc(content, (node) => {
81+
if (typeof node === 'string') {
82+
strings.push(node);
83+
} else if (
84+
node &&
85+
typeof node === 'object' &&
86+
'type' in node &&
87+
node.type === 'group'
88+
) {
89+
const groupStrings = flattenDoc(
90+
(node as { contents: AST.builders.Doc }).contents,
91+
);
92+
if (groupStrings.some((s) => s.startsWith('{{!') || s.trim() === '!')) {
93+
return { ...node, break: true };
94+
}
95+
}
96+
return node;
97+
});
98+
99+
const useHardline = strings.some(
76100
(c) =>
77101
// contains angle bracket tag
78102
/<.+>/.test(c) ||
@@ -83,7 +107,7 @@ export function printTemplateTag(
83107

84108
const doc = [
85109
TEMPLATE_TAG_OPEN,
86-
AST.builders.indent([line, AST.builders.group(content)]),
110+
AST.builders.indent([line, AST.builders.group(processedContent)]),
87111
line,
88112
TEMPLATE_TAG_CLOSE,
89113
];

src/utils/doc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import type { doc } from 'prettier';
1+
import { doc as AST } from 'prettier';
22

33
/**
44
* Flattens the given doc into a string array, tossing line breaks, etc, for
55
* analysis.
66
*/
7-
export function flattenDoc(doc: doc.builders.Doc): string[] {
7+
export function flattenDoc(doc: AST.builders.Doc): string[] {
88
if (Array.isArray(doc)) {
99
return doc.flatMap(flattenDoc);
1010
} else if (typeof doc === 'string') {

tests/cases/gts/issue-453.gts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<template>
2+
<SomeComponent
3+
@foo={{false}}
4+
{{! @glint-expect-error }}
5+
@bar={{false}}
6+
/>
7+
</template>

tests/unit-tests/__snapshots__/format.test.ts.snap

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,17 @@ export default class PooComponent extends Component {
578578
"
579579
`;
580580
581+
exports[`format > config > default > it formats ../cases/gts/issue-453.gts 1`] = `
582+
"<template>
583+
<SomeComponent
584+
@foo={{false}}
585+
{{! @glint-expect-error }}
586+
@bar={{false}}
587+
/>
588+
</template>
589+
"
590+
`;
591+
581592
exports[`format > config > default > it formats ../cases/gts/js-only.gts 1`] = `
582593
"const num: number = 1;
583594
"

tests/unit-tests/config/__snapshots__/semi-false.test.ts.snap

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,17 @@ export default class PooComponent extends Component {
578578
"
579579
`;
580580
581+
exports[`config > semi: false > it formats ../cases/gts/issue-453.gts 1`] = `
582+
"<template>
583+
<SomeComponent
584+
@foo={{false}}
585+
{{! @glint-expect-error }}
586+
@bar={{false}}
587+
/>
588+
</template>
589+
"
590+
`;
591+
581592
exports[`config > semi: false > it formats ../cases/gts/js-only.gts 1`] = `
582593
"const num: number = 1
583594
"

tests/unit-tests/config/__snapshots__/template-export-default.test.ts.snap

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,17 @@ export default class PooComponent extends Component {
578578
"
579579
`;
580580
581+
exports[`config > templateExportDefault: true > it formats ../cases/gts/issue-453.gts 1`] = `
582+
"export default <template>
583+
<SomeComponent
584+
@foo={{false}}
585+
{{! @glint-expect-error }}
586+
@bar={{false}}
587+
/>
588+
</template>
589+
"
590+
`;
591+
581592
exports[`config > templateExportDefault: true > it formats ../cases/gts/js-only.gts 1`] = `
582593
"const num: number = 1;
583594
"

0 commit comments

Comments
 (0)