From 33f286d528780db41bf29db076c4de390fc337f1 Mon Sep 17 00:00:00 2001 From: mikesealey Date: Tue, 11 Feb 2025 17:09:34 +0000 Subject: [PATCH] correctly types gen-collection-info.ts --- packages/string-templates/package.json | 1 + .../scripts/gen-collection-info.ts | 49 ++++++++++--------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/packages/string-templates/package.json b/packages/string-templates/package.json index acd0ea4fa8..a80c2ec15c 100644 --- a/packages/string-templates/package.json +++ b/packages/string-templates/package.json @@ -33,6 +33,7 @@ "@rollup/plugin-json": "^4.1.0", "@rollup/plugin-node-resolve": "^15.2.3", "@rollup/plugin-typescript": "8.3.0", + "@types/doctrine": "^0.0.9", "doctrine": "^3.0.0", "jest": "29.7.0", "marked": "^4.0.10", diff --git a/packages/string-templates/scripts/gen-collection-info.ts b/packages/string-templates/scripts/gen-collection-info.ts index 8b3825bf5c..ff26228ea9 100644 --- a/packages/string-templates/scripts/gen-collection-info.ts +++ b/packages/string-templates/scripts/gen-collection-info.ts @@ -7,18 +7,30 @@ import { marked } from "marked" import { join, dirname } from "path" const helpers = require("@budibase/handlebars-helpers") -const doctrine = require("doctrine") +import doctrine, { Annotation } from "doctrine" -type HelperInfo = { +type BudibaseAnnotation = Annotation & { + example?: string acceptsInline?: boolean acceptsBlock?: boolean +} + +type Helper = { + args: string[] + numArgs: number example?: string description: string - tags?: any[] + requiresBlock?: boolean +} + +type Manifest = { + [category: string]: { + [helper: string]: Helper + } } const FILENAME = join(__dirname, "..", "src", "manifest.json") -const outputJSON: any = {} +const outputJSON: Manifest = {} const ADDED_HELPERS = { date: { date: { @@ -38,11 +50,11 @@ const ADDED_HELPERS = { }, } -function fixSpecialCases(name: string, obj: any) { +function fixSpecialCases(name: string, obj: Helper) { const args = obj.args if (name === "ifNth") { - args[0] = "a" - args[1] = "b" + obj.args = ["a", "b", "options"] + obj.numArgs = 3 } if (name === "eachIndex") { obj.description = "Iterates the array, listing an item and the index of it." @@ -66,10 +78,10 @@ function lookForward(lines: string[], funcLines: string[], idx: number) { return true } -function getCommentInfo(file: string, func: string): HelperInfo { +function getCommentInfo(file: string, func: string): BudibaseAnnotation { const lines = file.split("\n") const funcLines = func.split("\n") - let comment = null + let comment: string | null = null for (let idx = 0; idx < lines.length; ++idx) { // from here work back until we have the comment if (lookForward(lines, funcLines, idx)) { @@ -91,15 +103,9 @@ function getCommentInfo(file: string, func: string): HelperInfo { } } if (comment == null) { - return { description: "" } + return { description: "", tags: [] } } - const docs: { - acceptsInline?: boolean - acceptsBlock?: boolean - example: string - description: string - tags: any[] - } = doctrine.parse(comment, { unwrap: true }) + const docs: BudibaseAnnotation = doctrine.parse(comment, { unwrap: true }) // some hacky fixes docs.description = docs.description.replace(/\n/g, " ") docs.description = docs.description.replace(/[ ]{2,}/g, " ") @@ -135,7 +141,7 @@ function run() { )}/lib/${collection}.js`, "utf8" ) - const collectionInfo: any = {} + const collectionInfo: { [name: string]: Helper } = {} // collect information about helper let hbsHelperInfo = helpers[collection]() for (let entry of Object.entries(hbsHelperInfo)) { @@ -154,11 +160,8 @@ function run() { const jsDocInfo = getCommentInfo(collectionFile, fnc) let args = jsDocInfo.tags .filter(tag => tag.title === "param") - .map( - tag => - tag.description && - tag.description.replace(/`/g, "").split(" ")[0].trim() - ) + .filter(tag => tag.description) + .map(tag => tag.description!.replace(/`/g, "").split(" ")[0].trim()) collectionInfo[name] = fixSpecialCases(name, { args, numArgs: args.length,