Skip to content

Commit

Permalink
Merge pull request #542 from pmcelhaney/fix-bugs-found-from-github
Browse files Browse the repository at this point in the history
Fix bugs found from GitHub
  • Loading branch information
pmcelhaney committed Sep 1, 2023
2 parents 63cfb99 + 42b72e6 commit 3de1c2b
Show file tree
Hide file tree
Showing 11 changed files with 229 additions and 165 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-eyes-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"counterfact": patch
---

fix a few edge cases found when running against Github's extensive OpenAPI description: https://github.com/github/rest-api-description
6 changes: 5 additions & 1 deletion src/typescript-generator/context-coder.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ class Context {
return "new Context()";
}

script.repository.get(script.path).exportDefault(this);
const parentPath = nodePath
.normalize(nodePath.join(script.path, "../../$.context.ts"))
.replaceAll("\\", "/");

script.repository.get(parentPath).exportDefault(this);

return { raw: 'export { default } from "../$.context.mjs"' };
}
Expand Down
4 changes: 0 additions & 4 deletions src/typescript-generator/printers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,3 @@ export function printObject(entries) {
.map(([key, value]) => `"${key}": ${value}`)
.join(",\n")}\n}`;
}

export function printArray(items) {
return `[\n${items.join(",\n")}\n]`;
}
16 changes: 14 additions & 2 deletions src/typescript-generator/schema-type-coder.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class SchemaTypeCoder extends Coder {
data.required?.includes(name) || property.data.required;
const optionalFlag = isRequired ? "" : "?";

return `${name}${optionalFlag}: ${new SchemaTypeCoder(property).write(
return `"${name}"${optionalFlag}: ${new SchemaTypeCoder(property).write(
script,
)}`;
});
Expand All @@ -55,6 +55,18 @@ export class SchemaTypeCoder extends Coder {
)}>`;
}

writePrimitive(value) {
if (typeof value === "string") {
return `"${value}"`;
}

if (value === null) {
return "null";
}

return value;
}

writeType(script, type) {
if (type === "object") {
return this.objectSchema(script);
Expand Down Expand Up @@ -95,7 +107,7 @@ export class SchemaTypeCoder extends Coder {

writeEnum(script, requirement) {
return requirement.data
.map((item) => (typeof item === "string" ? `"${item}"` : item))
.map((item) => this.writePrimitive(item))
.join(" | ");
}

Expand Down
10 changes: 5 additions & 5 deletions src/typescript-generator/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class Script {
this.repository = repository;
this.exports = new Map();
this.imports = new Map();
this.externalImports = new Map();
this.externalImport = new Map();
this.cache = new Map();
this.typeCache = new Map();
this.path = path;
Expand Down Expand Up @@ -125,7 +125,7 @@ export class Script {
}

importExternal(name, modulePath, isType = false) {
this.externalImports.set(name, { isType, modulePath });
this.externalImport.set(name, { isType, modulePath });

return name;
}
Expand All @@ -150,9 +150,9 @@ export class Script {
);
}

externalImportsStatements() {
externalImportStatements() {
return Array.from(
this.externalImports,
this.externalImport,
([name, { isDefault, isType, modulePath }]) =>
`import${isType ? " type" : ""} ${
isDefault ? name : `{ ${name} }`
Expand Down Expand Up @@ -199,7 +199,7 @@ export class Script {
contents() {
return prettier.format(
[
this.externalImportsStatements().join("\n"),
this.externalImportStatements().join("\n"),
this.importStatements().join("\n"),
"\n\n",
this.exportStatements().join("\n\n"),
Expand Down

This file was deleted.

Loading

0 comments on commit 3de1c2b

Please sign in to comment.