Skip to content

Commit 9f4210f

Browse files
authored
Fix attribute/body order (#148)
Will have strange behavior if there's a comment inside which will then ignore the wrong next node Fixes #146 Also added support for running only dedicated tests
1 parent 3c3b308 commit 9f4210f

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

src/print/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
177177
node.type !== 'Element' ||
178178
selfClosingTags.indexOf(node.name) !== -1);
179179

180+
// Order important: print attributes first
181+
const attributes = path.map((childPath) => childPath.call(print), 'attributes');
180182
let body: Doc;
181183

182184
if (isEmpty) {
@@ -206,7 +208,7 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
206208
close,
207209
])
208210
: '',
209-
...path.map((childPath) => childPath.call(print), 'attributes'),
211+
...attributes,
210212
options.svelteBracketNewLine
211213
? dedent(isSelfClosingTag ? line : softline)
212214
: '',
@@ -439,7 +441,7 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
439441
case 'Comment': {
440442
/**
441443
* If there is no sibling node that starts right after us but the parent indicates
442-
* that there used to be, that means that node was actually an embedded `<style>`
444+
* that there used to be, that means that node was actually an embedded `<style>`
443445
* or `<script>` node that was cut out.
444446
* If so, the comment does not refer to the next line we will see.
445447
* The `embed` function handles printing the comment in the right place.
@@ -545,7 +547,7 @@ function printChildren(path: FastPath, print: PrintFn): Doc[] {
545547
*/
546548
function outputChildDoc(childDoc?: Doc, fromNode?: Node) {
547549
if (!isPreformat) {
548-
if ((!childDoc || !fromNode || canBreakBefore(fromNode))) {
550+
if (!childDoc || !fromNode || canBreakBefore(fromNode)) {
549551
linebreakPossible();
550552

551553
const lastChild = childDocs[childDocs.length - 1];

test/formatting/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@ import test from 'ava';
22
import { readdirSync, readFileSync, existsSync } from 'fs';
33
import { format } from 'prettier';
44

5-
const dirs = readdirSync('test/formatting/samples');
5+
let dirs = readdirSync('test/formatting/samples');
6+
const endsWithOnly = (f: string): boolean => f.endsWith('.only');
7+
const hasOnly = dirs.some(endsWithOnly);
8+
dirs = !hasOnly ? dirs : dirs.filter(endsWithOnly);
9+
10+
if (process.env.CI && hasOnly) {
11+
throw new Error('.only tests present');
12+
}
613

714
for (const dir of dirs) {
815
const input = readFileSync(`test/formatting/samples/${dir}/input.html`, 'utf-8').replace(

test/printer/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@ import test from 'ava';
22
import { readdirSync, readFileSync, existsSync } from 'fs';
33
import { format } from 'prettier';
44

5-
const files = readdirSync('test/printer/samples').filter(name => name.endsWith('.html'));
5+
let files = readdirSync('test/printer/samples').filter((name) => name.endsWith('.html'));
6+
const endsWithOnly = (f: string): boolean => f.endsWith('.only.html');
7+
const hasOnly = files.some(endsWithOnly);
8+
files = !hasOnly ? files : files.filter(endsWithOnly);
9+
10+
if (process.env.CI && hasOnly) {
11+
throw new Error('.only tests present');
12+
}
613

714
for (const file of files) {
815
const input = readFileSync(`test/printer/samples/${file}`, 'utf-8').replace(/\r?\n/g, '\n');
916
const options = readOptions(`test/printer/samples/${file.replace('.html', '.options.json')}`);
1017

11-
test(`printer: ${file.slice(0, file.length - '.html'.length)}`, t => {
18+
test(`printer: ${file.slice(0, file.length - '.html'.length)}`, (t) => {
1219
const actualOutput = format(input, {
1320
parser: 'svelte' as any,
1421
plugins: [require.resolve('../../src')],
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<div class="test">
2+
<!-- c -->
3+
</div>
4+
5+
<div class="test"><!-- c --></div>

0 commit comments

Comments
 (0)