Skip to content

Commit 66ec48b

Browse files
committed
fix: handle types on each/await contexts
1 parent f284d1b commit 66ec48b

File tree

4 files changed

+28
-16
lines changed

4 files changed

+28
-16
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.1.1
4+
5+
- (fix) handle types on each/await contexts
6+
37
## 3.1.0
48

59
- (feat) add experimental support for Svelte 5

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "prettier-plugin-svelte",
3-
"version": "3.1.0",
3+
"version": "3.1.1",
44
"description": "Svelte plugin for prettier",
55
"main": "plugin.js",
66
"files": [

src/print/index.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
515515
'{#each ',
516516
printJS(path, print, 'expression'),
517517
' as',
518-
expandNode(node.context),
518+
expandNode(node.context, options.originalText),
519519
];
520520

521521
if (node.index) {
@@ -549,7 +549,7 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
549549
'{#await ',
550550
printJS(path, print, 'expression'),
551551
' then',
552-
expandNode(node.value),
552+
expandNode(node.value, options.originalText),
553553
'}',
554554
]),
555555
path.call(print, 'then'),
@@ -560,7 +560,7 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
560560
'{#await ',
561561
printJS(path, print, 'expression'),
562562
' catch',
563-
expandNode(node.error),
563+
expandNode(node.error, options.originalText),
564564
'}',
565565
]),
566566
path.call(print, 'catch'),
@@ -574,15 +574,15 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
574574

575575
if (hasThenBlock) {
576576
block.push(
577-
group(['{:then', expandNode(node.value), '}']),
577+
group(['{:then', expandNode(node.value, options.originalText), '}']),
578578
path.call(print, 'then'),
579579
);
580580
}
581581
}
582582

583583
if ((hasPendingBlock || hasThenBlock) && hasCatchBlock) {
584584
block.push(
585-
group(['{:catch', expandNode(node.error), '}']),
585+
group(['{:catch', expandNode(node.error, options.originalText), '}']),
586586
path.call(print, 'catch'),
587587
);
588588
}
@@ -1188,7 +1188,15 @@ function printJS(path: FastPath, print: PrintFn, name: string) {
11881188
return path.call(print, name);
11891189
}
11901190

1191-
function expandNode(node: any, parent?: any): string {
1191+
function expandNode(node: any, original: string): string {
1192+
let str = _expandNode(node);
1193+
if (node?.typeAnnotation) {
1194+
str += ': ' + original.slice(node.typeAnnotation.start, node.typeAnnotation.end);
1195+
}
1196+
return str;
1197+
}
1198+
1199+
function _expandNode(node: any, parent?: any): string {
11921200
if (node === null) {
11931201
return '';
11941202
}
@@ -1201,27 +1209,27 @@ function expandNode(node: any, parent?: any): string {
12011209
switch (node.type) {
12021210
case 'ArrayExpression':
12031211
case 'ArrayPattern':
1204-
return ' [' + node.elements.map(expandNode).join(',').slice(1) + ']';
1212+
return ' [' + node.elements.map(_expandNode).join(',').slice(1) + ']';
12051213
case 'AssignmentPattern':
1206-
return expandNode(node.left) + ' =' + expandNode(node.right);
1214+
return _expandNode(node.left) + ' =' + _expandNode(node.right);
12071215
case 'Identifier':
12081216
return ' ' + node.name;
12091217
case 'Literal':
12101218
return ' ' + node.raw;
12111219
case 'ObjectExpression':
1212-
return ' {' + node.properties.map((p: any) => expandNode(p, node)).join(',') + ' }';
1220+
return ' {' + node.properties.map((p: any) => _expandNode(p, node)).join(',') + ' }';
12131221
case 'ObjectPattern':
1214-
return ' {' + node.properties.map(expandNode).join(',') + ' }';
1222+
return ' {' + node.properties.map(_expandNode).join(',') + ' }';
12151223
case 'Property':
12161224
if (node.value.type === 'ObjectPattern' || node.value.type === 'ArrayPattern') {
1217-
return ' ' + node.key.name + ':' + expandNode(node.value);
1225+
return ' ' + node.key.name + ':' + _expandNode(node.value);
12181226
} else if (
12191227
(node.value.type === 'Identifier' && node.key.name !== node.value.name) ||
12201228
(parent && parent.type === 'ObjectExpression')
12211229
) {
1222-
return expandNode(node.key) + ':' + expandNode(node.value);
1230+
return _expandNode(node.key) + ':' + _expandNode(node.value);
12231231
} else {
1224-
return expandNode(node.value);
1232+
return _expandNode(node.value);
12251233
}
12261234
case 'RestElement':
12271235
return ' ...' + node.argument.name;

0 commit comments

Comments
 (0)