diff --git a/src/printer.ts b/src/printer.ts index 5198a672..b883aa95 100644 --- a/src/printer.ts +++ b/src/printer.ts @@ -452,6 +452,7 @@ export class PugPrinter { } catch (error: unknown) { if (typeof error === 'string') { if (error.includes('Unexpected token Lexer Error')) { + // ref: https://github.com/prettier/plugin-pug/issues/9#issuecomment-511214531 if (!error.includes('Unexpected character [`]')) { logger.debug('[PugPrinter:formatText]: Using fallback strategy'); } @@ -461,21 +462,24 @@ export class PugPrinter { `code: \`${code.trim()}\`` ); } else if (error.includes("Unexpected token '('")) { - if (this.framework !== 'vue') { + // ref: https://github.com/prettier/plugin-pug/issues/115 + if (this.framework === 'angular') { logger.warn( '[PugPrinter:formatText]: Found unexpected token `(`.', `code: \`${code.trim()}\`` ); } } else if (error.includes('Missing expected `)`')) { - if (this.framework !== 'vue') { + // ref: https://github.com/prettier/plugin-pug/issues/143 + if (this.framework === 'angular') { logger.warn( '[PugPrinter:formatText]: Missing expected `)`.', `code: \`${code.trim()}\`` ); } } else if (error.includes('Missing expected `:`')) { - if (this.framework !== 'vue') { + // ref: https://github.com/prettier/plugin-pug/issues/147 + if (this.framework === 'angular') { logger.warn( '[PugPrinter:formatText]: Missing expected `:`.', `code: \`${code.trim()}\`` diff --git a/tests/formatText/warnings/bindings-should-not-contain-assignments.pug b/tests/formatText/warnings/bindings-should-not-contain-assignments.pug new file mode 100644 index 00000000..b503326f --- /dev/null +++ b/tests/formatText/warnings/bindings-should-not-contain-assignments.pug @@ -0,0 +1,4 @@ +div(:test="test = 1") +div([test]="test = 1") +div + | {{'foo' | baz:bar}} diff --git a/tests/formatText/warnings/found-unexpected-token-open-parentheses.pug b/tests/formatText/warnings/found-unexpected-token-open-parentheses.pug new file mode 100644 index 00000000..4c3efb2f --- /dev/null +++ b/tests/formatText/warnings/found-unexpected-token-open-parentheses.pug @@ -0,0 +1 @@ +p.assignees-tooltip__time() Issue date: {{ issueDate | dateFormat("DD MMM YYYY") }} diff --git a/tests/formatText/warnings/missing-expected-close-parentheses.pug b/tests/formatText/warnings/missing-expected-close-parentheses.pug new file mode 100644 index 00000000..84089f68 --- /dev/null +++ b/tests/formatText/warnings/missing-expected-close-parentheses.pug @@ -0,0 +1 @@ +span#skills {{ join(calendarEvent.shift.skills, (x) => x.name) }} diff --git a/tests/formatText/warnings/missing-expected-colon.pug b/tests/formatText/warnings/missing-expected-colon.pug new file mode 100644 index 00000000..06cfe7db --- /dev/null +++ b/tests/formatText/warnings/missing-expected-colon.pug @@ -0,0 +1 @@ +span {{$t(expanded ? "panel_title_without_title" : "panel_title", {index: index + 1, title})}} diff --git a/tests/formatText/warnings/warnings.test.ts b/tests/formatText/warnings/warnings.test.ts new file mode 100644 index 00000000..285dbec5 --- /dev/null +++ b/tests/formatText/warnings/warnings.test.ts @@ -0,0 +1,80 @@ +import { readFileSync } from 'fs'; +import { resolve } from 'path'; +import { format } from 'prettier'; +import { plugin } from '../../../src/index'; + +describe('Frameworks', () => { + const consoleSpy: jest.SpyInstance> = jest.spyOn(console, 'warn'); + + describe('formatText:warnings', () => { + beforeEach(() => { + consoleSpy.mockClear(); + }); + + test("should warn for 'Bindings should not contain assignments'", () => { + const code: string = readFileSync( + resolve(__dirname, 'bindings-should-not-contain-assignments.pug'), + 'utf8' + ); + format(code, { parser: 'pug', plugins: [plugin] }); + const expected: string[] = [ + '[PugPrinter:formatText]: Bindings should not contain assignments:', + 'code: `...`' + ]; + expect(consoleSpy).toHaveBeenCalledTimes(1); + expect(consoleSpy).toHaveBeenLastCalledWith(...expected); + expect(consoleSpy.mock.calls).toEqual([expected]); + }); + + test("should warn for 'Unexpected token '(''", () => { + const code: string = readFileSync( + resolve(__dirname, 'found-unexpected-token-open-parentheses.pug'), + 'utf8' + ); + format(code, { + parser: 'pug', + plugins: [plugin], + + // @ts-expect-error + pugFramework: 'angular' + }); + const expected: string[] = [ + '[PugPrinter:formatText]: Found unexpected token `(`.', + 'code: `issueDate | dateFormat("DD MMM YYYY")`' + ]; + expect(consoleSpy).toHaveBeenCalledTimes(1); + expect(consoleSpy).toHaveBeenLastCalledWith(...expected); + expect(consoleSpy.mock.calls).toEqual([expected]); + }); + + test("should warn for 'Missing expected `)`'", () => { + const code: string = readFileSync(resolve(__dirname, 'missing-expected-close-parentheses.pug'), 'utf8'); + format(code, { + parser: 'pug', + plugins: [plugin], + + // @ts-expect-error + pugFramework: 'angular' + }); + const expected: string[] = ['[PugPrinter:formatText]: Missing expected `)`.', 'code: `...`']; + expect(consoleSpy).toHaveBeenCalledTimes(1); + expect(consoleSpy).toHaveBeenLastCalledWith(...expected); + expect(consoleSpy.mock.calls).toEqual([expected]); + }); + + test("should warn for 'Missing expected `:`'", () => { + const code: string = readFileSync(resolve(__dirname, 'missing-expected-colon.pug'), 'utf8'); + format(code, { + parser: 'pug', + plugins: [plugin], + + // @ts-expect-error + pugFramework: 'angular' + }); + const expected: string[] = ['[PugPrinter:formatText]: Missing expected `:`.', 'code: `...`']; + expect(consoleSpy).toHaveBeenCalledTimes(1); + expect(consoleSpy).toHaveBeenLastCalledWith(...expected); + expect(consoleSpy.mock.calls).toEqual([expected]); + }); + }); +});