Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test framework specific warnings #193

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Draft
10 changes: 7 additions & 3 deletions src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand All @@ -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()}\``
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
div(:test="test = 1")
div([test]="test = 1")
div
| {{'foo' | baz:bar}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
p.assignees-tooltip__time() Issue date: {{ issueDate | dateFormat("DD MMM YYYY") }}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
span#skills {{ join(calendarEvent.shift.skills, (x) => x.name) }}
1 change: 1 addition & 0 deletions tests/formatText/warnings/missing-expected-colon.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
span {{$t(expanded ? "panel_title_without_title" : "panel_title", {index: index + 1, title})}}
80 changes: 80 additions & 0 deletions tests/formatText/warnings/warnings.test.ts
Original file line number Diff line number Diff line change
@@ -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<void, Parameters<Console['warn']>> = 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]);
});
});
});