Skip to content

Commit 9e6620a

Browse files
authored
Fix instable pugClassNotation with printWidth (#318)
1 parent 59a59d1 commit 9e6620a

File tree

4 files changed

+119
-17
lines changed

4 files changed

+119
-17
lines changed

src/printer.ts

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,21 @@ export class PugPrinter {
769769
// Add leading and trailing parentheses
770770
this.currentLineLength += 2;
771771
}
772-
logger.debug(this.currentLineLength);
772+
if (this.options.pugClassLocation === 'after-attributes') {
773+
let tempClassIndex: number = tempIndex;
774+
let tempClassToken: EndAttributesToken | ClassToken = this.tokens[++tempClassIndex] as
775+
| EndAttributesToken
776+
| ClassToken;
777+
while (tempClassToken.type === 'class') {
778+
const val: string = tempClassToken.val.toString();
779+
// Add leading . for classes
780+
this.currentLineLength += 1 + val.length;
781+
logger.debug({ tokenVal: val, length: val.length }, this.currentLineLength);
782+
783+
tempClassToken = this.tokens[++tempClassIndex] as EndAttributesToken | ClassToken;
784+
}
785+
}
786+
logger.debug('final line length', { currentLineLength: this.currentLineLength });
773787
if (
774788
!this.currentlyInPugInterpolation &&
775789
!this.wrapAttributes &&
@@ -1020,6 +1034,8 @@ export class PugPrinter {
10201034
this.classLiteralAfterAttributes.length
10211035
);
10221036
this.result += `.${classes.join('.')}`;
1037+
}
1038+
if (this.options.pugClassLocation === 'after-attributes') {
10231039
this.possibleClassPosition = this.result.length;
10241040
}
10251041
if (this.nextToken?.type === 'text' || this.nextToken?.type === 'path') {
@@ -1069,9 +1085,7 @@ export class PugPrinter {
10691085
}
10701086
} else {
10711087
const val: string = `.${token.val}`;
1072-
if (this.options.pugClassLocation === 'before-attributes') {
1073-
this.currentLineLength += val.length;
1074-
}
1088+
this.currentLineLength += val.length;
10751089
logger.debug(
10761090
'before class',
10771091
{ result: this.result, val, length: val.length, previousToken: this.previousToken },
@@ -1107,19 +1121,6 @@ export class PugPrinter {
11071121
default: {
11081122
if (this.options.pugClassLocation === 'after-attributes') {
11091123
this.classLiteralAfterAttributes.push(val.slice(1));
1110-
logger.debug('class default', {
1111-
classLiteralAfterAttributes: this.classLiteralAfterAttributes
1112-
});
1113-
let result: string = this.result.slice(0, this.possibleClassPosition);
1114-
if (['text', 'newline', 'indent', 'eos', 'code', undefined].includes(this.nextToken?.type)) {
1115-
const classes: string[] = this.classLiteralAfterAttributes.splice(
1116-
0,
1117-
this.classLiteralAfterAttributes.length
1118-
);
1119-
result += '.' + classes.join('.');
1120-
}
1121-
this.result = [result, this.result.slice(this.possibleClassPosition)].join('');
1122-
this.possibleClassPosition = this.result.length;
11231124
} else {
11241125
const prefix: string = this.result.slice(0, this.possibleClassPosition);
11251126
this.result = [prefix, val, this.result.slice(this.possibleClassPosition)].join('');
@@ -1128,6 +1129,19 @@ export class PugPrinter {
11281129
break;
11291130
}
11301131
}
1132+
if (this.options.pugClassLocation === 'after-attributes' && this.classLiteralAfterAttributes.length > 0) {
1133+
let result: string = this.result.slice(0, this.possibleClassPosition);
1134+
if (['text', 'newline', 'indent', 'outdent', 'eos', 'code', undefined].includes(this.nextToken?.type)) {
1135+
const classes: string[] = this.classLiteralAfterAttributes.splice(
1136+
0,
1137+
this.classLiteralAfterAttributes.length
1138+
);
1139+
result += '.' + classes.join('.');
1140+
}
1141+
this.result = [result, this.result.slice(this.possibleClassPosition)].join('');
1142+
this.possibleClassPosition = this.result.length;
1143+
this.replaceTagWithLiteralIfPossible(/div\./, '.');
1144+
}
11311145
logger.debug('after class', { result: this.result, val, length: val.length }, this.currentLineLength);
11321146
if (this.nextToken?.type === 'text') {
11331147
this.currentLineLength += 1;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
v-row(no-gutters)
2+
v-col(cols="12")
3+
div(v-t="'global.copy-variables-title'").text-body-2.font-weight-medium
4+
v-col(cols="12")
5+
a(
6+
v-for="variable in variables",
7+
:key="variable",
8+
v-text="`$\\{${variable}}`",
9+
@click="copyToClipboard(variable)"
10+
).text-body-2.d-inline.mr-4
11+
div(
12+
v-t="'entity-management.entity-group.crud.entity-types-txt'"
13+
).text-body-2.text--secondary.mb-2.mt-4.class--1519
14+
15+
div(
16+
v-t="'type.place.property'"
17+
).class-1.class-2.class-3.class-4.class-5.class-6.class-7.get-to-121.class-10.class-50.sm
18+
19+
div(role="document").modal-dialog.modal-lg
20+
.modal-content
21+
.modal-header.text-center.d-block
22+
h1#modal-title.h4.modal-title.d-inline-block.ml-4= "test goes here"
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { readFileSync } from 'fs';
2+
import { resolve } from 'path';
3+
import { format } from 'prettier';
4+
import { plugin } from './../../../src/index';
5+
6+
describe('Issues', () => {
7+
test('should respect printWidth and pugClassLocation with pugSortAttributesEnd', () => {
8+
const expected: string = readFileSync(resolve(__dirname, 'formatted.pug'), 'utf8');
9+
const code: string = readFileSync(resolve(__dirname, 'unformatted.pug'), 'utf8');
10+
const actual: string = format(code, {
11+
parser: 'pug',
12+
plugins: [plugin],
13+
14+
printWidth: 120,
15+
16+
pugSortAttributesEnd: ['^@click'],
17+
pugClassLocation: 'after-attributes'
18+
});
19+
20+
expect(actual).toBe(expected);
21+
});
22+
23+
test('should keep same format after two runs with printWidth and pugClassLocation', () => {
24+
const expected: string = readFileSync(resolve(__dirname, 'formatted.pug'), 'utf8');
25+
const code: string = readFileSync(resolve(__dirname, 'unformatted.pug'), 'utf8');
26+
const run1: string = format(code, {
27+
parser: 'pug',
28+
plugins: [plugin],
29+
30+
printWidth: 120,
31+
32+
pugClassLocation: 'after-attributes'
33+
});
34+
35+
const run2: string = format(run1, {
36+
parser: 'pug',
37+
plugins: [plugin],
38+
39+
printWidth: 120,
40+
41+
pugClassLocation: 'after-attributes'
42+
});
43+
44+
expect(run2).toBe(expected);
45+
});
46+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
v-row(no-gutters)
2+
v-col(cols="12")
3+
.text-body-2.font-weight-medium(v-t="'global.copy-variables-title'")
4+
v-col(cols="12")
5+
a.text-body-2.d-inline.mr-4(
6+
v-for="variable in variables",
7+
:key="variable",
8+
v-text="`$\\{${variable}}`",
9+
@click="copyToClipboard(variable)"
10+
)
11+
div(
12+
v-t="'entity-management.entity-group.crud.entity-types-txt'"
13+
).text-body-2.text--secondary.mb-2.mt-4.class--1519
14+
15+
div(v-t="'type.place.property'").class-1.class-2.class-3.class-4.class-5.class-6.class-7.get-to-121.class-10.class-50.sm
16+
17+
.modal-dialog.modal-lg(role="document")
18+
.modal-content
19+
.modal-header.text-center.d-block
20+
h1#modal-title.h4.modal-title.d-inline-block.ml-4= 'test goes here'

0 commit comments

Comments
 (0)