From 56544e38de7f2e98a2d1c2060a621e75dfaef163 Mon Sep 17 00:00:00 2001 From: Antonio Eduardo Date: Sun, 4 Apr 2021 07:14:01 -0300 Subject: [PATCH] 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(''); + }); + }); +});