Skip to content

Commit

Permalink
Handle yield token
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Quadflieg committed Nov 16, 2019
1 parent ef3fc45 commit 94fdc0c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,10 @@ export const plugin: Plugin = {
result = printIndent(previousToken, result, indent, indentLevel);
result += '-';
break;
case 'yield':
result = printIndent(previousToken, result, indent, indentLevel);
result += 'yield';
break;
default:
throw new Error('Unhandled token: ' + JSON.stringify(token));
}
Expand Down
8 changes: 6 additions & 2 deletions src/pug-lexer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ declare module 'pug-lexer' {
| 'text-html'
| 'text'
| 'when'
| 'while';
| 'while'
| 'yield';

export interface LexToken<Type extends LexTokenType> {
type: Type;
Expand Down Expand Up @@ -197,6 +198,8 @@ declare module 'pug-lexer' {

export type BlockcodeToken = LexToken<'blockcode'>;

export type YieldToken = LexToken<'yield'>;

export type Token =
| AndAttributesToken
| AttributeToken
Expand Down Expand Up @@ -238,7 +241,8 @@ declare module 'pug-lexer' {
| TextHtmlToken
| TextToken
| WhenToken
| WhileToken;
| WhileToken
| YieldToken;

export type LexerFunction = (type: string, exp?: any) => boolean;
export interface LexerOptions {
Expand Down
4 changes: 4 additions & 0 deletions test/yield/formatted.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
head
script(src="/jquery.js")
yield
script(src="/jquery.ui.js")
4 changes: 4 additions & 0 deletions test/yield/unformatted.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
head
script(src="/jquery.js")
yield
script(src="/jquery.ui.js")
14 changes: 14 additions & 0 deletions test/yield/yield.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { readFileSync } from 'fs';
import { resolve } from 'path';
import { format } from 'prettier';
import { plugin } from './../../src/index';

describe('Yield', () => {
test('should handle yield token', () => {
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);
});
});

0 comments on commit 94fdc0c

Please sign in to comment.