Skip to content

Commit

Permalink
Merge pull request #192 from adamDilger/master
Browse files Browse the repository at this point in the history
fixed empty/no script tag in vue component error
  • Loading branch information
byara authored Dec 14, 2022
2 parents 7747321 + 5dabf83 commit e1a32fd
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 3 deletions.
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>

0 comments on commit e1a32fd

Please sign in to comment.