Skip to content

Commit

Permalink
Fix class attributes not counting towards attribute wrapping (#503)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrHaila authored Sep 7, 2024
1 parent b09da87 commit 4ca4d4b
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,15 @@ export class PugPrinter {
switch (tempToken.name) {
case 'class':
case 'id': {
// If classes or IDs are defined as attributes and not converted to literals, count them toward attribute wrapping.
if (
(tempToken.name === 'class' &&
this.options.pugClassNotation !== 'literal') ||
(tempToken.name === 'id' &&
this.options.pugIdNotation !== 'literal')
) {
numNormalAttributes++;
}
hasLiteralAttributes = true;
const val: string = tempToken.val.toString();
if (isQuoted(val)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
div(class="class")
div(
class="class",
attribute="value"
)
.class(attribute="value")
.class(
attribute="value",
another-attribute="another value"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
div(class="class")
div(class="class" attribute="value")
div.class(attribute="value")
div.class(attribute="value" another-attribute="another value")
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { compareFiles } from 'tests/common';
import { describe, expect, it } from 'vitest';

describe('Options', () => {
describe('pugWrapAttributesThreshold', () => {
it('should count attribute classes toward wrapping if pugClassNotation === as-is', async () => {
const { actual, expected } = await compareFiles(import.meta.url, {
formatOptions: {
pugWrapAttributesThreshold: 1,
pugClassNotation: 'as-is',
},
});

expect(actual).toBe(expected);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
div(id="foo")
div(
id="foo",
attribute="value"
)
#foo(attribute="value")
#foo(
attribute="value",
another-attribute="another value"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
div(id="foo")
div(id="foo" attribute="value")
div#foo(attribute="value")
div#foo(attribute="value" another-attribute="another value")
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { compareFiles } from 'tests/common';
import { describe, expect, it } from 'vitest';

describe('Options', () => {
describe('pugWrapAttributesThreshold', () => {
it('should count attribute IDs toward wrapping if pugIdNotation === as-is', async () => {
const { actual, expected } = await compareFiles(import.meta.url, {
formatOptions: {
pugWrapAttributesThreshold: 1,
pugIdNotation: 'as-is',
},
});

expect(actual).toBe(expected);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
div(class="class")
div(
class="class",
attribute="value"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
div(class="class")
div(class="class" attribute="value")
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { compareFiles } from 'tests/common';
import { describe, expect, it } from 'vitest';

describe('Options', () => {
describe('pugWrapAttributesThreshold', () => {
it('should count attribute classes toward wrapping if pugClassNotation === attribute', async () => {
const { actual, expected } = await compareFiles(import.meta.url, {
formatOptions: {
pugWrapAttributesThreshold: 1,
pugClassNotation: 'attribute',
},
});

expect(actual).toBe(expected);
});
});
});

0 comments on commit 4ca4d4b

Please sign in to comment.