Skip to content

Commit

Permalink
(#276) Fixed dollar sign group replace in vue preprocessor (#283)
Browse files Browse the repository at this point in the history
* (#276) Fixed dollar sign group replace in vue preprocessor

* (#218) format both script and script setup tags if they exist
  • Loading branch information
adamDilger authored Dec 2, 2024
1 parent 8072158 commit 88776b3
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/preprocessors/vue-preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, $<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;
}
122 changes: 122 additions & 0 deletions tests/Vue/__snapshots__/ppsi.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,109 @@ function add(a, b) {
`;

exports[`setupAndScript.vue - vue-verify: setupAndScript.vue 1`] = `
<script>
// I am top level comment in this file.
import z from 'z';
import threeLevelRelativePath from "../../../threeLevelRelativePath";
import sameLevelRelativePath from "./sameLevelRelativePath";
import thirdParty from "third-party";
import oneLevelRelativePath from "../oneLevelRelativePath";
import otherthing from "@core/otherthing";
import abc from "@core/abc";
import twoLevelRelativePath from "../../twoLevelRelativePath";
import component from "@ui/hello";
import fourLevelRelativePath from "../../../../fourLevelRelativePath";
import something from "@server/something";
import xyz from "@ui/xyz";
import { defineComponent } from 'vue'
function firstAdd(a,b) {
return a + b;
}
</script>
<script setup>
// I am top level comment in this file.
import z from 'z';
import threeLevelRelativePath from "../../../threeLevelRelativePath";
import sameLevelRelativePath from "./sameLevelRelativePath";
import thirdParty from "third-party";
import oneLevelRelativePath from "../oneLevelRelativePath";
import otherthing from "@core/otherthing";
import abc from "@core/abc";
import twoLevelRelativePath from "../../twoLevelRelativePath";
import component from "@ui/hello";
import fourLevelRelativePath from "../../../../fourLevelRelativePath";
import something from "@server/something";
import xyz from "@ui/xyz";
import { defineComponent } from 'vue'
function add(a,b) {
return a + b;
}
</script>
<template>
<div></div>
</template>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<script>
// I am top level comment in this file.
import thirdParty from "third-party";
import { defineComponent } from "vue";
import z from "z";
import abc from "@core/abc";
import otherthing from "@core/otherthing";
import something from "@server/something";
import component from "@ui/hello";
import xyz from "@ui/xyz";
import fourLevelRelativePath from "../../../../fourLevelRelativePath";
import threeLevelRelativePath from "../../../threeLevelRelativePath";
import twoLevelRelativePath from "../../twoLevelRelativePath";
import oneLevelRelativePath from "../oneLevelRelativePath";
import sameLevelRelativePath from "./sameLevelRelativePath";
function firstAdd(a, b) {
return a + b;
}
</script>
<script setup>
// I am top level comment in this file.
import thirdParty from "third-party";
import { defineComponent } from "vue";
import z from "z";
import abc from "@core/abc";
import otherthing from "@core/otherthing";
import something from "@server/something";
import component from "@ui/hello";
import xyz from "@ui/xyz";
import fourLevelRelativePath from "../../../../fourLevelRelativePath";
import threeLevelRelativePath from "../../../threeLevelRelativePath";
import twoLevelRelativePath from "../../twoLevelRelativePath";
import oneLevelRelativePath from "../oneLevelRelativePath";
import sameLevelRelativePath from "./sameLevelRelativePath";
function add(a, b) {
return a + b;
}
</script>
<template>
<div></div>
</template>
`;

exports[`sfc.vue - vue-verify: sfc.vue 1`] = `
<script>
// I am top level comment in this file.
Expand Down Expand Up @@ -137,6 +240,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
45 changes: 45 additions & 0 deletions tests/Vue/setupAndScript.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<script>
// I am top level comment in this file.
import z from 'z';
import threeLevelRelativePath from "../../../threeLevelRelativePath";
import sameLevelRelativePath from "./sameLevelRelativePath";
import thirdParty from "third-party";
import oneLevelRelativePath from "../oneLevelRelativePath";
import otherthing from "@core/otherthing";
import abc from "@core/abc";
import twoLevelRelativePath from "../../twoLevelRelativePath";
import component from "@ui/hello";
import fourLevelRelativePath from "../../../../fourLevelRelativePath";
import something from "@server/something";
import xyz from "@ui/xyz";
import { defineComponent } from 'vue'
function firstAdd(a,b) {
return a + b;
}
</script>

<script setup>
// I am top level comment in this file.
import z from 'z';
import threeLevelRelativePath from "../../../threeLevelRelativePath";
import sameLevelRelativePath from "./sameLevelRelativePath";
import thirdParty from "third-party";
import oneLevelRelativePath from "../oneLevelRelativePath";
import otherthing from "@core/otherthing";
import abc from "@core/abc";
import twoLevelRelativePath from "../../twoLevelRelativePath";
import component from "@ui/hello";
import fourLevelRelativePath from "../../../../fourLevelRelativePath";
import something from "@server/something";
import xyz from "@ui/xyz";
import { defineComponent } from 'vue'
function add(a,b) {
return a + b;
}
</script>

<template>
<div></div>
</template>
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 88776b3

Please sign in to comment.