diff --git a/src/preprocessors/vue-preprocessor.ts b/src/preprocessors/vue-preprocessor.ts index b29bbc44..66b2bca4 100644 --- a/src/preprocessors/vue-preprocessor.ts +++ b/src/preprocessors/vue-preprocessor.ts @@ -5,10 +5,31 @@ export function vuePreprocessor(code: string, options: PrettierOptions) { const { parse } = require('@vue/compiler-sfc'); const { descriptor } = parse(code); - const content = (descriptor.script ?? descriptor.scriptSetup)?.content; - if (!content) { + const scriptContent = descriptor.script?.content; + const scriptSetupContent = descriptor.scriptSetup?.content; + + if (!scriptContent && !scriptSetupContent) { return code; } - return code.replace(content, `\n${preprocessor(content, options)}\n`); + let transformedCode = code; + + const replacer = (content: string) => { + // we pass the second argument as a function to avoid issues with the replacement string + // if string contained special groups (like $&, $`, $', $n, $, etc.) this would produce invalid results + return transformedCode.replace( + content, + () => `\n${preprocessor(content, options)}\n`, + ); + }; + + if (scriptContent) { + transformedCode = replacer(scriptContent); + } + + if (scriptSetupContent) { + transformedCode = replacer(scriptSetupContent); + } + + return transformedCode; } diff --git a/tests/Vue/__snapshots__/ppsi.spec.js.snap b/tests/Vue/__snapshots__/ppsi.spec.js.snap index 065c444f..afad6e57 100644 --- a/tests/Vue/__snapshots__/ppsi.spec.js.snap +++ b/tests/Vue/__snapshots__/ppsi.spec.js.snap @@ -57,6 +57,109 @@ function add(a, b) { `; +exports[`setupAndScript.vue - vue-verify: setupAndScript.vue 1`] = ` + + + + + +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + + + + + +`; + exports[`sfc.vue - vue-verify: sfc.vue 1`] = ` + +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + + +`; + exports[`ts.vue - vue-verify: ts.vue 1`] = ` + + + + diff --git a/tests/Vue/sfcWithSpecialReplacerGroups.vue b/tests/Vue/sfcWithSpecialReplacerGroups.vue new file mode 100644 index 00000000..d4c5bef2 --- /dev/null +++ b/tests/Vue/sfcWithSpecialReplacerGroups.vue @@ -0,0 +1,6 @@ + +