Skip to content

Commit c63b024

Browse files
authored
fix: ensure bind get/set is broken up correctly when too long (#480)
fixes #479
1 parent 76c04eb commit c63b024

File tree

5 files changed

+31
-1
lines changed

5 files changed

+31
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# prettier-plugin-svelte changelog
22

3+
## 3.3.3 (unreleased)
4+
5+
- (fix) Svelte 5: ensure bind get/set is broken up correctly when too long
6+
37
## 3.3.2
48

59
- (fix) Svelte 5: handle type annotations on Svelte control flow blocks

src/embed.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,11 @@ export function embed(path: FastPath, _options: Options) {
128128
printJS(parent, 'expression', {});
129129
break;
130130
case 'ConstTag':
131-
case 'Binding':
132131
printJS(parent, 'expression', { removeParentheses: true });
133132
break;
133+
case 'Binding':
134+
printJS(parent, 'expression', { removeParentheses: true, surroundWithSoftline: true });
135+
break;
134136
case 'RenderTag':
135137
if (node === parent.expression) {
136138
// TODO: remove this if block at some point, snippet API changed in .next-..
@@ -196,6 +198,9 @@ export function embed(path: FastPath, _options: Options) {
196198
throw new Error('Prettier AST changed, asFunction logic needs to change');
197199
}
198200
}
201+
if (node.surroundWithSoftline) {
202+
docs = group(indent([softline, group(docs), dedent(softline)]));
203+
}
199204
return docs;
200205
} catch (e) {
201206
return getText(node, options, true);
@@ -409,6 +414,7 @@ function printJS(
409414
forceSingleQuote?: boolean;
410415
forceSingleLine?: boolean;
411416
removeParentheses?: boolean;
417+
surroundWithSoftline?: boolean;
412418
},
413419
) {
414420
const part = node[name] as BaseNode | undefined;
@@ -419,4 +425,5 @@ function printJS(
419425
part.forceSingleQuote = options.forceSingleQuote;
420426
part.forceSingleLine = options.forceSingleLine;
421427
part.removeParentheses = options.removeParentheses;
428+
part.surroundWithSoftline = options.surroundWithSoftline;
422429
}

src/print/nodes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export interface BaseNode {
1111
forceSingleLine?: boolean;
1212
/** Whether or not to remove outer `()` when printing as JS */
1313
removeParentheses?: boolean;
14+
/** Whether or not to surround the result with a group and softline so that an exceeding print with keeps the output on the same line, if possible */
15+
surroundWithSoftline?: boolean;
1416
}
1517

1618
export interface FragmentNode extends BaseNode {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
11
<input bind:value={() => value, (v) => (value = v)} />
22

33
<input bind:value={get, set} />
4+
5+
<input
6+
bind:value={binding_on_a_separate_line123, binding_on_a_separate_line123}
7+
/>
8+
9+
<input
10+
bind:value={
11+
binding_value_on_a_separate_line123, binding_value_on_a_separate_line123
12+
}
13+
/>
14+
15+
<input
16+
bind:value={
17+
() => getter_setter_each_need_own_line,
18+
(v) => (getter_setter_each_need_own_line = v)
19+
}
20+
/>

0 commit comments

Comments
 (0)