From a0b0ba1226c8161948cd6d8b559a9341e55c0418 Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Sat, 26 Sep 2020 14:05:52 +0200 Subject: [PATCH 1/3] add support for running only dedicated tests --- test/formatting/index.ts | 9 ++++++++- test/printer/index.ts | 11 +++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/test/formatting/index.ts b/test/formatting/index.ts index df978bdb..10fdbf19 100644 --- a/test/formatting/index.ts +++ b/test/formatting/index.ts @@ -2,7 +2,14 @@ import test from 'ava'; import { readdirSync, readFileSync, existsSync } from 'fs'; import { format } from 'prettier'; -const dirs = readdirSync('test/formatting/samples'); +let dirs = readdirSync('test/formatting/samples'); +const endsWithOnly = (f: string): boolean => f.endsWith('.only'); +const hasOnly = dirs.some(endsWithOnly); +dirs = !hasOnly ? dirs : dirs.filter(endsWithOnly); + +if (process.env.CI && hasOnly) { + throw new Error('.only tests present'); +} for (const dir of dirs) { const input = readFileSync(`test/formatting/samples/${dir}/input.html`, 'utf-8').replace( diff --git a/test/printer/index.ts b/test/printer/index.ts index ac09bdb0..6f38cd9e 100644 --- a/test/printer/index.ts +++ b/test/printer/index.ts @@ -2,13 +2,20 @@ import test from 'ava'; import { readdirSync, readFileSync, existsSync } from 'fs'; import { format } from 'prettier'; -const files = readdirSync('test/printer/samples').filter(name => name.endsWith('.html')); +let files = readdirSync('test/printer/samples').filter((name) => name.endsWith('.html')); +const endsWithOnly = (f: string): boolean => f.endsWith('.only.html'); +const hasOnly = files.some(endsWithOnly); +files = !hasOnly ? files : files.filter(endsWithOnly); + +if (process.env.CI && hasOnly) { + throw new Error('.only tests present'); +} for (const file of files) { const input = readFileSync(`test/printer/samples/${file}`, 'utf-8').replace(/\r?\n/g, '\n'); const options = readOptions(`test/printer/samples/${file.replace('.html', '.options.json')}`); - test(`printer: ${file.slice(0, file.length - '.html'.length)}`, t => { + test(`printer: ${file.slice(0, file.length - '.html'.length)}`, (t) => { const actualOutput = format(input, { parser: 'svelte' as any, plugins: [require.resolve('../../src')], From 167477718d18b3b7bee75f98181ff90b5e3dad78 Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Sat, 26 Sep 2020 14:06:15 +0200 Subject: [PATCH 2/3] test CI fail --- .../samples/element-with-class-and-comment-inside.only.html | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 test/printer/samples/element-with-class-and-comment-inside.only.html diff --git a/test/printer/samples/element-with-class-and-comment-inside.only.html b/test/printer/samples/element-with-class-and-comment-inside.only.html new file mode 100644 index 00000000..7529bb91 --- /dev/null +++ b/test/printer/samples/element-with-class-and-comment-inside.only.html @@ -0,0 +1,5 @@ +
+ +
+ +
From e8842180a0c8df176f65039671fdf7f0b066e659 Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Sat, 26 Sep 2020 14:14:21 +0200 Subject: [PATCH 3/3] Fix attribute/body order Will have strange behavior if there's a comment inside which will then ignore the wrong next node Fixes #146 --- src/print/index.ts | 8 +++++--- ...ly.html => element-with-class-and-comment-inside.html} | 0 2 files changed, 5 insertions(+), 3 deletions(-) rename test/printer/samples/{element-with-class-and-comment-inside.only.html => element-with-class-and-comment-inside.html} (100%) diff --git a/src/print/index.ts b/src/print/index.ts index 8de00406..14cd52c0 100644 --- a/src/print/index.ts +++ b/src/print/index.ts @@ -177,6 +177,8 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D node.type !== 'Element' || selfClosingTags.indexOf(node.name) !== -1); + // Order important: print attributes first + const attributes = path.map((childPath) => childPath.call(print), 'attributes'); let body: Doc; if (isEmpty) { @@ -206,7 +208,7 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D close, ]) : '', - ...path.map((childPath) => childPath.call(print), 'attributes'), + ...attributes, options.svelteBracketNewLine ? dedent(isSelfClosingTag ? line : softline) : '', @@ -439,7 +441,7 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D case 'Comment': { /** * If there is no sibling node that starts right after us but the parent indicates - * that there used to be, that means that node was actually an embedded `