'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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const t = require('@babel/types')
 
const {
  VAR_FILTER
} = require('../../constants')
 
const GLOBAL_METHODS = [
  'parseInt',
  'parseFloat',
  'isNaN',
  'isFinite',
  'decodeURI',
  'decodeURIComponent',
  'encodeURI',
  'encodeURIComponent'
]
 
const GLOBAL_OBJECTS = {
  Math: [
    'abs',
    'acos',
    'asin',
    'atan',
    'atan2',
    'ceil',
    'cos',
    'exp',
    'floor',
    'log',
    'max',
    'min',
    'pow',
    'random',
    'round',
    'sin',
    'sqrt',
    'tan'
  ],
  JSON: [
    'stringify',
    'parse'
  ]
}
 
const BUILT_IN_METHODS = [
  // number
  'toString',
  'toLocaleString',
  'valueOf',
  'toFixed',
  'toExponential',
  'toPrecision',
  // string
  // 'toString',
  // 'valueOf',
  'charAt',
  'charCodeAt',
  'concat',
  'indexOf',
  'lastIndexOf',
  'localeCompare',
  'match',
  'replace',
  'search',
  'slice',
  'split',
  'substring',
  'toLowerCase',
  'toLocaleLowerCase',
  'toUpperCase',
  'toLocaleUpperCase',
  'trim',
  // boolean
  // 'toString',
  // 'valueOf',
  // object
  // 'toString',
  // function
  // 'toString',
  // array
  // 'toString',
  // 'concat',
  'join',
  'pop',
  'push',
  'reverse',
  'shift',
  // 'slice',
  'sort',
  'splice',
  'unshift',
  // 'indexOf',
  // 'lastIndexOf',
  'every',
  'some',
  'forEach',
  'map',
  'filter',
  'reduce',
  'reduceRight'
]
 
function getGlobalMethodFilter (callExpr) {
  const callee = callExpr.callee
  if (callee) {
    const name = callee.name
    if (name && GLOBAL_METHODS.includes(name)) {
      return t.callExpression(
        t.memberExpression(
          t.identifier(VAR_FILTER),
          t.identifier(name)
        ),
        callExpr.arguments
      )
    }
  }
  return false
}
 
function getGlobalObjectFilter (callExpr) {
  const callee = callExpr.callee
  if (t.isMemberExpression(callee)) {
    const object = callee.object
    const property = callee.property
    const propertyName = property.name || property.value
    const methods = GLOBAL_OBJECTS[object.name]
    if (methods && methods.includes(propertyName)) {
      return t.callExpression(
        t.memberExpression(
          t.identifier(VAR_FILTER),
          t.identifier(propertyName)
        ),
        callExpr.arguments
      )
    }
  }
  return false
}
 
function getMemberFilter (callExpr) {
  const callee = callExpr.callee
  if (t.isMemberExpression(callee)) {
    const property = callee.property
    const propertyName = property.name || property.value
    if (BUILT_IN_METHODS.includes(propertyName)) {
      return t.callExpression(
        t.memberExpression(
          t.identifier(VAR_FILTER),
          t.identifier(propertyName)
        ),
        [
          callee.object,
          ...callExpr.arguments
        ]
      )
    }
  }
}
 
function processFilter (callExpr, path) {
  const globalMethodFilter = getGlobalMethodFilter(callExpr)
  if (globalMethodFilter) {
    path.replaceWith(globalMethodFilter)
    return true
  }
  const globalObjectFilter = getGlobalObjectFilter(callExpr)
  if (globalObjectFilter) {
    path.replaceWith(globalObjectFilter)
    return true
  }
  const memberFilter = getMemberFilter(callExpr)
  if (memberFilter) {
    path.replaceWith(memberFilter)
    return true
  }
  return false
}
 
module.exports = processFilter