diff --git a/packages/string-templates/rollup.config.js b/packages/string-templates/rollup.config.js index cb17fd893f..8b5f7a32e4 100644 --- a/packages/string-templates/rollup.config.js +++ b/packages/string-templates/rollup.config.js @@ -18,7 +18,7 @@ export default [ file: "./dist/bundle.cjs", }, plugins: [ - typescript({ tsconfig: "tsconfig.build.json" }), + typescript({ tsconfig: "tsconfig.json" }), resolve({ preferBuiltins: true, browser: true, diff --git a/packages/string-templates/src/conversion/index.ts b/packages/string-templates/src/conversion/index.ts index c6ec012ad9..1fb8d97467 100644 --- a/packages/string-templates/src/conversion/index.ts +++ b/packages/string-templates/src/conversion/index.ts @@ -114,7 +114,7 @@ export function convertHBSBlock(block, blockNumber) { const parts = splitBySpace(layer) if (value || parts.length > 1 || list[parts[0]]) { // first of layer should always be the helper - const helper = parts.splice(0, 1) + const [helper] = parts.splice(0, 1) if (list[helper]) { value = `helpers.${helper}(${buildList(parts, value)})` } diff --git a/packages/string-templates/src/errors.ts b/packages/string-templates/src/errors.ts index a0108d5018..79a8a525ef 100644 --- a/packages/string-templates/src/errors.ts +++ b/packages/string-templates/src/errors.ts @@ -1,7 +1,3 @@ export class JsErrorTimeout extends Error { code = "ERR_SCRIPT_EXECUTION_TIMEOUT" - - constructor() { - super() - } } diff --git a/packages/string-templates/src/helpers/Helper.ts b/packages/string-templates/src/helpers/Helper.ts index e7e7488c0f..e254b2086d 100644 --- a/packages/string-templates/src/helpers/Helper.ts +++ b/packages/string-templates/src/helpers/Helper.ts @@ -1,4 +1,8 @@ export default class Helper { + private name + private fn + private useValueFallback + constructor(name, fn, useValueFallback = true) { this.name = name this.fn = fn diff --git a/packages/string-templates/src/helpers/date.ts b/packages/string-templates/src/helpers/date.ts index 9d663573bd..70dc0bd617 100644 --- a/packages/string-templates/src/helpers/date.ts +++ b/packages/string-templates/src/helpers/date.ts @@ -1,12 +1,22 @@ import dayjs from "dayjs" -dayjs.extend(require("dayjs/plugin/duration")) -dayjs.extend(require("dayjs/plugin/advancedFormat")) -dayjs.extend(require("dayjs/plugin/isoWeek")) -dayjs.extend(require("dayjs/plugin/weekYear")) -dayjs.extend(require("dayjs/plugin/weekOfYear")) -dayjs.extend(require("dayjs/plugin/relativeTime")) -dayjs.extend(require("dayjs/plugin/utc")) -dayjs.extend(require("dayjs/plugin/timezone")) + +import dayjsDurationPlugin from "dayjs/plugin/duration" +import dayjsAdvancedFormatPlugin from "dayjs/plugin/advancedFormat" +import dayjsIsoWeekPlugin from "dayjs/plugin/isoWeek" +import dayjsWeekYearPlugin from "dayjs/plugin/weekYear" +import dayjsWeekOfYearPlugin from "dayjs/plugin/weekOfYear" +import dayjsRelativeTimePlugin from "dayjs/plugin/relativeTime" +import dayjsUtcPlugin from "dayjs/plugin/utc" +import dayjsTimezonePlugin from "dayjs/plugin/timezone" + +dayjs.extend(dayjsDurationPlugin) +dayjs.extend(dayjsAdvancedFormatPlugin) +dayjs.extend(dayjsIsoWeekPlugin) +dayjs.extend(dayjsWeekYearPlugin) +dayjs.extend(dayjsWeekOfYearPlugin) +dayjs.extend(dayjsRelativeTimePlugin) +dayjs.extend(dayjsUtcPlugin) +dayjs.extend(dayjsTimezonePlugin) /** * This file was largely taken from the helper-date package - we did this for two reasons: @@ -58,7 +68,7 @@ function getContext(thisArg, locals, options) { return context } -function initialConfig(str, pattern, options) { +function initialConfig(str, pattern, options = {}) { if (isOptions(pattern)) { options = pattern pattern = null @@ -72,7 +82,7 @@ function initialConfig(str, pattern, options) { return { str, pattern, options } } -function setLocale(str, pattern, options) { +function setLocale(str, pattern, options = {}) { // 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) } diff --git a/packages/string-templates/src/helpers/index.ts b/packages/string-templates/src/helpers/index.ts index bad861567d..19d4ee3dff 100644 --- a/packages/string-templates/src/helpers/index.ts +++ b/packages/string-templates/src/helpers/index.ts @@ -44,8 +44,9 @@ const HELPERS = [ if (value == null || typeof value !== "string") { return value == null ? "" : value } - if (value && value.string) { - value = value.string + // TODO: check, this should always be false + if (value && (value as any).string) { + value = (value as any).string } let text = value if (__opts && __opts.escapeNewlines) { diff --git a/packages/string-templates/src/helpers/list.ts b/packages/string-templates/src/helpers/list.ts index 8f08bc6f12..fde42b659a 100644 --- a/packages/string-templates/src/helpers/list.ts +++ b/packages/string-templates/src/helpers/list.ts @@ -1,7 +1,7 @@ import { date, duration } from "./date" // https://github.com/evanw/esbuild/issues/56 -const externalCollections = { +const externalCollections: Record any> = { math: require("@budibase/handlebars-helpers/lib/math"), array: require("@budibase/handlebars-helpers/lib/array"), number: require("@budibase/handlebars-helpers/lib/number"), diff --git a/packages/string-templates/src/processors/index.ts b/packages/string-templates/src/processors/index.ts index a5bcf84eaf..7187bb6fd8 100644 --- a/packages/string-templates/src/processors/index.ts +++ b/packages/string-templates/src/processors/index.ts @@ -2,7 +2,7 @@ import { FIND_HBS_REGEX } from "../utilities" import * as preprocessor from "./preprocessor" import * as postprocessor from "./postprocessor" -function process(output, processors, opts) { +function process(output, processors, opts = {}) { for (let processor of processors) { // if a literal statement has occurred stop if (typeof output !== "string") { diff --git a/packages/string-templates/src/processors/postprocessor.ts b/packages/string-templates/src/processors/postprocessor.ts index 3122d43e53..39125b225a 100644 --- a/packages/string-templates/src/processors/postprocessor.ts +++ b/packages/string-templates/src/processors/postprocessor.ts @@ -6,6 +6,9 @@ export const PostProcessorNames = { /* eslint-disable no-unused-vars */ class Postprocessor { + name + private fn + constructor(name, fn) { this.name = name this.fn = fn diff --git a/packages/string-templates/src/processors/preprocessor.ts b/packages/string-templates/src/processors/preprocessor.ts index 39cfc07359..1fcbac629f 100644 --- a/packages/string-templates/src/processors/preprocessor.ts +++ b/packages/string-templates/src/processors/preprocessor.ts @@ -11,7 +11,7 @@ export const PreprocessorNames = { /* eslint-disable no-unused-vars */ class Preprocessor { - private name: string + name: string private fn: any constructor(name, fn) { diff --git a/packages/string-templates/src/utilities.ts b/packages/string-templates/src/utilities.ts index 017f38db47..bcb9987c89 100644 --- a/packages/string-templates/src/utilities.ts +++ b/packages/string-templates/src/utilities.ts @@ -15,14 +15,14 @@ export const isJSAllowed = () => { // originally this could be done with a single regex using look behinds // but safari does not support this feature // original regex: /(? { +export const findDoubleHbsInstances = (string: string): string[] => { let copied = string const doubleRegex = new RegExp(FIND_HBS_REGEX) const regex = new RegExp(FIND_TRIPLE_HBS_REGEX) const tripleMatches = copied.match(regex) // remove triple braces if (tripleMatches) { - tripleMatches.forEach(match => { + tripleMatches.forEach((match: string) => { copied = copied.replace(match, "") }) } @@ -30,16 +30,21 @@ export const findDoubleHbsInstances = string => { return doubleMatches ? doubleMatches : [] } -export const isAlphaNumeric = char => { +export const isAlphaNumeric = (char: string) => { return char.match(ALPHA_NUMERIC_REGEX) } -export const swapStrings = (string, start, length, swap) => { +export const swapStrings = ( + string: string, + start: number, + length: number, + swap: string +) => { return string.slice(0, start) + swap + string.slice(start + length) } export const removeHandlebarsStatements = ( - string, + string: string, replacement = "Invalid binding" ) => { let regexp = new RegExp(FIND_HBS_REGEX) @@ -54,10 +59,10 @@ export const removeHandlebarsStatements = ( return string } -export const btoa = plainText => { +export const btoa = (plainText: string) => { return Buffer.from(plainText, "utf-8").toString("base64") } -export const atob = base64 => { +export const atob = (base64: string) => { return Buffer.from(base64, "base64").toString("utf-8") } diff --git a/packages/string-templates/tsconfig.json b/packages/string-templates/tsconfig.json index a7c7a0df67..52e3d78d20 100644 --- a/packages/string-templates/tsconfig.json +++ b/packages/string-templates/tsconfig.json @@ -1,6 +1,9 @@ { "include": ["src/**/*"], "compilerOptions": { + "target": "es6", + "moduleResolution": "node", + "lib": ["dom"], "allowJs": true, "declaration": true, "emitDeclarationOnly": true,