Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix) better print of long attribute values with mustache tags #221

Merged
merged 2 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* (fix) adjust snip regex to not break commented-out script/style tags ([#212](https://github.com/sveltejs/prettier-plugin-svelte/issues/212))
* (fix) Don't snip style tag that is inside script tag ([#219](https://github.com/sveltejs/prettier-plugin-svelte/issues/219), [#70](https://github.com/sveltejs/prettier-plugin-svelte/issues/70))
* (fix) Better formatting of long attribute values with mustache tags inbetween ([#221](https://github.com/sveltejs/prettier-plugin-svelte/pull/221))

## 2.2.0

Expand Down
17 changes: 16 additions & 1 deletion src/print/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ASTNode, Node } from './nodes';
import { FastPath } from 'prettier';
import { Doc, FastPath } from 'prettier';
import { formattableAttributes } from '../lib/elements';

/**
Expand Down Expand Up @@ -33,3 +33,18 @@ export function findLastIndex<T>(isMatch: (item: T, idx: number) => boolean, ite

return -1;
}

export function replaceEndOfLineWith(text: string, replacement: Doc) {
const parts: Doc[] = [];
for (const part of text.split('\n')) {
if (parts.length > 0) {
parts.push(replacement);
}
if (part.endsWith('\r')) {
parts.push(part.slice(0, -1));
} else {
parts.push(part);
}
}
return parts;
}
10 changes: 8 additions & 2 deletions src/print/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getText } from '../lib/getText';
import { hasSnippedContent, unsnipContent } from '../lib/snipTagContent';
import { parseSortOrder, SortOrderPart } from '../options';
import { isEmptyDoc, isLine, trim, trimRight } from './doc-helpers';
import { flatten, isASTNode, isPreTagContent } from './helpers';
import { flatten, isASTNode, isPreTagContent, replaceEndOfLineWith } from './helpers';
import {
checkWhitespaceAtEndOfSvelteBlock,
checkWhitespaceAtStartOfSvelteBlock,
Expand Down Expand Up @@ -159,7 +159,13 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
*/
return fill(splitTextToDocs(node));
} else {
return getUnencodedText(node);
const rawText = getUnencodedText(node);
if (path.getParentNode().type === 'Attribute') {
// Direct child of attribute value -> add literallines at end of lines
// so that other things don't break in unexpected places
return concat(replaceEndOfLineWith(rawText, literalline));
}
return rawText;
}
case 'Element':
case 'InlineComponent':
Expand Down
10 changes: 9 additions & 1 deletion src/print/node-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
PendingBlockNode,
ThenBlockNode,
CommentNode,
SlotTemplateNode,
} from './nodes';
import { blockElements, TagName } from '../lib/elements';
import { FastPath, ParserOptions } from 'prettier';
Expand Down Expand Up @@ -149,7 +150,14 @@ export function isIgnoreDirective(node: Node | undefined | null): boolean {
}

export function printRaw(
node: ElementNode | InlineComponentNode | SlotNode | WindowNode | HeadNode | TitleNode,
node:
| ElementNode
| InlineComponentNode
| SlotNode
| WindowNode
| HeadNode
| TitleNode
| SlotTemplateNode,
originalText: string,
stripLeadingAndTrailingNewline: boolean = false,
): string {
Expand Down
1 change: 1 addition & 0 deletions src/print/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ export interface SlotTemplateNode extends BaseNode {
type: 'SlotTemplate';
name: string;
attributes: Node[];
children: Node[];
}

export type Node =
Expand Down
10 changes: 10 additions & 0 deletions test/printer/samples/attributes-long-text-with-mustache.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<button
style="color: {color};font-size: {fontSize};--bg-color: {bgColor}; --bg-hover: {bgHover};
--bg-active: {bgActive};--radius: {round};--ripple: {rippleColor};height: {height};width: {width};min-width: {width};
--shadow: {shadows[shadow]};
--shadow-h: {shadows[shadowHover]};
--shadow-a: {shadows[shadowActive]};
--opacity: {disabled ? 0.5 : 1.0}

I'm unaffected"
/>