Skip to content

Commit 79062f9

Browse files
Add new option pugPreserveWhitespace (#567)
1 parent 4bbcc9f commit 79062f9

File tree

14 files changed

+144
-4
lines changed

14 files changed

+144
-4
lines changed

docs/guide/pug-specific-options.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,54 @@ Output:
543543

544544
---
545545

546+
## Preserve whitespace
547+
548+
`pugPreserveWhitespace`
549+
550+
### Description
551+
552+
Define if additional whitespace should be preserved in text.
553+
554+
### Options
555+
556+
**Type:** `boolean`
557+
**Default:** `true`
558+
559+
#### `false`
560+
561+
Input:
562+
563+
```pug
564+
p
565+
| whitespace gets removed
566+
```
567+
568+
Output:
569+
570+
```pug
571+
p
572+
| whitespace gets removed
573+
```
574+
575+
#### `true`
576+
577+
Input:
578+
579+
```pug
580+
p
581+
| whitespace does not get removed
582+
```
583+
584+
Output:
585+
586+
```pug
587+
p
588+
|
589+
| whitespace does not get removed
590+
```
591+
592+
---
593+
546594
## Indent Closing Bracket in Multiline Elements
547595

548596
`pugClosingBracketIndentDepth`

src/options/converge.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export function convergeOptions(
5151
pugFramework: options.pugFramework,
5252
pugExplicitDiv: options.pugExplicitDiv,
5353
pugPreserveAttributeBrackets: options.pugPreserveAttributeBrackets,
54+
pugPreserveWhitespace: options.pugPreserveWhitespace,
5455
pugClosingBracketIndentDepth: options.pugClosingBracketIndentDepth ?? 0,
5556
};
5657
}

src/options/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { PUG_EXPLICIT_DIV } from './pug-explicit-div';
2727
import { PUG_FRAMEWORK } from './pug-framework';
2828
import { PUG_ID_NOTATION } from './pug-id-notation';
2929
import { PUG_PRESERVE_ATTRIBUTE_BRACKETS } from './pug-preserve-attribute-brackets';
30+
import { PUG_PRESERVE_WHITESPACE } from './pug-preserve-whitespace';
3031
import { PUG_SINGLE_FILE_COMPONENT_INDENTATION } from './pug-single-file-component-indentation';
3132
import {
3233
PUG_WRAP_ATTRIBUTES_PATTERN,
@@ -62,6 +63,7 @@ export const options: SupportOptions = {
6263
pugSingleFileComponentIndentation: PUG_SINGLE_FILE_COMPONENT_INDENTATION,
6364
pugFramework: PUG_FRAMEWORK,
6465
pugPreserveAttributeBrackets: PUG_PRESERVE_ATTRIBUTE_BRACKETS,
66+
pugPreserveWhitespace: PUG_PRESERVE_WHITESPACE,
6567
};
6668

6769
export { CATEGORY_PUG } from './constants';
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import type { BooleanSupportOption } from 'prettier';
2+
import { CATEGORY_PUG } from './constants';
3+
4+
/** Pug preserve whitespace. */
5+
export const PUG_PRESERVE_WHITESPACE: BooleanSupportOption = {
6+
// since: '3.3.0',
7+
category: CATEGORY_PUG,
8+
type: 'boolean',
9+
// TODO @alexis-aquanty 2025-03-31: considering defaulting to false in v4
10+
default: true,
11+
description: 'Preserve additional whitespace in text.',
12+
};

src/options/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,6 @@ export interface PugParserOptions
6464
pugFramework: PugFramework;
6565

6666
pugPreserveAttributeBrackets: boolean;
67+
68+
pugPreserveWhitespace: boolean;
6769
}

src/prettier.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,5 +148,11 @@ declare module 'prettier' {
148148
* @default false
149149
*/
150150
pugPreserveAttributeBrackets?: PugParserOptions['pugPreserveAttributeBrackets'];
151+
/**
152+
* Preserve additional whitespace in text.
153+
*
154+
* @default true
155+
*/
156+
pugPreserveWhitespace?: PugParserOptions['pugPreserveWhitespace'];
151157
}
152158
}

src/printer.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ export interface PugPrinterOptions {
127127
readonly pugSemi: boolean;
128128
readonly bracketSameLine: boolean;
129129
readonly pugBracketSameLine: boolean;
130-
131130
readonly pugAttributeSeparator: PugAttributeSeparator;
132131
readonly pugCommentPreserveSpaces: PugCommentPreserveSpaces;
133132
readonly pugSortAttributes: PugSortAttributes;
@@ -144,6 +143,7 @@ export interface PugPrinterOptions {
144143
readonly pugFramework: PugFramework;
145144
readonly pugExplicitDiv: boolean;
146145
readonly pugPreserveAttributeBrackets: boolean;
146+
readonly pugPreserveWhitespace: boolean;
147147
readonly pugClosingBracketIndentDepth: PugClosingBracketIndentDepth;
148148
}
149149

@@ -1680,7 +1680,7 @@ export class PugPrinter {
16801680
switch (this.previousToken?.type) {
16811681
case 'newline': {
16821682
result += this.indentString.repeat(this.indentLevel);
1683-
if (/^ .+$/.test(val)) {
1683+
if (this.options.pugPreserveWhitespace && /^ .+$/.test(val)) {
16841684
result += '|\n';
16851685
result += this.indentString.repeat(this.indentLevel);
16861686
}
@@ -1699,13 +1699,16 @@ export class PugPrinter {
16991699
case 'indent':
17001700
case 'outdent': {
17011701
result += this.computedIndent;
1702-
if (/^ .+$/.test(val)) {
1702+
if (this.options.pugPreserveWhitespace && /^ .+$/.test(val)) {
17031703
result += '|\n';
17041704
result += this.indentString.repeat(this.indentLevel);
17051705
}
17061706

17071707
result += '|';
1708-
if (/.*\S.*/.test(token.val)) {
1708+
if (
1709+
/.*\S.*/.test(token.val) ||
1710+
this.nextToken?.type === 'start-pug-interpolation'
1711+
) {
17091712
result += ' ';
17101713
}
17111714

@@ -2046,6 +2049,7 @@ export class PugPrinter {
20462049
): string {
20472050
let result: string = '';
20482051
if (
2052+
this.pipelessText &&
20492053
this.tokens[this.currentIndex - 2]?.type === 'newline' &&
20502054
this.previousToken?.type === 'text' &&
20512055
this.previousToken.val.trim().length === 0
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
div
2+
| #[b foo]
3+
| #[b bar]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { compareFiles } from 'tests/common';
2+
import { describe, expect, it } from 'vitest';
3+
4+
describe('Issues', () => {
5+
it('should output correct whitespace for interpolation within piped text', async () => {
6+
const { expected, actual } = await compareFiles(import.meta.url);
7+
expect(actual).toBe(expected);
8+
});
9+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
div
2+
|#[b foo]
3+
| #[b bar]

0 commit comments

Comments
 (0)