8000 feat(define-prop): migrate defineProp from @vue/language-tools by zhiyuanzmj · Pull Request #961 · vue-macros/vue-macros · GitHub
[go: up one dir, main page]

Skip to content

feat(define-prop): migrate defineProp from @vue/language-tools #961

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8000
1 change: 0 additions & 1 deletion packages/define-prop/macros-global.d.ts

This file was deleted.

17 changes: 0 additions & 17 deletions packages/define-prop/macros-johnson.d.ts

This file was deleted.

12 changes: 0 additions & 12 deletions packages/define-prop/macros.d.ts

This file was deleted.

10 changes: 2 additions & 8 deletions packages/define-prop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,7 @@
"dev": "./src/webpack.ts",
"default": "./dist/webpack.js"
},
"./*": [
"./*",
"./*.d.ts"
]
"./*": "./*"
},
"typesVersions": {
"*": {
Expand All @@ -89,10 +86,7 @@
"./rspack": "./dist/rspack.js",
"./vite": "./dist/vite.js",
"./webpack": "./dist/webpack.js",
"./*": [
"./*",
"./*.d.ts"
]
"./*": "./*"
},
"tag": "next"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

exports[`fixtures > tests/fixtures/johnson-edition/basic.vue 1`] = `
"// basic.js
var basic = \`<script setup lang="ts">
var basic = \`<!-- @experimentalDefinePropProposal "johnsonEdition" -->

<script setup lang="ts">
const __MACROS_props = defineProps({
qux: { type: String },
foo: { type: String, default: 'foo' },
Expand All @@ -15,8 +17,7 @@ const __MACROS_props = defineProps({

import { toRef as __MACROS_toRef } from "vue";
import { expectTypeOf } from 'expect-type'
import type { ComputedRef } from 'vue'
import { defineProp } from '../../../macros-johnson'
import { type ComputedRef } from 'vue'

// defineProp()
const qux = __MACROS_toRef(__props, "qux")
Expand Down Expand Up @@ -72,7 +73,6 @@ import { toRef as __MACROS_toRef } from "vue";
import { expectTypeOf } from 'expect-type'
import type { ComputedRef } from 'vue'
import type { Qux } from '../types'
import { defineProp } from '../../../macros'

// defineProp(prop_name)
const foo = __MACROS_toRef(__props, "foo")
Expand Down
5 changes: 3 additions & 2 deletions packages/define-prop/tests/fixtures/johnson-edition/basic.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<!-- @experimentalDefinePropProposal "johnsonEdition" -->

<script setup lang="ts">
import { expectTypeOf } from 'expect-type'
import type { ComputedRef } from 'vue'
import { defineProp } from '../../../macros-johnson'
import { type ComputedRef } from 'vue'

// defineProp()
const qux = defineProp<string>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import { expectTypeOf } from 'expect-type'
import type { ComputedRef } from 'vue'
import type { Qux } from '../types'
import { defineProp } from '../../../macros'

// defineProp(prop_name)
const foo = defineProp<string>('foo')
Expand Down
1 change: 0 additions & 1 deletion packages/macros/macros-global.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/// <reference types="@vue-macros/define-prop/macros-global" />
/// <reference types="@vue-macros/define-emit/macros-global" />
/// <reference types="@vue-macros/define-models/macros-global" />
/// <reference types="unplugin-vue-define-options/macros-global" />
Expand Down
1 change: 0 additions & 1 deletion packages/macros/macros.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import type { RecordToUnion, UnionToIntersection } from '@vue-macros/common'
export * from '@vue-macros/define-emit/macros'
export * from '@vue-macros/define-models/macros'
export * from 'unplugin-vue-define-options/macros'
export * from '@vue-macros/define-prop/macros'
export * from '@vue-macros/define-props/macros'
export * from '@vue-macros/define-render/macros'
export * from '@vue-macros/define-slots/macros'
Expand Down
1 change: 0 additions & 1 deletion packages/macros/tests/fixtures/mixed-define-props.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<script setup lang="ts">
import { defineProp } from 'vue-macros/macros' with { type: 'macro' }
const { foo } = definePropsRefs<{ foo?: string }>()
const bar = defineProp('bar')
const { baz } = defineModels<{ baz: string }>()
Expand Down
37 changes: 33 additions & 4 deletions packages/volar/src/define-prop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function transformDefineProp({
codes: Code[]
defineProps: DefineProp[]
version: number
edition: 'kevinEdition' | 'johnsonEdition'
edition: string
}) {
addProps(
codes,
Expand All @@ -50,6 +50,17 @@ function transformDefineProp({

if (edition === 'kevinEdition') {
codes.push(`
type __VLS_PropOptions<T> = Exclude<import('vue').Prop<T>, import('vue').PropType<T>>;
declare function defineProp<T>(
name: string,
options:
| ({ default: T } & __VLS_PropOptions<T>)
| ({ required: true } & __VLS_PropOptions<T>)
): import('vue').ComputedRef<T>;
declare function defineProp<T>(
name?: string,
options?: __VLS_PropOptions<T>
): import('vue').ComputedRef<T | undefined>;
declare function $defineProp<T>(
name: string,
options:
Expand All @@ -73,6 +84,21 @@ type __VLS_PropOptions<T> = Omit<
>,
'required'
>;
declare function defineProp<T>(
value: T | (() => T) | undefined,
required: true,
options?: __VLS_PropOptions<T>
): import('vue').ComputedRef<T>;
declare function defineProp<T>(
value: T | (() => T),
required?: boolean,
options?: __VLS_PropOptions<T>
): import('vue').ComputedRef<T>;
declare function defineProp<T>(
value?: T | (() => T),
required?: boolean,
options?: __VLS_PropOptions<T>
): | import('vue').ComputedRef<T | undefined>;
declare function $defineProp<T>(
value: T | (() => T) | undefined,
required: true,
Expand All @@ -96,7 +122,7 @@ declare function $defineProp<T>(
function getDefineProp(
ts: typeof import('typescript'),
sfc: Sfc,
edition: 'kevinEdition' | 'johnsonEdition',
edition: string,
) {
const defineProps: DefineProp[] = []
function visitNode(
Expand Down Expand Up @@ -222,7 +248,7 @@ const plugin: VueMacrosPlugin<'defineProp'> = (ctx, options = {}) => {
const filter = createFilter(options)
const {
modules: { typescript: ts },
vueCompilerOptions: { experimentalDefinePropProposal, target },
vueCompilerOptions: { target },
} = ctx

return {
Expand All @@ -236,8 +262,11 @@ const plugin: VueMacrosPlugin<'defineProp'> = (ctx, options = {}) => {
)
return

const experimentalDefinePropProposal = sfc.content.match(
/<!-- @experimentalDefinePropProposal "(kevinEdition|johnsonEdition)" -->/,
)
const edition =
options.edition || experimentalDefinePropProposal || 'kevinEdition'
experimentalDefinePropProposal?.[1] || options.edition || 'kevinEdition'
const defineProps = getDefineProp(ts, sfc, edition)
if (defineProps.length === 0) return

Expand Down
3 changes: 1 addition & 2 deletions playground/astro/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"jsx": "preserve"
},
"vueCompilerOptions": {
"plugins": ["vue-macros/volar"],
"experimentalDefinePropProposal": "kevinEdition"
"plugins": ["vue-macros/volar"]
}
}
3 changes: 1 addition & 2 deletions playground/vue3/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
"skipLibCheck": true
},
"vueCompilerOptions": {
"plugins": ["vue-macros/volar"],
"experimentalDefinePropProposal": "kevinEdition"
"plugins": ["vue-macros/volar"]
},
"include": ["src", "*", "../../packages/shim.d.ts"]
}
Loading
Loading
0