From 7366dfc8df755039878023ccc9172cc0d407d714 Mon Sep 17 00:00:00 2001 From: Christopher Quadflieg <christopher.quadflieg@adsoul.com> Date: Fri, 10 Jan 2020 17:21:08 +0100 Subject: [PATCH 1/4] Add test for issue 59 --- test/issues/issue-59/formatted.pug | 2 ++ test/issues/issue-59/issue-59.test.ts | 14 ++++++++++++++ test/issues/issue-59/unformatted.pug | 2 ++ 3 files changed, 18 insertions(+) create mode 100644 test/issues/issue-59/formatted.pug create mode 100644 test/issues/issue-59/issue-59.test.ts create mode 100644 test/issues/issue-59/unformatted.pug diff --git a/test/issues/issue-59/formatted.pug b/test/issues/issue-59/formatted.pug new file mode 100644 index 00000000..45f70be0 --- /dev/null +++ b/test/issues/issue-59/formatted.pug @@ -0,0 +1,2 @@ +h2 ID \#{{ id }} +h2 \#{{ id }} diff --git a/test/issues/issue-59/issue-59.test.ts b/test/issues/issue-59/issue-59.test.ts new file mode 100644 index 00000000..9c92a33e --- /dev/null +++ b/test/issues/issue-59/issue-59.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 not remove valid backslash escaping', () => { + 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-59/unformatted.pug b/test/issues/issue-59/unformatted.pug new file mode 100644 index 00000000..45f70be0 --- /dev/null +++ b/test/issues/issue-59/unformatted.pug @@ -0,0 +1,2 @@ +h2 ID \#{{ id }} +h2 \#{{ id }} From ebc9ef6c10e1b407ffb0a800cc8f60f329ae98c9 Mon Sep 17 00:00:00 2001 From: Christopher Quadflieg <christopher.quadflieg@adsoul.com> Date: Fri, 10 Jan 2020 17:40:35 +0100 Subject: [PATCH 2/4] Extend test of issue 59 --- test/issues/issue-59/formatted.pug | 20 ++++++++++++++++++++ test/issues/issue-59/unformatted.pug | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/test/issues/issue-59/formatted.pug b/test/issues/issue-59/formatted.pug index 45f70be0..acf9ec81 100644 --- a/test/issues/issue-59/formatted.pug +++ b/test/issues/issue-59/formatted.pug @@ -1,2 +1,22 @@ +- const id = 42; + h2 ID \#{{ id }} h2 \#{{ id }} + +h2 ID #{id} +h2 #{id} +h2 ID #[b id] +h2 #[b id] +h2 ID #[b #{id}] +h2 #[b #{id}] + +h2 ID \#{id} +h2 \#{id} +h2 ID \#[b id] +h2 \#[b id] +h2 ID \#[b #{id}] +h2 ID #[b \#{id}] +h2 ID \#[b \#{id}] +h2 \#[b #{id}] +h2 #[b \#{id}] +h2 \#[b \#{id}] diff --git a/test/issues/issue-59/unformatted.pug b/test/issues/issue-59/unformatted.pug index 45f70be0..acf9ec81 100644 --- a/test/issues/issue-59/unformatted.pug +++ b/test/issues/issue-59/unformatted.pug @@ -1,2 +1,22 @@ +- const id = 42; + h2 ID \#{{ id }} h2 \#{{ id }} + +h2 ID #{id} +h2 #{id} +h2 ID #[b id] +h2 #[b id] +h2 ID #[b #{id}] +h2 #[b #{id}] + +h2 ID \#{id} +h2 \#{id} +h2 ID \#[b id] +h2 \#[b id] +h2 ID \#[b #{id}] +h2 ID #[b \#{id}] +h2 ID \#[b \#{id}] +h2 \#[b #{id}] +h2 #[b \#{id}] +h2 \#[b \#{id}] From 821df69d04b411cfa79b6734889bfa48404adc3b Mon Sep 17 00:00:00 2001 From: Christopher Quadflieg <christopher.quadflieg@adsoul.com> Date: Fri, 10 Jan 2020 17:41:02 +0100 Subject: [PATCH 3/4] Fix valid backslash escaping --- src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 82764511..5734c20b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -477,9 +477,9 @@ export const plugin: Plugin = { val = val.trim(); val = formatText(val, singleQuote); - } - val = val.replace(/^#(\{|\[)/g, '\\#$1'); + val = val.replace(/#(\{|\[)/g, '\\#$1'); + } if ( ['tag', 'id', 'interpolation', 'call', '&attributes', 'filter'].includes( From 1aa2c4a300fdac022e22ebe86cd3a22ab2a71d7c Mon Sep 17 00:00:00 2001 From: Christopher Quadflieg <christopher.quadflieg@adsoul.com> Date: Fri, 10 Jan 2020 17:42:52 +0100 Subject: [PATCH 4/4] Extend test of issue 59 --- test/issues/issue-59/formatted.pug | 3 +++ test/issues/issue-59/unformatted.pug | 3 +++ 2 files changed, 6 insertions(+) diff --git a/test/issues/issue-59/formatted.pug b/test/issues/issue-59/formatted.pug index acf9ec81..4e95d1e5 100644 --- a/test/issues/issue-59/formatted.pug +++ b/test/issues/issue-59/formatted.pug @@ -3,6 +3,9 @@ h2 ID \#{{ id }} h2 \#{{ id }} +h2 ID #{'#{{ id }}'} +h2 #{'#{{ id }}'} + h2 ID #{id} h2 #{id} h2 ID #[b id] diff --git a/test/issues/issue-59/unformatted.pug b/test/issues/issue-59/unformatted.pug index acf9ec81..4e95d1e5 100644 --- a/test/issues/issue-59/unformatted.pug +++ b/test/issues/issue-59/unformatted.pug @@ -3,6 +3,9 @@ h2 ID \#{{ id }} h2 \#{{ id }} +h2 ID #{'#{{ id }}'} +h2 #{'#{{ id }}'} + h2 ID #{id} h2 #{id} h2 ID #[b id]