Skip to content

Commit

Permalink
🤖 Merge PR DefinitelyTyped#66229 Add @types/swipl-stdio by @tscpp
Browse files Browse the repository at this point in the history
* Add @types/swipl-stdio

* Remove boolean from ResultEntry
  • Loading branch information
tscpp authored and pull[bot] committed Dec 6, 2023
1 parent 3b1cebf commit ccc24ed
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 0 deletions.
153 changes: 153 additions & 0 deletions types/swipl-stdio/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// Type definitions for swipl-stdio 1.0
// Project: https://github.com/rla/node-swipl-stdio
// Definitions by: Elias Skogevall <https://github.com/tscpp>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped

/// <reference types="node" />

import { EventEmitter } from 'node:events';
import { ChildProcessWithoutNullStreams } from 'node:child_process';

export type ResultEntry = number | string | ResultList | ResultDict;
export type Result = Record<string, ResultEntry>;

export interface ResultList {
head: ResultEntry;
tail: ResultList | '[]';
}

export interface ResultDict {
tag: string;
content: Result;
}

declare class Deferred<T = unknown> {
promise: Promise<T>;
resolve: (this: Promise<T>, value: T) => void;
reject: (this: Promise<T>, reason?: string) => void;
}
export type { Deferred };

export type QueryStateString =
| (typeof QueryState)['FRESH']
| (typeof QueryState)['OPEN']
| (typeof QueryState)['WAITING']
| (typeof QueryState)['CLOSED'];

declare class QueryState {
state: QueryStateString;

setWaiting(): void;
setOpen(): void;
setClosed(): void;

isFresh(): boolean;
isOpen(): boolean;
isWaiting(): boolean;
isClosed(): boolean;

setState(state: QueryStateString): void;

static readonly FRESH: 'fresh';
static readonly OPEN: 'open';
static readonly WAITING: 'waiting';
static readonly CLOSED: 'closed';
}
export type { QueryState };

declare class QueuedQuery {
constructor(string: string, deferred: Deferred);
string: string;
deferred: Deferred;
}
export type { QueuedQuery };

declare class Query {
query: string;
engine: Engine;
deferred: Deferred | null;
state: QueryState;

next(): Promise<Result | false>;
close(): Promise<void>;
}
export type { Query };

export type EngineStateString =
| (typeof EngineState)['ACCEPTING']
| (typeof EngineState)['QUERY']
| (typeof EngineState)['WAITING']
| (typeof EngineState)['CLOSED'];

declare class EngineState extends EventEmitter {
state: EngineStateString;

isClosed(): boolean;
isAccepting(): boolean;
isQuery(): boolean;

setAccepting(): void;
setClosed(): void;
setQuery(): void;
setWaiting(): void;

setState(state: EngineStateString): void;

static readonly ACCEPTING: 'accepting';
static readonly QUERY: 'query';
static readonly WAITING: 'waiting';
static readonly CLOSED: 'closed';
}
export type { EngineState };

export class Engine {
swipl: ChildProcessWithoutNullStreams;
state: EngineState;
status: number;
query: Query | null;
queue: QueuedQuery[];
createQuery(string: string): Query | Promise<Query>;
call(string: string): Promise<Result | false>;
close(): void;
}

export type Term = List | Variable | Compound | Dict;
export type TermLike = Term | string | number | null | undefined | { toProlog(): string };

declare class List {
items: Term[];
constructor(items: readonly Term[]);
toProlog(): string;
}
export type { List };

declare class Variable {
name: string;
constructor(name: any);
toProlog(): string;
}
export type { Variable };

declare class Compound {
name: string;
args: Term[];
constructor(name: string, args: readonly Term[]);
toProlog(): string;
}
export type { Compound };

declare class Dict {
tag: string | Variable;
content: Record<string, string | number | boolean | Term>;
constructor(tag: any, content: any);
toProlog(): string;
}
export type { Dict };

export namespace term {
function list(items: readonly TermLike[]): List;
function variable(name: string): Variable;
function compound(name: string, args: readonly TermLike[]): Compound;
function dict(tag: string | Variable, content: object): Dict;
function serialize(term: TermLike): string;
}
31 changes: 31 additions & 0 deletions types/swipl-stdio/swipl-stdio-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Engine, term, ResultEntry } from 'swipl-stdio';

const engine = new Engine();

(async () => {
// Calling query using "engine.call"
const result = await engine.call('member(X, [1,2,3,4])');
if (result) {
// $ExpectType ResultEntry
result.X;
}

// Calling query using "engine.createQuery"
const query = await engine.createQuery('member(X, [1,2,3,4])');
try {
const result = await query.next();
if (result) {
// $ExpectType ResultEntry
result.X;
}
} finally {
await query.close();
}

engine.close();
})();

// Serialize

// $ExpectType string
const safe = term.serialize(term.compound('member', [term.variable('X'), term.list([1, 2, 3, 4])]));
23 changes: 23 additions & 0 deletions types/swipl-stdio/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"swipl-stdio-tests.ts"
]
}
1 change: 1 addition & 0 deletions types/swipl-stdio/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "extends": "@definitelytyped/dtslint/dt.json" }

0 comments on commit ccc24ed

Please sign in to comment.