Repository: pmcelhaney/counterfact
Pull request #241 (2022-10-03 17:34:37)

make the generated code a bit cleaner


Patch:
diff --git a/.changeset/big-icons-lie.md b/.changeset/big-icons-lie.md
new file mode 100644
index 0000000..15d2997
--- /dev/null
+++ b/.changeset/big-icons-lie.md
@@ -0,0 +1,5 @@
+---
+"counterfact": patch
+---
+
+made the generated code a bit cleaner
diff --git a/src/typescript-generator/operation-coder.js b/src/typescript-generator/operation-coder.js
index 64811fe..c794d09 100644
--- a/src/typescript-generator/operation-coder.js
+++ b/src/typescript-generator/operation-coder.js
@@ -23,8 +23,8 @@ export class OperationCoder extends Coder {
       return "() => { /* no response content specified in the OpenAPI document */ }";
     }
 
-    return `({ response}) => {
-      return response[${
+    return `($) => {
+      return $.response[${
         firstStatusCode === "default" ? 200 : firstStatusCode
       }].random();
     }`;
diff --git a/src/typescript-generator/script.js b/src/typescript-generator/script.js
index 5f85307..c18f589 100644
--- a/src/typescript-generator/script.js
+++ b/src/typescript-generator/script.js
@@ -181,9 +181,10 @@ export class Script {
 
   contents() {
     return [
-      ...this.externalImportsStatements(),
-      ...this.importStatements(),
-      ...this.exportStatements(),
-    ].join("\n");
+      this.externalImportsStatements().join("\n"),
+      this.importStatements().join("\n"),
+      "\n\n",
+      this.exportStatements().join("\n\n"),
+    ].join("");
   }
 }


Test patch:
diff --git a/test/typescript-generator/__snapshots__/end-to-end.test.js.snap b/test/typescript-generator/__snapshots__/end-to-end.test.js.snap
index 9cb9386..c5be045 100644
--- a/test/typescript-generator/__snapshots__/end-to-end.test.js.snap
+++ b/test/typescript-generator/__snapshots__/end-to-end.test.js.snap
@@ -11,8 +11,8 @@ Map {
     },
     "exports": Map {
       "POST" => Object {
-        "code": "({ response}) => {
-      return response[200].random();
+        "code": "($) => {
+      return $.response[200].random();
     }",
         "done": true,
         "id": "OperationCoder@petstore.yaml#/paths/~1pet/post",
@@ -23,8 +23,8 @@ Map {
         "typeDeclaration": "HTTP_POST",
       },
       "PUT" => Object {
-        "code": "({ response}) => {
-      return response[200].random();
+        "code": "($) => {
+      return $.response[200].random();
     }",
         "done": true,
         "id": "OperationCoder@petstore.yaml#/paths/~1pet/put",
@@ -926,8 +926,8 @@ Map {
     },
     "exports": Map {
       "GET" => Object {
-        "code": "({ response}) => {
-      return response[200].random();
+        "code": "($) => {
+      return $.response[200].random();
     }",
         "done": true,
         "id": "OperationCoder@petstore.yaml#/paths/~1pet~1findByStatus/get",
@@ -1350,8 +1350,8 @@ Map {
     },
     "exports": Map {
       "GET" => Object {
-        "code": "({ response}) => {
-      return response[200].random();
+        "code": "($) => {
+      return $.response[200].random();
     }",
         "done": true,
         "id": "OperationCoder@petstore.yaml#/paths/~1pet~1findByTags/get",
@@ -1778,8 +1778,8 @@ Map {
     },
     "exports": Map {
       "GET" => Object {
-        "code": "({ response}) => {
-      return response[200].random();
+        "code": "($) => {
+      return $.response[200].random();
     }",
         "done": true,
         "id": "OperationCoder@petstore.yaml#/paths/~1pet~1{petId}/get",
@@ -3118,8 +3118,8 @@ Map {
     },
     "exports": Map {
       "POST" => Object {
-        "code": "({ response}) => {
-      return response[200].random();
+        "code": "($) => {
+      return $.response[200].random();
     }",
         "done": true,
         "id": "OperationCoder@petstore.yaml#/paths/~1pet~1{petId}~1uploadImage/post",
@@ -3380,8 +3380,8 @@ Map {
     },
     "exports": Map {
       "GET" => Object {
-        "code": "({ response}) => {
-      return response[200].random();
+        "code": "($) => {
+      return $.response[200].random();
     }",
         "done": true,
         "id": "OperationCoder@petstore.yaml#/paths/~1store~1inventory/get",
@@ -3580,8 +3580,8 @@ Map {
     },
     "exports": Map {
       "POST" => Object {
-        "code": "({ response}) => {
-      return response[200].random();
+        "code": "($) => {
+      return $.response[200].random();
     }",
         "done": true,
         "id": "OperationCoder@petstore.yaml#/paths/~1store~1order/post",
@@ -3864,8 +3864,8 @@ Map {
     },
     "exports": Map {
       "GET" => Object {
-        "code": "({ response}) => {
-      return response[200].random();
+        "code": "($) => {
+      return $.response[200].random();
     }",
         "done": true,
         "id": "OperationCoder@petstore.yaml#/paths/~1store~1order~1{orderId}/get",
@@ -4540,8 +4540,8 @@ Map {
     },
     "exports": Map {
       "POST" => Object {
-        "code": "({ response}) => {
-      return response[200].random();
+        "code": "($) => {
+      return $.response[200].random();
     }",
         "done": true,
         "id": "OperationCoder@petstore.yaml#/paths/~1user/post",
@@ -4814,8 +4814,8 @@ Map {
     },
     "exports": Map {
       "POST" => Object {
-        "code": "({ response}) => {
-      return response[200].random();
+        "code": "($) => {
+      return $.response[200].random();
     }",
         "done": true,
         "id": "OperationCoder@petstore.yaml#/paths/~1user~1createWithList/post",
@@ -5112,8 +5112,8 @@ Map {
     },
     "exports": Map {
       "GET" => Object {
-        "code": "({ response}) => {
-      return response[200].random();
+        "code": "($) => {
+      return $.response[200].random();
     }",
         "done": true,
         "id": "OperationCoder@petstore.yaml#/paths/~1user~1login/get",
@@ -5546,8 +5546,8 @@ Map {
     },
     "exports": Map {
       "GET" => Object {
-        "code": "({ response}) => {
-      return response[200].random();
+        "code": "($) => {
+      return $.response[200].random();
     }",
         "done": true,
         "id": "OperationCoder@petstore.yaml#/paths/~1user~1{username}/get",
diff --git a/test/typescript-generator/__snapshots__/operation-coder.test.js.snap b/test/typescript-generator/__snapshots__/operation-coder.test.js.snap
index b9862b7..1a2bf24 100644
--- a/test/typescript-generator/__snapshots__/operation-coder.test.js.snap
+++ b/test/typescript-generator/__snapshots__/operation-coder.test.js.snap
@@ -1,15 +1,15 @@
 // Jest Snapshot v1, https://goo.gl/fbAQLP
 
 exports[`an OperationCoder generates a complex get operation 1`] = `
-"({ response }) => {
-  return response[200].random();
+"($) => {
+  return $.response[200].random();
 };
 "
 `;
 
 exports[`an OperationCoder generates a simple post operation 1`] = `
-"({ response }) => {
-  return response[200].random();
+"($) => {
+  return $.response[200].random();
 };
 "
 `;
diff --git a/test/typescript-generator/integration.test.js b/test/typescript-generator/integration.test.js
index f8a9220..7f088e5 100644
--- a/test/typescript-generator/integration.test.js
+++ b/test/typescript-generator/integration.test.js
@@ -47,10 +47,10 @@ describe("integration Test", () => {
     await account.finished();
 
     expect(account.contents()).toBe(
-      "export const HTTP_GET = () => {};\nexport const HTTP_POST = () => {};"
+      "\n\nexport const HTTP_GET = () => {};\n\nexport const HTTP_POST = () => {};"
     );
     expect(accountId.contents()).toBe(
-      "export const HTTP_GET = () => {};\nexport const HTTP_PUT = () => {};"
+      "\n\nexport const HTTP_GET = () => {};\n\nexport const HTTP_PUT = () => {};"
     );
   });
 });
diff --git a/test/typescript-generator/script.test.js b/test/typescript-generator/script.test.js
index 3b23c60..981a44a 100644
--- a/test/typescript-generator/script.test.js
+++ b/test/typescript-generator/script.test.js
@@ -147,7 +147,7 @@ describe("a Script", () => {
     ];
 
     expect(script.contents()).toBe(
-      'import { foo } from \'./foo.js;\nexport const bar = "Bar";\nexport default class {};'
+      'import { foo } from \'./foo.js;\n\nexport const bar = "Bar";\n\nexport default class {};'
     );
   });
 });