Skip to content

Commit 8876a30

Browse files
committed
wip
1 parent 10fedea commit 8876a30

File tree

2 files changed

+19
-29
lines changed

2 files changed

+19
-29
lines changed

src/print/index.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ export function print(path: AstPath, options: ParserOptions, print: PrintFn): Do
470470
case 'ExpressionTag':
471471
return ['{', printJS(path, print, 'expression'), '}'];
472472
case 'IfBlock': {
473-
const def: Doc[] = [
473+
let def: Doc[] = [
474474
node.elseif ? '{:else ' : '{#',
475475
'if ',
476476
printJS(path, print, 'test'),
@@ -489,11 +489,13 @@ export function print(path: AstPath, options: ParserOptions, print: PrintFn): Do
489489
}
490490

491491
def.push(
492-
printSvelteBlockFragment(path, print, 'alternate', node.elseif),
492+
printSvelteBlockFragment(path, print, 'alternate'),
493493
);
494494
}
495495

496-
if (!node.elseif) {
496+
if (node.elseif) {
497+
def = dedent(def);
498+
} else {
497499
def.push('{/if}');
498500
}
499501

@@ -732,7 +734,7 @@ export function print(path: AstPath, options: ParserOptions, print: PrintFn): Do
732734
function printTopLevelParts(
733735
n: Root,
734736
options: ParserOptions,
735-
path: AstPath<any>,
737+
path: AstPath,
736738
print: PrintFn,
737739
): Doc {
738740
if (options.svelteSortOrder === 'none') {
@@ -833,12 +835,7 @@ function printAttributeNodeValue(
833835
}
834836
}
835837

836-
function printSvelteBlockFragment(
837-
path: AstPath,
838-
print: PrintFn,
839-
name: string,
840-
shouldIndent = true
841-
): Doc {
838+
function printSvelteBlockFragment( path: AstPath, print: PrintFn, name: string): Doc {
842839
const node = path.node[name] as Fragment;
843840

844841
const children = node.nodes;
@@ -870,10 +867,7 @@ function printSvelteBlockFragment(
870867
trimTextNodeRight(lastChild);
871868
}
872869

873-
// return [indent([startline, group(printChildren(path, print, options))]), endline];
874-
return shouldIndent
875-
? [indent([startline, group(path.call(print, name))]), endline]
876-
: [startline, group(path.call(print, name)), endline];
870+
return [indent([startline, group(path.call(print, name))]), endline];
877871
}
878872

879873
function printPre(
@@ -893,7 +887,7 @@ function printPre(
893887
result.push(line);
894888
});
895889
} else {
896-
result.push(path.call(print, 'children', i));
890+
result.push(path.call(print, 'fragment', 'nodes', i));
897891
}
898892
}
899893
return result;

src/print/node-helpers.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
} from './nodes';
2424
import { blockElements, TagName } from '../lib/elements';
2525
import { AstPath } from 'prettier';
26-
import { findLastIndex, isASTNode, isPreTagContent } from './helpers';
26+
import { findLastIndex, isPreTagContent } from './helpers';
2727
import { ParserOptions, isBracketSameLine } from '../options';
2828

2929
const unsupportedLanguages = ['coffee', 'coffeescript', 'styl', 'stylus', 'sass'];
@@ -76,21 +76,17 @@ export function getChildren(node: SvelteNode): SvelteNode[] {
7676
export function getSiblings(path: AstPath): SvelteNode[] {
7777
let parent: SvelteNode = path.getParentNode();
7878

79+
if (parent.type === 'Fragment') return parent.nodes;
80+
7981
return getChildren(parent);
8082
}
8183

8284
/**
83-
* Returns the previous sibling node.
85+
* Returns the next sibling node.
8486
*/
85-
export function getPreviousNode(path: AstPath): SvelteNode | undefined {
87+
export function getNextNode(path: AstPath): SvelteNode | undefined {
8688
const node: SvelteNode = path.getNode();
87-
return getSiblings(path).find((child) => child.end === node.start);
88-
}
8989

90-
/**
91-
* Returns the next sibling node.
92-
*/
93-
export function getNextNode(path: AstPath, node: SvelteNode = path.getNode()): SvelteNode | undefined {
9490
return getSiblings(path).find((child) => child.start === node.end);
9591
}
9692

@@ -122,7 +118,7 @@ export function getLeadingComment(path: AstPath): Comment | undefined {
122118
* Did there use to be any embedded object (that has been snipped out of the AST to be moved)
123119
* at the specified position?
124120
*/
125-
export function doesEmbedStartAfterNode(node: SvelteNode, path: AstPath, siblings = getSiblings(path)) {
121+
export function doesEmbedStartAfterNode(node: SvelteNode, path: AstPath) {
126122
// If node is not at the top level of html, an embed cannot start after it,
127123
// because embeds are only at the top level
128124
if (!isNodeTopLevelHTML(node, path)) {
@@ -132,15 +128,15 @@ export function doesEmbedStartAfterNode(node: SvelteNode, path: AstPath, sibling
132128
const position = node.end;
133129
const root = path.stack[0] as Root;
134130

135-
const embeds = [root.css, root.html, root.instance, root.js, root.module] as SvelteNode[];
136-
131+
const embeds = [root.css, root.fragment, root.instance, root.module, root.options] as SvelteNode[];
132+
const siblings = getSiblings(path);
137133
const nextNode = siblings[siblings.indexOf(node) + 1];
138134
return embeds.find((n) => n && n.start >= position && (!nextNode || n.end <= nextNode.start));
139135
}
140136

141137
export function isNodeTopLevelHTML(node: SvelteNode, path: AstPath): boolean {
142-
const root = path.stack[0];
143-
return !!root.html && !!root.html.children && root.html.children.includes(node);
138+
const root = path.stack[0] as Root | undefined;
139+
return !!root && root.fragment.nodes.includes(node);
144140
}
145141

146142
export function isEmptyTextNode(node: SvelteNode | undefined): node is Text {

0 commit comments

Comments
 (0)