Skip to content

Commit bf0a2a1

Browse files
committed
feat: support var or fun rename
1 parent 6ebe6a2 commit bf0a2a1

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

src/analyze/setupScript.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ export function processSetup(ast: t.Node, parentScope?: Scope, parentPath?: t.No
293293
}
294294
});
295295
}
296-
if(path.node.id.type === 'ObjectPattern'){
296+
else if(path.node.id.type === 'ObjectPattern'){
297297
path.node.id.properties.forEach((property) => {
298298
if(property.type === 'ObjectProperty' && property.value.type === 'Identifier') {
299299
const name = property.value.name;
@@ -336,7 +336,7 @@ export function processSetup(ast: t.Node, parentScope?: Scope, parentPath?: t.No
336336
}
337337
});
338338
}
339-
if([
339+
else if([
340340
'CallExpression',
341341
'ArrowFunctionExpression',
342342
'FunctionDeclaration',
@@ -382,6 +382,38 @@ export function processSetup(ast: t.Node, parentScope?: Scope, parentPath?: t.No
382382
}, path.scope, path);
383383
}
384384
}
385+
else if(path.node.id.type === 'Identifier') {
386+
const name = path.node.id.name;
387+
if(path.node.init.type === 'Identifier') {
388+
const binding = path.scope.getBinding(path.node.init.name);
389+
if(
390+
graph.nodes.has(path.node.init.name)
391+
&& (binding?.scope.block.type === 'Program'
392+
|| (parentScope === binding?.scope)
393+
)
394+
) {
395+
graph.edges.get(name)?.add(path.node.init.name);
396+
}
397+
} else {
398+
traverse(path.node.init, {
399+
Identifier(path1) {
400+
const binding = path1.scope.getBinding(path1.node.name);
401+
if(
402+
graph.nodes.has(path1.node.name)
403+
&& (
404+
path1.parent.type !== 'MemberExpression'
405+
|| path1.parent.object === path1.node
406+
)
407+
&& (binding?.scope.block.type === 'Program'
408+
|| (parentScope === binding?.scope)
409+
)
410+
) {
411+
graph.edges.get(name)?.add(path1.node.name);
412+
}
413+
},
414+
}, path.scope, path);
415+
}
416+
}
385417
}
386418
},
387419

test/options-setup/TestComponent.graph.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,19 @@ const number: TypedNode = {label: 'number', type: NodeType.var, info: {line: 65,
66
const count: TypedNode = {label: 'count', type: NodeType.var, info: {line: 67, column: 10}};
77
const plus: TypedNode = {label: 'plus', type: NodeType.fun, info: {line: 70, column: 6}};
88
const add: TypedNode = {label: 'add', type: NodeType.fun, info: {line: 74, column: 6}};
9-
const a: TypedNode = {label: 'a', type: NodeType.var, info: {line: 83, column: 6}};
9+
const a: TypedNode = {label: 'a', type: NodeType.var, info: {line: 81, column: 10}};
10+
const b: TypedNode = {label: 'b', type: NodeType.var, info: {line: 82, column: 10}};
11+
const c: TypedNode = {label: 'c', type: NodeType.var, info: {line: 87, column: 6}};
1012

1113
edges.set(number, new Set([]));
14+
edges.set(b, new Set([count]));
15+
edges.set(c, new Set([count]));
1216
edges.set(count, new Set([]));
1317
edges.set(plus, new Set([plus]));
1418
edges.set(add, new Set([number, add, count]));
15-
edges.set(a, new Set([count]));
19+
// edges.set(a, new Set([count]));
1620

1721
export const graph = {
18-
nodes: new Set([number, count, plus, add, a]),
22+
nodes: new Set([number, b, count, c, plus, add]),
1923
edges,
2024
};

test/options-setup/TestComponent.vue

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,15 @@ export default {
7777
methods.add();
7878
console.log(count)
7979
}
80-
}
80+
};
81+
82+
const a = {count};
83+
const b = count;
8184
8285
return {
8386
...toRefs(data),
84-
a: count,
87+
b,
88+
c: count,
8589
...methods,
8690
}
8791
}

0 commit comments

Comments
 (0)