Skip to content

Commit

Permalink
feat: support for methods recursive call
Browse files Browse the repository at this point in the history
  • Loading branch information
zcf0508 committed Sep 13, 2023
1 parent 0ec27fd commit f7a7879
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/analyze/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ export function analyze(
const methodsNode = path1.node;
if(methodsNode.value.type === 'ObjectExpression') {
methodsNode.value.properties.forEach(prop => {
if(prop.type === 'ObjectMethod' && prop.key.type === 'Identifier') {
if((prop.type === 'ObjectMethod' || prop.type === 'ObjectProperty') && prop.key.type === 'Identifier') {
const name = prop.key.name;
traverse(prop, {
MemberExpression(path2) {
Expand Down
48 changes: 33 additions & 15 deletions src/analyze/setupScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,15 @@ export function processSetup(ast: t.Node, parentScope?: Scope, parentPath?: t.No
FunctionDeclaration(path) {
const name = path.node.id?.name;
if(name && graph.nodes.has(name)) {
path.traverse({
traverse(path.node.body, {
Identifier(path1) {
const binding = path1.scope.getBinding(path1.node.name);
if(
graph.nodes.has(path1.node.name)
&& path1.node.name !== name
&& (
path1.parent.type !== 'MemberExpression'
|| path1.parent.object === path1.node
)
&& (binding?.scope.block.type === 'Program'
|| (parentScope === binding?.scope)
)
Expand All @@ -233,7 +236,6 @@ export function processSetup(ast: t.Node, parentScope?: Scope, parentPath?: t.No
if(
spread.includes(path1.node.object.name)
&& path1.node.property.type === 'Identifier'
&& path1.node.property.name !== name
&& (binding?.scope.block.type === 'Program'
|| (parentScope === binding?.scope)
)
Expand All @@ -242,7 +244,7 @@ export function processSetup(ast: t.Node, parentScope?: Scope, parentPath?: t.No
}
}
},
});
}, path.scope, path);
}
},

Expand All @@ -258,7 +260,10 @@ export function processSetup(ast: t.Node, parentScope?: Scope, parentPath?: t.No
const binding = path1.scope.getBinding(path1.node.name);
if(
graph.nodes.has(path1.node.name)
&& path1.node.name !== name
&& (
path1.parent.type !== 'MemberExpression'
|| path1.parent.object === path1.node
)
&& (binding?.scope.block.type === 'Program'
|| (parentScope === binding?.scope)
)
Expand All @@ -275,7 +280,6 @@ export function processSetup(ast: t.Node, parentScope?: Scope, parentPath?: t.No
if(
spread.includes(path1.node.object.name)
&& path1.node.property.type === 'Identifier'
&& path1.node.property.name !== name
&& (binding?.scope.block.type === 'Program'
|| (parentScope === binding?.scope)
)
Expand All @@ -299,7 +303,10 @@ export function processSetup(ast: t.Node, parentScope?: Scope, parentPath?: t.No
const binding = path1.scope.getBinding(path1.node.name);
if(
graph.nodes.has(path1.node.name)
&& path1.node.name !== name
&& (
path1.parent.type !== 'MemberExpression'
|| path1.parent.object === path1.node
)
&& (binding?.scope.block.type === 'Program'
|| (parentScope === binding?.scope)
)
Expand All @@ -316,7 +323,6 @@ export function processSetup(ast: t.Node, parentScope?: Scope, parentPath?: t.No
if(
spread.includes(path1.node.object.name)
&& path1.node.property.type === 'Identifier'
&& path1.node.property.name !== name
&& (binding?.scope.block.type === 'Program'
|| (parentScope === binding?.scope)
)
Expand Down Expand Up @@ -345,7 +351,10 @@ export function processSetup(ast: t.Node, parentScope?: Scope, parentPath?: t.No
const binding = path1.scope.getBinding(path1.node.name);
if(
graph.nodes.has(path1.node.name)
&& path1.node.name !== name
&& (
path1.parent.type !== 'MemberExpression'
|| path1.parent.object === path1.node
)
&& (binding?.scope.block.type === 'Program'
|| (parentScope === binding?.scope)
)
Expand All @@ -362,7 +371,6 @@ export function processSetup(ast: t.Node, parentScope?: Scope, parentPath?: t.No
if(
spread.includes(path1.node.object.name)
&& path1.node.property.type === 'Identifier'
&& path1.node.property.name !== name
&& (binding?.scope.block.type === 'Program'
|| (parentScope === binding?.scope)
)
Expand All @@ -381,11 +389,18 @@ export function processSetup(ast: t.Node, parentScope?: Scope, parentPath?: t.No
if(path.node.key.type === 'Identifier' && graph.nodes.has(path.node.key.name)) {
const name = path.node.key.name;

path.traverse({
traverse(path.node.body, {
Identifier(path1) {
const binding = path1.scope.getBinding(path1.node.name);
if(
graph.nodes.has(path1.node.name)
&& path1.node.name !== name
graph.nodes.has(path1.node.name)
&& (
path1.parent.type !== 'MemberExpression'
|| path1.parent.object === path1.node
)
&& (binding?.scope.block.type === 'Program'
|| (parentScope === binding?.scope)
)
) {
graph.edges.get(name)?.add(path1.node.name);
}
Expand All @@ -395,16 +410,19 @@ export function processSetup(ast: t.Node, parentScope?: Scope, parentPath?: t.No
path1.node.object.type === 'Identifier'
&& spread.includes(path1.node.object.name)
) {
const binding = path1.scope.getBinding(path1.node.object.name);
if(
spread.includes(path1.node.object.name)
&& path1.node.property.type === 'Identifier'
&& path1.node.property.name !== name
&& (binding?.scope.block.type === 'Program'
|| (parentScope === binding?.scope)
)
) {
graph.edges.get(name)?.add(path1.node.property.name);
}
}
},
});
}, path.scope, path);
}
},
}, parentScope, parentPath);
Expand Down
6 changes: 3 additions & 3 deletions test/options-base-defineComponent/TestComponent.graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ const number: TypedNode = {label: 'number', type: NodeType.var, info: {line: 65,
const count: TypedNode = {label: 'count', type: NodeType.var, info:{line: 69, column: 6}};
const double: TypedNode = {label: 'double', type: NodeType.var, info:{line: 72, column: 6}};
const plus: TypedNode = {label: 'plus', type: NodeType.fun, info: {line: 82, column: 6}};
const add: TypedNode = {label: 'add', type: NodeType.fun, info: {line: 85, column: 6}};
const add: TypedNode = {label: 'add', type: NodeType.fun, info: {line: 86, column: 6}};

edges.set(number, new Set([]));
edges.set(count, new Set([number]));
edges.set(double, new Set([number]));
edges.set(plus, new Set([]));
edges.set(add, new Set([number]));
edges.set(plus, new Set([add]));
edges.set(add, new Set([number, plus]));

export const graph = {
nodes: new Set([number, count, double, plus, add]),
Expand Down
2 changes: 2 additions & 0 deletions test/options-base-defineComponent/TestComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@
methods: {
plus: ()=> {
counterStore.increment();
this.add();
},
add() {
counterStore.add(Number(this.number));
this.plus()
}
}
})
Expand Down
6 changes: 3 additions & 3 deletions test/options-base/TestComponent.graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ const edges = new Map<TypedNode, Set<TypedNode>>();
const number: TypedNode = {label: 'number', type: NodeType.var, info: {line: 65, column: 8}};
const count: TypedNode = {label: 'count', type: NodeType.var, info: {line: 69, column: 6}};
const plus: TypedNode = {label: 'plus', type: NodeType.fun, info: {line: 74, column: 6}};
const add: TypedNode = {label: 'add', type: NodeType.fun, info: {line: 77, column: 6}};
const add: TypedNode = {label: 'add', type: NodeType.fun, info: {line: 78, column: 6}};

edges.set(number, new Set([]));
edges.set(count, new Set([]));
edges.set(plus, new Set([]));
edges.set(add, new Set([number]));
edges.set(plus, new Set([add]));
edges.set(add, new Set([number, plus]));

export const graph = {
nodes: new Set([number, count, plus, add]),
Expand Down
2 changes: 2 additions & 0 deletions test/options-base/TestComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@
methods: {
plus:()=> {
counterStore.increment();
this.add();
},
add() {
counterStore.add(Number(this.number));
this.plus();
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions test/options-setup/TestComponent.graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ const edges = new Map<TypedNode, Set<TypedNode>>();
const number: TypedNode = {label: 'number', type: NodeType.var, info: {line: 65, column: 6}};
const count: TypedNode = {label: 'count', type: NodeType.var, info: {line: 67, column: 10}};
const plus: TypedNode = {label: 'plus', type: NodeType.fun, info: {line: 70, column: 6}};
const add: TypedNode = {label: 'add', type: NodeType.fun, info: {line: 73, column: 6}};
const add: TypedNode = {label: 'add', type: NodeType.fun, info: {line: 74, column: 6}};

edges.set(number, new Set([]));
edges.set(count, new Set([]));
edges.set(plus, new Set([]));
edges.set(add, new Set([number]));
edges.set(plus, new Set([add]));
edges.set(add, new Set([number, plus]));

export const graph = {
nodes: new Set([number, count, plus, add]),
Expand Down
2 changes: 2 additions & 0 deletions test/options-setup/TestComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ export default {
const methods = {
plus() {
counterStore.increment();
methods.add();
},
add() {
counterStore.add(Number(data.number));
methods.plus();
}
}
Expand Down
6 changes: 3 additions & 3 deletions test/setup-block/TestComponent.graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ const updateName: TypedNode = {label: 'updateName', type: NodeType.fun, info: {l
const funA: TypedNode = {label: 'funA', type: NodeType.fun, info: {line: 36, column: 9}};
const varB: TypedNode = {label: 'varB', type: NodeType.var, info: {line:43, column: 8}};
const funC: TypedNode = {label: 'funC', type: NodeType.fun, info: {line: 45, column: 9}};
const varD: TypedNode = {label: 'varD', type: NodeType.var,info: {line: 51, column: 8}};
const varE: TypedNode = {label: 'varE', type: NodeType.var, info: {line: 51, column: 14}};
const varD: TypedNode = {label: 'varD', type: NodeType.var,info: {line: 52, column: 8}};
const varE: TypedNode = {label: 'varE', type: NodeType.var, info: {line: 52, column: 14}};
const restArr: TypedNode = {label: 'restArr', type: NodeType.var, info: {line: 25, column: 10}};
const restObj: TypedNode = {label: 'restObj', type: NodeType.var, info: {line: 26, column: 11}};

Expand All @@ -30,7 +30,7 @@ edges.set(addAge, new Set([age]));
edges.set(updateName, new Set([data]));
edges.set(funA, new Set([]));
edges.set(varB, new Set([funA]));
edges.set(funC, new Set([]));
edges.set(funC, new Set([funC]));
edges.set(varD, new Set([funC, varB]));
edges.set(varE, new Set([funC, varB]));

Expand Down
1 change: 1 addition & 0 deletions test/setup-block/TestComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const { varB } = funA()
function funC(varB) {
const varD = 1
const varE = 2
funC()
return [varD, varE]
}
Expand Down

0 comments on commit f7a7879

Please sign in to comment.