Skip to content

Commit

Permalink
refactor!(define-models): rename defineModel to defineModels
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Apr 3, 2023
1 parent 14d7886 commit 5d65f5b
Show file tree
Hide file tree
Showing 102 changed files with 181 additions and 177 deletions.
2 changes: 1 addition & 1 deletion .changeset/brave-dots-speak.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
'unplugin-vue-define-options': patch
'@vue-macros/named-template': patch
'@vue-macros/single-define': patch
'@vue-macros/define-model': patch
'@vue-macros/define-models': patch
---

simplify import helper function
8 changes: 8 additions & 0 deletions .changeset/metal-seahorses-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'unplugin-vue-macros': major
'@vue-macros/common': patch
'@vue-macros/volar': patch
'@vue-macros/nuxt': patch
---

rename defineModel to defineModels
4 changes: 2 additions & 2 deletions docs/.vitepress/locales/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ export const sidebar = (lang: string): DefaultTheme.SidebarItem[] => {
link: `${urlPrefix}/macros/define-slots`,
},
{
text: 'defineModel',
link: `${urlPrefix}/macros/define-model`,
text: 'defineModels',
link: `${urlPrefix}/macros/define-models`,
},
{
text: 'shortEmits',
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/bundler-integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ npm i -D @vue-macros/volar
"vueCompilerOptions": {
"plugins": [
"@vue-macros/volar/define-options",
"@vue-macros/volar/define-model",
"@vue-macros/volar/define-models",
"@vue-macros/volar/define-props",
"@vue-macros/volar/define-props-refs",
"@vue-macros/volar/short-vmodel",
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ VueMacros({
},

/** Defaults to true */
defineModel: {
defineModels: {
/**
* Unified mode, only works for Vue 2
*
Expand Down
20 changes: 9 additions & 11 deletions docs/macros/define-model.md → docs/macros/define-models.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# defineModel
# defineModels

<StabilityLevel level="stable" />

Declaring and mutate `v-model` props as the same as normal variable using the `defineModel`.

For Vue >= 3.3, this feature will be turned off by default.
Declaring and mutate `v-model` props as the same as normal variable using the `defineModels`.

| Features | Supported |
| :----------: | :----------------: |
Expand All @@ -17,7 +15,7 @@ For Vue >= 3.3, this feature will be turned off by default.

```ts
VueMacros({
defineModel: {
defineModels: {
/**
* Unified mode, only works for Vue 2
*
Expand All @@ -34,7 +32,7 @@ Requires [`@vueuse/core`](https://www.npmjs.com/package/@vueuse/core), install i

```vue
<script setup lang="ts">
const { modelValue, count } = defineModel<{
const { modelValue, count } = defineModels<{
modelValue: string
count: number
}>()
Expand All @@ -48,7 +46,7 @@ modelValue.value = 'newValue'

```vue
<script setup lang="ts">
const { modelValue } = defineModel({
const { modelValue } = defineModels({
modelValue: String,
})
</script>
Expand All @@ -60,7 +58,7 @@ const { modelValue } = defineModel({

```vue 3-6
<script setup lang="ts">
const { modelValue } = defineModel<{
const { modelValue } = defineModels<{
modelValue: ModelOptions<
string,
{ defaultValue: 'something'; deep: true; passive: true }
Expand All @@ -81,7 +79,7 @@ Assignment expression is only supported in `<script setup>` block. In other word

```vue {7-9}
<script setup lang="ts">
let { modelValue, count } = $defineModel<{
let { modelValue, count } = $defineModels<{
modelValue: string
count: number
}>()
Expand Down Expand Up @@ -122,10 +120,10 @@ emit('update:count', count + 1)
"vueCompilerOptions": {
"target": 3, // or 2.7 for Vue 2
"plugins": [
"@vue-macros/volar/define-model"
"@vue-macros/volar/define-models"
// ...more feature
],
"defineModel": {
"defineModels": {
// Only works when target is 2.7.
"unified": true
}
Expand Down
2 changes: 1 addition & 1 deletion docs/macros/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Please make sure `unplugin-vue-macros` is set up correctly. If you haven't yet,
- [defineOptions](/macros/define-options)
- [defineSlots](/macros/define-slots)
- [WIP](https://github.com/vuejs/core/pull/7982)
- [defineModel](/macros/define-model) (:warning: inconsistent with official version)
- [defineModels](/macros/define-models) (:warning: inconsistent with official version)
- WIP
- [shortEmits](/macros/short-emits)

Expand Down
2 changes: 1 addition & 1 deletion docs/zh-CN/guide/bundler-integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ npm i -D @vue-macros/volar
"vueCompilerOptions": {
"plugins": [
"@vue-macros/volar/define-options",
"@vue-macros/volar/define-model",
"@vue-macros/volar/define-models",
"@vue-macros/volar/define-props",
"@vue-macros/volar/define-props-refs",
"@vue-macros/volar/short-vmodel",
Expand Down
2 changes: 1 addition & 1 deletion docs/zh-CN/guide/configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ VueMacros({
},

/** 默认是 true */
defineModel: {
defineModels: {
/**
* unified 模式,仅在 Vue 2 有效
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# defineModel
# defineModels

<StabilityLevel level="stable" />

使用 `defineModel` 可以简化声明和修改 `v-model` 值的步骤,就像是在使用一个普通变量一样。

在 Vue >= 3.3 中,此功能将默认关闭。
使用 `defineModels` 可以简化声明和修改 `v-model` 值的步骤,就像是在使用一个普通变量一样。

| 特性 | 支持 |
| :----------: | :----------------: |
Expand All @@ -17,7 +15,7 @@

```ts
VueMacros({
defineModel: {
defineModels: {
/**
* Unified 模式,仅在 Vue 2 下有效
*
Expand All @@ -34,7 +32,7 @@ VueMacros({

```vue
<script setup lang="ts">
const { modelValue, count } = defineModel<{
const { modelValue, count } = defineModels<{
modelValue: string
count: number
}>()
Expand All @@ -48,7 +46,7 @@ modelValue.value = 'newValue'

```vue
<script setup lang="ts">
const { modelValue } = defineModel({
const { modelValue } = defineModels({
modelValue: String,
})
</script>
Expand All @@ -60,7 +58,7 @@ const { modelValue } = defineModel({

```vue 3-6
<script setup lang="ts">
const { modelValue } = defineModel<{
const { modelValue } = defineModels<{
modelValue: ModelOptions<
string,
{ defaultValue: 'something'; deep: true; passive: true }
Expand All @@ -81,7 +79,7 @@ const { modelValue } = defineModel<{

```vue {7-9}
<script setup lang="ts">
let { modelValue, count } = $defineModel<{
let { modelValue, count } = $defineModels<{
modelValue: string
count: number
}>()
Expand Down Expand Up @@ -122,10 +120,10 @@ emit('update:count', count + 1)
"vueCompilerOptions": {
"target": 3, // 或 2.7 用于 Vue 2
"plugins": [
"@vue-macros/volar/define-model"
"@vue-macros/volar/define-models"
// ...更多功能
],
"defineModel": {
"defineModels": {
// 仅在 target 是 2.7 时有效
"unified": true
}
Expand Down
2 changes: 1 addition & 1 deletion docs/zh-CN/macros/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
- [defineOptions](/zh-CN/macros/define-options)
- [defineSlots](/zh-CN/macros/define-slots)
- [WIP](https://github.com/vuejs/core/pull/7982)
- [defineModel](/zh-CN/macros/define-model) (:warning: 与官方版本不一致)
- [defineModels](/zh-CN/macros/define-models) (:warning: 与官方版本不一致)
- WIP
- [shortEmits](/zh-CN/macros/short-emits)

Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ export const DEFINE_PROPS_REFS = 'definePropsRefs'
export const DEFINE_EMITS = 'defineEmits'
export const WITH_DEFAULTS = 'withDefaults'
export const DEFINE_OPTIONS = 'defineOptions'
export const DEFINE_MODEL = 'defineModel'
export const DEFINE_MODEL_DOLLAR = '$defineModel'
export const DEFINE_MODELS = 'defineModels'
export const DEFINE_MODELS_DOLLAR = '$defineModels'
export const DEFINE_SETUP_COMPONENT = 'defineSetupComponent'
export const DEFINE_RENDER = 'defineRender'
export const DEFINE_SLOTS = 'defineSlots'
Expand Down
3 changes: 0 additions & 3 deletions packages/define-model/README.md

This file was deleted.

6 changes: 0 additions & 6 deletions packages/define-model/macros-global.d.ts

This file was deleted.

File renamed without changes.
3 changes: 3 additions & 0 deletions packages/define-models/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @vue-macros/define-models [![npm](https://img.shields.io/npm/v/@vue-macros/define-models.svg)](https://npmjs.com/package/@vue-macros/define-models)

Please refer to [README.md](https://github.com/sxzz/unplugin-vue-macros#readme)
6 changes: 6 additions & 0 deletions packages/define-models/macros-global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
declare const defineModels: typeof import('./macros').defineModels
declare const $defineModels: typeof import('./macros').$defineModels
declare type ModelOptions<
T,
O extends import('./macros').UseModelOptions<T> = {}
> = import('./macros').ModelOptions<T, O>
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export type UseModelOptions<T> = Omit<UseVModelOptions<T>, 'passive'> & {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export type ModelOptions<T, O extends UseModelOptions<T> = {}> = T

export const defineModel: <T>() => {
export const defineModels: <T>() => {
[K in keyof T]-?: WritableComputedRef<T[K]>
}

export const $defineModel: <T>() => T
export const $defineModels: <T>() => T
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"name": "@vue-macros/define-model",
"version": "1.4.3",
"name": "@vue-macros/define-models",
"version": "1.0.0",
"packageManager": "pnpm@7.30.3",
"description": "define-model feature from Vue Macros.",
"description": "define-models feature from Vue Macros.",
"keywords": [
"vue-macros",
"macros",
"vue",
"sfc",
"setup",
"script-setup",
"define-model",
"define-models",
"unplugin"
],
"license": "MIT",
Expand All @@ -21,7 +21,7 @@
"repository": {
"type": "git",
"url": "git+https://github.com/sxzz/unplugin-vue-macros.git",
"directory": "packages/define-model"
"directory": "packages/define-models"
},
"author": "三咲智子 <sxzz@sxzz.moe>",
"files": [
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const helperPrefix = '/vue-macros/define-model'
export const helperPrefix = '/vue-macros/define-models'

export const emitHelperId = `${helperPrefix}/emit-helper`
export { default as emitHelperCode } from './emit-helper?raw'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { extractIdentifiers, walkAST } from 'ast-walker-scope'
import {
DEFINE_EMITS,
DEFINE_MODEL,
DEFINE_MODEL_DOLLAR,
DEFINE_MODELS,
DEFINE_MODELS_DOLLAR,
DEFINE_OPTIONS,
DEFINE_PROPS,
HELPER_PREFIX,
Expand Down Expand Up @@ -82,7 +82,7 @@ export function transformDefineModel(
const typeDeclRaw = node.typeParameters?.params?.[0]
if (!typeDeclRaw)
throw new SyntaxError(
`${fnName}() expected a type parameter when used with ${DEFINE_MODEL}.`
`${fnName}() expected a type parameter when used with ${DEFINE_MODELS}.`
)

const typeDecl = resolveQualifiedType(
Expand All @@ -107,7 +107,7 @@ export function transformDefineModel(
emitsIdentifier = declId.name
}
} else if (type === 'emits') {
emitsIdentifier = `_${DEFINE_MODEL}_emit`
emitsIdentifier = `_${DEFINE_MODELS}_emit`
s.prependRight(setupOffset + node.start!, `const ${emitsIdentifier} = `)
}

Expand All @@ -119,19 +119,19 @@ export function transformDefineModel(
declId?: LVal,
kind?: VariableDeclaration['kind']
) {
if (isCallOf(node, DEFINE_MODEL)) mode = 'runtime'
else if (isCallOf(node, DEFINE_MODEL_DOLLAR)) mode = 'reactivity-transform'
if (isCallOf(node, DEFINE_MODELS)) mode = 'runtime'
else if (isCallOf(node, DEFINE_MODELS_DOLLAR)) mode = 'reactivity-transform'
else return false

if (hasDefineModel) {
throw new SyntaxError(`duplicate ${DEFINE_MODEL}() call`)
throw new SyntaxError(`duplicate ${DEFINE_MODELS}() call`)
}
hasDefineModel = true
modelDecl = node

const propsTypeDeclRaw = node.typeParameters?.params[0]
if (!propsTypeDeclRaw) {
throw new SyntaxError(`expected a type parameter for ${DEFINE_MODEL}.`)
throw new SyntaxError(`expected a type parameter for ${DEFINE_MODELS}.`)
}
modelTypeDecl = resolveQualifiedType(
propsTypeDeclRaw,
Expand All @@ -140,7 +140,7 @@ export function transformDefineModel(

if (!modelTypeDecl) {
throw new SyntaxError(
`type argument passed to ${DEFINE_MODEL}() must be a literal type, ` +
`type argument passed to ${DEFINE_MODELS}() must be a literal type, ` +
`or a reference to an interface or literal type.`
)
}
Expand Down Expand Up @@ -494,7 +494,7 @@ export function transformDefineModel(
})
}

if (!code.includes(DEFINE_MODEL)) return
if (!code.includes(DEFINE_MODELS)) return
const { script, scriptSetup, getSetupAst, getScriptAst } = parseSFC(code, id)
if (!scriptSetup) return

Expand Down Expand Up @@ -558,12 +558,12 @@ export function transformDefineModel(

if (runtimeDefineFn)
throw new SyntaxError(
`${runtimeDefineFn}() cannot accept non-type arguments when used with ${DEFINE_MODEL}()`
`${runtimeDefineFn}() cannot accept non-type arguments when used with ${DEFINE_MODELS}()`
)

if (modelTypeDecl.type !== 'TSTypeLiteral') {
throw new SyntaxError(
`type argument passed to ${DEFINE_MODEL}() must be a literal type, or a reference to an interface or literal type.`
`type argument passed to ${DEFINE_MODELS}() must be a literal type, or a reference to an interface or literal type.`
)
}

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function resolveOption(
}
}

const name = 'unplugin-vue-define-model'
const name = 'unplugin-vue-define-models'

export default createUnplugin<Options | undefined, false>(
(userOptions = {}, { framework }) => {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 5d65f5b

Please sign in to comment.