Skip to content

Commit 342bb69

Browse files
authored
Do not reformat interpolation attribute values (#94)
2 parents eb258fc + cb127c4 commit 342bb69

File tree

5 files changed

+72
-2
lines changed

5 files changed

+72
-2
lines changed

src/printer.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ import {
5656
resolveClosingBracketPositionOption
5757
} from './options';
5858
import { isAngularAction, isAngularBinding, isAngularDirective, isAngularInterpolation } from './utils/angular';
59-
import { isQuoted, makeString, previousNormalAttributeToken, unwrapLineFeeds } from './utils/common';
59+
import {
60+
isQuoted,
61+
isMultilineInterpolation,
62+
makeString,
63+
previousNormalAttributeToken,
64+
unwrapLineFeeds
65+
} from './utils/common';
6066
import { isVueEventBinding, isVueExpression } from './utils/vue';
6167

6268
const logger: Logger = createLogger(console);
@@ -526,7 +532,9 @@ export class PugPrinter {
526532
}
527533
} else {
528534
let val = token.val;
529-
if (isVueExpression(token.name)) {
535+
if (isMultilineInterpolation(val)) {
536+
// do not reformat multiline strings surrounded by `
537+
} else if (isVueExpression(token.name)) {
530538
val = this.formatVueExpression(val);
531539
} else if (isVueEventBinding(token.name)) {
532540
val = this.formatVueEventBinding(val);

src/utils/common.ts

+4
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ export function isQuoted(val: string): boolean {
6565
return /^["'](.*)["']$/.test(val);
6666
}
6767

68+
export function isMultilineInterpolation(val: string): boolean {
69+
return /^`[\s\S]*`$/m.test(val) && val.includes('\n');
70+
}
71+
6872
// Copy of https://github.com/prettier/prettier/blob/master/src/common/util.js#L647
6973
export function makeString(
7074
rawContent: string,

test/issues/issue-80/formatted.pug

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
div
2+
span.aui-lozenge(
3+
:class=`{
4+
"aui-lozenge-inprogress": pipeline.status == PipelineStatus.Running,
5+
"aui-lozenge-error": pipeline.status == PipelineStatus.Failed
6+
}`
7+
)
8+
9+
span.aui-lozenge(
10+
:class='[isActive ? "some-class-name" : "some-other-class-name"]'
11+
)
12+
13+
span.aui-lozenge(
14+
:class='[isActive ? "some-class-name" : "some-other-class-name"]'
15+
)
16+
17+
span.aui-lozenge(
18+
:class='[isActive ? "some-class-name" : "some-other-class-name"]'
19+
)

test/issues/issue-80/issue-80.test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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 not reformat multiline interpolation strings', () => {
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' as any,
12+
plugins: [plugin],
13+
14+
semi: false,
15+
singleQuote: true
16+
});
17+
18+
expect(actual).toBe(expected);
19+
});
20+
});

test/issues/issue-80/unformatted.pug

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
div
2+
span.aui-lozenge(
3+
:class=`{
4+
"aui-lozenge-inprogress": pipeline.status == PipelineStatus.Running,
5+
"aui-lozenge-error": pipeline.status == PipelineStatus.Failed
6+
}`
7+
)
8+
9+
span.aui-lozenge(
10+
:class=`[ isActive ? "some-class-name" : "some-other-class-name" ]`
11+
)
12+
13+
span.aui-lozenge(
14+
:class="[ isActive ? 'some-class-name' : 'some-other-class-name' ]"
15+
)
16+
17+
span.aui-lozenge(
18+
:class='[ isActive ? "some-class-name" : "some-other-class-name" ]'
19+
)

0 commit comments

Comments
 (0)