-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(compiler-sfc): compileScript parseOnly mode
This is an internal feature meant for IDE support
- Loading branch information
Showing
3 changed files
with
277 additions
and
58 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
packages/compiler-sfc/__tests__/compileScriptParseOnlyMode.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { TextRange } from '../src/parse' | ||
import { compileSFCScript } from './utils' | ||
|
||
describe('compileScript parseOnly mode', () => { | ||
function compile(src: string) { | ||
return compileSFCScript(src, { parseOnly: true }) | ||
} | ||
|
||
function getRange(src: string, range: TextRange) { | ||
return src.slice(range.start, range.end) | ||
} | ||
|
||
test('bindings', () => { | ||
const scriptSrc = ` | ||
import { foo } from './x' | ||
` | ||
const scriptSetupSrc = ` | ||
import { bar } from './x' | ||
const a = 123 | ||
function b() {} | ||
class c {} | ||
` | ||
const src = ` | ||
<script>${scriptSrc}</script> | ||
<script setup>${scriptSetupSrc}</script> | ||
` | ||
const { ranges } = compile(src) | ||
|
||
expect(getRange(scriptSrc, ranges!.scriptBindings[0])).toBe('foo') | ||
expect( | ||
ranges!.scriptSetupBindings.map(r => getRange(scriptSetupSrc, r)) | ||
).toMatchObject(['bar', 'a', 'b', 'c']) | ||
}) | ||
|
||
test('defineProps', () => { | ||
const src = ` | ||
defineProps({ foo: String }) | ||
` | ||
const { ranges } = compile(`<script setup>${src}</script>`) | ||
expect(getRange(src, ranges!.propsRuntimeArg!)).toBe(`{ foo: String }`) | ||
}) | ||
|
||
test('defineProps (type)', () => { | ||
const src = ` | ||
interface Props { x?: number } | ||
defineProps<Props>() | ||
` | ||
const { ranges } = compile(`<script setup lang="ts">${src}</script>`) | ||
expect(getRange(src, ranges!.propsTypeArg!)).toBe(`Props`) | ||
}) | ||
|
||
test('withDefaults', () => { | ||
const src = ` | ||
interface Props { x?: number } | ||
withDefaults(defineProps<Props>(), { x: 1 }) | ||
` | ||
const { ranges } = compile(`<script setup lang="ts">${src}</script>`) | ||
expect(getRange(src, ranges!.withDefaultsArg!)).toBe(`{ x: 1 }`) | ||
}) | ||
|
||
test('defineEmits', () => { | ||
const src = ` | ||
defineEmits(['foo']) | ||
` | ||
const { ranges } = compile(`<script setup>${src}</script>`) | ||
expect(getRange(src, ranges!.emitsRuntimeArg!)).toBe(`['foo']`) | ||
}) | ||
|
||
test('defineEmits (type)', () => { | ||
const src = ` | ||
defineEmits<{ (e: 'x'): void }>() | ||
` | ||
const { ranges } = compile(`<script setup lang="ts">${src}</script>`) | ||
expect(getRange(src, ranges!.emitsTypeArg!)).toBe(`{ (e: 'x'): void }`) | ||
}) | ||
}) |
Oops, something went wrong.