@parcel/graph 2.0.0-nightly.2460 → 2.0.0-nightly.2465

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/Graph.js CHANGED
@@ -34,7 +34,7 @@ const ALL_EDGE_TYPES = '@@all_edge_types';
34
34
  exports.ALL_EDGE_TYPES = ALL_EDGE_TYPES;
35
35
 
36
36
  class Graph {
37
- nextNodeId = 0;
37
+ nextNodeId = 1;
38
38
 
39
39
  constructor(opts) {
40
40
  var _opts$nextNodeId;
@@ -117,7 +117,7 @@ class Graph {
117
117
  return this.nodes.get(id);
118
118
  }
119
119
 
120
- addEdge(from, to, type = null) {
120
+ addEdge(from, to, type = 1) {
121
121
  if (!this.getNode(from)) {
122
122
  throw new Error(`"from" node '${(0, _types.fromNodeId)(from)}' not found`);
123
123
  }
@@ -130,11 +130,11 @@ class Graph {
130
130
  this.inboundEdges.addEdge(to, from, type);
131
131
  }
132
132
 
133
- hasEdge(from, to, type = null) {
133
+ hasEdge(from, to, type = 1) {
134
134
  return this.outboundEdges.hasEdge(from, to, type);
135
135
  }
136
136
 
137
- getNodeIdsConnectedTo(nodeId, type = null) {
137
+ getNodeIdsConnectedTo(nodeId, type = 1) {
138
138
  this._assertHasNodeId(nodeId);
139
139
 
140
140
  let inboundByType = this.inboundEdges.getEdgesByType(nodeId);
@@ -172,7 +172,7 @@ class Graph {
172
172
  return [...nodes];
173
173
  }
174
174
 
175
- getNodeIdsConnectedFrom(nodeId, type = null) {
175
+ getNodeIdsConnectedFrom(nodeId, type = 1) {
176
176
  this._assertHasNodeId(nodeId);
177
177
 
178
178
  let outboundByType = this.outboundEdges.getEdgesByType(nodeId);
@@ -234,7 +234,7 @@ class Graph {
234
234
  (0, _assert().default)(wasRemoved);
235
235
  }
236
236
 
237
- removeEdges(nodeId, type = null) {
237
+ removeEdges(nodeId, type = 1) {
238
238
  this._assertHasNodeId(nodeId);
239
239
 
240
240
  for (let to of this.outboundEdges.getEdges(nodeId, type)) {
@@ -243,7 +243,7 @@ class Graph {
243
243
  } // Removes edge and node the edge is to if the node is orphaned
244
244
 
245
245
 
246
- removeEdge(from, to, type = null, removeOrphans = true) {
246
+ removeEdge(from, to, type = 1, removeOrphans = true) {
247
247
  if (!this.outboundEdges.hasEdge(from, to, type)) {
248
248
  throw new Error(`Outbound edge from ${(0, _types.fromNodeId)(from)} to ${(0, _types.fromNodeId)(to)} not found!`);
249
249
  }
@@ -301,7 +301,7 @@ class Graph {
301
301
  this.nodes.set(nodeId, node);
302
302
  }
303
303
 
304
- replaceNode(fromNodeId, toNodeId, type = null) {
304
+ replaceNode(fromNodeId, toNodeId, type = 1) {
305
305
  this._assertHasNodeId(fromNodeId);
306
306
 
307
307
  for (let parent of this.inboundEdges.getEdges(fromNodeId, type)) {
@@ -313,7 +313,7 @@ class Graph {
313
313
  } // Update a node's downstream nodes making sure to prune any orphaned branches
314
314
 
315
315
 
316
- replaceNodeIdsConnectedTo(fromNodeId, toNodeIds, replaceFilter, type = null) {
316
+ replaceNodeIdsConnectedTo(fromNodeId, toNodeIds, replaceFilter, type = 1) {
317
317
  this._assertHasNodeId(fromNodeId);
318
318
 
319
319
  let outboundEdges = this.outboundEdges.getEdges(fromNodeId, type);
@@ -332,7 +332,7 @@ class Graph {
332
332
  }
333
333
  }
334
334
 
335
- traverse(visit, startNodeId, type = null) {
335
+ traverse(visit, startNodeId, type = 1) {
336
336
  return this.dfs({
337
337
  visit,
338
338
  startNodeId,
@@ -344,7 +344,7 @@ class Graph {
344
344
  return this.traverse(mapVisitor(filter, visit), startNodeId, type);
345
345
  }
346
346
 
347
- traverseAncestors(startNodeId, visit, type = null) {
347
+ traverseAncestors(startNodeId, visit, type = 1) {
348
348
  return this.dfs({
349
349
  visit,
350
350
  startNodeId,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parcel/graph",
3
- "version": "2.0.0-nightly.2460+fb9818a7",
3
+ "version": "2.0.0-nightly.2465+b6faa0c8",
4
4
  "description": "Blazing fast, zero configuration web application bundler",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
@@ -22,5 +22,5 @@
22
22
  "dependencies": {
23
23
  "nullthrows": "^1.1.1"
24
24
  },
25
- "gitHead": "fb9818a7dcdfc930e3a2efac910a0e70964df05f"
25
+ "gitHead": "b6faa0c819d562726ac8708f7f91ff0cb65b8311"
26
26
  }
@@ -4,16 +4,16 @@ import type {ContentKey, NodeId} from './types';
4
4
  import Graph, {type GraphOpts} from './Graph';
5
5
  import nullthrows from 'nullthrows';
6
6
 
7
- export type SerializedContentGraph<TNode, TEdgeType: string | null = null> = {|
7
+ export type SerializedContentGraph<TNode, TEdgeType: number = 1> = {|
8
8
  ...GraphOpts<TNode, TEdgeType>,
9
9
  _contentKeyToNodeId: Map<ContentKey, NodeId>,
10
10
  _nodeIdToContentKey: Map<NodeId, ContentKey>,
11
11
  |};
12
12
 
13
- export default class ContentGraph<
13
+ export default class ContentGraph<TNode, TEdgeType: number = 1> extends Graph<
14
14
  TNode,
15
- TEdgeType: string | null = null,
16
- > extends Graph<TNode, TEdgeType> {
15
+ TEdgeType,
16
+ > {
17
17
  _contentKeyToNodeId: Map<ContentKey, NodeId>;
18
18
  _nodeIdToContentKey: Map<NodeId, ContentKey>;
19
19
 
package/src/Graph.js CHANGED
@@ -7,21 +7,22 @@ import type {TraversalActions, GraphVisitor} from '@parcel/types';
7
7
  import assert from 'assert';
8
8
  import nullthrows from 'nullthrows';
9
9
 
10
- export type GraphOpts<TNode, TEdgeType: string | null = null> = {|
10
+ type NullEdgeType = 1;
11
+ export type GraphOpts<TNode, TEdgeType: number = 1> = {|
11
12
  nodes?: Map<NodeId, TNode>,
12
- edges?: AdjacencyListMap<TEdgeType | null>,
13
+ edges?: AdjacencyListMap<TEdgeType | NullEdgeType>,
13
14
  rootNodeId?: ?NodeId,
14
15
  nextNodeId?: ?number,
15
16
  |};
16
17
 
17
18
  export const ALL_EDGE_TYPES = '@@all_edge_types';
18
19
 
19
- export default class Graph<TNode, TEdgeType: string | null = null> {
20
+ export default class Graph<TNode, TEdgeType: number = 1> {
20
21
  nodes: Map<NodeId, TNode>;
21
- inboundEdges: AdjacencyList<TEdgeType | null>;
22
- outboundEdges: AdjacencyList<TEdgeType | null>;
22
+ inboundEdges: AdjacencyList<TEdgeType | NullEdgeType>;
23
+ outboundEdges: AdjacencyList<TEdgeType | NullEdgeType>;
23
24
  rootNodeId: ?NodeId;
24
- nextNodeId: number = 0;
25
+ nextNodeId: number = 1;
25
26
 
26
27
  constructor(opts: ?GraphOpts<TNode, TEdgeType>) {
27
28
  this.nodes = opts?.nodes || new Map();
@@ -71,7 +72,7 @@ export default class Graph<TNode, TEdgeType: string | null = null> {
71
72
 
72
73
  // Returns a list of all edges in the graph. This can be large, so iterating
73
74
  // the complete list can be costly in large graphs. Used when merging graphs.
74
- getAllEdges(): Array<Edge<TEdgeType | null>> {
75
+ getAllEdges(): Array<Edge<TEdgeType | NullEdgeType>> {
75
76
  let edges = [];
76
77
  for (let [from, edgeList] of this.outboundEdges.getListMap()) {
77
78
  for (let [type, toNodes] of edgeList) {
@@ -97,7 +98,7 @@ export default class Graph<TNode, TEdgeType: string | null = null> {
97
98
  return this.nodes.get(id);
98
99
  }
99
100
 
100
- addEdge(from: NodeId, to: NodeId, type: TEdgeType | null = null): void {
101
+ addEdge(from: NodeId, to: NodeId, type: TEdgeType | NullEdgeType = 1): void {
101
102
  if (!this.getNode(from)) {
102
103
  throw new Error(`"from" node '${fromNodeId(from)}' not found`);
103
104
  }
@@ -110,13 +111,17 @@ export default class Graph<TNode, TEdgeType: string | null = null> {
110
111
  this.inboundEdges.addEdge(to, from, type);
111
112
  }
112
113
 
113
- hasEdge(from: NodeId, to: NodeId, type?: TEdgeType | null = null): boolean {
114
+ hasEdge(
115
+ from: NodeId,
116
+ to: NodeId,
117
+ type?: TEdgeType | NullEdgeType = 1,
118
+ ): boolean {
114
119
  return this.outboundEdges.hasEdge(from, to, type);
115
120
  }
116
121
 
117
122
  getNodeIdsConnectedTo(
118
123
  nodeId: NodeId,
119
- type: TEdgeType | null | Array<TEdgeType | null> = null,
124
+ type: TEdgeType | NullEdgeType | Array<TEdgeType | NullEdgeType> = 1,
120
125
  ): Array<NodeId> {
121
126
  this._assertHasNodeId(nodeId);
122
127
 
@@ -149,7 +154,7 @@ export default class Graph<TNode, TEdgeType: string | null = null> {
149
154
 
150
155
  getNodeIdsConnectedFrom(
151
156
  nodeId: NodeId,
152
- type: TEdgeType | null | Array<TEdgeType | null> = null,
157
+ type: TEdgeType | NullEdgeType | Array<TEdgeType | NullEdgeType> = 1,
153
158
  ): Array<NodeId> {
154
159
  this._assertHasNodeId(nodeId);
155
160
  let outboundByType = this.outboundEdges.getEdgesByType(nodeId);
@@ -206,7 +211,7 @@ export default class Graph<TNode, TEdgeType: string | null = null> {
206
211
  assert(wasRemoved);
207
212
  }
208
213
 
209
- removeEdges(nodeId: NodeId, type: TEdgeType | null = null) {
214
+ removeEdges(nodeId: NodeId, type: TEdgeType | NullEdgeType = 1) {
210
215
  this._assertHasNodeId(nodeId);
211
216
 
212
217
  for (let to of this.outboundEdges.getEdges(nodeId, type)) {
@@ -218,7 +223,7 @@ export default class Graph<TNode, TEdgeType: string | null = null> {
218
223
  removeEdge(
219
224
  from: NodeId,
220
225
  to: NodeId,
221
- type: TEdgeType | null = null,
226
+ type: TEdgeType | NullEdgeType = 1,
222
227
  removeOrphans: boolean = true,
223
228
  ) {
224
229
  if (!this.outboundEdges.hasEdge(from, to, type)) {
@@ -290,7 +295,7 @@ export default class Graph<TNode, TEdgeType: string | null = null> {
290
295
  replaceNode(
291
296
  fromNodeId: NodeId,
292
297
  toNodeId: NodeId,
293
- type: TEdgeType | null = null,
298
+ type: TEdgeType | NullEdgeType = 1,
294
299
  ): void {
295
300
  this._assertHasNodeId(fromNodeId);
296
301
  for (let parent of this.inboundEdges.getEdges(fromNodeId, type)) {
@@ -305,7 +310,7 @@ export default class Graph<TNode, TEdgeType: string | null = null> {
305
310
  fromNodeId: NodeId,
306
311
  toNodeIds: $ReadOnlyArray<NodeId>,
307
312
  replaceFilter?: null | (NodeId => boolean),
308
- type?: TEdgeType | null = null,
313
+ type?: TEdgeType | NullEdgeType = 1,
309
314
  ): void {
310
315
  this._assertHasNodeId(fromNodeId);
311
316
 
@@ -331,7 +336,7 @@ export default class Graph<TNode, TEdgeType: string | null = null> {
331
336
  traverse<TContext>(
332
337
  visit: GraphVisitor<NodeId, TContext>,
333
338
  startNodeId: ?NodeId,
334
- type: TEdgeType | null | Array<TEdgeType | null> = null,
339
+ type: TEdgeType | NullEdgeType | Array<TEdgeType | NullEdgeType> = 1,
335
340
  ): ?TContext {
336
341
  return this.dfs({
337
342
  visit,
@@ -344,7 +349,7 @@ export default class Graph<TNode, TEdgeType: string | null = null> {
344
349
  filter: (NodeId, TraversalActions) => ?TValue,
345
350
  visit: GraphVisitor<TValue, TContext>,
346
351
  startNodeId: ?NodeId,
347
- type?: TEdgeType | null | Array<TEdgeType | null>,
352
+ type?: TEdgeType | Array<TEdgeType | NullEdgeType>,
348
353
  ): ?TContext {
349
354
  return this.traverse(mapVisitor(filter, visit), startNodeId, type);
350
355
  }
@@ -352,7 +357,7 @@ export default class Graph<TNode, TEdgeType: string | null = null> {
352
357
  traverseAncestors<TContext>(
353
358
  startNodeId: ?NodeId,
354
359
  visit: GraphVisitor<NodeId, TContext>,
355
- type: TEdgeType | null | Array<TEdgeType | null> = null,
360
+ type: TEdgeType | NullEdgeType | Array<TEdgeType | NullEdgeType> = 1,
356
361
  ): ?TContext {
357
362
  return this.dfs({
358
363
  visit,
package/src/types.js CHANGED
@@ -11,7 +11,7 @@ export function fromNodeId(x: NodeId): number {
11
11
 
12
12
  export type ContentKey = string;
13
13
 
14
- export type Edge<TEdgeType: string | null> = {|
14
+ export type Edge<TEdgeType: number> = {|
15
15
  from: NodeId,
16
16
  to: NodeId,
17
17
  type: TEdgeType,
@@ -87,7 +87,7 @@ describe('Graph', () => {
87
87
  let nodeB = graph.addNode('b');
88
88
  let nodeC = graph.addNode('c');
89
89
  graph.addEdge(nodeA, nodeB);
90
- graph.addEdge(nodeA, nodeC, 'edgetype');
90
+ graph.addEdge(nodeA, nodeC, 1);
91
91
  assert(graph.isOrphanedNode(nodeA));
92
92
  assert(!graph.isOrphanedNode(nodeB));
93
93
  assert(!graph.isOrphanedNode(nodeC));
@@ -114,9 +114,7 @@ describe('Graph', () => {
114
114
  assert(graph.nodes.has(nodeD));
115
115
  assert(!graph.nodes.has(nodeB));
116
116
  assert(!graph.nodes.has(nodeC));
117
- assert.deepEqual(graph.getAllEdges(), [
118
- {from: nodeA, to: nodeD, type: null},
119
- ]);
117
+ assert.deepEqual(graph.getAllEdges(), [{from: nodeA, to: nodeD, type: 1}]);
120
118
  });
121
119
 
122
120
  it('removing a node recursively deletes orphaned nodes', () => {
@@ -157,8 +155,8 @@ describe('Graph', () => {
157
155
 
158
156
  assert.deepEqual([...graph.nodes.keys()], [nodeA, nodeC, nodeF]);
159
157
  assert.deepEqual(graph.getAllEdges(), [
160
- {from: nodeA, to: nodeC, type: null},
161
- {from: nodeC, to: nodeF, type: null},
158
+ {from: nodeA, to: nodeC, type: 1},
159
+ {from: nodeC, to: nodeF, type: 1},
162
160
  ]);
163
161
  });
164
162
 
@@ -202,8 +200,8 @@ describe('Graph', () => {
202
200
 
203
201
  assert.deepEqual([...graph.nodes.keys()], [nodeA, nodeC, nodeF]);
204
202
  assert.deepEqual(graph.getAllEdges(), [
205
- {from: nodeA, to: nodeC, type: null},
206
- {from: nodeC, to: nodeF, type: null},
203
+ {from: nodeA, to: nodeC, type: 1},
204
+ {from: nodeC, to: nodeF, type: 1},
207
205
  ]);
208
206
  });
209
207
 
@@ -237,11 +235,11 @@ describe('Graph', () => {
237
235
 
238
236
  assert.deepEqual(nodesBefore, getNodeIds());
239
237
  assert.deepEqual(graph.getAllEdges(), [
240
- {from: nodeA, to: nodeB, type: null},
241
- {from: nodeB, to: nodeC, type: null},
242
- {from: nodeB, to: nodeD, type: null},
243
- {from: nodeD, to: nodeE, type: null},
244
- {from: nodeE, to: nodeB, type: null},
238
+ {from: nodeA, to: nodeB, type: 1},
239
+ {from: nodeB, to: nodeC, type: 1},
240
+ {from: nodeB, to: nodeD, type: 1},
241
+ {from: nodeD, to: nodeE, type: 1},
242
+ {from: nodeE, to: nodeB, type: 1},
245
243
  ]);
246
244
  });
247
245
 
@@ -280,8 +278,8 @@ describe('Graph', () => {
280
278
  assert(!graph.hasNode(nodeC));
281
279
  assert(graph.hasNode(nodeD));
282
280
  assert.deepEqual(graph.getAllEdges(), [
283
- {from: nodeA, to: nodeB, type: null},
284
- {from: nodeA, to: nodeD, type: null},
281
+ {from: nodeA, to: nodeB, type: 1},
282
+ {from: nodeA, to: nodeD, type: 1},
285
283
  ]);
286
284
  });
287
285
 
@@ -292,10 +290,10 @@ describe('Graph', () => {
292
290
  let nodeC = graph.addNode('c');
293
291
  let nodeD = graph.addNode('d');
294
292
 
295
- graph.addEdge(nodeA, nodeB, 'edgetype');
293
+ graph.addEdge(nodeA, nodeB, 2);
296
294
  graph.addEdge(nodeA, nodeD);
297
295
  graph.addEdge(nodeB, nodeC);
298
- graph.addEdge(nodeB, nodeD, 'edgetype');
296
+ graph.addEdge(nodeB, nodeD, 2);
299
297
 
300
298
  graph.setRootNodeId(nodeA);
301
299
 
@@ -305,7 +303,7 @@ describe('Graph', () => {
305
303
  visited.push(nodeId);
306
304
  },
307
305
  null, // use root as startNode
308
- 'edgetype',
306
+ 2,
309
307
  );
310
308
 
311
309
  assert.deepEqual(visited, [nodeA, nodeB, nodeD]);