From b05dc6ab498dc39b513e193c68125745959f0132 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 21 Feb 2024 23:43:12 +0100 Subject: [PATCH] Fix typings --- packages/string-templates/src/index.ts | 9 +-- .../string-templates/test/helpers.spec.ts | 6 +- .../string-templates/test/javascript.spec.ts | 2 +- .../string-templates/test/manifest.spec.ts | 8 +-- packages/string-templates/test/utils.ts | 56 +++++++++++-------- packages/string-templates/tsconfig.json | 3 +- 6 files changed, 46 insertions(+), 38 deletions(-) diff --git a/packages/string-templates/src/index.ts b/packages/string-templates/src/index.ts index bf5a250c21..2549d0b34c 100644 --- a/packages/string-templates/src/index.ts +++ b/packages/string-templates/src/index.ts @@ -79,7 +79,7 @@ function createTemplate(string, opts) { * @param {object|undefined} [opts] optional - specify some options for processing. * @returns {Promise} The structure input, as fully updated as possible. */ -export async function processObject(object, context, opts) { +export async function processObject(object, context, opts?) { testObject(object) for (let key of Object.keys(object || {})) { if (object[key] != null) { @@ -214,7 +214,7 @@ export function makePropSafe(property) { * @param [opts] optional - specify some options for processing. * @returns {boolean} Whether or not the input string is valid. */ -export function isValid(string, opts) { +export function isValid(string, opts?) { const validCases = [ "string", "number", @@ -256,11 +256,8 @@ export function isValid(string, opts) { * This manifest provides information about each of the helpers and how it can be used. * @returns The manifest JSON which has been generated from the helpers. */ -let manifest +import manifest from "../manifest.json" export function getManifest() { - if (!manifest) { - manifest = fs.readFileSync(require.resolve("../manifest.json"), "utf-8") - } return manifest } diff --git a/packages/string-templates/test/helpers.spec.ts b/packages/string-templates/test/helpers.spec.ts index 0207ee5568..efa6b0fdc3 100644 --- a/packages/string-templates/test/helpers.spec.ts +++ b/packages/string-templates/test/helpers.spec.ts @@ -188,9 +188,7 @@ describe("test the date helpers", () => { time: date.toUTCString(), } ) - const formatted = new dayjs(date) - .tz("America/New_York") - .format("HH-mm-ss Z") + const formatted = dayjs(date).tz("America/New_York").format("HH-mm-ss Z") expect(output).toBe(formatted) }) @@ -200,7 +198,7 @@ describe("test the date helpers", () => { time: date.toUTCString(), }) const timezone = dayjs.tz.guess() - const offset = new dayjs(date).tz(timezone).format("Z") + const offset = dayjs(date).tz(timezone).format("Z") expect(output).toBe(offset) }) }) diff --git a/packages/string-templates/test/javascript.spec.ts b/packages/string-templates/test/javascript.spec.ts index 3c9524e165..ab68e70830 100644 --- a/packages/string-templates/test/javascript.spec.ts +++ b/packages/string-templates/test/javascript.spec.ts @@ -7,7 +7,7 @@ const { } = require("../src/index") const { UUID_REGEX } = require("./constants") -const processJS = (js, context) => { +const processJS = (js, context?) => { return processStringSync(encodeJSBinding(js), context) } diff --git a/packages/string-templates/test/manifest.spec.ts b/packages/string-templates/test/manifest.spec.ts index cd2041f5d2..9fbf4e8019 100644 --- a/packages/string-templates/test/manifest.spec.ts +++ b/packages/string-templates/test/manifest.spec.ts @@ -1,4 +1,4 @@ -const vm = require("vm") +import vm from "vm" jest.mock("@budibase/handlebars-helpers/lib/math", () => { const actual = jest.requireActual("@budibase/handlebars-helpers/lib/math") @@ -17,10 +17,10 @@ jest.mock("@budibase/handlebars-helpers/lib/uuid", () => { } }) -const { processString, setJSRunner } = require("../src/index") +import { processString, setJSRunner } from "../src/index" -const tk = require("timekeeper") -const { getParsedManifest, runJsHelpersTests } = require("./utils") +import tk from "timekeeper" +import { getParsedManifest, runJsHelpersTests } from "./utils" tk.freeze("2021-01-21T12:00:00") diff --git a/packages/string-templates/test/utils.ts b/packages/string-templates/test/utils.ts index 927a6e3aeb..64622305f9 100644 --- a/packages/string-templates/test/utils.ts +++ b/packages/string-templates/test/utils.ts @@ -1,11 +1,7 @@ -const { getManifest } = require("../src") -const { getJsHelperList } = require("../src/helpers") +import { getManifest } from "../src" +import { getJsHelperList } from "../src/helpers" -const { - convertToJS, - processStringSync, - encodeJSBinding, -} = require("../src/index.js") +import { convertToJS, processStringSync, encodeJSBinding } from "../src/index" function tryParseJson(str) { if (typeof str !== "string") { @@ -19,23 +15,35 @@ function tryParseJson(str) { } } -const getParsedManifest = () => { +type ExampleType = [ + string, + { + hbs: string + js: string + requiresHbsBody: boolean + } +] + +export const getParsedManifest = () => { const manifest = getManifest() const collections = Object.keys(manifest) + const examples = collections.reduce((acc, collection) => { - const functions = Object.entries(manifest[collection]) - .filter(([_, details]) => details.example) - .map(([name, details]) => { + const functions = Object.entries<{ + example: string + requiresBlock: boolean + }>(manifest[collection]) + .filter( + ([_, details]) => + details.example?.split("->").map(x => x.trim()).length > 1 + ) + .map(([name, details]): ExampleType => { const example = details.example let [hbs, js] = example.split("->").map(x => x.trim()) - if (!js) { - // The function has no return value - return - } // Trim 's js = js.replace(/^'|'$/g, "") - let parsedExpected + let parsedExpected: string if ((parsedExpected = tryParseJson(js))) { if (Array.isArray(parsedExpected)) { if (typeof parsedExpected[0] === "object") { @@ -48,19 +56,23 @@ const getParsedManifest = () => { const requiresHbsBody = details.requiresBlock return [name, { hbs, js, requiresHbsBody }] }) - .filter(x => !!x) - if (Object.keys(functions).length) { + if (functions.length) { acc[collection] = functions } return acc - }, {}) + }, {} as Record) return examples } -module.exports.getParsedManifest = getParsedManifest -module.exports.runJsHelpersTests = ({ funcWrap, testsToSkip } = {}) => { +export const runJsHelpersTests = ({ + funcWrap, + testsToSkip, +}: { + funcWrap?: any + testsToSkip?: any +} = {}) => { funcWrap = funcWrap || (delegate => delegate()) const manifest = getParsedManifest() @@ -73,7 +85,7 @@ module.exports.runJsHelpersTests = ({ funcWrap, testsToSkip } = {}) => { } describe("can be parsed and run as js", () => { - const jsHelpers = getJsHelperList() + const jsHelpers = getJsHelperList()! const jsExamples = Object.keys(manifest).reduce((acc, v) => { acc[v] = manifest[v].filter(([key]) => jsHelpers[key]) return acc diff --git a/packages/string-templates/tsconfig.json b/packages/string-templates/tsconfig.json index 07084de469..06cfa67b03 100644 --- a/packages/string-templates/tsconfig.json +++ b/packages/string-templates/tsconfig.json @@ -7,6 +7,7 @@ "allowJs": true, "outDir": "dist", "esModuleInterop": true, - "types": ["node", "jest"] + "types": ["node", "jest"], + "resolveJsonModule": true } }