Skip to content

Commit

Permalink
refactor(volar): use muggle-string utils (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Jan 26, 2023
1 parent 5fee1ad commit cf20f6f
Show file tree
Hide file tree
Showing 34 changed files with 129 additions and 101 deletions.
5 changes: 5 additions & 0 deletions .changeset/honest-comics-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@vue-macros/volar': minor
---

use muggle-string utils
13 changes: 9 additions & 4 deletions .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ jobs:
- name: Install
run: nci

- name: Build
run: nr build
env:
TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
TURBO_TEAM: sxzz

- name: Lint
run: nr lint

Expand All @@ -46,10 +52,6 @@ jobs:

runs-on: ${{ matrix.os }}

env:
TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
TURBO_TEAM: sxzz

steps:
- uses: actions/checkout@v3

Expand All @@ -69,6 +71,9 @@ jobs:

- name: Build
run: nr build
env:
TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
TURBO_TEAM: sxzz

- name: Unit Test
run: nr test
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"publish": "pnpm build && changeset publish",
"typecheck": "run-p \"typecheck:*\"",
"typecheck:lib": "tsc --noEmit -p tsconfig.lib.json",
"typecheck:test": "tsc --noEmit -p tsconfig.test.json"
"typecheck:test": "tsc --noEmit -p tsconfig.test.json",
"typecheck:fixture": "vue-tsc --noEmit -p tsconfig.fixture.json"
},
"devDependencies": {
"@babel/types": "^7.20.7",
Expand All @@ -35,6 +36,7 @@
"@vitest/coverage-c8": "^0.28.1",
"@vitest/ui": "^0.28.1",
"@vue-macros/test-utils": "workspace:*",
"@vue-macros/volar": "workspace:*",
"eslint": "^8.32.0",
"eslint-define-config": "^1.14.0",
"expect-type": "^0.15.0",
Expand All @@ -48,6 +50,7 @@
"vite": "^4.0.4",
"vitest": "^0.28.1",
"vue": "^3.2.45",
"vue-tsc": "^1.0.24",
"vue2": "npm:vue@^2.7.14",
"webpack": "^5.75.0"
},
Expand Down
2 changes: 2 additions & 0 deletions packages/api/tests/fixtures/circular-referencing/bar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export * from './foo'
export type Bar = 'bar'

// @ts-expect-error
export type A = B
// @ts-expect-error
export type B = A
1 change: 1 addition & 0 deletions packages/better-define/tests/fixtures/optional-method.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
defineProps<{
onClick?(): void
onSubmit(): void
// @ts-expect-error
onSubmit?(): void
}>()
</script>
12 changes: 6 additions & 6 deletions packages/define-model/tests/__snapshots__/fixtures.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ const emit = defineEmits<{(evt: 'input', value: string): void
}>()
{
value = 'hello'
title = 'world'
value.value = 'hello'
title.value = 'world'
emit('change')
{
Expand Down Expand Up @@ -323,8 +323,8 @@ const emit = defineEmits<{(evt: 'changeInput', value: string): void
}>()
{
value = 'hello'
title = 'world'
value.value = 'hello'
title.value = 'world'
emit('change')
{
Expand Down Expand Up @@ -361,8 +361,8 @@ const emit = defineEmits<{(evt: 'changeInput', value: string): void
}>()
{
value = 'hello'
title = 'world'
value.value = 'hello'
title.value = 'world'
emit('change')
{
Expand Down
4 changes: 2 additions & 2 deletions packages/define-model/tests/fixtures/runtime/vue2/basic.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const emit = defineEmits<{
}>()
{
value = 'hello'
title = 'world'
value.value = 'hello'
title.value = 'world'
emit('change')
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ const emit = defineEmits<{
}>()
{
value = 'hello'
title = 'world'
value.value = 'hello'
title.value = 'world'
emit('change')
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ const emit = defineEmits<{
}>()
{
value = 'hello'
title = 'world'
value.value = 'hello'
title.value = 'world'
emit('change')
{
Expand Down
1 change: 1 addition & 0 deletions packages/define-render/tests/fixtures/without-fn.vue
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<script setup lang="tsx">
// @ts-nocheck
defineRender(<div>Hello</div>)
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ const { foo = 'foo', bar = 10, baz, qux = fn(), } = defineProps<{
</script>
<template>
<div></div>
</template>
function fn() {}
</script>
"
`;
6 changes: 2 additions & 4 deletions packages/export-props/tests/fixtures/basic.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ export const foo: string = 'foo'
export const bar = 10
export let baz: string | undefined
export var qux = fn()
</script>
<template>
<div></div>
</template>
function fn() {}
</script>
1 change: 0 additions & 1 deletion packages/nuxt/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { defineNuxtModule } from '@nuxt/kit'
// @ts-ignore error when no bundle (CI typecheck)
import VueMacros from 'unplugin-vue-macros/vite'
import { transformShortVmodel } from '@vue-macros/short-vmodel'
import type { Options } from 'unplugin-vue-macros'
Expand Down
1 change: 0 additions & 1 deletion packages/volar/define-model.js

This file was deleted.

1 change: 0 additions & 1 deletion packages/volar/define-props.js

This file was deleted.

1 change: 0 additions & 1 deletion packages/volar/define-slots.js

This file was deleted.

1 change: 0 additions & 1 deletion packages/volar/export-props.js

This file was deleted.

16 changes: 2 additions & 14 deletions packages/volar/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,8 @@
"*.js"
],
"exports": {
"./define-model": {
"require": "./define-model.js"
},
"./define-slots": {
"require": "./define-slots.js"
},
"./export-props": {
"require": "./export-props.js"
},
"./short-vmodel": {
"require": "./short-vmodel.js"
},
"./define-props": {
"require": "./define-props.js"
"./*": {
"require": "./dist/*.js"
}
},
"scripts": {
Expand Down
1 change: 0 additions & 1 deletion packages/volar/short-vmodel.js

This file was deleted.

33 changes: 28 additions & 5 deletions packages/volar/src/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { FileRangeCapabilities } from '@volar/language-core'
import { replace } from 'muggle-string'
import type { Segment } from 'muggle-string'
import type { FileRangeCapabilities } from '@volar/language-core'

export function getVueLibraryName(vueVersion: number) {
return vueVersion < 2.7 ? '@vue/runtime-dom' : 'vue'
Expand All @@ -10,13 +11,35 @@ export function addProps(
decl: Segment<FileRangeCapabilities>[],
vueLibName: string
) {
const idx = content.indexOf('setup() {\n')
if (idx === -1) return false
content.splice(idx, 0, ...['props: ({} as ', ...decl, '),\n'])

replace(
content,
/setup\(\) {/,
'setup() {\n',
'props: ({} as ',
...decl,
'),'
)
content.push(
`type __VLS_NonUndefinedable<T> = T extends undefined ? never : T;\n`,
`type __VLS_TypePropsToRuntimeProps<T> = { [K in keyof T]-?: {} extends Pick<T, K> ? { type: import('${vueLibName}').PropType<__VLS_NonUndefinedable<T[K]>> } : { type: import('${vueLibName}').PropType<T[K]>, required: true } };\n`
)
return true
}

export function addEmits(
content: Segment<FileRangeCapabilities>[],
decl: Segment<FileRangeCapabilities>[]
) {
const idx = content.indexOf('setup() {\n')
if (idx === -1) return false

replace(
content,
/setup\(\) {/,
'setup() {\n',
'emits: ({} as ',
...decl,
'),'
)
return true
}
23 changes: 4 additions & 19 deletions packages/volar/src/define-model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { DEFINE_MODEL, DEFINE_MODEL_DOLLAR } from '@vue-macros/common'
import { FileKind, FileRangeCapabilities } from '@volar/language-core'
import { addProps, getVueLibraryName } from './common'
import type { FileCapabilities } from '@volar/language-core'
import { addEmits, addProps, getVueLibraryName } from './common'
import type { Segment } from 'muggle-string'
import type {
Sfc,
Expand Down Expand Up @@ -36,7 +35,7 @@ function transformDefineModel({
['__VLS_TypePropsToRuntimeProps<__VLS_ModelToProps<', seg, '>>'],
vueLibName
)
mergeEmits() || addEmits()
mergeEmits() || addEmits(codes, ['__VLS_ModelToEmits<', seg, '>'])

codes.push(
`type __VLS_GetPropKey<K> = K extends 'modelValue'${
Expand Down Expand Up @@ -68,19 +67,6 @@ function transformDefineModel({
codes.splice(idx + 2, 1, '>> & __VLS_ModelToEmits<', seg, '>),\n')
return true
}

function addEmits() {
const idx = codes.indexOf('setup() {\n')
if (idx === -1) return false

const segs: Segment<FileCapabilities>[] = [
'emits: ({} as __VLS_ModelToEmits<',
seg,
'>),\n',
]
codes.splice(idx, 0, ...segs)
return true
}
}

function getTypeArg(
Expand Down Expand Up @@ -137,8 +123,7 @@ function resolve({
const vueVersion = vueCompilerOptions.target
const vueLibName = getVueLibraryName(vueVersion)
const unified =
(vueVersion < 3 && (vueCompilerOptions as any)?.defineModel?.unified) ??
true
(vueVersion < 3 && vueCompilerOptions?.defineModel?.unified) ?? true

transformDefineModel({
codes: embeddedFile.content,
Expand Down Expand Up @@ -166,4 +151,4 @@ const plugin: VueLanguagePlugin = ({
},
}
}
export default plugin
export = plugin
2 changes: 1 addition & 1 deletion packages/volar/src/define-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ const plugin: VueLanguagePlugin = ({ vueCompilerOptions }) => {
}
}

export default plugin
export = plugin
15 changes: 9 additions & 6 deletions packages/volar/src/define-slots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ const transform = ({
sfc: Sfc
}) => {
if (embeddedFile.kind !== FileKind.TypeScriptHostFile) return
if (!toString(embeddedFile.content).includes(DEFINE_SLOTS)) return

if (!embeddedFile.content.includes('return __VLS_slots;\n')) return
const textContent = toString(embeddedFile.content)
if (
!textContent.includes(DEFINE_SLOTS) ||
!textContent.includes('return __VLS_slots')
)
return

replace(
embeddedFile.content,
'return __VLS_slots;\n',
'return __VLS_slots',
`return __VLS_slots as __VLS_DefineSlots<`,
() => [
// slots type
Expand All @@ -32,7 +35,7 @@ const transform = ({
typeArg!.pos,
FileRangeCapabilities.full,
],
'>;\n'
'>'
)
embeddedFile.content.push(
`type __VLS_DefineSlots<T> = { [SlotName in keyof T]: (_: T[SlotName]) => any }`
Expand Down Expand Up @@ -79,4 +82,4 @@ const plugin: VueLanguagePlugin = ({ modules: { typescript: ts } }) => {
}
}

export default plugin
export = plugin

0 comments on commit cf20f6f

Please sign in to comment.