From 56544e38de7f2e98a2d1c2060a621e75dfaef163 Mon Sep 17 00:00:00 2001 From: Antonio Eduardo Date: Sun, 4 Apr 2021 07:14:01 -0300 Subject: [PATCH 01/10] Add warning tester --- src/logger.ts | 27 +++++++++++ src/printer.ts | 13 ++++-- .../warnings/unexpected-end-of-expression.pug | 1 + tests/formatText/warnings/warnings.test.ts | 46 +++++++++++++++++++ 4 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 tests/formatText/warnings/unexpected-end-of-expression.pug create mode 100644 tests/formatText/warnings/warnings.test.ts diff --git a/src/logger.ts b/src/logger.ts index 4b5b09dc..37bb2080 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -17,6 +17,11 @@ export interface ILogger { error: typeof console.error; } +/** + * + */ +export type LoggerListener = (level: LogLevel, message?: unknown, ...optionalParams: any[]) => void; + /** The logger class. */ export class Logger implements ILogger { private static readonly LOG_LEVELS: ['debug', 'log', 'info', 'warn', 'error'] = [ @@ -27,6 +32,8 @@ export class Logger implements ILogger { 'error' ]; + private readonly listeners: LoggerListener[] = []; + /** * Constructs a new logger. * @@ -103,12 +110,32 @@ export class Logger implements ILogger { this.message(LogLevel.ERROR, message, ...optionalParams); } + /** + * Adds a log listener. + * + * @param callback The listener callback. + */ + public addListener(callback: LoggerListener): void { + this.listeners.push(callback); + } + + /** + * Removes a log listener. + * + * @param callback The listener callback. + */ + public removeListener(callback: LoggerListener): void { + const index: number = this.listeners.indexOf(callback); + this.listeners.splice(index, 1); + } + private message(level: LogLevel, message?: any, ...optionalParams: any[]): void { if (this.level !== LogLevel.OFF && this.level <= level) { const logLevel: 'debug' | 'log' | 'info' | 'warn' | 'error' | undefined = Logger.LOG_LEVELS[level as number]; if (logLevel) { this.logger[logLevel](message, ...optionalParams); + this.listeners.forEach((cb) => cb(level, message, ...optionalParams)); } } } diff --git a/src/printer.ts b/src/printer.ts index 9f3f0b72..5e718767 100644 --- a/src/printer.ts +++ b/src/printer.ts @@ -81,7 +81,14 @@ import { } from './utils/common'; import { isVueEventBinding, isVueExpression, isVueVForWithOf, isVueVOnExpression } from './utils/vue'; +/** + * Printer logger instance. + */ const logger: Logger = createLogger(console); +/** + * + */ +export const loggerInstance: Logger = logger; if (process.env.NODE_ENV === 'test') { logger.setLogLevel(LogLevel.DEBUG); } @@ -436,21 +443,21 @@ export class PugPrinter { `code: \`${code.trim()}\`` ); } else if (error.includes("Unexpected token '('")) { - if (this.framework !== 'vue') { + if (this.framework === 'angular') { logger.warn( '[PugPrinter:formatText]: Found unexpected token `(`.', `code: \`${code.trim()}\`` ); } } else if (error.includes('Missing expected `)`')) { - if (this.framework !== 'vue') { + if (this.framework === 'angular') { logger.warn( '[PugPrinter:formatText]: Missing expected `)`.', `code: \`${code.trim()}\`` ); } } else if (error.includes('Missing expected `:`')) { - if (this.framework !== 'vue') { + if (this.framework === 'angular') { logger.warn( '[PugPrinter:formatText]: Missing expected `:`.', `code: \`${code.trim()}\`` diff --git a/tests/formatText/warnings/unexpected-end-of-expression.pug b/tests/formatText/warnings/unexpected-end-of-expression.pug new file mode 100644 index 00000000..9e687f67 --- /dev/null +++ b/tests/formatText/warnings/unexpected-end-of-expression.pug @@ -0,0 +1 @@ +p {{ foo( }} diff --git a/tests/formatText/warnings/warnings.test.ts b/tests/formatText/warnings/warnings.test.ts new file mode 100644 index 00000000..204352af --- /dev/null +++ b/tests/formatText/warnings/warnings.test.ts @@ -0,0 +1,46 @@ +import { readFileSync } from 'fs'; +import { resolve } from 'path'; +import { format } from 'prettier'; +import { plugin } from '../../../src/index'; +import { LoggerListener } from '../../../src/logger'; +import { loggerInstance } from '../../../src/printer'; + +const loggerListener: LoggerListener = jest.fn(); + +const getFormatWarnings = (): string[] => + loggerListener.mock.calls + .filter((call) => call[0] === 3) + .filter((call) => call[1].toString().startsWith('[PugPrinter:formatText]: ')) + .map((call) => call[1].toString().substring(25)); + +const getCode = (filename: string): string => readFileSync(resolve(__dirname, filename), 'utf8'); + +beforeAll(() => { + loggerInstance.addListener(loggerListener); +}); +beforeEach(() => { + // Clear all instances and calls to constructor and all methods: + loggerListener.mockClear(); +}); +afterAll(() => { + loggerInstance.removeListener(loggerListener); +}); + +describe('Frameworks', () => { + describe('Angular', () => { + test('foo-bar', () => { + // this test is not completed yet + const code: string = getCode('unexpected-end-of-expression.pug'); + format(code, { + parser: 'pug', + plugins: [plugin], + + // @ts-expect-error + pugFramework: 'angular' + }); + const thrownWarnings: string[] = getFormatWarnings(); + + expect(thrownWarnings).toContain(''); + }); + }); +}); From 54255257906c5a37d9c059d45f4ec4cfe1bbafc9 Mon Sep 17 00:00:00 2001 From: Antonio Eduardo Date: Fri, 9 Apr 2021 03:34:07 -0300 Subject: [PATCH 02/10] Revert changes in logger runtimer --- src/logger.ts | 27 --------------------------- 1 file changed, 27 deletions(-) diff --git a/src/logger.ts b/src/logger.ts index 37bb2080..4b5b09dc 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -17,11 +17,6 @@ export interface ILogger { error: typeof console.error; } -/** - * - */ -export type LoggerListener = (level: LogLevel, message?: unknown, ...optionalParams: any[]) => void; - /** The logger class. */ export class Logger implements ILogger { private static readonly LOG_LEVELS: ['debug', 'log', 'info', 'warn', 'error'] = [ @@ -32,8 +27,6 @@ export class Logger implements ILogger { 'error' ]; - private readonly listeners: LoggerListener[] = []; - /** * Constructs a new logger. * @@ -110,32 +103,12 @@ export class Logger implements ILogger { this.message(LogLevel.ERROR, message, ...optionalParams); } - /** - * Adds a log listener. - * - * @param callback The listener callback. - */ - public addListener(callback: LoggerListener): void { - this.listeners.push(callback); - } - - /** - * Removes a log listener. - * - * @param callback The listener callback. - */ - public removeListener(callback: LoggerListener): void { - const index: number = this.listeners.indexOf(callback); - this.listeners.splice(index, 1); - } - private message(level: LogLevel, message?: any, ...optionalParams: any[]): void { if (this.level !== LogLevel.OFF && this.level <= level) { const logLevel: 'debug' | 'log' | 'info' | 'warn' | 'error' | undefined = Logger.LOG_LEVELS[level as number]; if (logLevel) { this.logger[logLevel](message, ...optionalParams); - this.listeners.forEach((cb) => cb(level, message, ...optionalParams)); } } } From 30834df500f1a9f136762af7e79e29cd628944ae Mon Sep 17 00:00:00 2001 From: Antonio Eduardo Date: Fri, 9 Apr 2021 03:35:17 -0300 Subject: [PATCH 03/10] Revert changes in logger runtimer --- src/printer.ts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/printer.ts b/src/printer.ts index 5e718767..08517637 100644 --- a/src/printer.ts +++ b/src/printer.ts @@ -81,14 +81,7 @@ import { } from './utils/common'; import { isVueEventBinding, isVueExpression, isVueVForWithOf, isVueVOnExpression } from './utils/vue'; -/** - * Printer logger instance. - */ const logger: Logger = createLogger(console); -/** - * - */ -export const loggerInstance: Logger = logger; if (process.env.NODE_ENV === 'test') { logger.setLogLevel(LogLevel.DEBUG); } From 7d5b14bd623239ec069838d1902eba444c7ab7a8 Mon Sep 17 00:00:00 2001 From: Antonio Eduardo Date: Fri, 9 Apr 2021 04:05:49 -0300 Subject: [PATCH 04/10] Test warns with jest.spyOn --- tests/formatText/warnings/warnings.test.ts | 35 +++++++++------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/tests/formatText/warnings/warnings.test.ts b/tests/formatText/warnings/warnings.test.ts index 204352af..e9611739 100644 --- a/tests/formatText/warnings/warnings.test.ts +++ b/tests/formatText/warnings/warnings.test.ts @@ -2,34 +2,26 @@ import { readFileSync } from 'fs'; import { resolve } from 'path'; import { format } from 'prettier'; import { plugin } from '../../../src/index'; -import { LoggerListener } from '../../../src/logger'; -import { loggerInstance } from '../../../src/printer'; -const loggerListener: LoggerListener = jest.fn(); +/** + * Mocked console.warn calls. + */ +type Call = [message?: any, ...optionalParams: any[]]; -const getFormatWarnings = (): string[] => - loggerListener.mock.calls - .filter((call) => call[0] === 3) - .filter((call) => call[1].toString().startsWith('[PugPrinter:formatText]: ')) - .map((call) => call[1].toString().substring(25)); +const getFormatWarnings: (calls: Call[]) => string[] = (calls) => + calls + .map(([message]) => message) + .filter((message) => typeof message === 'string') + .filter((message) => message.startsWith('[PugPrinter:formatText]: ')) + .map((message) => message.substring(25)); -const getCode = (filename: string): string => readFileSync(resolve(__dirname, filename), 'utf8'); - -beforeAll(() => { - loggerInstance.addListener(loggerListener); -}); -beforeEach(() => { - // Clear all instances and calls to constructor and all methods: - loggerListener.mockClear(); -}); -afterAll(() => { - loggerInstance.removeListener(loggerListener); -}); +const getCode: (filename: string) => string = (filename) => readFileSync(resolve(__dirname, filename), 'utf8'); describe('Frameworks', () => { describe('Angular', () => { test('foo-bar', () => { // this test is not completed yet + const consoleSpy = jest.spyOn(console, 'warn'); const code: string = getCode('unexpected-end-of-expression.pug'); format(code, { parser: 'pug', @@ -38,7 +30,8 @@ describe('Frameworks', () => { // @ts-expect-error pugFramework: 'angular' }); - const thrownWarnings: string[] = getFormatWarnings(); + const thrownWarnings: string[] = getFormatWarnings(consoleSpy.mock.calls); + console.log(thrownWarnings); expect(thrownWarnings).toContain(''); }); From 9576b67eafb53f55fa8160c9de7a309dca5bd8e4 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Fri, 9 Apr 2021 13:26:26 +0200 Subject: [PATCH 05/10] Improve test code and fix lint errors --- tests/formatText/warnings/warnings.test.ts | 31 +++++++++++++++------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/tests/formatText/warnings/warnings.test.ts b/tests/formatText/warnings/warnings.test.ts index e9611739..d371564f 100644 --- a/tests/formatText/warnings/warnings.test.ts +++ b/tests/formatText/warnings/warnings.test.ts @@ -6,23 +6,34 @@ import { plugin } from '../../../src/index'; /** * Mocked console.warn calls. */ -type Call = [message?: any, ...optionalParams: any[]]; +type Call = Parameters; -const getFormatWarnings: (calls: Call[]) => string[] = (calls) => - calls - .map(([message]) => message) - .filter((message) => typeof message === 'string') - .filter((message) => message.startsWith('[PugPrinter:formatText]: ')) - .map((message) => message.substring(25)); +const DEFAULT_LOG_PREFIX: string = '[PugPrinter:formatText]: '; -const getCode: (filename: string) => string = (filename) => readFileSync(resolve(__dirname, filename), 'utf8'); +/** + * Return all warning messages. + * + * @param calls `console.warn` calls. + * @param logPrefix Prefix of the log message. + * @returns Return array of warning messages. + */ +function getFormatWarnings(calls: Call[], logPrefix: string = DEFAULT_LOG_PREFIX): string[] { + return calls + .map( + ([message]) => + // Assume the first argument is of type string, we filter it anyways in the next line + message as string + ) + .filter((message) => typeof message === 'string' && message.startsWith(logPrefix)) + .map((message) => message.slice(logPrefix.length)); +} describe('Frameworks', () => { describe('Angular', () => { test('foo-bar', () => { // this test is not completed yet - const consoleSpy = jest.spyOn(console, 'warn'); - const code: string = getCode('unexpected-end-of-expression.pug'); + const consoleSpy: jest.SpyInstance = jest.spyOn(console, 'warn'); + const code: string = readFileSync(resolve(__dirname, 'unexpected-end-of-expression.pug'), 'utf8'); format(code, { parser: 'pug', plugins: [plugin], From afd289a765431cf7d7e40525b05e7775bf801e09 Mon Sep 17 00:00:00 2001 From: Antonio Eduardo Date: Sat, 10 Apr 2021 00:54:52 -0300 Subject: [PATCH 06/10] Add warning test --- .../missing-expected-close-parenthesis.pug | 1 + .../warnings/unexpected-end-of-expression.pug | 1 - tests/formatText/warnings/warnings.test.ts | 15 ++++++--------- 3 files changed, 7 insertions(+), 10 deletions(-) create mode 100644 tests/formatText/warnings/missing-expected-close-parenthesis.pug delete mode 100644 tests/formatText/warnings/unexpected-end-of-expression.pug diff --git a/tests/formatText/warnings/missing-expected-close-parenthesis.pug b/tests/formatText/warnings/missing-expected-close-parenthesis.pug new file mode 100644 index 00000000..84089f68 --- /dev/null +++ b/tests/formatText/warnings/missing-expected-close-parenthesis.pug @@ -0,0 +1 @@ +span#skills {{ join(calendarEvent.shift.skills, (x) => x.name) }} diff --git a/tests/formatText/warnings/unexpected-end-of-expression.pug b/tests/formatText/warnings/unexpected-end-of-expression.pug deleted file mode 100644 index 9e687f67..00000000 --- a/tests/formatText/warnings/unexpected-end-of-expression.pug +++ /dev/null @@ -1 +0,0 @@ -p {{ foo( }} diff --git a/tests/formatText/warnings/warnings.test.ts b/tests/formatText/warnings/warnings.test.ts index d371564f..8fc23ea5 100644 --- a/tests/formatText/warnings/warnings.test.ts +++ b/tests/formatText/warnings/warnings.test.ts @@ -19,21 +19,17 @@ const DEFAULT_LOG_PREFIX: string = '[PugPrinter:formatText]: '; */ function getFormatWarnings(calls: Call[], logPrefix: string = DEFAULT_LOG_PREFIX): string[] { return calls - .map( - ([message]) => - // Assume the first argument is of type string, we filter it anyways in the next line - message as string - ) + .map((params) => params.join('')) .filter((message) => typeof message === 'string' && message.startsWith(logPrefix)) .map((message) => message.slice(logPrefix.length)); } describe('Frameworks', () => { describe('Angular', () => { - test('foo-bar', () => { + test('should warn for missing parenthesis', () => { // this test is not completed yet const consoleSpy: jest.SpyInstance = jest.spyOn(console, 'warn'); - const code: string = readFileSync(resolve(__dirname, 'unexpected-end-of-expression.pug'), 'utf8'); + const code: string = readFileSync(resolve(__dirname, 'missing-expected-close-parenthesis.pug'), 'utf8'); format(code, { parser: 'pug', plugins: [plugin], @@ -41,10 +37,11 @@ describe('Frameworks', () => { // @ts-expect-error pugFramework: 'angular' }); + const expected: string = 'SyntaxError: Missing expected )\n'; const thrownWarnings: string[] = getFormatWarnings(consoleSpy.mock.calls); - console.log(thrownWarnings); + const hasWarning: boolean = thrownWarnings.some((warning) => warning.startsWith(expected)); - expect(thrownWarnings).toContain(''); + expect(hasWarning).toBe(true); }); }); }); From c2cb85ca9a61dff325e5bebd126d34170aa34804 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Sun, 11 Apr 2021 13:27:03 +0200 Subject: [PATCH 07/10] Correct spelling --- ...parenthesis.pug => missing-expected-close-parentheses.pug} | 0 tests/formatText/warnings/warnings.test.ts | 4 ++-- 2 files changed, 2 insertions(+), 2 deletions(-) rename tests/formatText/warnings/{missing-expected-close-parenthesis.pug => missing-expected-close-parentheses.pug} (100%) diff --git a/tests/formatText/warnings/missing-expected-close-parenthesis.pug b/tests/formatText/warnings/missing-expected-close-parentheses.pug similarity index 100% rename from tests/formatText/warnings/missing-expected-close-parenthesis.pug rename to tests/formatText/warnings/missing-expected-close-parentheses.pug diff --git a/tests/formatText/warnings/warnings.test.ts b/tests/formatText/warnings/warnings.test.ts index 8fc23ea5..779a43ff 100644 --- a/tests/formatText/warnings/warnings.test.ts +++ b/tests/formatText/warnings/warnings.test.ts @@ -26,10 +26,10 @@ function getFormatWarnings(calls: Call[], logPrefix: string = DEFAULT_LOG_PREFIX describe('Frameworks', () => { describe('Angular', () => { - test('should warn for missing parenthesis', () => { + test('should warn for missing parentheses', () => { // this test is not completed yet const consoleSpy: jest.SpyInstance = jest.spyOn(console, 'warn'); - const code: string = readFileSync(resolve(__dirname, 'missing-expected-close-parenthesis.pug'), 'utf8'); + const code: string = readFileSync(resolve(__dirname, 'missing-expected-close-parentheses.pug'), 'utf8'); format(code, { parser: 'pug', plugins: [plugin], From 27fb25a30021a91348b26c2f9774b9b025e3cc5b Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Sun, 11 Apr 2021 13:27:23 +0200 Subject: [PATCH 08/10] Remove comment --- tests/formatText/warnings/warnings.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/formatText/warnings/warnings.test.ts b/tests/formatText/warnings/warnings.test.ts index 779a43ff..af65f2c7 100644 --- a/tests/formatText/warnings/warnings.test.ts +++ b/tests/formatText/warnings/warnings.test.ts @@ -27,7 +27,6 @@ function getFormatWarnings(calls: Call[], logPrefix: string = DEFAULT_LOG_PREFIX describe('Frameworks', () => { describe('Angular', () => { test('should warn for missing parentheses', () => { - // this test is not completed yet const consoleSpy: jest.SpyInstance = jest.spyOn(console, 'warn'); const code: string = readFileSync(resolve(__dirname, 'missing-expected-close-parentheses.pug'), 'utf8'); format(code, { From 522f09512f87fa66d29f18b9a64de04b42dd63cf Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Sun, 11 Apr 2021 14:52:10 +0200 Subject: [PATCH 09/10] Add reference comments to related issues --- src/printer.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/printer.ts b/src/printer.ts index 08517637..3cd24181 100644 --- a/src/printer.ts +++ b/src/printer.ts @@ -427,6 +427,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'); } @@ -436,6 +437,7 @@ export class PugPrinter { `code: \`${code.trim()}\`` ); } else if (error.includes("Unexpected token '('")) { + // ref: https://github.com/prettier/plugin-pug/issues/115 if (this.framework === 'angular') { logger.warn( '[PugPrinter:formatText]: Found unexpected token `(`.', @@ -443,6 +445,7 @@ export class PugPrinter { ); } } else if (error.includes('Missing expected `)`')) { + // ref: https://github.com/prettier/plugin-pug/issues/143 if (this.framework === 'angular') { logger.warn( '[PugPrinter:formatText]: Missing expected `)`.', @@ -450,6 +453,7 @@ export class PugPrinter { ); } } else if (error.includes('Missing expected `:`')) { + // ref: https://github.com/prettier/plugin-pug/issues/147 if (this.framework === 'angular') { logger.warn( '[PugPrinter:formatText]: Missing expected `:`.', From 26b18e072812494d9dd7cdde06c4450c10238298 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Sun, 11 Apr 2021 14:52:32 +0200 Subject: [PATCH 10/10] Draft warning test for most common warnings --- ...indings-should-not-contain-assignments.pug | 4 + ...ound-unexpected-token-open-parentheses.pug | 1 + .../warnings/missing-expected-colon.pug | 1 + tests/formatText/warnings/warnings.test.ts | 90 +++++++++++++------ 4 files changed, 68 insertions(+), 28 deletions(-) create mode 100644 tests/formatText/warnings/bindings-should-not-contain-assignments.pug create mode 100644 tests/formatText/warnings/found-unexpected-token-open-parentheses.pug create mode 100644 tests/formatText/warnings/missing-expected-colon.pug 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-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 index af65f2c7..285dbec5 100644 --- a/tests/formatText/warnings/warnings.test.ts +++ b/tests/formatText/warnings/warnings.test.ts @@ -3,31 +3,51 @@ import { resolve } from 'path'; import { format } from 'prettier'; import { plugin } from '../../../src/index'; -/** - * Mocked console.warn calls. - */ -type Call = Parameters; - -const DEFAULT_LOG_PREFIX: string = '[PugPrinter:formatText]: '; - -/** - * Return all warning messages. - * - * @param calls `console.warn` calls. - * @param logPrefix Prefix of the log message. - * @returns Return array of warning messages. - */ -function getFormatWarnings(calls: Call[], logPrefix: string = DEFAULT_LOG_PREFIX): string[] { - return calls - .map((params) => params.join('')) - .filter((message) => typeof message === 'string' && message.startsWith(logPrefix)) - .map((message) => message.slice(logPrefix.length)); -} - describe('Frameworks', () => { - describe('Angular', () => { - test('should warn for missing parentheses', () => { - const consoleSpy: jest.SpyInstance = jest.spyOn(console, 'warn'); + 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', @@ -36,11 +56,25 @@ describe('Frameworks', () => { // @ts-expect-error pugFramework: 'angular' }); - const expected: string = 'SyntaxError: Missing expected )\n'; - const thrownWarnings: string[] = getFormatWarnings(consoleSpy.mock.calls); - const hasWarning: boolean = thrownWarnings.some((warning) => warning.startsWith(expected)); + const expected: string[] = ['[PugPrinter:formatText]: Missing expected `)`.', 'code: `...`']; + expect(consoleSpy).toHaveBeenCalledTimes(1); + expect(consoleSpy).toHaveBeenLastCalledWith(...expected); + expect(consoleSpy.mock.calls).toEqual([expected]); + }); - expect(hasWarning).toBe(true); + 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]); }); }); });