From 253ca2729d808fc051215876aa4af986e4caa43c Mon Sep 17 00:00:00 2001 From: Evan You Date: Fri, 2 Jul 2021 08:24:24 -0400 Subject: [PATCH] fix(compiler-sfc): fix parse-only mode when there is no script setup block --- .../__tests__/compileScriptParseOnlyMode.spec.ts | 10 ++++++++++ packages/compiler-sfc/src/compileScript.ts | 9 +++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/compiler-sfc/__tests__/compileScriptParseOnlyMode.spec.ts b/packages/compiler-sfc/__tests__/compileScriptParseOnlyMode.spec.ts index 50a8a112245..86a5a7bf00c 100644 --- a/packages/compiler-sfc/__tests__/compileScriptParseOnlyMode.spec.ts +++ b/packages/compiler-sfc/__tests__/compileScriptParseOnlyMode.spec.ts @@ -74,4 +74,14 @@ describe('compileScript parseOnly mode', () => { const { ranges } = compile(``) expect(getRange(src, ranges!.emitsTypeArg!)).toBe(`{ (e: 'x'): void }`) }) + + test('no script setup block', () => { + const src = `import { x } from './x'` + const { ranges } = compile(``) + expect(getRange(src, ranges!.scriptBindings[0])).toBe(`x`) + }) + + test('no script block', () => { + expect(() => compile(``)).not.toThrow() + }) }) diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index bf4cec6719e..6d4733f0b85 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -1,5 +1,10 @@ import MagicString from 'magic-string' -import { BindingMetadata, BindingTypes, UNREF } from '@vue/compiler-core' +import { + BindingMetadata, + BindingTypes, + locStub, + UNREF +} from '@vue/compiler-core' import { ScriptSetupTextRanges, SFCDescriptor, @@ -126,7 +131,7 @@ export function compileScript( type: 'script', content: '', attrs: {}, - loc: null as any + loc: locStub } }