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

Sort both <script> and <script setup> for vue sfc, fix #218 #219

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions src/preprocessors/vue-preprocessor.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
import type { SFCBlock, SFCDescriptor } from '@vue/compiler-sfc';

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

export function vuePreprocessor(code: string, options: PrettierOptions) {
const { parse } = require('@vue/compiler-sfc');
const { descriptor } = parse(code);
const descriptor: SFCDescriptor = parse(code).descriptor;

const content = (descriptor.script ?? descriptor.scriptSetup)?.content;
if (!content) {
// 1. filter valid blocks
const blocks = [descriptor.script, descriptor.scriptSetup].filter(
(block) => block?.content,
) as SFCBlock[];
if (!blocks.length) {
return code;
}

return code.replace(content, `\n${preprocessor(content, options)}\n`);
// 2. sort blocks by start position
blocks.sort((a, b) => a.loc.start.offset - b.loc.start.offset);

// 3. replace blocks
// Using offsets to avoid string replace catching the wrong place and improve efficiency
let offset = 0;
let result = '';
for (const block of blocks) {
// https://github.com/vuejs/core/blob/9060bf0353e88cc1f4cf06981b9799c5c1e09466/packages/compiler-core/src/ast.ts#L71
// The node's range. The `start` is inclusive and `end` is exclusive.
// [start, end)
const { start, end } = block.loc;
const transformed = `\n${preprocessor(block.content, options)}\n`;
result += code.slice(offset, start.offset) + transformed;
offset = end.offset;
}

// 4. append the rest
result += code.slice(offset);

return result;
}
109 changes: 109 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,115 @@ function add(a, b) {

`;

exports[`setupWithScript.vue - vue-verify: setupWithScript.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 add(a,b) {
return a + b;
}

export default defineComponent({

})
</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 add(a, b) {
return a + b;
}

export default defineComponent({});
</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
49 changes: 49 additions & 0 deletions tests/Vue/setupWithScript.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<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 add(a,b) {
return a + b;
}

export default defineComponent({

})
</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>