correctly types gen-collection-info.ts

This commit is contained in:
mikesealey 2025-02-11 17:09:34 +00:00
parent 18b296a850
commit 33f286d528
2 changed files with 27 additions and 23 deletions

View File

@ -33,6 +33,7 @@
"@rollup/plugin-json": "^4.1.0", "@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^15.2.3", "@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-typescript": "8.3.0", "@rollup/plugin-typescript": "8.3.0",
"@types/doctrine": "^0.0.9",
"doctrine": "^3.0.0", "doctrine": "^3.0.0",
"jest": "29.7.0", "jest": "29.7.0",
"marked": "^4.0.10", "marked": "^4.0.10",

View File

@ -7,18 +7,30 @@ import { marked } from "marked"
import { join, dirname } from "path" import { join, dirname } from "path"
const helpers = require("@budibase/handlebars-helpers") const helpers = require("@budibase/handlebars-helpers")
const doctrine = require("doctrine") import doctrine, { Annotation } from "doctrine"
type HelperInfo = { type BudibaseAnnotation = Annotation & {
example?: string
acceptsInline?: boolean acceptsInline?: boolean
acceptsBlock?: boolean acceptsBlock?: boolean
}
type Helper = {
args: string[]
numArgs: number
example?: string example?: string
description: string description: string
tags?: any[] requiresBlock?: boolean
}
type Manifest = {
[category: string]: {
[helper: string]: Helper
}
} }
const FILENAME = join(__dirname, "..", "src", "manifest.json") const FILENAME = join(__dirname, "..", "src", "manifest.json")
const outputJSON: any = {} const outputJSON: Manifest = {}
const ADDED_HELPERS = { const ADDED_HELPERS = {
date: { date: {
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 const args = obj.args
if (name === "ifNth") { if (name === "ifNth") {
args[0] = "a" obj.args = ["a", "b", "options"]
args[1] = "b" obj.numArgs = 3
} }
if (name === "eachIndex") { if (name === "eachIndex") {
obj.description = "Iterates the array, listing an item and the index of it." 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 return true
} }
function getCommentInfo(file: string, func: string): HelperInfo { function getCommentInfo(file: string, func: string): BudibaseAnnotation {
const lines = file.split("\n") const lines = file.split("\n")
const funcLines = func.split("\n") const funcLines = func.split("\n")
let comment = null let comment: string | null = null
for (let idx = 0; idx < lines.length; ++idx) { for (let idx = 0; idx < lines.length; ++idx) {
// from here work back until we have the comment // from here work back until we have the comment
if (lookForward(lines, funcLines, idx)) { if (lookForward(lines, funcLines, idx)) {
@ -91,15 +103,9 @@ function getCommentInfo(file: string, func: string): HelperInfo {
} }
} }
if (comment == null) { if (comment == null) {
return { description: "" } return { description: "", tags: [] }
} }
const docs: { const docs: BudibaseAnnotation = doctrine.parse(comment, { unwrap: true })
acceptsInline?: boolean
acceptsBlock?: boolean
example: string
description: string
tags: any[]
} = doctrine.parse(comment, { unwrap: true })
// some hacky fixes // some hacky fixes
docs.description = docs.description.replace(/\n/g, " ") docs.description = docs.description.replace(/\n/g, " ")
docs.description = docs.description.replace(/[ ]{2,}/g, " ") docs.description = docs.description.replace(/[ ]{2,}/g, " ")
@ -135,7 +141,7 @@ function run() {
)}/lib/${collection}.js`, )}/lib/${collection}.js`,
"utf8" "utf8"
) )
const collectionInfo: any = {} const collectionInfo: { [name: string]: Helper } = {}
// collect information about helper // collect information about helper
let hbsHelperInfo = helpers[collection]() let hbsHelperInfo = helpers[collection]()
for (let entry of Object.entries(hbsHelperInfo)) { for (let entry of Object.entries(hbsHelperInfo)) {
@ -154,11 +160,8 @@ function run() {
const jsDocInfo = getCommentInfo(collectionFile, fnc) const jsDocInfo = getCommentInfo(collectionFile, fnc)
let args = jsDocInfo.tags let args = jsDocInfo.tags
.filter(tag => tag.title === "param") .filter(tag => tag.title === "param")
.map( .filter(tag => tag.description)
tag => .map(tag => tag.description!.replace(/`/g, "").split(" ")[0].trim())
tag.description &&
tag.description.replace(/`/g, "").split(" ")[0].trim()
)
collectionInfo[name] = fixSpecialCases(name, { collectionInfo[name] = fixSpecialCases(name, {
args, args,
numArgs: args.length, numArgs: args.length,