Skip to content

Commit

Permalink
Vue: Sort both script and setup script (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
IanVS committed May 17, 2023
1 parent b74ccc3 commit 22ccb2f
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/preprocessors/vue.ts
@@ -1,14 +1,31 @@
import type { parse as Parse } from '@vue/compiler-sfc';

import { PrettierOptions } from '../types';
import { preprocessor } from './preprocessor';

export function vuePreprocessor(code: string, options: PrettierOptions) {
let preprocessedCode = code;
try {
const { parse } = require('@vue/compiler-sfc');
const { parse }: { parse: typeof Parse } = require('@vue/compiler-sfc');
const { descriptor } = parse(code);
const content =
(descriptor.script ?? descriptor.scriptSetup)?.content ?? code;

return code.replace(content, `\n${preprocessor(content, options)}\n`);
if (descriptor.script) {
const { content } = descriptor.script;
preprocessedCode = preprocessedCode.replace(
content,
`\n${preprocessor(content, options)}\n`,
);
}

if (descriptor.scriptSetup) {
const { content } = descriptor.scriptSetup;
preprocessedCode = preprocessedCode.replace(
content,
`\n${preprocessor(content, options)}\n`,
);
}

return preprocessedCode;
} catch (err) {
if ((err as NodeJS.ErrnoException).code === 'MODULE_NOT_FOUND') {
console.warn(
Expand Down
94 changes: 94 additions & 0 deletions tests/Vue/__snapshots__/ppsi.spec.ts.snap
@@ -1,5 +1,99 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`no-script.vue - vue-verify > no-script.vue 1`] = `
<template>
<router-view />
</template>
<style lang="less">
#app {
height: 100%;
background-color: inherit;
}
</style>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<template>
<router-view />
</template>
<style lang="less">
#app {
height: 100%;
background-color: inherit;
}
</style>
`;

exports[`script-and-setup.vue - vue-verify > script-and-setup.vue 1`] = `
<script>
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 { defineComponent } from 'vue'
function add(a,b) {
return a + b;
}
</script>
<script setup>
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 { defineComponent } from 'vue'
function add(a,b) {
return a + b;
}
</script>
<template>
<div></div>
</template>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<script>
import thirdParty from "third-party";
import { defineComponent } from "vue";
import z from "z";
import otherthing from "@core/otherthing";
import threeLevelRelativePath from "../../../threeLevelRelativePath";
import oneLevelRelativePath from "../oneLevelRelativePath";
import sameLevelRelativePath from "./sameLevelRelativePath";
function add(a, b) {
return a + b;
}
</script>
<script setup>
import thirdParty from "third-party";
import { defineComponent } from "vue";
import z from "z";
import otherthing from "@core/otherthing";
import threeLevelRelativePath from "../../../threeLevelRelativePath";
import oneLevelRelativePath from "../oneLevelRelativePath";
import sameLevelRelativePath from "./sameLevelRelativePath";
function add(a, b) {
return a + b;
}
</script>
<template>
<div></div>
</template>
`;

exports[`setup.vue - vue-verify > setup.vue 1`] = `
<script setup>
// I am top level comment in this file.
Expand Down
10 changes: 10 additions & 0 deletions tests/Vue/no-script.vue
@@ -0,0 +1,10 @@
<template>
<router-view />
</template>

<style lang="less">
#app {
height: 100%;
background-color: inherit;
}
</style>
29 changes: 29 additions & 0 deletions tests/Vue/script-and-setup.vue
@@ -0,0 +1,29 @@
<script>
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 { defineComponent } from 'vue'
function add(a,b) {
return a + b;
}
</script>

<script setup>
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 { defineComponent } from 'vue'
function add(a,b) {
return a + b;
}
</script>

<template>
<div></div>
</template>

0 comments on commit 22ccb2f

Please sign in to comment.