@@ -7,15 +7,29 @@ import {
77 partIsFile ,
88 createNode
99} from "./utils" ;
10+ import {
11+ createTreeNodeMatcher ,
12+ findNodeInContents ,
13+ getDomain
14+ } from "./treeOrder" ;
1015import { getURL , getFilenameFromPath } from "./getURL" ;
1116
1217import type { Node } from "./types" ;
1318import type { SourceRecord } from "../../reducers/types" ;
1419
15- function createNodeInTree ( part : string , path : string , tree : Node ) {
20+ function createNodeInTree (
21+ part : string ,
22+ path : string ,
23+ tree : Node ,
24+ index : number
25+ ) {
1626 const node = createNode ( part , path , [ ] ) ;
27+
1728 // we are modifying the tree
18- tree . contents = [ ...tree . contents , node ] ;
29+ const contents = tree . contents . slice ( 0 ) ;
30+ contents . splice ( index , 0 , node ) ;
31+ tree . contents = contents ;
32+
1933 return node ;
2034}
2135
@@ -31,23 +45,28 @@ function findOrCreateNode(
3145 path : string ,
3246 part : string ,
3347 index : number ,
34- url : Object
48+ url : Object ,
49+ debuggeeHost : ?string
3550) {
36- const child = subTree . contents . find ( c => c . name === part ) ;
51+ const addedPartIsFile = partIsFile ( index , parts , url ) ;
52+ const { found : childFound , index : childIndex } = findNodeInContents (
53+ subTree ,
54+ createTreeNodeMatcher ( part , ! addedPartIsFile , debuggeeHost )
55+ ) ;
3756
3857 // we create and enter the new node
39- if ( ! child ) {
40- return createNodeInTree ( part , path , subTree ) ;
58+ if ( ! childFound ) {
59+ return createNodeInTree ( part , path , subTree , childIndex ) ;
4160 }
4261
4362 // we found a path with the same name as the part. We need to determine
4463 // if this is the correct child, or if we have a naming conflict
45- const addedPartIsFile = partIsFile ( index , parts , url ) ;
64+ const child = subTree . contents [ childIndex ] ;
4665 const childIsFile = ! nodeHasChildren ( child ) ;
4766
4867 // if we have a naming conflict, we'll create a new node
4968 if ( ( childIsFile && ! addedPartIsFile ) || ( ! childIsFile && addedPartIsFile ) ) {
50- return createNodeInTree ( part , path , subTree ) ;
69+ return createNodeInTree ( part , path , subTree , childIndex ) ;
5170 }
5271
5372 // if there is no naming conflict, we can traverse into the child
@@ -58,7 +77,7 @@ function findOrCreateNode(
5877 * walk the source tree to the final node for a given url,
5978 * adding new nodes along the way
6079 */
61- function traverseTree ( url : Object , tree : Node ) {
80+ function traverseTree ( url : Object , tree : Node , debuggeeHost : ? string ) {
6281 url . path = decodeURIComponent ( url . path ) ;
6382
6483 const parts = url . path . split ( "/" ) . filter ( p => p !== "" ) ;
@@ -67,7 +86,16 @@ function traverseTree(url: Object, tree: Node) {
6786 let path = "" ;
6887 return parts . reduce ( ( subTree , part , index ) => {
6988 path = `${ path } /${ part } ` ;
70- return findOrCreateNode ( parts , subTree , path , part , index , url ) ;
89+ const debuggeeHostIfRoot = index === 0 ? debuggeeHost : null ;
90+ return findOrCreateNode (
91+ parts ,
92+ subTree ,
93+ path ,
94+ part ,
95+ index ,
96+ url ,
97+ debuggeeHostIfRoot
98+ ) ;
7199 } , tree ) ;
72100}
73101
@@ -84,18 +112,24 @@ function addSourceToNode(node: Node, url: Object, source: SourceRecord) {
84112 }
85113
86114 const name = getFilenameFromPath ( url . path ) ;
87- const existingNode = node . contents . find ( childNode => childNode . name === name ) ;
115+ const { found : childFound , index : childIndex } = findNodeInContents (
116+ node ,
117+ createTreeNodeMatcher ( name , false , null )
118+ ) ;
88119
89120 // if we are readding an existing file in the node, overwrite the existing
90121 // file and return the node's contents
91- if ( existingNode ) {
122+ if ( childFound ) {
123+ const existingNode = node . contents [ childIndex ] ;
92124 existingNode . contents = source ;
93125 return node . contents ;
94126 }
95127
96128 // if this is a new file, add the new file;
97129 const newNode = createNode ( name , source . get ( "url" ) , source ) ;
98- return [ ...node . contents , newNode ] ;
130+ const contents = node . contents . slice ( 0 ) ;
131+ contents . splice ( childIndex , 0 , newNode ) ;
132+ return contents ;
99133}
100134
101135/**
@@ -108,11 +142,12 @@ export function addToTree(
108142 debuggeeUrl : string
109143) {
110144 const url = getURL ( source . get ( "url" ) , debuggeeUrl ) ;
145+ const debuggeeHost = getDomain ( debuggeeUrl ) ;
111146
112147 if ( isInvalidUrl ( url , source ) ) {
113148 return ;
114149 }
115150
116- const finalNode = traverseTree ( url , tree ) ;
151+ const finalNode = traverseTree ( url , tree , debuggeeHost ) ;
117152 finalNode . contents = addSourceToNode ( finalNode , url , source ) ;
118153}
0 commit comments