Skip to content

Commit 4ca4d4b

Browse files
authored
Fix class attributes not counting towards attribute wrapping (#503)
1 parent b09da87 commit 4ca4d4b

10 files changed

Lines changed: 95 additions & 0 deletions

File tree

src/printer.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,15 @@ export class PugPrinter {
829829
switch (tempToken.name) {
830830
case 'class':
831831
case 'id': {
832+
// If classes or IDs are defined as attributes and not converted to literals, count them toward attribute wrapping.
833+
if (
834+
(tempToken.name === 'class' &&
835+
this.options.pugClassNotation !== 'literal') ||
836+
(tempToken.name === 'id' &&
837+
this.options.pugIdNotation !== 'literal')
838+
) {
839+
numNormalAttributes++;
840+
}
832841
hasLiteralAttributes = true;
833842
const val: string = tempToken.val.toString();
834843
if (isQuoted(val)) {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
div(class="class")
2+
div(
3+
class="class",
4+
attribute="value"
5+
)
6+
.class(attribute="value")
7+
.class(
8+
attribute="value",
9+
another-attribute="another value"
10+
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
div(class="class")
2+
div(class="class" attribute="value")
3+
div.class(attribute="value")
4+
div.class(attribute="value" another-attribute="another value")
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { compareFiles } from 'tests/common';
2+
import { describe, expect, it } from 'vitest';
3+
4+
describe('Options', () => {
5+
describe('pugWrapAttributesThreshold', () => {
6+
it('should count attribute classes toward wrapping if pugClassNotation === as-is', async () => {
7+
const { actual, expected } = await compareFiles(import.meta.url, {
8+
formatOptions: {
9+
pugWrapAttributesThreshold: 1,
10+
pugClassNotation: 'as-is',
11+
},
12+
});
13+
14+
expect(actual).toBe(expected);
15+
});
16+
});
17+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
div(id="foo")
2+
div(
3+
id="foo",
4+
attribute="value"
5+
)
6+
#foo(attribute="value")
7+
#foo(
8+
attribute="value",
9+
another-attribute="another value"
10+
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
div(id="foo")
2+
div(id="foo" attribute="value")
3+
div#foo(attribute="value")
4+
div#foo(attribute="value" another-attribute="another value")
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { compareFiles } from 'tests/common';
2+
import { describe, expect, it } from 'vitest';
3+
4+
describe('Options', () => {
5+
describe('pugWrapAttributesThreshold', () => {
6+
it('should count attribute IDs toward wrapping if pugIdNotation === as-is', async () => {
7+
const { actual, expected } = await compareFiles(import.meta.url, {
8+
formatOptions: {
9+
pugWrapAttributesThreshold: 1,
10+
pugIdNotation: 'as-is',
11+
},
12+
});
13+
14+
expect(actual).toBe(expected);
15+
});
16+
});
17+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
div(class="class")
2+
div(
3+
class="class",
4+
attribute="value"
5+
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
div(class="class")
2+
div(class="class" attribute="value")
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { compareFiles } from 'tests/common';
2+
import { describe, expect, it } from 'vitest';
3+
4+
describe('Options', () => {
5+
describe('pugWrapAttributesThreshold', () => {
6+
it('should count attribute classes toward wrapping if pugClassNotation === attribute', async () => {
7+
const { actual, expected } = await compareFiles(import.meta.url, {
8+
formatOptions: {
9+
pugWrapAttributesThreshold: 1,
10+
pugClassNotation: 'attribute',
11+
},
12+
});
13+
14+
expect(actual).toBe(expected);
15+
});
16+
});
17+
});

0 commit comments

Comments
 (0)