aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/Scripting.ts
diff options
context:
space:
mode:
authorNathan-SR <144961007+Nathan-SR@users.noreply.github.com>2024-10-01 03:58:47 -0400
committerNathan-SR <144961007+Nathan-SR@users.noreply.github.com>2024-10-01 03:58:47 -0400
commit6d35629dd8f997208130981aac1daf36bc83b134 (patch)
tree7b7e3baac15e5f7b4fcb48d90372c48d4552ae4c /src/client/util/Scripting.ts
parentcf45abf8ada938caddb226c825166d4acdee3086 (diff)
parentba01c7376ed4a2b817a26a430faf4041524aef35 (diff)
Merge branch 'master' of https://github.com/brown-dash/Dash-Web into nathan-starter
Diffstat (limited to 'src/client/util/Scripting.ts')
-rw-r--r--src/client/util/Scripting.ts38
1 files changed, 18 insertions, 20 deletions
diff --git a/src/client/util/Scripting.ts b/src/client/util/Scripting.ts
index 5202e62a3..e00d71555 100644
--- a/src/client/util/Scripting.ts
+++ b/src/client/util/Scripting.ts
@@ -1,11 +1,7 @@
-/* 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 typescriptlib from 'type_decls.d';
import * as ts from 'typescript';
import { Doc, FieldType } from '../../fields/Doc';
import { RefField } from '../../fields/RefField';
@@ -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') {
@@ -93,7 +89,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 };
}
};
@@ -110,7 +106,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);
@@ -164,18 +160,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 + ' ');
}) ||
@@ -191,8 +188,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); // if already compiled, found is the result; cache set below
@@ -254,7 +252,7 @@ export function CompileScript(script: string, options: ScriptOptions = {}): Comp
const funcScript = `(function(${paramString})${reqTypes} { ${body} })`;
host.writeFile('file.ts', funcScript);
- if (typecheck) host.writeFile('node_modules/typescript/lib/lib.d.ts', typescriptlib.default);
+ if (typecheck) host.writeFile('node_modules/typescript/lib/lib.d.ts', typescriptlib);
const program = ts.createProgram(['file.ts'], {}, host);
const testResult = program.emit();
const outputText = host.readFile('file.js');