Skip to content

Commit ddf8efc

Browse files
committed
feat: detect CallExpression and AssignmentExpression at root of setup block
1 parent 25fd9c5 commit ddf8efc

File tree

4 files changed

+71
-5
lines changed

4 files changed

+71
-5
lines changed

src/analyze/setupScript.ts

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -693,15 +693,52 @@ export function processSetup(
693693
ExpressionStatement(path) {
694694
if (path.type === 'ExpressionStatement'
695695
&& path.node.expression.type === 'CallExpression'
696-
&& path.node.expression.callee.type === 'Identifier') {
697-
traverseHooks(path.node.expression, path.scope);
696+
&& path.node.expression.callee.type === 'Identifier'
697+
) {
698+
const name = path.node.expression.callee.name;
699+
if (
700+
graph.nodes.has(name)
701+
&& (path.scope.block.type === 'Program')
702+
) {
703+
const _node = nodeCollection.getNode(name);
704+
if (_node?.info?.used) {
705+
_node?.info?.used?.add('Call Expression');
706+
}
707+
else if (_node) {
708+
_node.info = {
709+
..._node?.info,
710+
used: new Set(['Call Expression']),
711+
};
712+
}
713+
}
714+
else {
715+
traverseHooks(path.node.expression, path.scope);
716+
}
698717
}
699718
if (path.type === 'ExpressionStatement'
700719
&& path.node.expression.type === 'AssignmentExpression'
701720
&& path.node.expression.right.type === 'CallExpression'
702-
&& path.node.expression.right.callee.type === 'Identifier') {
721+
&& path.node.expression.right.callee.type === 'Identifier'
722+
) {
703723
traverseHooks(path.node.expression.right, path.scope);
704724
}
725+
if (path.type === 'ExpressionStatement'
726+
&& path.node.expression.type === 'AssignmentExpression'
727+
&& path.node.expression.left.type === 'Identifier'
728+
&& graph.nodes.has(path.node.expression.left.name)
729+
&& (path.scope.block.type === 'Program')
730+
) {
731+
const _node = nodeCollection.getNode(path.node.expression.left.name);
732+
if (_node?.info?.used) {
733+
_node?.info?.used?.add('Assignment Expression');
734+
}
735+
else if (_node) {
736+
_node.info = {
737+
..._node?.info,
738+
used: new Set(['Assignment Expression']),
739+
};
740+
}
741+
}
705742
},
706743
}, parentScope, parentPath);
707744

src/vis.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ type CustomNode = Node & {
55
info: TypedNode['info']
66
};
77

8+
function filterNodeUserd(used: Set<string> | undefined) {
9+
const usedArray = Array.from(used || []);
10+
return new Set(usedArray.filter(u => ![
11+
'Assignment Expression',
12+
'Call Expression',
13+
].includes(u)));
14+
}
15+
816
export function getVisData(
917
graph: {
1018
nodes: Set<TypedNode>
@@ -29,8 +37,8 @@ export function getVisData(
2937
? 'used'
3038
: 'normal',
3139
title: `${
32-
node.info?.used?.size
33-
? `used by ${Array.from(node.info?.used || [])?.map(i => `\`${i}\``).join(',')}\n\n`
40+
filterNodeUserd(node.info?.used).size
41+
? `used by ${Array.from(filterNodeUserd(node.info?.used))?.map(i => `\`${i}\``).join(',')}\n\n`
3442
: ''
3543
}${
3644
usedNodes.has(node.label)

test/output/suggent-gen.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,4 +311,16 @@
311311
},
312312
"type": "info",
313313
},
314+
{
315+
"message": "Node [stop1] is not used, perhaps you can remove it.",
316+
"nodeInfo": {
317+
"info": {
318+
"column": 6,
319+
"line": 107,
320+
},
321+
"label": "stop1",
322+
"type": "var",
323+
},
324+
"type": "info",
325+
},
314326
]

test/output/vue/setup-block.vue.graph.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,7 @@
455455
"line": 111,
456456
"used": Set {
457457
"watch",
458+
"Assignment Expression",
458459
},
459460
},
460461
"label": "stop2",
@@ -465,6 +466,9 @@
465466
"info": {
466467
"column": 2,
467468
"line": 104,
469+
"used": Set {
470+
"Call Expression",
471+
},
468472
},
469473
"label": "fun1c",
470474
"type": "var",
@@ -495,6 +499,7 @@
495499
"line": 111,
496500
"used": Set {
497501
"watch",
502+
"Assignment Expression",
498503
},
499504
},
500505
"label": "stop2",
@@ -758,6 +763,9 @@
758763
"info": {
759764
"column": 2,
760765
"line": 104,
766+
"used": Set {
767+
"Call Expression",
768+
},
761769
},
762770
"label": "fun1c",
763771
"type": "var",
@@ -776,6 +784,7 @@
776784
"line": 111,
777785
"used": Set {
778786
"watch",
787+
"Assignment Expression",
779788
},
780789
},
781790
"label": "stop2",

0 commit comments

Comments
 (0)