-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
144 lines (143 loc) · 6.54 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
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
const parser = require('@babel/parser');
const traverse = require('@babel/traverse').default;
const generate = require('@babel/generator').default;
const types = require('@babel/types');
const os = require('os');
module.exports = function openVscode() {
return {
name: 'open-vscode',
transform(originCode, originMap, ast) {
if (
originMap.endsWith('.vue')
) {
if (!originCode.includes('_withModifiers')) {
originCode = originCode.replace(
'openBlock as _openBlock',
`withModifiers as _withModifiers, openBlock as _openBlock`,
);
}
// 下面是通过AST对应的组件,然后给组件添加点击事件
const ast = parser.parse(originCode, {
sourceType: 'unambiguous',
});
traverse(ast, {
FunctionDeclaration(path, state) {
// 判断是render函数,对render函数进行处理
if (
path.node.id.name === '_sfc_render' &&
path.node.body.body &&
path.node.body.body.length
) {
path.node.body.body.forEach((item) => {
if (item.type === 'ReturnStatement') {
if (item.argument.expressions && item.argument.expressions.length) {
item.argument.expressions.forEach((expressionsItem) => {
if (expressionsItem.arguments && expressionsItem.arguments[1]) {
if (expressionsItem.arguments[1].type === 'ObjectExpression') {
// 判断是否存在click事件的数组,要是存在,则添加数组,否则,添加click事件
let hasClick = false,
hasIndex = 0,
hasArray = false;
expressionsItem.arguments[1].properties.forEach(
(propertiesItem, index) => {
if (propertiesItem.key.name === 'onClick') {
hasClick = true;
hasIndex = index;
// 判断是否是array(多个click事件)
if (propertiesItem.value.type === 'ArrayExpression') {
hasArray = true;
}
}
},
);
let addNode = types.logicalExpression(
'||',
types.memberExpression(
types.identifier('_cache'),
types.numericLiteral(109),
true,
),
types.assignmentExpression(
'=',
types.memberExpression(
types.identifier('_cache'),
types.numericLiteral(109),
true,
),
types.callExpression(types.identifier('_withModifiers'), [
types.arrowFunctionExpression(
[types.identifier('$event')],
types.callExpression(
types.memberExpression(
types.memberExpression(
types.identifier('_ctx'),
types.identifier('$openVscode'),
),
types.identifier('call'),
),
[
types.thisExpression(),
types.stringLiteral(`vscode://file${os.type() == 'Windows_NT'? '/': ''}${originMap}`),
],
),
),
types.arrayExpression([
types.stringLiteral('shift'),
types.stringLiteral('alt'),
]),
]),
),
);
if (hasClick) {
try {
if (hasArray) {
// 要已经是数组的情况下,才能添加
expressionsItem.arguments[1].properties[hasIndex].value =
types.arrayExpression(
[
...expressionsItem.arguments[1].properties[hasIndex].value
.elements,
],
addNode,
);
} else {
// 要不是数组的情况下,添加的为对应的value(也就是单个click值)
expressionsItem.arguments[1].properties[hasIndex] =
types.objectProperty(
types.identifier(`onClick`),
types.arrayExpression(
[expressionsItem.arguments[1].properties[hasIndex].value,
addNode,]
),
);
}
} catch (error) {
console.warn(error);
}
} else {
// 要是没有click事件的情况下,添加click事件
expressionsItem.arguments[1].properties.push(
types.objectProperty(
types.identifier(`onClick`),
addNode,
),
);
}
}
}
});
}
}
});
}
},
});
const { code, map } = generate(ast);
if (code.includes('vscode://file')) {
return code;
}
}
return originCode;
},
};
}