Skip to content

Commit b09e029

Browse files
authored
Stop unnecessary quote escaping on vue v-bind strings (#580)
1 parent c886105 commit b09e029

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

src/printer.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -680,21 +680,19 @@ export class PugPrinter {
680680
): Promise<string> {
681681
const { trimTrailingSemicolon = false, trimLeadingSemicolon = true } =
682682
formatOptions;
683-
val = val.trim();
684683
const options: Options = { ...this.codeInterpolationOptions };
684+
685+
val = val.trim();
685686
const wasQuoted: boolean = isQuoted(val);
687+
686688
if (wasQuoted) {
687689
options.singleQuote = !this.options.pugSingleQuote;
688690
val = val.slice(1, -1); // Remove quotes
689691
}
690692

691693
val = await format(val, { parser, ...options });
692-
val =
693-
this.quotes === '"'
694-
? // Escape double quotes, but only if they are not already escaped
695-
val.replaceAll(/(?<!\\)((?:\\\\)*)"/g, '$1\\"')
696-
: val.replaceAll("'", "\\'");
697694
val = unwrapLineFeeds(val);
695+
698696
if (trimTrailingSemicolon && val.at(-1) === ';') {
699697
val = val.slice(0, -1);
700698
}
@@ -703,7 +701,17 @@ export class PugPrinter {
703701
val = val.slice(1);
704702
}
705703

706-
return wasQuoted ? this.quoteString(val) : val;
704+
if (wasQuoted) {
705+
val =
706+
this.quotes === '"'
707+
? // Escape double quotes, but only if they are not already escaped
708+
(val = val.replaceAll(/(?<!\\)((?:\\\\)*)"/g, '$1\\"'))
709+
: (val = val.replaceAll("'", "\\'"));
710+
711+
val = this.quoteString(val);
712+
}
713+
714+
return val;
707715
}
708716

709717
private async formatStyleAttribute(val: string): Promise<string> {

tests/frameworks/vue/v-bind/formatted.pug

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@ a(v-bind="{ ...$attrs }") ...
22
a(v-bind:href="url") ...
33
a(:href="url") ...
44
a(:[key]="url") ...
5+
6+
div(:value=attributes["data-value"])
7+
div(:value=attributes['"data-value"'])
8+
div(:value=attributes["\"'data-value'\""])
9+
div(:value="\"\'data-value\'\"")

tests/frameworks/vue/v-bind/unformatted.pug

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@ a(v-bind="{ ...$attrs }") ...
22
a(v-bind:href=" url ") ...
33
a(:href=" url ") ...
44
a(:[key]=" url ") ...
5+
6+
div(:value=attributes['data-value'])
7+
div(:value=attributes['"data-value"'])
8+
div(:value=attributes['"\'data-value\'"'])
9+
div(:value='"\'data-value\'"')

0 commit comments

Comments
 (0)