Skip to content

Commit

Permalink
Transform trailing whitespace to recommended way (#418)
Browse files Browse the repository at this point in the history
Co-authored-by: Shinigami92 <[email protected]>
  • Loading branch information
jwerre and Shinigami92 authored Oct 29, 2022
1 parent 4ec814b commit aed6523
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1479,6 +1479,8 @@ export class PugPrinter {
let val: string = token.val;
let needsTrailingWhitespace: boolean = false;

const endsWithWhitespace: boolean = val[val.length - 1] === ' ';

if (this.pipelessText) {
switch (this.previousToken?.type) {
case 'newline':
Expand All @@ -1499,7 +1501,7 @@ export class PugPrinter {
);
}
} else {
if (this.nextToken && val[val.length - 1] === ' ') {
if (this.nextToken && endsWithWhitespace) {
switch (this.nextToken.type) {
case 'interpolated-code':
case 'start-pug-interpolation':
Expand Down Expand Up @@ -1568,6 +1570,10 @@ export class PugPrinter {
result += ' ';
}

if (endsWithWhitespace && this.nextToken?.type === 'indent') {
result += '\n' + this.indentString.repeat(this.indentLevel + 1) + '|';
}

return result;
}

Expand Down
13 changes: 13 additions & 0 deletions tests/issues/issue-417/formatted.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
p Aliquam pulvinar enim eu enim finibus euismod.
|
a(href="https://example.com") Link
|
| Suspendisse suscipit nunc non mauris sodales elementum.

div
p Aliquam pulvinar enim eu enim finibus euismod.
p Aliquam pulvinar enim eu enim finibus euismod.
|
a(href="https://example.com") Link
|
| Suspendisse suscipit nunc non mauris sodales elementum.
9 changes: 9 additions & 0 deletions tests/issues/issue-417/issue-417.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { compareFiles } from 'tests/common';
import { describe, expect, it } from 'vitest';

describe('Issues', () => {
it('should add two empty piped lines before and after link tag', () => {
const { expected, actual } = compareFiles(__dirname);
expect(actual).toBe(expected);
});
});
9 changes: 9 additions & 0 deletions tests/issues/issue-417/unformatted.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
p Aliquam pulvinar enim eu enim finibus euismod.
a(href="https://example.com") Link
| Suspendisse suscipit nunc non mauris sodales elementum.

div
p Aliquam pulvinar enim eu enim finibus euismod.
p Aliquam pulvinar enim eu enim finibus euismod.
a(href="https://example.com") Link
| Suspendisse suscipit nunc non mauris sodales elementum.

0 comments on commit aed6523

Please sign in to comment.