aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/Scripting.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/client/util/Scripting.ts')
-rw-r--r--src/client/util/Scripting.ts34
1 files changed, 16 insertions, 18 deletions
diff --git a/src/client/util/Scripting.ts b/src/client/util/Scripting.ts
index 6948469cc..133f8f2ce 100644
--- a/src/client/util/Scripting.ts
+++ b/src/client/util/Scripting.ts
@@ -1,10 +1,6 @@
-/* eslint-disable import/no-unresolved */
-/* eslint-disable import/no-webpack-loader-syntax */
// export const ts = (window as any).ts;
-// // @ts-ignore
// import * as typescriptlib from '!!raw-loader!../../../node_modules/typescript/lib/lib.d.ts'
// import * as typescriptes5 from '!!raw-loader!../../../node_modules/typescript/lib/lib.es5.d.ts'
-// eslint-disable-next-line node/no-unpublished-import
import * as typescriptlib from '!!raw-loader!./type_decls.d';
import * as ts from 'typescript';
import { Doc, FieldType } from '../../fields/Doc';
@@ -16,13 +12,13 @@ export { ts };
export interface ScriptSuccess {
success: true;
- result: any;
+ result: unknown;
}
export interface ScriptError {
success: false;
- error: any;
- result: any;
+ error: unknown;
+ result: unknown;
}
export type ScriptResult = ScriptSuccess | ScriptError;
@@ -34,12 +30,12 @@ export interface CompiledScript {
readonly originalScript: string;
// eslint-disable-next-line no-use-before-define
readonly options: Readonly<ScriptOptions>;
- run(args?: { [name: string]: any }, onError?: (res: any) => void, errorVal?: any): ScriptResult;
+ run(args?: { [name: string]: unknown }, onError?: (res: string) => void, errorVal?: unknown): ScriptResult;
}
export interface CompileError {
compiled: false;
- errors: any[];
+ errors: ts.Diagnostic[];
}
export type CompileResult = CompiledScript | CompileError;
@@ -51,7 +47,7 @@ export function isCompileError(toBeDetermined: CompileResult): toBeDetermined is
}
// eslint-disable-next-line no-use-before-define
-function Run(script: string | undefined, customParams: string[], diagnostics: any[], originalScript: string, options: ScriptOptions): CompileResult {
+function Run(script: string | undefined, customParams: string[], diagnostics: ts.Diagnostic[], originalScript: string, options: ScriptOptions): CompileResult {
const errors = diagnostics.filter(diag => diag.category === ts.DiagnosticCategory.Error);
if ((options.typecheck !== false && errors.length) || !script) {
return { compiled: false, errors };
@@ -74,8 +70,8 @@ function Run(script: string | undefined, customParams: string[], diagnostics: an
if (!compiledFunction) return { compiled: false, errors };
const { capturedVariables = {} } = options;
// eslint-disable-next-line default-param-last
- const run = (args: { [name: string]: any } = {}, onError?: (e: any) => void, errorVal?: any): ScriptResult => {
- const argsArray: any[] = [];
+ const run = (args: { [name: string]: unknown } = {}, onError?: (e: string) => void, errorVal?: ts.Diagnostic): ScriptResult => {
+ const argsArray: unknown[] = [];
// eslint-disable-next-line no-restricted-syntax
for (const name of customParams) {
if (name !== 'this') {
@@ -94,7 +90,7 @@ function Run(script: string | undefined, customParams: string[], diagnostics: an
return { success: true, result };
} catch (error) {
batch?.end();
- onError?.(script + ' ' + error);
+ onError?.(script + ' ' + (error as string).toString());
return { success: false, error, result: errorVal };
}
};
@@ -111,7 +107,7 @@ class ScriptingCompilerHost {
files: File[] = [];
// getSourceFile(fileName: string, languageVersion: ts.ScriptTarget, onError?: ((message: string) => void) | undefined, shouldCreateNewSourceFile?: boolean | undefined): ts.SourceFile | undefined {
- getSourceFile(fileName: string, languageVersion: any /* , onError?: ((message: string) => void) | undefined, shouldCreateNewSourceFile?: boolean | undefined */): any | undefined {
+ getSourceFile(fileName: string, languageVersion: ts.ScriptTarget | ts.CreateSourceFileOptions /* , onError?: ((message: string) => void) | undefined, shouldCreateNewSourceFile?: boolean | undefined */): ts.SourceFile | undefined {
const contents = this.readFile(fileName);
if (contents !== undefined) {
return ts.createSourceFile(fileName, contents, languageVersion, true);
@@ -165,18 +161,19 @@ export interface ScriptOptions {
requiredType?: string; // does function required a typed return value
addReturn?: boolean; // does the compiler automatically add a return statement
params?: { [name: string]: string }; // list of function parameters and their types
- capturedVariables?: { [name: string]: Doc | number | string | boolean }; // list of captured variables
+ capturedVariables?: { [name: string]: Doc | number | string | boolean | undefined }; // list of captured variables
typecheck?: boolean; // should the compiler perform typechecking
editable?: boolean; // can the script edit Docs
traverser?: TraverserParam;
transformer?: Transformer; // does the editor display a text label by each document that can be used as a captured document reference
- globals?: { [name: string]: any };
+ globals?: { [name: string]: unknown };
}
// function forEachNode(node:ts.Node, fn:(node:any) => void);
function forEachNode(node: ts.Node, onEnter: Traverser, onExit?: Traverser, indentation = '') {
return (
onEnter(node, indentation) ||
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
ts.forEachChild(node, (n: any) => {
forEachNode(n, onEnter, onExit, indentation + ' ');
}) ||
@@ -187,8 +184,9 @@ function forEachNode(node: ts.Node, onEnter: Traverser, onExit?: Traverser, inde
export function CompileScript(script: string, options: ScriptOptions = {}): CompileResult {
const captured = options.capturedVariables ?? {};
const signature = Object.keys(captured).reduce((p, v) => {
- const formatCapture = (obj: any) => `${v}=${obj instanceof RefField ? 'XXX' : obj.toString()}`;
- if (captured[v] instanceof Array) return p + (captured[v] as any).map(formatCapture);
+ const formatCapture = (obj: FieldType | undefined) => `${v}=${obj instanceof RefField ? 'XXX' : obj?.toString()}`;
+ const captureVal = captured[v];
+ if (captureVal instanceof Array) return p + captureVal.map(formatCapture);
return p + formatCapture(captured[v]);
}, '');
const found = ScriptField.GetScriptFieldCache(script + ':' + signature);