Quellcode durchsuchen

Enable maximum strictness in TS compiler

Joeri Exelmans vor 3 Jahren
Ursprung
Commit
3dc7daad9b
3 geänderte Dateien mit 17 neuen und 11 gelöschten Zeilen
  1. 6 2
      onion.test.ts
  2. 10 8
      onion.ts
  3. 1 1
      tsconfig.json

+ 6 - 2
onion.test.ts

@@ -7,7 +7,7 @@ function getUuidCallback() {
   }
 }
 
-function assert(expression, msg) {
+function assert(expression: boolean, msg: string) {
   if (!expression) {
     throw new Error(msg);
   }
@@ -47,7 +47,11 @@ describe("CRUD operations", () => {
 
     assert(s.readEdge(n2, 'x') === 43, "expected n2.x === 43");
 
-    assert(s.readEdge(n2, 'a') === undefined, "edge should not exist.")
+    s.deleteEdge(n1, 'edge');
+
+    assert(s.readEdge(n1, 'edge') === undefined, "edge should have been deleted.");
+
+    assert(s.readEdge(n2, 'a') === undefined, "edge should not exist.");
   });
 
   it("Delete non-existing node", () => {

+ 10 - 8
onion.ts

@@ -7,11 +7,11 @@ type PrimitiveType = string | number | boolean;
 export class UUID {
   uuid: PrimitiveType;
   
-  constructor(uuid) {
+  constructor(uuid: PrimitiveType) {
     this.uuid = uuid;
   }
 
-  [inspect.custom](depth, options) {
+  [inspect.custom](depth: number, options: object) {
     return "UUID{" + inspect(this.uuid, options) + "}"
   }
 }
@@ -93,11 +93,13 @@ class NodeMap {
   }
 }
 
+type UUIDCallbackType = () => UUID;
+
 export class GraphState {
   nodes: NodeMap;
-  uuidCallback: () => UUID;
+  uuidCallback: UUIDCallbackType;
 
-  constructor(uuidCallback) {
+  constructor(uuidCallback: UUIDCallbackType) {
     this.nodes = new NodeMap();
     this.uuidCallback = uuidCallback;
   }
@@ -133,11 +135,11 @@ export class GraphState {
     }
   }
 
-  // Set a node's outgoing edge to point to a node
+  // Create or update a node's outgoing edge to point to a node
   // Idempotent.
   setEdge(srcId: NodeId, label: string, targetId: NodeId) {
     // gotta remove the existing edge first, if it exists
-    this.unsetEdge(srcId, label);
+    this.deleteEdge(srcId, label);
 
     const srcNode = this.nodes.get(srcId);
     srcNode.outgoing.set(label, targetId);
@@ -145,9 +147,9 @@ export class GraphState {
     targetNode.incoming.push({label, srcId});
   }
 
-  // Unset, i.e., delete an edge
+  // Delete an edge.
   // Idempotent.
-  unsetEdge(srcId: NodeId, label: string) {
+  deleteEdge(srcId: NodeId, label: string) {
     const srcNode = this.nodes.get(srcId);
     const existingTargetId = srcNode.outgoing.get(label);
     if (existingTargetId !== undefined) {

+ 1 - 1
tsconfig.json

@@ -2,6 +2,6 @@
   "compilerOptions": {
     "types": ["mocha", "node"],
     "target": "es6",
-    "strictNullChecks": true
+    "strict": true
   }
 }