mh-two-thousand-and-two
2024-04-12 3d2ec2fd0578d3ba0a414b0cc4e4a2ae60878596
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const t = require('@babel/types')
 
const {
  getModelEventFunctionExpr
} = require('./util')
 
module.exports = function processDir (paths, path, state) {
  const directivesPath = paths.directives
  if (directivesPath) {
    /**
         * directives: [{
         *       name: "model",
         *       rawName: "v-model",
         *      value: (aaa.cart_amount),
         *     expression: "aaa.cart_amount"
         *}],
         */
    const modelObjectExpr = directivesPath.node.value.elements.find(
      objectExpression => {
        return objectExpression.properties.find(property => {
          return property.key.name === 'name' && property.value.value === 'model'
        })
      }
    )
    if (modelObjectExpr) {
      const exprProperty = modelObjectExpr.properties.find(property => {
        return property.key.name === 'expression'
      })
      const modifiersProperty = modelObjectExpr.properties.find(property => {
        return property.key.name === 'modifiers'
      })
      if (exprProperty) {
        const onPath = paths.on
 
        const existingInput = onPath.node.value.properties.find(
          property => property.key.value === 'input'
        )
        if (existingInput) {
          let existingInputFuncExpr
          // remove old model input event
          if (!t.isArrayExpression(existingInput.value)) {
            existingInputFuncExpr = existingInput.value
            existingInput.value = t.arrayExpression([])
          } else {
            existingInputFuncExpr = existingInput.value.elements.shift()
          }
          if (existingInputFuncExpr) {
            const modifiers = []
            if (modifiersProperty) {
              const properties = modifiersProperty.value.properties
              if (properties.find(property => property.key.value === 'number')) {
                modifiers.push(t.stringLiteral('number'))
              }
              if (properties.find(property => property.key.value === 'trim')) {
                modifiers.push(t.stringLiteral('trim'))
              }
            }
            existingInput.value.elements.unshift(
              getModelEventFunctionExpr(
                existingInputFuncExpr,
                exprProperty.value.value.trim(),
                modifiers
              )
            )
          }
        }
      }
    }
  }
  return []
}