Skip to content

Commit

Permalink
Merge pull request #5 from chenweiyi/master
Browse files Browse the repository at this point in the history
feat: support rest type for setup script
  • Loading branch information
zcf0508 authored Sep 11, 2023
2 parents 7406a17 + 96e61c8 commit d504825
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
42 changes: 42 additions & 0 deletions src/analyze/setupScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,27 @@ export function processSetup(ast: t.Node, parentScope?: Scope, parentPath?: t.No
const name = element.name;
const binding = path.scope.getBinding(name);

if(
binding
&& (path.parent.type === 'Program'
|| (parentPath?.type === 'ObjectMethod' && parentPath.body === path.parent)
)
&& !(declaration.init?.type === 'CallExpression'
&& declaration.init?.callee.type === 'Identifier'
&& ['defineProps', 'defineEmits'].includes(declaration.init?.callee.name)
)
) {
graph.nodes.add(name);
nodeCollection.addNode(name, declaration);
if(!graph.edges.get(name)) {
graph.edges.set(name, new Set());
}
}
}
if (element?.type === 'RestElement' && element.argument.type === 'Identifier') {
const name = element.argument.name;
const binding = path.scope.getBinding(name);

if(
binding
&& (path.parent.type === 'Program'
Expand Down Expand Up @@ -67,6 +88,27 @@ export function processSetup(ast: t.Node, parentScope?: Scope, parentPath?: t.No
}
}
}

if(property.type === 'RestElement' && property.argument.type === 'Identifier') {
const name = property.argument.name;
const binding = path.scope.getBinding(name);
if(
binding
&& (path.parent.type === 'Program'
|| (parentPath?.type === 'ObjectMethod' && parentPath.body === path.parent)
)
&& !(declaration.init?.type === 'CallExpression'
&& declaration.init?.callee.type === 'Identifier'
&& ['defineProps', 'defineEmits'].includes(declaration.init?.callee.name)
)
) {
graph.nodes.add(name);
nodeCollection.addNode(name, declaration);
if(!graph.edges.get(name)) {
graph.edges.set(name, new Set());
}
}
}
});
}
if(declaration.id?.type === 'Identifier') {
Expand Down
6 changes: 5 additions & 1 deletion test/setup-block/TestComponent.graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const varB = {label: 'varB', type: NodeType.var};
const funC = {label: 'funC', type: NodeType.fun};
const varD = {label: 'varD', type: NodeType.var};
const varE = {label: 'varE', type: NodeType.var};
const restArr = {label: 'restArr', type: NodeType.var};
const restObj = {label: 'restObj', type: NodeType.var};


edges.set(route, new Set([]));
Expand All @@ -29,8 +31,10 @@ edges.set(varB, new Set([funA]));
edges.set(funC, new Set([]));
edges.set(varD, new Set([funC, varB]));
edges.set(varE, new Set([funC, varB]));
edges.set(restArr, new Set([]));
edges.set(restObj, new Set([]));

export const graph = {
nodes: new Set([route, path, lmsg, data, age, addAge, updateName, funA, varB, funC, varD, varE]),
nodes: new Set([route, path, lmsg, data, age, addAge, updateName, funA, varB, funC, varD, varE, restArr, restObj]),
edges,
};
3 changes: 3 additions & 0 deletions test/setup-block/TestComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ const data = reactive({
const age = ref(18)
const [...restArr] = arrList
const { ...restObj } = objList
function addAge() {
age.value++
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
"vitest/globals"
]
},
"include": ["src", "test", ".eslintrc.js"],
"include": ["src", "test", ".eslintrc.js", "vitest.config.ts"],
"exclude": ["node_modules", "dist", "playground", "vscode"]
}

0 comments on commit d504825

Please sign in to comment.