From 76c04ebfdff4306842e8ab0cd96b1c53c7041dde Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Fri, 22 Nov 2024 12:59:12 +0100 Subject: [PATCH] fix: preserve `style`/`script` tags at the end of the file when using `svelteSortOrder: "none"` fixes #472 fixes #406 --- CHANGELOG.md | 1 + package-lock.json | 4 ++-- package.json | 2 +- src/print/index.ts | 6 ++++++ test/printer/samples/sort-order-none3.only.html | 7 +++++++ test/printer/samples/sort-order-none3.options.json | 3 +++ 6 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 test/printer/samples/sort-order-none3.only.html create mode 100644 test/printer/samples/sort-order-none3.options.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 44edf69e..c6075af7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## 3.3.2 - (fix) Svelte 5: handle type annotations on Svelte control flow blocks +- (fix) preserve `style`/`script` tags at the end of the file when using `svelteSortOrder: "none"` ## 3.3.1 diff --git a/package-lock.json b/package-lock.json index 12d7d41e..ad7984e3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "prettier-plugin-svelte", - "version": "3.3.1", + "version": "3.3.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "prettier-plugin-svelte", - "version": "3.3.1", + "version": "3.3.2", "license": "MIT", "devDependencies": { "@rollup/plugin-alias": "^5.1.0", diff --git a/package.json b/package.json index 3b1058b3..43925650 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "prettier-plugin-svelte", - "version": "3.3.1", + "version": "3.3.2", "description": "Svelte plugin for prettier", "main": "plugin.js", "files": [ diff --git a/src/print/index.ts b/src/print/index.ts index e6c6a3e4..01cbce0e 100644 --- a/src/print/index.ts +++ b/src/print/index.ts @@ -733,15 +733,19 @@ function printTopLevelParts( ): Doc { if (options.svelteSortOrder === 'none') { const topLevelPartsByEnd: Record = {}; + const topLevelPartsByStart: Record = {}; if (n.module) { topLevelPartsByEnd[n.module.end] = n.module; + topLevelPartsByStart[n.module.start] = n.module; } if (n.instance) { topLevelPartsByEnd[n.instance.end] = n.instance; + topLevelPartsByStart[n.instance.start] = n.instance; } if (n.css) { topLevelPartsByEnd[n.css.end] = n.css; + topLevelPartsByStart[n.css.start] = n.css; } const children = getChildren(n.html); @@ -750,6 +754,8 @@ function printTopLevelParts( if (topLevelPartsByEnd[node.start]) { children.splice(i, 0, topLevelPartsByEnd[node.start]); delete topLevelPartsByEnd[node.start]; + } else if (i === children.length - 1 && topLevelPartsByStart[node.end]) { + children.push(topLevelPartsByStart[node.end]); } } diff --git a/test/printer/samples/sort-order-none3.only.html b/test/printer/samples/sort-order-none3.only.html new file mode 100644 index 00000000..8d4edeb2 --- /dev/null +++ b/test/printer/samples/sort-order-none3.only.html @@ -0,0 +1,7 @@ +
this template ends with a special tag
+ + diff --git a/test/printer/samples/sort-order-none3.options.json b/test/printer/samples/sort-order-none3.options.json new file mode 100644 index 00000000..fd6e47a8 --- /dev/null +++ b/test/printer/samples/sort-order-none3.options.json @@ -0,0 +1,3 @@ +{ + "svelteSortOrder": "none" +}