Skip to content

Commit

Permalink
feat: detect CallExpression and AssignmentExpression at root of s…
Browse files Browse the repository at this point in the history
…etup block
  • Loading branch information
zcf0508 committed Jul 23, 2024
1 parent 25fd9c5 commit ddf8efc
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
43 changes: 40 additions & 3 deletions src/analyze/setupScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -693,15 +693,52 @@ export function processSetup(
ExpressionStatement(path) {
if (path.type === 'ExpressionStatement'
&& path.node.expression.type === 'CallExpression'
&& path.node.expression.callee.type === 'Identifier') {
traverseHooks(path.node.expression, path.scope);
&& path.node.expression.callee.type === 'Identifier'
) {
const name = path.node.expression.callee.name;
if (
graph.nodes.has(name)
&& (path.scope.block.type === 'Program')
) {
const _node = nodeCollection.getNode(name);
if (_node?.info?.used) {
_node?.info?.used?.add('Call Expression');
}
else if (_node) {
_node.info = {
..._node?.info,
used: new Set(['Call Expression']),
};
}
}
else {
traverseHooks(path.node.expression, path.scope);
}
}
if (path.type === 'ExpressionStatement'
&& path.node.expression.type === 'AssignmentExpression'
&& path.node.expression.right.type === 'CallExpression'
&& path.node.expression.right.callee.type === 'Identifier') {
&& path.node.expression.right.callee.type === 'Identifier'
) {
traverseHooks(path.node.expression.right, path.scope);
}
if (path.type === 'ExpressionStatement'
&& path.node.expression.type === 'AssignmentExpression'
&& path.node.expression.left.type === 'Identifier'
&& graph.nodes.has(path.node.expression.left.name)
&& (path.scope.block.type === 'Program')
) {
const _node = nodeCollection.getNode(path.node.expression.left.name);
if (_node?.info?.used) {
_node?.info?.used?.add('Assignment Expression');
}
else if (_node) {
_node.info = {
..._node?.info,
used: new Set(['Assignment Expression']),
};
}
}
},
}, parentScope, parentPath);

Expand Down
12 changes: 10 additions & 2 deletions src/vis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ type CustomNode = Node & {
info: TypedNode['info']
};

function filterNodeUserd(used: Set<string> | undefined) {
const usedArray = Array.from(used || []);
return new Set(usedArray.filter(u => ![
'Assignment Expression',
'Call Expression',
].includes(u)));
}

export function getVisData(
graph: {
nodes: Set<TypedNode>
Expand All @@ -29,8 +37,8 @@ export function getVisData(
? 'used'
: 'normal',
title: `${
node.info?.used?.size
? `used by ${Array.from(node.info?.used || [])?.map(i => `\`${i}\``).join(',')}\n\n`
filterNodeUserd(node.info?.used).size
? `used by ${Array.from(filterNodeUserd(node.info?.used))?.map(i => `\`${i}\``).join(',')}\n\n`
: ''
}${
usedNodes.has(node.label)
Expand Down
12 changes: 12 additions & 0 deletions test/output/suggent-gen.txt
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,16 @@
},
"type": "info",
},
{
"message": "Node [stop1] is not used, perhaps you can remove it.",
"nodeInfo": {
"info": {
"column": 6,
"line": 107,
},
"label": "stop1",
"type": "var",
},
"type": "info",
},
]
9 changes: 9 additions & 0 deletions test/output/vue/setup-block.vue.graph.txt
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@
"line": 111,
"used": Set {
"watch",
"Assignment Expression",
},
},
"label": "stop2",
Expand All @@ -465,6 +466,9 @@
"info": {
"column": 2,
"line": 104,
"used": Set {
"Call Expression",
},
},
"label": "fun1c",
"type": "var",
Expand Down Expand Up @@ -495,6 +499,7 @@
"line": 111,
"used": Set {
"watch",
"Assignment Expression",
},
},
"label": "stop2",
Expand Down Expand Up @@ -758,6 +763,9 @@
"info": {
"column": 2,
"line": 104,
"used": Set {
"Call Expression",
},
},
"label": "fun1c",
"type": "var",
Expand All @@ -776,6 +784,7 @@
"line": 111,
"used": Set {
"watch",
"Assignment Expression",
},
},
"label": "stop2",
Expand Down

0 comments on commit ddf8efc

Please sign in to comment.