From 10f4bda946c7a670d477c9f66a2bdcf1b60dcb38 Mon Sep 17 00:00:00 2001 From: Adam Dilger Date: Tue, 29 Nov 2022 12:16:23 +1100 Subject: [PATCH] fixed empty/no script tag in vue component error --- examples/example.vue | 33 +++++++++++++++++++++++++++ src/preprocessors/vue-preprocessor.ts | 9 +++++--- 2 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 examples/example.vue diff --git a/examples/example.vue b/examples/example.vue new file mode 100644 index 0000000..43fbdc7 --- /dev/null +++ b/examples/example.vue @@ -0,0 +1,33 @@ + + + + + diff --git a/src/preprocessors/vue-preprocessor.ts b/src/preprocessors/vue-preprocessor.ts index 2e45753..b29bbc4 100644 --- a/src/preprocessors/vue-preprocessor.ts +++ b/src/preprocessors/vue-preprocessor.ts @@ -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`); }