-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (55 loc) · 1.83 KB
/
index.js
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
function findRoot(t, callExpression) {
const { callee, args } = callExpression
// only act on callee `Expression`s that are `MemberExpression`s
if (!t.isMemberExpression(callee)) return
const { object, property, computed } = callee
// ignore already transformed builder patterns
// could causes races if visitors visit concurrently
// if (property.name === 'pipe') return
// if property is computed (eg. a[b]), return
if (computed) return
// test if we are at the root
if (
(object.name === 'gulp' && property.name === 'src') ||
property.name === 'src'
) {
console.log(object.name, property.name)
return true
}
// only act on object (MemberExpression) `Expression`s that are `CallExpression`s
// i.e. object can be an `Expression`, but we are only looking for `CallExpression`s
if (!t.isCallExpression(object)) return
const nestedCallExpression = object
return findRoot(t, nestedCallExpression)
}
module.exports = function babelPluginImprovedGulpPipelines({ types: t }) {
return {
name: 'babel-plugin-improved-gulp-pipelines',
visitor: {
CallExpression(path) {
const { node } = path
const hasRoot = findRoot(t, node)
if (!hasRoot) return
let { callee, args } = node
args = args || []
// only act on callee `Expression`s that are `MemberExpression`s
if (!t.isMemberExpression(callee)) return
const { object, property, computed } = callee
const suffix = object
// path.replaceWith(
// object, t.identifier('pipe2')
// )
// ulp.rc("src'")
const gulpSrc = t.callExpression(
t.memberExpression(t.identifier('ulp'), t.identifier('rc')),
[t.stringLiteral('src')],
)
//
const piped = t.callExpression(t.identifier('pipe'), [
t.stringLiteral(property.name),
])
path.replaceWith(t.memberExpression(gulpSrc, t.identifier('pipe')))
},
},
}
}