Skip to content

Commit

Permalink
fix: preserve style/script tags at the end of the file when using…
Browse files Browse the repository at this point in the history
… `svelteSortOrder: "none"`

fixes #472
fixes #406
  • Loading branch information
dummdidumm committed Nov 22, 2024
1 parent d81ffcb commit 76c04eb
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

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.3.1",
"version": "3.3.2",
"description": "Svelte plugin for prettier",
"main": "plugin.js",
"files": [
Expand Down
6 changes: 6 additions & 0 deletions src/print/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -733,15 +733,19 @@ function printTopLevelParts(
): Doc {
if (options.svelteSortOrder === 'none') {
const topLevelPartsByEnd: Record<number, any> = {};
const topLevelPartsByStart: Record<number, any> = {};

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);
Expand All @@ -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]);
}
}

Expand Down
7 changes: 7 additions & 0 deletions test/printer/samples/sort-order-none3.only.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div>this template ends with a special tag</div>

<style>
div {
color: red;
}
</style>
3 changes: 3 additions & 0 deletions test/printer/samples/sort-order-none3.options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"svelteSortOrder": "none"
}

0 comments on commit 76c04eb

Please sign in to comment.