MCPcopy
hub / github.com/vuejs/core / processDefineModel

Function processDefineModel

packages/compiler-sfc/src/script/defineModel.ts:16–119  ·  view source on GitHub ↗
(
  ctx: ScriptCompileContext,
  node: Node,
  declId?: LVal,
)

Source from the content-addressed store, hash-verified

14}
15
16export function processDefineModel(
17 ctx: ScriptCompileContext,
18 node: Node,
19 declId?: LVal,
20): boolean {
21 if (!isCallOf(node, DEFINE_MODEL)) {
22 return false
23 }
24
25 ctx.hasDefineModelCall = true
26
27 const type =
28 (node.typeParameters && node.typeParameters.params[0]) || undefined
29 let modelName: string
30 let options: Node | undefined
31 const arg0 = node.arguments[0] && unwrapTSNode(node.arguments[0])
32 const hasName =
33 arg0 &&
34 (arg0.type === 'StringLiteral' ||
35 (arg0.type === 'TemplateLiteral' && arg0.expressions.length === 0))
36 if (hasName) {
37 modelName =
38 arg0.type === 'StringLiteral' ? arg0.value : arg0.quasis[0].value.cooked!
39 options = node.arguments[1]
40 } else {
41 modelName = 'modelValue'
42 options = arg0
43 }
44
45 if (ctx.modelDecls[modelName]) {
46 ctx.error(`duplicate model name ${JSON.stringify(modelName)}`, node)
47 }
48
49 let optionsString = options && ctx.getString(options)
50 let optionsRemoved = !options
51 const runtimeOptionNodes: Node[] = []
52
53 if (
54 options &&
55 options.type === 'ObjectExpression' &&
56 !options.properties.some(p => p.type === 'SpreadElement' || p.computed)
57 ) {
58 let removed = 0
59 for (let i = options.properties.length - 1; i >= 0; i--) {
60 const p = options.properties[i]
61 const next = options.properties[i + 1]
62 const start = p.start!
63 const end = next ? next.start! : options.end! - 1
64 if (
65 (p.type === 'ObjectProperty' || p.type === 'ObjectMethod') &&
66 ((p.key.type === 'Identifier' &&
67 (p.key.name === 'get' || p.key.name === 'set')) ||
68 (p.key.type === 'StringLiteral' &&
69 (p.key.value === 'get' || p.key.value === 'set')))
70 ) {
71 // remove runtime-only options from prop options to avoid duplicates
72 optionsString =
73 optionsString.slice(0, start - options.start!) +

Callers 1

compileScriptFunction · 0.90

Calls 8

isCallOfFunction · 0.90
unwrapTSNodeFunction · 0.85
errorMethod · 0.80
getStringMethod · 0.80
someMethod · 0.80
removeMethod · 0.80
pushMethod · 0.65
helperMethod · 0.65

Tested by

no test coverage detected