Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: mergeSingleChildTrees #180

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 24 additions & 21 deletions plugin/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,34 @@ const addToPath = (
}
};

// TODO try to make it without recursion, but still typesafe
const mergeSingleChildTrees = (tree: ModuleTree): ModuleTree | ModuleTreeLeaf => {
if (tree.children.length === 1) {
const child = tree.children[0];
const name = `${tree.name}/${child.name}`;
if (isModuleTree(child)) {
tree.name = name;
tree.children = child.children;
return mergeSingleChildTrees(tree);
} else {
return {
name,
uid: child.uid,
};
}
} else {
tree.children = tree.children.map((node) => {
if (isModuleTree(node)) {
return mergeSingleChildTrees(node);
const stack: (ModuleTree | ModuleTreeLeaf)[] = [tree];

while (stack.length > 0) {
const current = stack.pop();

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add if(!current) break, instead of repeating it in both conditions

if (current && isModuleTree(current) && current.children.length === 1) {
const child = current.children[0];
const name = `${current.name}/${child.name}`;

if (isModuleTree(child)) {
current.name = name;
current.children = child.children;
stack.push(current);
} else {
return node;
const leaf: ModuleTreeLeaf = { name, uid: child.uid };
return leaf;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one not correct. We modify tree in place. But this branch will cut it completly

}
});
return tree;
} else if (current && isModuleTree(current) ) {
current.children.forEach(child => {
if (isModuleTree(child)) {
stack.push(child);
}
});
}
}

return tree;
};

export const buildTree = (
Expand Down
Loading