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

feat(*): Add Typescript type definitions #786

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions packages/ergo-compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
"directory": "packages/ergo-compiler"
},
"main": "index.js",
"typings": "types/index.d.ts",
"scripts": {
"pretest": "npm run lint",
"lint": "eslint .",
"postlint": "npm run licchk",
"licchk": "license-check",
"test": "mocha",
"test:cov": "nyc npm run test"
"test:cov": "nyc npm run test",
"types:check": "tsc -p types/tsconfig.json"
},
"contributors": [
{
Expand All @@ -39,6 +41,7 @@
"winston": "3.2.1"
},
"devDependencies": {
"@types/node": "14.14.6",
"chai": "4.2.0",
"chai-as-promised": "7.1.1",
"chai-things": "0.2.0",
Expand All @@ -48,7 +51,8 @@
"mocha": "5.2.0",
"nyc": "13.3.0",
"sinon": "6.3.5",
"sinon-chai": "3.2.0"
"sinon-chai": "3.2.0",
"typescript": "4.0.5"
},
"license-check-config": {
"src": [
Expand Down Expand Up @@ -89,4 +93,4 @@
"lines": 93
},
"gitHead": "bf65386e8590dff56f8b9bc0bb657263ff049437"
}
}
261 changes: 261 additions & 0 deletions packages/ergo-compiler/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
/// <reference types="node" />
/// <reference types="winston" />
/// <reference types="@accordproject/concerto-core" />

import { Logger, LeveledLogMethod } from 'winston';
import { BaseFileException, Factory, Introspector, ModelManager, Serializer } from '@accordproject/concerto-core'

// Winston logger

declare module 'winston' {
interface Logger {
setup(fn: (process: any, env: string, logDir: string) => void): void;
entry: LeveledLogMethod;
exit: LeveledLogMethod;
}
}

// Utils

declare namespace Util {
function momentToJson(): any;
function setCurrentTime(currentTime: string): any;
}

// Version

declare const version: string

// Exceptions

declare interface FileLocation {
start: { line: number, column: number };
end: { line: number, column: number };
}

declare class CompilerException extends BaseFileException {
constructor(message: string, fileLocation?: FileLocation, fullMessage?: string, fileName?: string, component?: string);
}

declare class TypeException extends CompilerException {
constructor(message: string, fileLocation?: FileLocation, fullMessage?: string, fileName?: string, component?: string);
}

// Target

declare type Target = 'cicero' | 'es5' | 'es6' | 'java';

// Source

declare interface Source {
name: string;
content: string;
}

// Ergo Input
declare interface Input {
$class: string;
}

// AP Model Manager

declare abstract class APModelManager extends ModelManager {
getModels(): { name: string; content: string; }[];
}

// Function Declaration

declare class FunctionDeclaration {
constructor(
modelManager: ModelManager,
language: string,
name: string,
visibility: string,
returnType: string,
throws: string,
parameterNames: string[],
parameterTypes: string[],
decorators: string[],
functionText: string
);
getFunctionText(): string;
getThrows(): string;
getLanguage(): string;
getDecorators(): string[];
getVisibility(): string;
getReturnType(): string;
getName(): string;
getParameterNames(): string[];
getParameterTypes(): string[];
}

// Script

declare class Script {
constructor(
modelManager: ModelManager,
identifier: string,
language: string,
contents: string,
contractName: string
);
getIdentifier(): string;
getContractName(): string;
getLanguage(): string;
getContents(): string;
getFunctionDeclarations(): FunctionDeclaration[];
getTokens(): any[];
}

// ScriptManager

declare interface ScriptManagerOptions {
warnings?: boolean
}

declare class ScriptManager {
constructor(target: Target, modelManager: ModelManager, options: ScriptManagerOptions);
changeTarget(target: Target, recompile: boolean): void;
createScript(identifier: string, language: string, contents: string): Script;
modifyScript(identifier: string, language: string, contents: string): void;
addTemplateFile(templateFile: string, fileName: string): void
addScript(script: Script): void;
updateScript(script: Script): void;
deleteScript(identifier: string): void;
getScripts(): Script[];
getAllScripts(): Script[];
getCombinedScripts(): string;
getTargetKind(target: Target): '.js' | '.ergo' | '.java';
getScriptsForTarget(target: Target): Script[];
getLogic(): Source[];
clearScripts(): void;
private getScript(identifier: string): Script;
private getCompiledScript(): Script;
private getCompiledJavaScript(): string;
getScriptIdentifiers(): string[];
compileLogic(force: boolean): Script;
allFunctionDeclarations(): FunctionDeclaration[];
hasFunctionDeclaration(name: string): void;
hasDispatch(): void;
hasInit(): void;

static _throwCompilerException(error: any): void;
}

// Logic Manager

declare interface LogicManagerOptions {
warnings?: boolean
}

declare class LogicManager {
constructor(target: Target, options: LogicManagerOptions);
getTarget(): Target;
setTarget(target: Target, recompile: boolean): void;
setContractName(contractName: string): void;
getContractName(): string;
private getDispatchCall(): string;
private getInvokeCall(clauseName: string): string
getIntrospector(): Introspector;
getFactory(): Factory;
getSerializer(): Serializer;
getScriptManager(): ScriptManager;
getModelManager(): ModelManager;
addLogicFile(logicFile: string, fileName: string): void;
addTemplateFile(modelFile: string, fileName: string): void;
addModelFile(modelFile: string, fileName: string): void;
addModelFiles(modelFiles: string[], modelFileNames?: string[]): void;
validateModelFiles(): void;
registerCompiledLogicSync(): void;
compileLogicSync(force: boolean): Script;
compileLogic(force: boolean): Promise<void>;
addErgoBuiltin(): void;
validateInput(input: any): any;
validateContract(contract: any, options: any): any;
validateInputRecord(input: any): any;
validateOutput(output: any): any;
validateOutputArray(outputArray: any[]): any[];
updateModel(content: string, name: string): void;
updateLogic(content: string, name: string): void;
}

// Compiler

declare class Compiler {
static parseCTOtoJSON(ctoContent: string): any;
static contractCallName(contractName: string): string;
static contractCallNamePromise(contractName: string): string;

static compileToJavaScript(
ergoSources: Source[],
ctoSources: Source[],
target: string,
link: boolean,
warnings: boolean
): string;

static compile(
ergoSources: Source[],
ctoSources: Source[],
target: string,
link: boolean,
warnings: boolean
): any;

static ergoErrorToString(error: any): string;
static ergoVerboseErrorToString(error: any): string;
static availableTargets(): string[];
static isValidTarget(target: string): boolean;
}

// File Loader

declare class FileLoader {
static loadZipFileContents(zip: any, path: string, json?: boolean, required?: boolean): Promise<string>;
static loadZipFilesContents(zip: any, regex: RegExp): Promise<any[]>;
static loadZipFileBuffer(zip: any, path: string, required?: boolean): Promise<Buffer>;
static loadFileContents(path: string, fileName: string, json?: boolean, required?: boolean): Promise<string>;
static loadFileBuffer(path: string, fileName: string, required?: boolean): Promise<Buffer>;
static loadFilesContents(path: string, regex: RegExp): Promise<any[]>
static normalizeNLs(input: string): string;
}

// Ergo Loader

declare class ErgoLoader {
static fromDirectory(path: string, options: LogicManagerOptions): Promise<LogicManager>;
static fromZip(buffer: Buffer, options: LogicManagerOptions): Promise<LogicManager>;
static fromFiles(files: string[], options: LogicManagerOptions): Promise<LogicManager>;
}

// Exports

export {
TypeException,
CompilerException,
APModelManager,
ScriptManager,
LogicManager,
Compiler,
FileLoader,
ErgoLoader,
Util,
Logger,
version,
}

declare module '@accordproject/ergo-compiler' {
export {
TypeException,
CompilerException,
APModelManager,
ScriptManager,
LogicManager,
Compiler,
FileLoader,
ErgoLoader,
Util,
Logger,
version,
}
}
6 changes: 6 additions & 0 deletions packages/ergo-compiler/types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"moduleResolution": "node",
"noEmit": true,
}
}
8 changes: 6 additions & 2 deletions packages/ergo-engine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"directory": "packages/ergo-engine"
},
"main": "index.js",
"typings": "types/index.d.ts",
"scripts": {
"webpack": "webpack --config webpack.config.js --mode production",
"prepublishOnly": "npm run webpack",
Expand All @@ -16,7 +17,8 @@
"postlint": "npm run licchk",
"licchk": "license-check",
"test": "mocha",
"test:cov": "nyc npm run test"
"test:cov": "nyc npm run test",
"types:check": "tsc -p types/tsconfig.json"
},
"contributors": [
{
Expand All @@ -42,6 +44,7 @@
"@babel/cli": "7.1.5",
"@babel/core": "7.1.6",
"@babel/preset-env": "7.1.6",
"@types/node": "14.14.6",
"archiver": "2.1.1",
"babel-core": "7.0.0-bridge.0",
"babel-loader": "8.0.4",
Expand All @@ -55,6 +58,7 @@
"mocha": "5.2.0",
"nyc": "13.3.0",
"raw-loader": "0.5.1",
"typescript": "4.0.5",
"webpack": "4.16.3",
"webpack-cli": "3.1.0"
},
Expand Down Expand Up @@ -93,4 +97,4 @@
"lines": 93
},
"gitHead": "bf65386e8590dff56f8b9bc0bb657263ff049437"
}
}
Loading