Skip to content

Commit

Permalink
(feat) add svelteSortOrder: none
Browse files Browse the repository at this point in the history
Closes #314
  • Loading branch information
dummdidumm committed Oct 10, 2022
1 parent 59f76c9 commit fe41b1c
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 11 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 @@
## 2.8.0 (unreleased)

* (feat) support `singleAttributePerLine` Prettier option ([#305](https://github.com/sveltejs/prettier-plugin-svelte/issues/305))
* (feat) add `svelteSortOrder: none` Prettier option which skips reordering scripts/styles/html ([#305](https://github.com/sveltejs/prettier-plugin-svelte/issues/314))

## 2.7.1

Expand Down
2 changes: 1 addition & 1 deletion src/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ function embedTag(
]);
let result = groupConcat([openingTag, body, '</', tag, '>']);

if (isTopLevel) {
if (isTopLevel && options.svelteSortOrder !== 'none') {
// top level embedded nodes have been moved from their normal position in the
// node tree. if there is a comment referring to it, it must be recreated at
// the new position.
Expand Down
6 changes: 6 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const options: Record<keyof PluginOptions, SupportOption> = {
makeChoice('markup-scripts-styles-options'),
makeChoice('styles-markup-scripts-options'),
makeChoice('styles-scripts-markup-options'),
makeChoice('none'),
// Deprecated, keep in 2.x for backwards-compatibility. svelte:options will be moved to the top
makeChoice('scripts-markup-styles'),
makeChoice('scripts-styles-markup'),
Expand Down Expand Up @@ -114,6 +115,7 @@ export type SortOrder =
| 'markup-scripts-styles-options'
| 'styles-markup-scripts-options'
| 'styles-scripts-markup-options'
| 'none'
| DeprecatedSortOrder;

export type DeprecatedSortOrder =
Expand All @@ -129,6 +131,10 @@ export type SortOrderPart = 'scripts' | 'markup' | 'styles' | 'options';
const sortOrderSeparator = '-';

export function parseSortOrder(sortOrder: SortOrder): SortOrderPart[] {
if (sortOrder === 'none') {
return [];
}

const order = sortOrder.split(sortOrderSeparator) as SortOrderPart[];
// For backwards compatibility: Add options to beginning if not present
if (!order.includes('options')) {
Expand Down
56 changes: 46 additions & 10 deletions src/print/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
isIgnoreStartDirective,
isIgnoreEndDirective,
isNodeTopLevelHTML,
getChildren,
} from './node-helpers';
import {
ASTNode,
Expand Down Expand Up @@ -388,7 +389,10 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
]);
}
case 'Options':
throw new Error('Options tags should have been handled by prepareChildren');
if (options.svelteSortOrder !== 'none') {
throw new Error('Options tags should have been handled by prepareChildren');
}
// else fall through to Body
case 'Body':
return groupConcat([
'<',
Expand Down Expand Up @@ -720,6 +724,36 @@ function printTopLevelParts(
path: FastPath<any>,
print: PrintFn,
): Doc {
if (options.svelteSortOrder === 'none') {
const topLevelPartsByEnd: Record<number, any> = {};

if (n.module) {
n.module.type = 'Script';
n.module.attributes = extractAttributes(getText(n.module, options));
topLevelPartsByEnd[n.module.end] = n.module;
}
if (n.instance) {
n.instance.type = 'Script';
n.instance.attributes = extractAttributes(getText(n.instance, options));
topLevelPartsByEnd[n.instance.end] = n.instance;
}
if (n.css) {
n.css.type = 'Style';
n.css.content.type = 'StyleProgram';
topLevelPartsByEnd[n.css.end] = n.css;
}

const children = getChildren(n.html);
for (let i = 0; i < children.length; i++) {
const node = children[i];
if (topLevelPartsByEnd[node.start]) {
children.splice(i, 0, topLevelPartsByEnd[node.start]);
delete topLevelPartsByEnd[node.start];
}
}
return path.call(print, 'html');
}

const parts: Record<SortOrderPart, Doc[]> = {
options: [],
scripts: [],
Expand Down Expand Up @@ -1022,16 +1056,18 @@ function prepareChildren(
continue;
}

if (isCommentFollowedByOptions(currentChild, idx)) {
svelteOptionsComment = printComment(currentChild);
const nextChild = children[idx + 1];
idx += nextChild && isEmptyTextNode(nextChild) ? 1 : 0;
continue;
}
if (options.svelteSortOrder !== 'none') {
if (isCommentFollowedByOptions(currentChild, idx)) {
svelteOptionsComment = printComment(currentChild);
const nextChild = children[idx + 1];
idx += nextChild && isEmptyTextNode(nextChild) ? 1 : 0;
continue;
}

if (currentChild.type === 'Options') {
printSvelteOptions(currentChild, idx, path, print);
continue;
if (currentChild.type === 'Options') {
printSvelteOptions(currentChild, idx, path, print);
continue;
}
}

childrenWithoutOptions.push(currentChild);
Expand Down
23 changes: 23 additions & 0 deletions test/printer/samples/sort-order-none.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<h1>Hello {name}!</h1>

<svelte:options />

<p>yeah why not</p>

<script>
const name = "world";
</script>

<script context="module">
const name1 = "world";
</script>

<p>I hope noone orders them like this</p>

<style>
h1 {
color: red;
}
</style>

<p>I really do</p>
3 changes: 3 additions & 0 deletions test/printer/samples/sort-order-none.options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"svelteSortOrder": "none"
}
23 changes: 23 additions & 0 deletions test/printer/samples/sort-order-none2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<script>
const name = "world";
</script>

<h1>Hello {name}!</h1>

<svelte:options />

<p>yeah why not</p>
<!-- hi -->
<script context="module">
const name1 = "world";
</script>

<p>I hope noone orders them like this</p>

<style>
h1 {
color: red;
}
</style>

<p>I really do</p>
3 changes: 3 additions & 0 deletions test/printer/samples/sort-order-none2.options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"svelteSortOrder": "none"
}

0 comments on commit fe41b1c

Please sign in to comment.