'f'
mh-two-thousand-and-two
2024-04-12 26f2711ef9461961fb953e2b497bd314ef95e345
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
const t = require('@babel/types')
 
const babelTraverse = require('@babel/traverse').default
 
const {
  METHOD_RENDER_LIST
} = require('../constants')
 
function getDataPath (identifier, parent, scope) {
  if (
    !(t.isCallExpression(parent)) &&
        // not id of a Declaration
        !(t.isDeclaration(parent) && parent.id === identifier) &&
        // not a params of a function
        !(t.isFunction(parent) && parent.params.indexOf(identifier) > -1) &&
        // not a key of Property
        !(parent.type === 'ObjectProperty' && parent.key === identifier && !parent.computed) &&
        // not in an Array destructure pattern
        !(parent.type === 'ArrayPattern') &&
        // not in an Object destructure pattern
        !(parent.parent && parent.parent.type === 'ObjectPattern') &&
        // not already in scope
        !scope.hasBinding(identifier.name)
  ) {
    return identifier.name
  }
}
 
function getDataPathByMemberExpression (node, ret) {
  if (t.isMemberExpression(node.object)) {
    getDataPathByMemberExpression(node.object, ret)
  } else if (t.isIdentifier(node.object)) {
    ret.push(node.object.name)
  }
  ret.push(node.property.name || node.property.value)
}
 
const visitor = {
  Identifier (path) {
    const dataPath = getDataPath(path.node, path.parent, path.scope)
    if (dataPath) {
      console.log('....identifier', dataPath)
    } else {
      // console.log('....ignore', path.node.name)
    }
  },
  MemberExpression (path) {
    const dataPathArray = []
    getDataPathByMemberExpression(path.node, dataPathArray)
    path.skip()
  },
  CallExpression (path) {
    const callee = path.node.callee
    if (callee.name === METHOD_RENDER_LIST) {
      // for
      // path.skip()
    }
  }
}
 
module.exports = function traverse (ast, state) {
  const data = Object.create(null)
  babelTraverse(ast, visitor, undefined, {
    data
  })
}