diff --git a/packages/string-templates/src/conversion/index.ts b/packages/string-templates/src/conversion/index.ts index d69576e940..faf65b6085 100644 --- a/packages/string-templates/src/conversion/index.ts +++ b/packages/string-templates/src/conversion/index.ts @@ -63,7 +63,7 @@ function buildList(parts: any[], value: any) { } function splitBySpace(layer: string) { - const parts = [] + const parts: string[] = [] let started = null, endChar = null, last = 0 diff --git a/packages/string-templates/src/helpers/date.ts b/packages/string-templates/src/helpers/date.ts index efcf516dfb..5b85aeedd4 100644 --- a/packages/string-templates/src/helpers/date.ts +++ b/packages/string-templates/src/helpers/date.ts @@ -27,11 +27,11 @@ dayjs.extend(dayjsTimezonePlugin) * https://github.com/helpers/helper-date */ -function isOptions(val) { +function isOptions(val: any) { return typeof val === "object" && typeof val.hash === "object" } -function isApp(thisArg) { +function isApp(thisArg: any) { return ( typeof thisArg === "object" && typeof thisArg.options === "object" && @@ -39,7 +39,7 @@ function isApp(thisArg) { ) } -function getContext(thisArg, locals, options) { +function getContext(thisArg: any, locals: any, options: any) { if (isOptions(thisArg)) { return getContext({}, locals, thisArg) } @@ -68,7 +68,7 @@ function getContext(thisArg, locals, options) { return context } -function initialConfig(str, pattern, options?) { +function initialConfig(str: any, pattern: any, options?: any) { if (isOptions(pattern)) { options = pattern pattern = null @@ -82,7 +82,7 @@ function initialConfig(str, pattern, options?) { return { str, pattern, options } } -function setLocale(str, pattern, options?) { +function setLocale(str: any, pattern: any, options?: any) { // if options is null then it'll get updated here const config = initialConfig(str, pattern, options) const defaults = { lang: "en", date: new Date(config.str) } @@ -93,7 +93,7 @@ function setLocale(str, pattern, options?) { dayjs.locale(opts.lang || opts.language) } -export const date = (str, pattern, options) => { +export const date = (str: any, pattern: any, options: any) => { const config = initialConfig(str, pattern, options) // if no args are passed, return a formatted date @@ -119,7 +119,7 @@ export const date = (str, pattern, options) => { return date.format(config.pattern) } -export const duration = (str, pattern, format) => { +export const duration = (str: any, pattern: any, format: any) => { const config = initialConfig(str, pattern) setLocale(config.str, config.pattern) diff --git a/packages/string-templates/src/helpers/external.ts b/packages/string-templates/src/helpers/external.ts index 8f0577a945..efb390ff8b 100644 --- a/packages/string-templates/src/helpers/external.ts +++ b/packages/string-templates/src/helpers/external.ts @@ -1,4 +1,4 @@ -import helpers from "@budibase/handlebars-helpers" +const helpers = require("@budibase/handlebars-helpers") import { date, duration } from "./date" import { HelperFunctionBuiltin } from "./constants" diff --git a/packages/string-templates/src/helpers/javascript.ts b/packages/string-templates/src/helpers/javascript.ts index b75c9f5ac1..c3833cd9a0 100644 --- a/packages/string-templates/src/helpers/javascript.ts +++ b/packages/string-templates/src/helpers/javascript.ts @@ -1,20 +1,23 @@ import { atob, isBackendService, isJSAllowed } from "../utilities" -import cloneDeep from "lodash.clonedeep" +import cloneDeep from "lodash/fp/clonedeep" import { LITERAL_MARKER } from "../helpers/constants" import { getJsHelperList } from "./list" // The method of executing JS scripts depends on the bundle being built. // This setter is used in the entrypoint (either index.js or index.mjs). -let runJS -export const setJSRunner = runner => (runJS = runner) +let runJS: (js: string, context: any) => any +export const setJSRunner = (runner: typeof runJS) => (runJS = runner) -export const removeJSRunner = () => (runJS = undefined) +export const removeJSRunner = () => { + runJS = undefined +} -let onErrorLog -export const setOnErrorLog = delegate => (onErrorLog = delegate) +let onErrorLog: (message: string) => void +export const setOnErrorLog = (delegate: typeof onErrorLog) => + (onErrorLog = delegate) // Helper utility to strip square brackets from a value -const removeSquareBrackets = value => { +const removeSquareBrackets = (value: string) => { if (!value || typeof value !== "string") { return value } @@ -28,7 +31,7 @@ const removeSquareBrackets = value => { // Our context getter function provided to JS code as $. // Extracts a value from context. -const getContextValue = (path, context) => { +const getContextValue = (path: string, context: any) => { let data = context path.split(".").forEach(key => { if (data == null || typeof data !== "object") { @@ -40,7 +43,7 @@ const getContextValue = (path, context) => { } // Evaluates JS code against a certain context -export function processJS(handlebars, context) { +export function processJS(handlebars: string, context: any) { if (!isJSAllowed() || (isBackendService() && !runJS)) { throw new Error("JS disabled in environment.") } @@ -53,7 +56,7 @@ export function processJS(handlebars, context) { // We clone the context to avoid mutation in the binding affecting real // app context. const sandboxContext = { - $: path => getContextValue(path, cloneDeep(context)), + $: (path: string) => getContextValue(path, cloneDeep(context)), helpers: getJsHelperList(), } diff --git a/packages/string-templates/src/helpers/list.ts b/packages/string-templates/src/helpers/list.ts index 7b25216976..010dbe18f3 100644 --- a/packages/string-templates/src/helpers/list.ts +++ b/packages/string-templates/src/helpers/list.ts @@ -20,7 +20,7 @@ const addedHelpers = { duration: duration, } -let helpers = undefined +let helpers: Record = undefined export function getJsHelperList() { if (helpers) { @@ -31,11 +31,12 @@ export function getJsHelperList() { for (let collection of Object.values(getExternalCollections())) { for (let [key, func] of Object.entries(collection)) { // Handlebars injects the hbs options to the helpers by default. We are adding an empty {} as a last parameter to simulate it - helpers[key] = (...props) => func(...props, {}) + helpers[key] = (...props: any) => func(...props, {}) } } - for (let key of Object.keys(addedHelpers)) { - helpers[key] = addedHelpers[key] + helpers = { + ...helpers, + addedHelpers, } for (const toRemove of helpersToRemoveForJs) { diff --git a/packages/string-templates/tsconfig.json b/packages/string-templates/tsconfig.json index 2fa760d011..585c3bf747 100644 --- a/packages/string-templates/tsconfig.json +++ b/packages/string-templates/tsconfig.json @@ -4,8 +4,8 @@ "declaration": true, "target": "es6", "moduleResolution": "node", + "noImplicitAny": true, "lib": ["dom"], - "allowJs": true, "outDir": "dist", "esModuleInterop": true, "types": ["node", "jest"],