Skip to content
This repository has been archived by the owner on Nov 4, 2023. It is now read-only.

Commit

Permalink
feat: mediocre, semi stable json6
Browse files Browse the repository at this point in the history
  • Loading branch information
cdaringe committed Jan 11, 2021
1 parent 7cbc8e9 commit 4ff33c6
Show file tree
Hide file tree
Showing 18 changed files with 4,766 additions and 241 deletions.
1 change: 0 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
cached
node_modules
.factorio-ts-cache
*.html
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"request": "launch",
"name": "Debug AVA test file",
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/ava",
"runtimeArgs": ["${file}"],
"args": ["${file}"],
"outputCapture": "std"
// "skipFiles": ["<node_internals>/**/*.js"]
}
Expand Down
1 change: 1 addition & 0 deletions rad.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-ignore
import type { Task, Tasks } from "https://deno.land/x/rad/src/mod.ts";

const generateApiJson: Task = `docker run --rm -v $PWD:/app cdaringe/factorio-api-scraper:latest`;
Expand Down
3 changes: 2 additions & 1 deletion src/factorio-meta/factorio-lua-json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,11 @@ export const fromLuaType = (ltype_: string): JSONSchema6 => {
}
if (!definitionTypes.includes(ltype)) {
definitionTypes.push(ltype.trim());
console.warn(`:::: Adding type ${ltype}`);
// console.warn(`:::: Adding type ${ltype}`);
}
const ref: JSONSchema6 = {};
Object.defineProperty(ref, "$ref", {
enumerable: true,
get() {
if (bigBadHacks.isReadingRefs) return `#/definitions/${ltype}`;
// return a def'n that has no children. such a weak defn has no children,
Expand Down
88 changes: 88 additions & 0 deletions src/ir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* intermediate representation
*/

export type WithDescription = { description: string };
export type WithType<T> = { __type: T };

export type Primitive = "boolean" | "number" | "string" | "null";

export const bool = () => "boolean";
export const str = () => "string";
export const nil = () => "null";
export const num = () => "number";

export type IRMap<K, V> = { keyType: K; valueType: V } & WithType<"map">;

export const map = <K, V>(keyType: K, valueType: V): IRMap<K, V> => ({
keyType,
valueType,
__type: "map",
});

export type IRArray<V> = { valueType: V } & WithType<"array">;

export const arr = <V>(valueType: V): IRArray<V> => ({
valueType,
__type: "array",
});

export type Collection<K, V> = IRArray<V> | IRMap<K, V>;

export type Type<K = unknown, V = unknown> = Primitive | Collection<K, V>;

export type Param = {
name: string;
type: Type;
isOptional?: boolean;
} & WithDescription &
WithType<"param">;

export const param = (
par: Pick<Param, "name" | "type" | "isOptional" | "description">
): Param => ({
__type: "param",
...par,
});

export type Function = {
name: string;
parameters: Type[];
return: Type;
} & WithDescription &
WithType<"function">;

export const fn = (
fun: Pick<Function, "name" | "parameters" | "return" | "description">
): Function => ({
__type: "function",
...fun,
});

export type Property = {
name: string;
type: Type;
isReadonly?: boolean;
} & WithDescription &
WithType<"property">;

export const property = (
prop: Pick<Property, "name" | "type" | "isReadonly" | "description">
): Property => ({
__type: "property",
...prop,
});

export type ClassMember = Property | Function;

export type Class = { members: IRMap<string, ClassMember> } & WithDescription &
WithType<"class">;

export const cls = (
clz: {
members: IRMap<string, ClassMember>;
} & Pick<Class, "description">
): Class => ({
__type: "class",
...clz,
});
16 changes: 12 additions & 4 deletions src/langs/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Exploit the https://www.npmjs.com/package/json-schema-to-typescript
* tsType for usable TS definitions
*/
import { JSONSchema6 } from "json-schema";
import type { JSONSchema6, JSONSchema6Object } from "json-schema";

declare module "json-schema" {
interface JSONSchema6 {
Expand Down Expand Up @@ -84,6 +84,7 @@ export const withType = {
const isRO = opts.parseMode
? !mode.some((rw: string) => !!rw.match(/w/i))
: false;
if (!schema.properties?.type) debugger;
return `${getDocBlock(schema)}${
isRO ? "readonly " : ""
}"${name}": ${toTsType(schema.properties?.type as JSONSchema6)}`;
Expand All @@ -101,7 +102,7 @@ export const withType = {
if (!retSchema) {
throw new Error("missing return value");
}
return `${getDocBlock(schema)}(${args}) => ${toTsType(retSchema)}`;
return `(${args}) => ${toTsType(retSchema)}`;
},
class: (schema: JSONSchema6, opts: { asStruct?: boolean }) => {
const members = schema.properties!.members as JSONSchema6;
Expand All @@ -123,15 +124,22 @@ export const withType = {
});
});
const methodStrs = methodNames.sort(sort).map((name) => {
return `${name}: ${withType.method(membersProperties[name])}`;
const mSchema = membersProperties[name];
return `${getDocBlock(mSchema)}${name}: ${withType.method(mSchema)}`;
});
const inheritsTypes: string[] =
(schema.properties as any)?.inherits?.items.map((it: any) => it?.const) ||
[];
const inheritsStr = inheritsTypes.length
? ` & ${inheritsTypes.join(" & ")}`
: "";
return `
{
${propStrs.length ? "/* properties */" : ""}
${propStrs.map((it) => ` ${it};`).join("\n")}
${methodStrs.length ? "/* methods */" : ""}
${methodStrs.map((it) => ` ${it};`).join("\n")}
}`;
}${inheritsStr}`;
},
};

0 comments on commit 4ff33c6

Please sign in to comment.