(ctx: ScriptCompileContext)
| 119 | } |
| 120 | |
| 121 | export function genModelProps(ctx: ScriptCompileContext): string | undefined { |
| 122 | if (!ctx.hasDefineModelCall) return |
| 123 | |
| 124 | const isProd = !!ctx.options.isProd |
| 125 | let modelPropsDecl = '' |
| 126 | for (const [name, { type, options: runtimeOptions }] of Object.entries( |
| 127 | ctx.modelDecls, |
| 128 | )) { |
| 129 | let skipCheck = false |
| 130 | let codegenOptions = `` |
| 131 | let runtimeTypes = type && inferRuntimeType(ctx, type) |
| 132 | if (runtimeTypes) { |
| 133 | const hasBoolean = runtimeTypes.includes('Boolean') |
| 134 | const hasFunction = runtimeTypes.includes('Function') |
| 135 | const hasUnknownType = runtimeTypes.includes(UNKNOWN_TYPE) |
| 136 | |
| 137 | if (hasUnknownType) { |
| 138 | if (hasBoolean || hasFunction) { |
| 139 | runtimeTypes = runtimeTypes.filter(t => t !== UNKNOWN_TYPE) |
| 140 | skipCheck = true |
| 141 | } else { |
| 142 | runtimeTypes = ['null'] |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | if (!isProd) { |
| 147 | codegenOptions = |
| 148 | `type: ${toRuntimeTypeString(runtimeTypes)}` + |
| 149 | (skipCheck ? ', skipCheck: true' : '') |
| 150 | } else if (hasBoolean || (runtimeOptions && hasFunction)) { |
| 151 | // preserve types if contains boolean, or |
| 152 | // function w/ runtime options that may contain default |
| 153 | codegenOptions = `type: ${toRuntimeTypeString(runtimeTypes)}` |
| 154 | } else { |
| 155 | // able to drop types in production |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | let decl: string |
| 160 | if (codegenOptions && runtimeOptions) { |
| 161 | decl = ctx.isTS |
| 162 | ? `{ ${codegenOptions}, ...${runtimeOptions} }` |
| 163 | : `Object.assign({ ${codegenOptions} }, ${runtimeOptions})` |
| 164 | } else if (codegenOptions) { |
| 165 | decl = `{ ${codegenOptions} }` |
| 166 | } else if (runtimeOptions) { |
| 167 | decl = runtimeOptions |
| 168 | } else { |
| 169 | decl = `{}` |
| 170 | } |
| 171 | modelPropsDecl += `\n ${JSON.stringify(name)}: ${decl},` |
| 172 | |
| 173 | // also generate modifiers prop |
| 174 | const modifierPropName = JSON.stringify( |
| 175 | name === 'modelValue' ? `modelModifiers` : `${name}Modifiers`, |
| 176 | ) |
| 177 | modelPropsDecl += `\n ${modifierPropName}: {},` |
| 178 | } |
no test coverage detected