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

fixed empty/no script tag in vue component error #192

Merged
merged 2 commits into from
Dec 14, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions examples/example.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<template>
<div>
<span>hello world
</span></div>
</template>

<script>
// I am top level comment in this file.
// I am second line of top level comment in this file.
import threeLevelRelativePath from '../../../threeLevelRelativePath';
import sameLevelRelativePath from './sameLevelRelativePath';
import thirdParty from 'third-party';
import React from 'react';
export { random } from './random';
import oneLevelRelativePath from '../oneLevelRelativePath';
import otherthing from '@core/otherthing';
import twoLevelRelativePath from '../../twoLevelRelativePath';
import component from '@ui/hello';
export default {
title: 'hello',
};
import fourLevelRelativePath from '../../../../fourLevelRelativePath';
import something from '@server/something';

function add(a, b) {
return a + b;
}

</script>

<style>
div { color: red; }
</style>
9 changes: 6 additions & 3 deletions src/preprocessors/vue-preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import { PrettierOptions } from '../types';
import { preprocessor } from './preprocessor';

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

const content = (descriptor.script ?? descriptor.scriptSetup)?.content;
if (!content) {
return code;
}

return code.replace(content, `\n${preprocessor(content, options)}\n`);
}
25 changes: 25 additions & 0 deletions tests/Vue/__snapshots__/ppsi.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,31 @@ export default defineComponent({});

`;

exports[`sfcNoScript.vue - vue-verify: sfcNoScript.vue 1`] = `
<template>
<div>
<span>hello world
</span></div>
</template>

<style>
div { color: red; }
</style>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<template>
<div>
<span>hello world </span>
</div>
</template>

<style>
div {
color: red;
}
</style>

`;

exports[`ts.vue - vue-verify: ts.vue 1`] = `
<script lang="ts">
// I am top level comment in this file.
Expand Down
9 changes: 9 additions & 0 deletions tests/Vue/sfcNoScript.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<template>
<div>
<span>hello world
</span></div>
</template>

<style>
div { color: red; }
</style>