Skip to content

Commit

Permalink
fix: handle types on each/await contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
dummdidumm committed Nov 20, 2023
1 parent f284d1b commit 66ec48b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# prettier-plugin-svelte changelog

## 3.1.1

- (fix) handle types on each/await contexts

## 3.1.0

- (feat) add experimental support for Svelte 5
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prettier-plugin-svelte",
"version": "3.1.0",
"version": "3.1.1",
"description": "Svelte plugin for prettier",
"main": "plugin.js",
"files": [
Expand Down
34 changes: 21 additions & 13 deletions src/print/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
'{#each ',
printJS(path, print, 'expression'),
' as',
expandNode(node.context),
expandNode(node.context, options.originalText),
];

if (node.index) {
Expand Down Expand Up @@ -549,7 +549,7 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
'{#await ',
printJS(path, print, 'expression'),
' then',
expandNode(node.value),
expandNode(node.value, options.originalText),
'}',
]),
path.call(print, 'then'),
Expand All @@ -560,7 +560,7 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
'{#await ',
printJS(path, print, 'expression'),
' catch',
expandNode(node.error),
expandNode(node.error, options.originalText),
'}',
]),
path.call(print, 'catch'),
Expand All @@ -574,15 +574,15 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D

if (hasThenBlock) {
block.push(
group(['{:then', expandNode(node.value), '}']),
group(['{:then', expandNode(node.value, options.originalText), '}']),
path.call(print, 'then'),
);
}
}

if ((hasPendingBlock || hasThenBlock) && hasCatchBlock) {
block.push(
group(['{:catch', expandNode(node.error), '}']),
group(['{:catch', expandNode(node.error, options.originalText), '}']),
path.call(print, 'catch'),
);
}
Expand Down Expand Up @@ -1188,7 +1188,15 @@ function printJS(path: FastPath, print: PrintFn, name: string) {
return path.call(print, name);
}

function expandNode(node: any, parent?: any): string {
function expandNode(node: any, original: string): string {
let str = _expandNode(node);
if (node?.typeAnnotation) {
str += ': ' + original.slice(node.typeAnnotation.start, node.typeAnnotation.end);
}
return str;
}

function _expandNode(node: any, parent?: any): string {
if (node === null) {
return '';
}
Expand All @@ -1201,27 +1209,27 @@ function expandNode(node: any, parent?: any): string {
switch (node.type) {
case 'ArrayExpression':
case 'ArrayPattern':
return ' [' + node.elements.map(expandNode).join(',').slice(1) + ']';
return ' [' + node.elements.map(_expandNode).join(',').slice(1) + ']';
case 'AssignmentPattern':
return expandNode(node.left) + ' =' + expandNode(node.right);
return _expandNode(node.left) + ' =' + _expandNode(node.right);
case 'Identifier':
return ' ' + node.name;
case 'Literal':
return ' ' + node.raw;
case 'ObjectExpression':
return ' {' + node.properties.map((p: any) => expandNode(p, node)).join(',') + ' }';
return ' {' + node.properties.map((p: any) => _expandNode(p, node)).join(',') + ' }';
case 'ObjectPattern':
return ' {' + node.properties.map(expandNode).join(',') + ' }';
return ' {' + node.properties.map(_expandNode).join(',') + ' }';
case 'Property':
if (node.value.type === 'ObjectPattern' || node.value.type === 'ArrayPattern') {
return ' ' + node.key.name + ':' + expandNode(node.value);
return ' ' + node.key.name + ':' + _expandNode(node.value);
} else if (
(node.value.type === 'Identifier' && node.key.name !== node.value.name) ||
(parent && parent.type === 'ObjectExpression')
) {
return expandNode(node.key) + ':' + expandNode(node.value);
return _expandNode(node.key) + ':' + _expandNode(node.value);
} else {
return expandNode(node.value);
return _expandNode(node.value);
}
case 'RestElement':
return ' ...' + node.argument.name;
Expand Down

0 comments on commit 66ec48b

Please sign in to comment.