Skip to content

Commit

Permalink
feat: clarify Pkg.version type (undefined instead of null)
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Yegupov committed Nov 27, 2019
1 parent 9a1b18d commit 3c133ff
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
10 changes: 5 additions & 5 deletions README.md
Expand Up @@ -49,21 +49,21 @@ export interface DepGraph {
};
readonly rootPkg: {
name: string;
version: string | null;
version?: string;
};
// all unique packages in the graph (including root package)
getPkgs(): Array<{
name: string;
version: string | null;
version?: string;
}>;
// all unique packages in the graph, except the root package
getDepPkgs(): Array<{
name: string;
version: string | null;
version?: string;
}>;
pkgPathsToRoot(pkg: Pkg): Array<Array<{
name: string;
version: string | null;
version?: string;
}>>;
countPathsToRoot(pkg: Pkg): number;
toJSON(): DepGraphData;
Expand All @@ -89,7 +89,7 @@ export interface DepGraphData {
id: string;
info: {
name: string;
version: string | null;
version?: string;
};
}>;
graph: {
Expand Down
3 changes: 1 addition & 2 deletions src/core/create-from-json.ts
Expand Up @@ -22,8 +22,7 @@ export function createFromJSON(depGraphData: DepGraphData): DepGraph {
const pkgNodes: {[pkgId: string]: Set<string>} = {};

for (const { id, info } of depGraphData.pkgs) {
// TODO: avoid this, instead just use `info` as is
pkgs[id] = info.version ? info : { ...info, version: null } as any;
pkgs[id] = info;
}

for (const node of depGraphData.graph.nodes) {
Expand Down
4 changes: 4 additions & 0 deletions src/core/dep-graph.ts
Expand Up @@ -161,6 +161,10 @@ class DepGraphImpl implements types.DepGraphInternal {
otherDepGraph = createFromJSON(other.toJSON()) as types.DepGraphInternal;
}

// In theory, for the graphs created by standard means, `_.isEquals(this._data, otherDepGraph._data)`
// should suffice, since node IDs will be generated in a predictable way.
// However, to support unconventional node IDs, we run our own deep
// comparison.
return this.nodeEquals(this, this.rootNodeId, otherDepGraph, otherDepGraph.rootNodeId, compareRoot);
}

Expand Down
4 changes: 2 additions & 2 deletions test/core/create-from-json.test.ts
Expand Up @@ -759,10 +759,10 @@ test('fromJSON a pkg missing version field', () => {
const depGraph = depGraphLib.createFromJSON(graphJson as any);
expect(depGraph.getPkgs().sort()).toEqual([
{ name: 'toor', version: '1.0.0' },
{ name: 'foo', version: null },
{ name: 'foo' },
]);
expect(depGraph.getDepPkgs().sort()).toEqual([
{ name: 'foo', version: null },
{ name: 'foo' },
]);
});

Expand Down

0 comments on commit 3c133ff

Please sign in to comment.