From a9a58bff324e92488c2a873440688374bdd278db Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Sat, 27 Jan 2024 23:48:49 +0200 Subject: [PATCH] chore: lint for tf/hcl blocks (#135) Co-authored-by: Muhammad Atif Ali --- lint.ts | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/lint.ts b/lint.ts index 12e733c..7b859a0 100644 --- a/lint.ts +++ b/lint.ts @@ -13,9 +13,39 @@ let badExit = false; // error reports an error to the console and sets badExit to true // so that the process will exit with a non-zero exit code. const error = (...data: any[]) => { - console.error(...data); - badExit = true; -} + console.error(...data); + badExit = true; +}; + +const verifyCodeBlocks = ( + tokens: marked.Token[], + res = { + codeIsTF: false, + codeIsHCL: false, + } +) => { + for (const token of tokens) { + // Check in-depth. + if (token.type === "list") { + verifyCodeBlocks(token.items, res); + continue; + } + if (token.type === "list_item") { + verifyCodeBlocks(token.tokens, res); + continue; + } + + if (token.type === "code") { + if (token.lang === "tf") { + res.codeIsTF = true; + } + if (token.lang === "hcl") { + res.codeIsHCL = true; + } + } + } + return res; +}; // Ensures that each README has the proper format. // Exits with 0 if all is good! @@ -89,6 +119,14 @@ for (const dir of dirs) { if (!code) { error(dir.name, "missing example code block after paragraph"); } + + const { codeIsTF, codeIsHCL } = verifyCodeBlocks(tokens); + if (!codeIsTF) { + error(dir.name, "missing example tf code block"); + } + if (codeIsHCL) { + error(dir.name, "hcl code block should be tf"); + } } if (badExit) {