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

Commit

Permalink
fix: typings
Browse files Browse the repository at this point in the history
  • Loading branch information
cdaringe committed Jan 18, 2021
1 parent 23cfc5e commit 2c338fb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
5 changes: 5 additions & 0 deletions src/factorio-meta/typedefs/libs/factorio-new-fns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
// @ts-ignore
declare const log: (ls: LocalisedString) => void;

// @ts-ignore
declare const print: (v: any) => void;

/** This function allows printing LocalisedStrings to stdout without polluting the logfile. This is primarily useful for communicating with external tools that launch Factorio as a child process. */
declare const localised_print: typeof log;

declare const table_size: <T extends {}>(v: T) => number;

declare const pairs: <K, V, M extends Map<K, V>>(v: M) => [K, V][];
14 changes: 9 additions & 5 deletions src/factorio-meta/typedefs/libs/serpent.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/** noSelfInFile **/
type SerpentOptions = {
/** indentation; triggers long multi-line output. */
indent: string;
Expand Down Expand Up @@ -49,11 +50,14 @@ type SerpentOptions = {
/** name; triggers full serialization with self-ref section. */
name: string;
};
declare const serpent: {
declare const serpent: Serpent;

/** @noSelf **/
interface Serpent {
/** full serialization; sets name, compact and sparse options; */
dump(tbl: any, options?: SerpentOptions): string;
dump(tbl: any, options?: Partial<SerpentOptions>): string;
/** single line pretty printing, no self-ref section; sets sortkeys and comment options; */
line(tbl: any, options?: SerpentOptions): string;
line(tbl: any, options?: Partial<SerpentOptions>): string;
/** multi-line indented pretty printing, no self-ref section; sets indent, sortkeys, and comment options. */
block(tbl: any, options?: SerpentOptions): string;
};
block(tbl: any, options?: Partial<SerpentOptions>): string;
}
23 changes: 19 additions & 4 deletions src/ir/printer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Type } from "./ir";
import { cls, optional, testIsType, Type } from "./ir";

export const printInner = (t: Type): string => {
switch (t.__type) {
Expand Down Expand Up @@ -26,6 +26,11 @@ export const printInner = (t: Type): string => {
if (t.isVariadic) {
return `...args: ${print(t.type)}[]`;
}
let innerType = t.type;
if (testIsType(innerType, optional)) {
t.isOptional = true;
innerType.type;
}
return `${t.name}${t.isOptional ? "?" : ""}: ${print(t.type)}`;
case "property":
return `${t.isReadonly ? "readonly " : ""}"${t.name}": ${print(t.type)}`;
Expand All @@ -35,11 +40,21 @@ export const printInner = (t: Type): string => {
return `${print(prop)};\n`;
})
.join(" ")} }`;
if (!t.isRoot) return inner;
return `interface ${t.name} \n${inner} `;
if (!t.isRoot) return `/** @noSelf **/\n${inner}`;
return `/** @noSelf **/\ninterface ${t.name} \n${inner} `;
case "class":
case "struct":
return `interface ${t.name} { \n ${t.members
let inhertsStr = "";
if (testIsType(t, cls)) {
if (t.inherits?.length) {
inhertsStr = ` extends ${t.inherits
.map((sym) => sym.text)
.join(", ")} `;
}
}
return `/** @noSelf **/\ninterface ${
t.name
} ${inhertsStr} { \n ${t.members
.map((member) => {
return `${print(member)};\n`;
})
Expand Down

0 comments on commit 2c338fb

Please sign in to comment.