'a'
mh-two-thousand-and-two
2024-04-12 44d2c92345cd156a59fc327b3060292a282d2893
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const t = require('@babel/types')
const traverse = require('@babel/traverse').default
 
function cached (fn) {
  const cache = Object.create(null)
  return function cachedFn (str) {
    const hit = cache[str]
    return hit || (cache[str] = fn(str))
  }
}
 
const capitalize = cached(str => {
  return str.charAt(0).toUpperCase() + str.slice(1)
})
 
const EVENTS = {
  click: 'tap',
  touchstart: 'touchStart',
  touchmove: 'touchMove',
  touchend: 'touchEnd',
  touchcancel: 'touchCancel',
  longtap: 'longTap',
  longpress: 'longTap',
  transitionend: 'transitionEnd',
  animationstart: 'animationStart',
  animationiteration: 'animationIteration',
  animationend: 'animationEnd',
  firstappear: 'firstAppear',
  // map
  markertap: 'markerTap',
  callouttap: 'calloutTap',
  controltap: 'controlTap',
  regionchange: 'regionChange',
  paneltap: 'panelTap',
  // scroll-view
  scrolltoupper: 'scrollToUpper',
  scrolltolower: 'scrollToLower',
  // movable-view
  changeend: 'changeEnd',
  // video
  timeupdate: 'timeUpdate',
  waiting: 'loading',
  fullscreenchange: 'fullScreenChange',
  useraction: 'userAction',
  renderstart: 'renderStart',
  loadedmetadata: 'renderStart',
  // swiper
  animationfinish: 'animationEnd'
}
 
module.exports = {
  directive: 'a:',
  specialEvents: {
    form: {
      reset: 'onReset'
    },
    map: {
      markertap: 'onMarkerTap',
      controltap: 'onControlTap',
      callouttap: 'onCalloutTap',
      regionchange: 'onRegionChange'
    }
  },
  createFilterTag (filterTag, {
    attrs
  }) {
    return `<import-sjs name="${attrs.module}" from="${attrs.src}"></import-sjs>`
  },
  getEventType (eventType) {
    return EVENTS[eventType] || eventType
  },
  formatEventType: function (eventName, isCatch) {
    return `${isCatch ? 'catch' : 'on'}${capitalize(eventName)}`
  },
  createScopedSlots (slotName, props, state) {
    const node = {
      type: 'slot',
      attr: {
        name: slotName
      },
      children: []
    }
    Object.keys(props).forEach(name => {
      node.attr[name] = props[name]
    })
    return node
  },
  resolveScopedSlots (slotName, {
    paramExprNode,
    returnExprNodes,
    traverseExpr,
    normalizeChildren
  }, state) {
    const node = {
      type: 'block',
      attr: {
        slot: slotName
      }
    }
    if (paramExprNode) {
      if (t.isIdentifier(paramExprNode)) {
        const scoped = paramExprNode.name
        node.attr['slot-scope'] = scoped
      } else if (t.isObjectPattern(paramExprNode)) {
        const paramName = '__SCOPED__'
        node.attr['slot-scope'] = paramName
        const start = returnExprNodes.start
        const end = returnExprNodes.end
        const names = []
        paramExprNode.properties.forEach(property => {
          const key = property.key
          const value = property.value
          if (t.isIdentifier(value)) {
            if (value.name !== key.name) {
              state.errors.add(`解构插槽 Prop 时,不支持将${key.name}重命名为${value.name},重命名后会影响性能`)
              return
            }
          } else if (t.isAssignmentPattern(value)) {
            state.errors.add(`解构插槽 Prop 时,不支持为${key.name}设置默认值`)
            return
          }
          names.push(key.name)
        })
        traverse({
          type: 'Program',
          start,
          end,
          body: [{
            type: 'ExpressionStatement',
            start,
            end,
            expression: returnExprNodes
          }],
          sourceType: 'module'
        }, {
          Identifier (path) {
            const node = path.node
            const name = node.name
            if (names.includes(name) && path.key !== 'key' && (path.key !== 'property' || path.parent.computed) && !(path.scope && path.scope.hasBinding(name))) {
              path.replaceWithSourceString(`${paramName}.${name}`)
            }
          }
        })
      }
    }
    node.children = normalizeChildren(traverseExpr(returnExprNodes, state))
    return node
  }
}