From 66ec48bb73a275826da7c0e5b0cae73192ae5a2c Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Tue, 21 Nov 2023 00:00:17 +0100 Subject: [PATCH] fix: handle types on each/await contexts --- CHANGELOG.md | 4 ++++ package-lock.json | 4 ++-- package.json | 2 +- src/print/index.ts | 34 +++++++++++++++++++++------------- 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ebce26..120f0e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/package-lock.json b/package-lock.json index 2a6a77b..cf4e12e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "prettier-plugin-svelte", - "version": "3.1.0", + "version": "3.1.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "prettier-plugin-svelte", - "version": "3.1.0", + "version": "3.1.1", "license": "MIT", "devDependencies": { "@rollup/plugin-commonjs": "14.0.0", diff --git a/package.json b/package.json index 84ef685..a92732c 100644 --- a/package.json +++ b/package.json @@ -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": [ diff --git a/src/print/index.ts b/src/print/index.ts index 654a2d8..394acc5 100644 --- a/src/print/index.ts +++ b/src/print/index.ts @@ -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) { @@ -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'), @@ -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'), @@ -574,7 +574,7 @@ 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'), ); } @@ -582,7 +582,7 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D if ((hasPendingBlock || hasThenBlock) && hasCatchBlock) { block.push( - group(['{:catch', expandNode(node.error), '}']), + group(['{:catch', expandNode(node.error, options.originalText), '}']), path.call(print, 'catch'), ); } @@ -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 ''; } @@ -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;