Skip to content

Commit

Permalink
(trivago#276) Fixed dollar sign group replace in vue preprocessor
Browse files Browse the repository at this point in the history
  • Loading branch information
adamDilger committed Feb 14, 2024
1 parent 61d0697 commit cc640ff
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/preprocessors/vue-preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ export function vuePreprocessor(code: string, options: PrettierOptions) {
return code;
}

return code.replace(content, `\n${preprocessor(content, options)}\n`);
// 'replacer' is a function so it returns the preprocessed code as-is.
// If it were passed as just a string and the string contained special groups (like $&, $`, $', $n, $<n>, etc.) this would produce invalid results
const replacer = () => `\n${preprocessor(content, options)}\n`;

return code.replace(content, replacer);
}
19 changes: 19 additions & 0 deletions tests/Vue/__snapshots__/ppsi.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,25 @@ div {
`;

exports[`sfcWithSpecialReplacerGroups.vue - vue-verify: sfcWithSpecialReplacerGroups.vue 1`] = `
<script setup>
let a = '$';
let b = '$';
let c = '$$';
</script>
<template><div>{{ a + b + c }}</div></template>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<script setup>
let a = "$";
let b = "$";
let c = "$$";
</script>
<template>
<div>{{ a + b + c }}</div>
</template>
`;

exports[`ts.vue - vue-verify: ts.vue 1`] = `
<script lang="ts">
// I am top level comment in this file.
Expand Down
6 changes: 6 additions & 0 deletions tests/Vue/sfcWithSpecialReplacerGroups.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script setup>
let a = '$';
let b = '$';
let c = '$$';
</script>
<template><div>{{ a + b + c }}</div></template>

0 comments on commit cc640ff

Please sign in to comment.