Skip to content

Commit 5fff976

Browse files
committed
add deep option for watch
1 parent a433902 commit 5fff976

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

Diff for: generator/codemods/vue/add-watch-deep.js

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/**
2+
* @param {Object} context
3+
* @param {import('jscodeshift').JSCodeshift} context.j
4+
* @param {ReturnType<import('jscodeshift').Core>} context.root
5+
*/
6+
module.exports = function addEmitDeclaration(context) {
7+
const { j, root } = context
8+
9+
// this.$watch(...) add deep option
10+
const this$watches = root.find(j.CallExpression, {
11+
callee: {
12+
type: 'MemberExpression',
13+
object: { type: 'ThisExpression' },
14+
property: {
15+
type: 'Identifier',
16+
name: '$watch'
17+
}
18+
}
19+
})
20+
21+
for (let i = 0; i < this$watches.length; i++) {
22+
const watchFunc = this$watches.at(i)
23+
const deepProperty = watchFunc.find(j.ObjectProperty, {
24+
key: {
25+
type: 'Identifier',
26+
name: 'deep'
27+
}
28+
})
29+
if (deepProperty.length > 0) {
30+
continue
31+
}
32+
const args = watchFunc.get().node.arguments
33+
if (args.length < 2) {
34+
continue
35+
}
36+
if (args[1].type != 'ObjectExpression') {
37+
if (args.length < 3) {
38+
watchFunc.replaceWith(nodePath => {
39+
nodePath.node.arguments.push(j.objectExpression([]))
40+
return nodePath.node
41+
})
42+
}
43+
const target = watchFunc.find(j.ObjectExpression).at(0)
44+
target.replaceWith(nodePath => {
45+
nodePath.node.properties.push(
46+
j.objectProperty(j.identifier('deep'), j.booleanLiteral(true))
47+
)
48+
return nodePath.node
49+
})
50+
}
51+
}
52+
53+
// watch: {...} add deep option
54+
const watchFuncs = root
55+
.find(j.ExportDefaultDeclaration)
56+
.at(0)
57+
.find(j.ObjectExpression)
58+
.at(0)
59+
.find(j.ObjectProperty, {
60+
key: {
61+
type: 'Identifier',
62+
name: 'watch'
63+
}
64+
})
65+
.at(0)
66+
.find(j.ObjectExpression)
67+
.at(0)
68+
.find(j.ObjectProperty)
69+
70+
for (let i = 0; i < watchFuncs.length; i++) {
71+
const watchProperty = watchFuncs.at(i)
72+
if (!inExportDefaultLevel(watchProperty, 2)) {
73+
continue
74+
}
75+
const deepProperty = watchProperty.find(j.ObjectProperty, {
76+
key: {
77+
type: 'Identifier',
78+
name: 'deep'
79+
}
80+
})
81+
if (deepProperty.length > 0) {
82+
continue
83+
}
84+
85+
if (watchProperty.get().node.value.type === 'ObjectExpression') {
86+
const target = watchProperty.find(j.ObjectExpression).at(0)
87+
target.replaceWith(nodePath => {
88+
nodePath.node.properties.push(
89+
j.objectProperty(j.identifier('deep'), j.booleanLiteral(true))
90+
)
91+
return nodePath.node
92+
})
93+
} else {
94+
watchProperty.replaceWith(nodePath => {
95+
nodePath.node.value = j.objectExpression([
96+
j.objectProperty(j.identifier('handler'), nodePath.node.value),
97+
j.objectProperty(j.identifier('deep'), j.booleanLiteral(true))
98+
])
99+
return nodePath.node
100+
})
101+
}
102+
}
103+
}
104+
105+
function getExportDefaultLevel(collection) {
106+
let path = collection.get()
107+
let level = 0
108+
while (path) {
109+
if (path.node.type === 'ExportDefaultDeclaration') {
110+
return level
111+
}
112+
path = path.parentPath
113+
level++
114+
}
115+
return -1
116+
}
117+
118+
function inExportDefaultLevel(collection, level) {
119+
const lvl = getExportDefaultLevel(collection)
120+
if (level * 3 === lvl) {
121+
return true
122+
}
123+
return false
124+
}

Diff for: generator/codemods/vue/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module.exports = function(fileInfo, api) {
55
const context = { j, root }
66

77
require('./add-emit-declaration')(context)
8+
require('./add-watch-deep')(context)
89

910
return root.toSource({ lineTerminator: '\n' })
1011
}

0 commit comments

Comments
 (0)