From 16fc061d35b0187dde6522b6a68bee220054966d Mon Sep 17 00:00:00 2001 From: AnatoleLucet Date: Sat, 23 May 2020 22:04:31 +0200 Subject: [PATCH] refactor: add changeImportLocation function add a changeImportLocation function in toFractalTree to avoid duplicated code --- src/index/generateTrees/toFractalTree.ts | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/index/generateTrees/toFractalTree.ts b/src/index/generateTrees/toFractalTree.ts index 5b6c8dc..fcc86ef 100644 --- a/src/index/generateTrees/toFractalTree.ts +++ b/src/index/generateTrees/toFractalTree.ts @@ -39,6 +39,15 @@ export function toFractalTree(graph: Graph, entryPoints: string[]) { return location; }; + const changeImportLocation = ( + filePath: string, + newLocation: string + ): void => { + tree[filePath] = newLocation; + + treeSet.add(newLocation); + }; + const fn = (filePath: string, folderPath: string, graph: Graph) => { const basename = path.basename(filePath); @@ -64,8 +73,7 @@ export function toFractalTree(graph: Graph, entryPoints: string[]) { directoryName = path.basename(location, path.extname(location)); if (!isGlobal) { - tree[filePath] = location; - treeSet.add(location); + changeImportLocation(filePath, location); } const imports = graph[filePath]; @@ -118,8 +126,7 @@ export function toFractalTree(graph: Graph, entryPoints: string[]) { : filename ); - tree[currentPath] = newFilePath; - treeSet.add(newFilePath); + changeImportLocation(currentPath, newFilePath); }); } @@ -143,8 +150,7 @@ export function toFractalTree(graph: Graph, entryPoints: string[]) { testFile ); - tree[testFile] = location; - treeSet.add(location); + changeImportLocation(testFile, location); } }