Skip to content

Commit 76c04eb

Browse files
committed
fix: preserve style/script tags at the end of the file when using svelteSortOrder: "none"
fixes #472 fixes #406
1 parent d81ffcb commit 76c04eb

File tree

6 files changed

+20
-3
lines changed

6 files changed

+20
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 3.3.2
44

55
- (fix) Svelte 5: handle type annotations on Svelte control flow blocks
6+
- (fix) preserve `style`/`script` tags at the end of the file when using `svelteSortOrder: "none"`
67

78
## 3.3.1
89

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.3.1",
3+
"version": "3.3.2",
44
"description": "Svelte plugin for prettier",
55
"main": "plugin.js",
66
"files": [

src/print/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,15 +733,19 @@ function printTopLevelParts(
733733
): Doc {
734734
if (options.svelteSortOrder === 'none') {
735735
const topLevelPartsByEnd: Record<number, any> = {};
736+
const topLevelPartsByStart: Record<number, any> = {};
736737

737738
if (n.module) {
738739
topLevelPartsByEnd[n.module.end] = n.module;
740+
topLevelPartsByStart[n.module.start] = n.module;
739741
}
740742
if (n.instance) {
741743
topLevelPartsByEnd[n.instance.end] = n.instance;
744+
topLevelPartsByStart[n.instance.start] = n.instance;
742745
}
743746
if (n.css) {
744747
topLevelPartsByEnd[n.css.end] = n.css;
748+
topLevelPartsByStart[n.css.start] = n.css;
745749
}
746750

747751
const children = getChildren(n.html);
@@ -750,6 +754,8 @@ function printTopLevelParts(
750754
if (topLevelPartsByEnd[node.start]) {
751755
children.splice(i, 0, topLevelPartsByEnd[node.start]);
752756
delete topLevelPartsByEnd[node.start];
757+
} else if (i === children.length - 1 && topLevelPartsByStart[node.end]) {
758+
children.push(topLevelPartsByStart[node.end]);
753759
}
754760
}
755761

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div>this template ends with a special tag</div>
2+
3+
<style>
4+
div {
5+
color: red;
6+
}
7+
</style>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"svelteSortOrder": "none"
3+
}

0 commit comments

Comments
 (0)