Skip to content

Commit

Permalink
Merge branch 'master' into issue-9
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 committed Jul 9, 2019
2 parents 0817c64 + e66ba82 commit 8890d4c
Show file tree
Hide file tree
Showing 11 changed files with 368 additions and 93 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# Next

[diff](https://github.com/prettier/plugin-pug/compare/1.0.0-alpha.8...master)
[diff](https://github.com/prettier/plugin-pug/compare/1.0.0-alpha.9...master)

# 1.0.0-alpha.9

[diff](https://github.com/prettier/plugin-pug/compare/1.0.0-alpha.8...1.0.0-alpha.9)

- Support mixins ([#11])
- Fix some issues with indentation
- Fix buffered attributes

[#11]: https://github.com/prettier/plugin-pug/pull/11

# 1.0.0-alpha.8

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@prettier/plugin-pug",
"version": "1.0.0-alpha.9",
"version": "1.0.0-alpha.10",
"description": "Prettier Pug Plugin",
"main": "dist/index.js",
"scripts": {
Expand Down Expand Up @@ -30,7 +30,7 @@
},
"devDependencies": {
"@types/jest": "~24.0.15",
"@types/node": "~12.0.10",
"@types/node": "~12.6.1",
"@types/prettier": "~1.16.4",
"@typescript-eslint/eslint-plugin": "~1.11.0",
"@typescript-eslint/parser": "~1.11.0",
Expand All @@ -39,7 +39,7 @@
"eslint-plugin-prettier": "~3.1.0",
"jest": "~24.8.0",
"ts-jest": "~24.0.2",
"typescript": "~3.5.2"
"typescript": "~3.5.3"
},
"peerDependencies": {
"prettier": "^1.18.0"
Expand Down
106 changes: 102 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ export const plugin: Plugin = {
val = `{${val.substring(2, val.length)}`;
}
}

if (token.mustEscape === false) {
result += '!';
}

result += `=${val}`;
}
break;
Expand Down Expand Up @@ -421,11 +426,19 @@ export const plugin: Plugin = {
result += `#{${token.val}}`;
break;
case 'code':
if (indentLevel > 0) {
// Insert one extra indent
result += indent;
if (previousToken) {
switch (previousToken.type) {
case 'newline':
case 'outdent':
result += indent.repeat(indentLevel);
break;
case 'indent':
result += indent;
break;
}
}
result += `- ${token.val}`;
result += token.buffer ? '=' : '-';
result += ` ${token.val}`;
break;
case 'id':
// Handle id attribute
Expand Down Expand Up @@ -492,6 +505,9 @@ export const plugin: Plugin = {
result += 'extends ';
break;
case 'path':
if (previousToken && previousToken.type === 'include') {
result += ' ';
}
result += token.val;
break;
case 'start-pug-interpolation':
Expand All @@ -509,6 +525,88 @@ export const plugin: Plugin = {
case 'filter':
result += `:${token.val}`;
break;
case 'call':
if (previousToken) {
switch (previousToken.type) {
case 'newline':
case 'outdent':
result += indent.repeat(indentLevel);
break;
case 'indent':
result += indent;
break;
}
}
result += `+${token.val}`;
let callArgs: string | null = token.args;
if (callArgs) {
callArgs = callArgs.trim();
callArgs = callArgs.replace(/\s\s+/g, ' ');
result += `(${callArgs})`;
}
break;
case 'mixin':
if (previousToken) {
switch (previousToken.type) {
case 'newline':
case 'outdent':
result += indent.repeat(indentLevel);
break;
case 'indent':
result += indent;
break;
}
}
result += `mixin ${token.val}`;
let mixinArgs: string | null = token.args;
if (mixinArgs) {
mixinArgs = mixinArgs.trim();
mixinArgs = mixinArgs.replace(/\s\s+/g, ' ');
result += `(${mixinArgs})`;
}
break;
case 'if':
if (previousToken) {
switch (previousToken.type) {
case 'newline':
case 'outdent':
result += indent.repeat(indentLevel);
break;
case 'indent':
result += indent;
break;
}
}
result += `if ${token.val}`;
break;
case 'mixin-block':
if (previousToken) {
switch (previousToken.type) {
case 'newline':
case 'outdent':
result += indent.repeat(indentLevel);
break;
case 'indent':
result += indent;
break;
}
}
result += 'block';
break;
case 'else':
if (previousToken) {
switch (previousToken.type) {
case 'newline':
case 'outdent':
result += indent.repeat(indentLevel);
break;
case 'indent':
result += indent;
break;
}
}
result += 'else';
break;
default:
throw new Error('Unhandled token: ' + JSON.stringify(token));
}
Expand Down
38 changes: 37 additions & 1 deletion src/pug-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,37 @@ export interface FilterToken {
val: string;
}

export interface CallToken {
type: 'call';
loc: Loc;
val: string;
args: string;
}

export interface MixinToken {
type: 'mixin';
loc: Loc;
val: string;
args: string | null;
}

export interface IfToken {
type: 'if';
loc: Loc;
val: string;
}

export interface MixinBlockToken {
type: 'mixin-block';
loc: Loc;
}

export interface ElseToken {
type: 'else';
loc: Loc;
val: string;
}

export type Token =
| TagToken
| StartAttributesToken
Expand All @@ -174,4 +205,9 @@ export type Token =
| StartPugInterpolationToken
| EndPugInterpolationToken
| IncludeToken
| FilterToken;
| FilterToken
| CallToken
| MixinToken
| IfToken
| MixinBlockToken
| ElseToken;
9 changes: 9 additions & 0 deletions test/issues/issue-11/formatted.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
doctype html

include ./includes/_helpers.pug

html
head
title Test
body
+stylesheet('test')
14 changes: 14 additions & 0 deletions test/issues/issue-11/issue-11.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('Issues', () => {
test('should handle mixins', () => {
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);
});
});
9 changes: 9 additions & 0 deletions test/issues/issue-11/unformatted.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
doctype html

include ./includes/_helpers.pug

html
head
title Test
body
+stylesheet('test')
40 changes: 40 additions & 0 deletions test/mixins/formatted.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//- Declaration
mixin list
ul
li foo
li bar
li baz
//- Use
+list
+list

.container
.for
.indentation
mixin pet(name)
li.pet= name
ul
+pet('cat')
+pet('dog')
+pet('pig')

mixin article(title)
.article
.article-wrapper
h1= title
if block
block
else
p No content provided

+article('Hello world')

+article('Hello world')
p This is my
p Amazing article

mixin link(href, name)
//- attributes == {class: "btn"}
a(class!=attributes.class, href=href)= name

+link('/foo', 'foo').btn
14 changes: 14 additions & 0 deletions test/mixins/mixins.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('Mixins', () => {
test('should handle mixins', () => {
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);
});
});
40 changes: 40 additions & 0 deletions test/mixins/unformatted.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//- Declaration
mixin list
ul
li foo
li bar
li baz
//- Use
+list
+list

.container
.for
.indentation
mixin pet(name)
li.pet= name
ul
+pet('cat')
+pet('dog')
+pet('pig')

mixin article(title)
.article
.article-wrapper
h1= title
if block
block
else
p No content provided

+article('Hello world')

+article('Hello world')
p This is my
p Amazing article

mixin link(href, name)
//- attributes == {class: "btn"}
a(class!=attributes.class href=href)= name

+link('/foo', 'foo')(class="btn")
Loading

0 comments on commit 8890d4c

Please sign in to comment.