Skip to content

Commit

Permalink
Fix trailing whitespace after class (#442)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 committed May 5, 2023
1 parent 7cd72ed commit 7773ffa
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1369,7 +1369,10 @@ export class PugPrinter {
{ result: this.result, val, length: val.length },
this.currentLineLength,
);
if (this.nextToken?.type === 'text') {
if (
this.nextToken?.type === 'text' &&
!/^\s+$/.test(this.nextToken.val)
) {
this.currentLineLength += 1;
this.result += ' ';
}
Expand Down Expand Up @@ -1487,7 +1490,8 @@ export class PugPrinter {
let val: string = token.val;
let needsTrailingWhitespace: boolean = false;

let endsWithWhitespace: boolean = val[val.length - 1] === ' ';
let endsWithWhitespace: boolean =
val[val.length - 1] === ' ' && !/^\s+$/.test(val);

if (this.pipelessText) {
switch (this.previousToken?.type) {
Expand Down
6 changes: 6 additions & 0 deletions tests/issues/issue-439/formatted.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
html
body
.wrapper
.container Content
.wrapper
.container Content
9 changes: 9 additions & 0 deletions tests/issues/issue-439/issue-439.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 handle trailing whitespace after class', () => {
const { expected, actual } = compareFiles(__dirname);
expect(actual).toBe(expected);
});
});
6 changes: 6 additions & 0 deletions tests/issues/issue-439/unformatted.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
html
body
.wrapper
.container Content
.wrapper
.container Content

0 comments on commit 7773ffa

Please sign in to comment.