Skip to content

Commit 5fb2bb4

Browse files
committed
seperate css and svelte visitors for clarity
1 parent b146e42 commit 5fb2bb4

File tree

1 file changed

+109
-107
lines changed
  • packages/svelte/src/compiler/print

1 file changed

+109
-107
lines changed

packages/svelte/src/compiler/print/index.js

Lines changed: 109 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ export function print(ast) {
1212
ast,
1313
/** @type {Visitors<AST.SvelteNode>} */ ({
1414
...ts({ comments: ast.type === 'Root' ? ast.comments : [] }),
15-
...visitors
15+
...svelte_visitors,
16+
...css_visitors
1617
})
1718
);
1819
}
@@ -40,10 +41,115 @@ function block(context, node, allow_inline = false) {
4041
}
4142
}
4243

43-
// TODO split css and svelte visitors? Currently they are really mixed together
44+
/** @type {Visitors<AST.SvelteNode>} */
45+
const css_visitors = {
46+
Atrule(node, context) {
47+
// TODO seems to produce too many new lines sometimes. Also new lines above style tag?
48+
context.write(`@${node.name}`);
49+
if (node.prelude) context.write(` ${node.prelude}`);
50+
51+
if (node.block) {
52+
context.write(' ');
53+
context.visit(node.block);
54+
} else {
55+
context.write(';');
56+
}
57+
},
58+
59+
ClassSelector(node, context) {
60+
context.write(`.${node.name}`);
61+
},
62+
63+
ComplexSelector(node, context) {
64+
for (const selector of node.children) {
65+
context.visit(selector);
66+
}
67+
},
68+
69+
Declaration(node, context) {
70+
context.write(`${node.property}: ${node.value};`);
71+
},
72+
73+
Nth(node, context) {
74+
context.write(node.value);
75+
},
76+
77+
PseudoClassSelector(node, context) {
78+
context.write(`:${node.name}`);
79+
80+
if (node.args) {
81+
context.write('(');
82+
83+
let started = false;
84+
85+
for (const arg of node.args.children) {
86+
if (started) {
87+
context.write(', ');
88+
}
89+
90+
context.visit(arg);
91+
92+
started = true;
93+
}
94+
95+
context.write(')');
96+
}
97+
},
98+
99+
PseudoElementSelector(node, context) {
100+
context.write(`::${node.name}`);
101+
},
102+
103+
RelativeSelector(node, context) {
104+
if (node.combinator) {
105+
if (node.combinator.name === ' ') {
106+
context.write(' ');
107+
} else {
108+
context.write(` ${node.combinator.name} `);
109+
}
110+
}
111+
112+
for (const selector of node.selectors) {
113+
context.visit(selector);
114+
}
115+
},
116+
117+
Rule(node, context) {
118+
let started = false;
119+
120+
for (const selector of node.prelude.children) {
121+
if (started) {
122+
context.write(',');
123+
context.newline();
124+
}
125+
126+
context.visit(selector);
127+
started = true;
128+
}
129+
130+
context.write(' ');
131+
context.visit(node.block);
132+
},
133+
134+
SelectorList(node, context) {
135+
let started = false;
136+
for (const selector of node.children) {
137+
if (started) {
138+
context.write(', ');
139+
}
140+
141+
context.visit(selector);
142+
started = true;
143+
}
144+
},
145+
146+
TypeSelector(node, context) {
147+
context.write(node.name);
148+
}
149+
};
44150

45151
/** @type {Visitors<AST.SvelteNode>} */
46-
const visitors = {
152+
const svelte_visitors = {
47153
Root(node, context) {
48154
if (node.options) {
49155
context.write('<svelte:options');
@@ -189,19 +295,6 @@ const visitors = {
189295
}
190296
},
191297

192-
Atrule(node, context) {
193-
// TODO seems to produce too many new lines sometimes. Also new lines above style tag?
194-
context.write(`@${node.name}`);
195-
if (node.prelude) context.write(` ${node.prelude}`);
196-
197-
if (node.block) {
198-
context.write(' ');
199-
context.visit(node.block);
200-
} else {
201-
context.write(';');
202-
}
203-
},
204-
205298
AttachTag(node, context) {
206299
context.write('{@attach ');
207300
context.visit(node.expression);
@@ -327,20 +420,10 @@ const visitors = {
327420
}
328421
},
329422

330-
ClassSelector(node, context) {
331-
context.write(`.${node.name}`);
332-
},
333-
334423
Comment(node, context) {
335424
context.write('<!--' + node.data + '-->');
336425
},
337426

338-
ComplexSelector(node, context) {
339-
for (const selector of node.children) {
340-
context.visit(selector);
341-
}
342-
},
343-
344427
Component(node, context) {
345428
context.write(`<${node.name}`);
346429

@@ -378,10 +461,6 @@ const visitors = {
378461
context.write('}');
379462
},
380463

381-
Declaration(node, context) {
382-
context.write(`${node.property}: ${node.value};`);
383-
},
384-
385464
EachBlock(node, context) {
386465
context.write('{#each ');
387466
context.visit(node.expression);
@@ -482,10 +561,6 @@ const visitors = {
482561
}
483562
},
484563

485-
Nth(node, context) {
486-
context.write(node.value);
487-
},
488-
489564
OnDirective(node, context) {
490565
context.write(`on:${node.name}`);
491566
for (const modifier of node.modifiers) {
@@ -501,32 +576,6 @@ const visitors = {
501576
}
502577
},
503578

504-
PseudoClassSelector(node, context) {
505-
context.write(`:${node.name}`);
506-
507-
if (node.args) {
508-
context.write('(');
509-
510-
let started = false;
511-
512-
for (const arg of node.args.children) {
513-
if (started) {
514-
context.write(', ');
515-
}
516-
517-
context.visit(arg);
518-
519-
started = true;
520-
}
521-
522-
context.write(')');
523-
}
524-
},
525-
526-
PseudoElementSelector(node, context) {
527-
context.write(`::${node.name}`);
528-
},
529-
530579
RegularElement(node, context) {
531580
const child_context = context.new();
532581

@@ -552,55 +601,12 @@ const visitors = {
552601
context.append(child_context);
553602
},
554603

555-
RelativeSelector(node, context) {
556-
if (node.combinator) {
557-
if (node.combinator.name === ' ') {
558-
context.write(' ');
559-
} else {
560-
context.write(` ${node.combinator.name} `);
561-
}
562-
}
563-
564-
for (const selector of node.selectors) {
565-
context.visit(selector);
566-
}
567-
},
568-
569604
RenderTag(node, context) {
570605
context.write('{@render ');
571606
context.visit(node.expression);
572607
context.write('}');
573608
},
574609

575-
Rule(node, context) {
576-
let started = false;
577-
578-
for (const selector of node.prelude.children) {
579-
if (started) {
580-
context.write(',');
581-
context.newline();
582-
}
583-
584-
context.visit(selector);
585-
started = true;
586-
}
587-
588-
context.write(' ');
589-
context.visit(node.block);
590-
},
591-
592-
SelectorList(node, context) {
593-
let started = false;
594-
for (const selector of node.children) {
595-
if (started) {
596-
context.write(', ');
597-
}
598-
599-
context.visit(selector);
600-
started = true;
601-
}
602-
},
603-
604610
SlotElement(node, context) {
605611
context.write('<slot');
606612

@@ -880,10 +886,6 @@ const visitors = {
880886
}
881887
},
882888

883-
TypeSelector(node, context) {
884-
context.write(node.name);
885-
},
886-
887889
UseDirective(node, context) {
888890
context.write(`use:${node.name}`);
889891
if (

0 commit comments

Comments
 (0)