From 06c3b118608da757544bafbf6bfce71a32ec51f1 Mon Sep 17 00:00:00 2001 From: Christopher Quadflieg Date: Wed, 23 Oct 2019 17:55:31 +0200 Subject: [PATCH 1/3] Add test for issue 27 --- test/issues/issue-27/formatted.pug | 3 +++ test/issues/issue-27/issue-27.test.ts | 14 ++++++++++++++ test/issues/issue-27/unformatted.pug | 3 +++ 3 files changed, 20 insertions(+) create mode 100644 test/issues/issue-27/formatted.pug create mode 100644 test/issues/issue-27/issue-27.test.ts create mode 100644 test/issues/issue-27/unformatted.pug diff --git a/test/issues/issue-27/formatted.pug b/test/issues/issue-27/formatted.pug new file mode 100644 index 00000000..184725a8 --- /dev/null +++ b/test/issues/issue-27/formatted.pug @@ -0,0 +1,3 @@ +tag(key="value") #{identifier} +tag + | #{identifier} diff --git a/test/issues/issue-27/issue-27.test.ts b/test/issues/issue-27/issue-27.test.ts new file mode 100644 index 00000000..fd207974 --- /dev/null +++ b/test/issues/issue-27/issue-27.test.ts @@ -0,0 +1,14 @@ +import { readFileSync } from 'fs'; +import { resolve } from 'path'; +import { format } from 'prettier'; +import { plugin } from './../../../src/index'; + +describe('Issues', () => { + test('should interpolation', () => { + const expected: string = readFileSync(resolve(__dirname, 'formatted.pug'), 'utf8'); + const code: string = readFileSync(resolve(__dirname, 'unformatted.pug'), 'utf8'); + const actual: string = format(code, { parser: 'pug' as any, plugins: [plugin] }); + + expect(actual).toBe(expected); + }); +}); diff --git a/test/issues/issue-27/unformatted.pug b/test/issues/issue-27/unformatted.pug new file mode 100644 index 00000000..3cc1f697 --- /dev/null +++ b/test/issues/issue-27/unformatted.pug @@ -0,0 +1,3 @@ +tag(key='value') #{identifier} +tag + | #{identifier} From 8552bf13da3dd1d5f0bf02a3196e25da92f8ce6a Mon Sep 17 00:00:00 2001 From: Christopher Quadflieg Date: Wed, 23 Oct 2019 18:02:57 +0200 Subject: [PATCH 2/3] Add space between end-attributes and interpolated-code --- src/index.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 2eced5ba..2a4a47ae 100644 --- a/src/index.ts +++ b/src/index.ts @@ -435,7 +435,10 @@ export const plugin: Plugin = { break; } case 'interpolated-code': - if (previousToken && previousToken.type === 'tag') { + if ( + previousToken && + (previousToken.type === 'tag' || previousToken.type === 'end-attributes') + ) { result += ' '; } result += `#{${token.val}}`; From 5cd172113bea882cad5876d376d5cd10257e5b48 Mon Sep 17 00:00:00 2001 From: Christopher Quadflieg Date: Wed, 23 Oct 2019 18:12:03 +0200 Subject: [PATCH 3/3] Add indents before interpolated code if necessary --- src/index.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 2a4a47ae..ac96c40f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -435,11 +435,17 @@ export const plugin: Plugin = { break; } case 'interpolated-code': - if ( - previousToken && - (previousToken.type === 'tag' || previousToken.type === 'end-attributes') - ) { - result += ' '; + if (previousToken) { + switch (previousToken.type) { + case 'tag': + case 'end-attributes': + result += ' '; + break; + case 'indent': + result = printIndent(previousToken, result, indent, indentLevel); + result += '| '; + break; + } } result += `#{${token.val}}`; break;