diff options
author | bobzel <zzzman@gmail.com> | 2024-05-19 00:05:18 -0400 |
---|---|---|
committer | bobzel <zzzman@gmail.com> | 2024-05-19 00:05:18 -0400 |
commit | 38742d5f491ed5232a381da63e126b609cf14aad (patch) | |
tree | a723b0c9dfffa717b70df383d21fb3b14567308f /src/client/util/Scripting.ts | |
parent | a3784cd3ab990d8016b1168eb0cbf7e9a2f22301 (diff) | |
parent | 0b451af28e5aef6b749da61e8a9fcd0a840789ac (diff) |
Merge branch 'restoringEslint' into aisosa-starter
Diffstat (limited to 'src/client/util/Scripting.ts')
-rw-r--r-- | src/client/util/Scripting.ts | 63 |
1 files changed, 32 insertions, 31 deletions
diff --git a/src/client/util/Scripting.ts b/src/client/util/Scripting.ts index 422e708bc..6948469cc 100644 --- a/src/client/util/Scripting.ts +++ b/src/client/util/Scripting.ts @@ -1,15 +1,17 @@ +/* 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' -// // @ts-ignore // import * as typescriptes5 from '!!raw-loader!../../../node_modules/typescript/lib/lib.es5.d.ts' -// @ts-ignore +// eslint-disable-next-line node/no-unpublished-import import * as typescriptlib from '!!raw-loader!./type_decls.d'; import * as ts from 'typescript'; -import { Doc, Field } from '../../fields/Doc'; +import { Doc, FieldType } from '../../fields/Doc'; import { RefField } from '../../fields/RefField'; import { ScriptField } from '../../fields/ScriptField'; -import { scriptingGlobals, ScriptingGlobals } from './ScriptingGlobals'; +import { ScriptingGlobals, scriptingGlobals } from './ScriptingGlobals'; + export { ts }; export interface ScriptSuccess { @@ -30,6 +32,7 @@ export type ScriptParam = { [name: string]: string }; export interface CompiledScript { readonly compiled: true; 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; } @@ -47,6 +50,7 @@ export function isCompileError(toBeDetermined: CompileResult): toBeDetermined is return false; } +// eslint-disable-next-line no-use-before-define function Run(script: string | undefined, customParams: string[], diagnostics: any[], originalScript: string, options: ScriptOptions): CompileResult { const errors = diagnostics.filter(diag => diag.category === ts.DiagnosticCategory.Error); if ((options.typecheck !== false && errors.length) || !script) { @@ -60,6 +64,7 @@ function Run(script: string | undefined, customParams: string[], diagnostics: an // let params: any[] = [Docs, ...fieldTypes]; const compiledFunction = (() => { try { + // eslint-disable-next-line no-new-func return new Function(...paramNames, `return ${script}`); } catch (e) { console.log(e); @@ -68,20 +73,17 @@ 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[] = []; + // eslint-disable-next-line no-restricted-syntax for (const name of customParams) { - if (name === 'this') { - continue; - } - if (name in args) { - argsArray.push(args[name]); - } else { - argsArray.push(capturedVariables[name]); + if (name !== 'this') { + argsArray.push(name in args ? args[name] : capturedVariables[name]); } } const thisParam = args.this || capturedVariables.this; - let batch: { end(): void } | undefined = undefined; + let batch: { end(): void } | undefined; try { if (!options.editable) { batch = Doc.MakeReadOnly(); @@ -109,7 +111,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: any /* , onError?: ((message: string) => void) | undefined, shouldCreateNewSourceFile?: boolean | undefined */): any | undefined { const contents = this.readFile(fileName); if (contents !== undefined) { return ts.createSourceFile(fileName, contents, languageVersion, true); @@ -118,11 +120,11 @@ class ScriptingCompilerHost { } // getDefaultLibFileName(options: ts.CompilerOptions): string { - getDefaultLibFileName(options: any): string { + getDefaultLibFileName(/* options: any */): string { return 'node_modules/typescript/lib/lib.d.ts'; // No idea what this means... } writeFile(fileName: string, content: string) { - const file = this.files.find(file => file.fileName === fileName); + const file = this.files.find(f => f.fileName === fileName); if (file) { file.content = content; } else { @@ -145,7 +147,7 @@ class ScriptingCompilerHost { return this.files.some(file => file.fileName === fileName); } readFile(fileName: string): string | undefined { - const file = this.files.find(file => file.fileName === fileName); + const file = this.files.find(f => f.fileName === fileName); if (file) { return file.content; } @@ -157,7 +159,7 @@ export type Traverser = (node: ts.Node, indentation: string) => boolean | void; export type TraverserParam = Traverser | { onEnter: Traverser; onLeave: Traverser }; export type Transformer = { transformer: ts.TransformerFactory<ts.Node>; - getVars?: () => { [name: string]: Field }; + getVars?: () => { [name: string]: FieldType }; }; export interface ScriptOptions { requiredType?: string; // does function required a typed return value @@ -193,7 +195,6 @@ export function CompileScript(script: string, options: ScriptOptions = {}): Comp if (found) return found as CompiledScript; const { requiredType = '', addReturn = false, params = {}, capturedVariables = {}, typecheck = true } = options; if (options.params && !options.params.this) options.params.this = Doc.name; - if (options.params && !options.params.self) options.params.self = Doc.name; if (options.globals) { ScriptingGlobals.setScriptingGlobals(options.globals); } @@ -210,12 +211,13 @@ export function CompileScript(script: string, options: ScriptOptions = {}): Comp const newCaptures = options.transformer.getVars?.(); if (Object.keys(newCaptures ?? {}).length) { // tslint:disable-next-line: prefer-object-spread - //options.capturedVariables = Object.assign(capturedVariables, newCaptures!) as any; + // options.capturedVariables = Object.assign(capturedVariables, newCaptures!) as any; - const transformed = result.transformed; + const { transformed } = result; const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed, }); + // eslint-disable-next-line no-param-reassign script = printer.printFile(transformed[0].getSourceFile()); } result.dispose(); @@ -224,19 +226,23 @@ export function CompileScript(script: string, options: ScriptOptions = {}): Comp if ('this' in params || 'this' in capturedVariables) { paramNames.push('this'); } + // eslint-disable-next-line no-restricted-syntax for (const key in params) { - if (key === 'this') continue; - paramNames.push(key); + if (key !== 'this') { + paramNames.push(key); + } } const paramList = paramNames.map(key => { const val = params[key]; return `${key}: ${val}`; }); + // eslint-disable-next-line no-restricted-syntax for (const key in capturedVariables) { - if (key === 'this') continue; - const val = capturedVariables[key]; - paramNames.push(key); - paramList.push(`${key}: ${typeof val === 'object' ? Object.getPrototypeOf(val).constructor.name : typeof val}`); + if (key !== 'this') { + const val = capturedVariables[key]; + paramNames.push(key); + paramList.push(`${key}: ${typeof val === 'object' ? Object.getPrototypeOf(val).constructor.name : typeof val}`); + } } const paramString = paramList.join(', '); const body = addReturn && !script.startsWith('{ return') ? `return ${script};` : script; @@ -259,8 +265,3 @@ export function CompileScript(script: string, options: ScriptOptions = {}): Comp !signature.includes('XXX') && ScriptField._scriptFieldCache.set(script + ':' + signature, result as CompiledScript); return result; } - -ScriptingGlobals.add(CompileScript); -ScriptingGlobals.add(function runScript(doc: Doc, script: ScriptField) { - return script?.script.run({ this: doc }).result; -}); |