Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vue: Sort both script and setup script #90

Merged
merged 3 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/preprocessors/vue.ts
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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>