Skip to content

Commit

Permalink
Handle slash token
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Quadflieg committed Nov 16, 2019
1 parent 94fdc0c commit 194f103
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ export const plugin: Plugin = {
result += ']';
break;
case 'interpolation':
result = printIndent(previousToken, result, indent, indentLevel);
result += `#{${token.val}}`;
break;
case 'include':
Expand Down Expand Up @@ -664,6 +665,9 @@ export const plugin: Plugin = {
result = printIndent(previousToken, result, indent, indentLevel);
result += 'yield';
break;
case 'slash':
result += '/';
break;
default:
throw new Error('Unhandled token: ' + JSON.stringify(token));
}
Expand Down
4 changes: 4 additions & 0 deletions src/pug-lexer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ declare module 'pug-lexer' {
| 'newline'
| 'outdent'
| 'path'
| 'slash'
| 'start-attributes'
| 'start-pipeless-text'
| 'start-pug-interpolation'
Expand Down Expand Up @@ -200,6 +201,8 @@ declare module 'pug-lexer' {

export type YieldToken = LexToken<'yield'>;

export type SlashToken = LexToken<'slash'>;

export type Token =
| AndAttributesToken
| AttributeToken
Expand Down Expand Up @@ -234,6 +237,7 @@ declare module 'pug-lexer' {
| NewlineToken
| OutdentToken
| PathToken
| SlashToken
| StartAttributesToken
| StartPipelessTextToken
| StartPugInterpolationToken
Expand Down
19 changes: 19 additions & 0 deletions test/slash/formatted.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

body
foo
foo(bar="baz")
foo/
foo(bar="baz")/
foo /
foo(bar="baz") /
#{'foo'}/
#{'foo'}(bar="baz")/
#{'foo'}/
#{'foo'}(bar="baz") /
//- can have a single space after them
img
//- can have lots of white space after them
img
#{
'foo'
}/
14 changes: 14 additions & 0 deletions test/slash/slash.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('Slash', () => {
test('should handle slash 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);
});
});
19 changes: 19 additions & 0 deletions test/slash/unformatted.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

body
foo
foo(bar='baz')
foo/
foo(bar='baz')/
foo /
foo(bar='baz') /
#{'foo'}/
#{'foo'}(bar='baz')/
#{'foo'} /
#{'foo'}(bar='baz') /
//- can have a single space after them
img
//- can have lots of white space after them
img
#{
'foo'
}/

0 comments on commit 194f103

Please sign in to comment.