Skip to content
This repository was archived by the owner on May 15, 2025. It is now read-only.

Commit a9a58bf

Browse files
mafredrimatifali
andauthored
chore: lint for tf/hcl blocks (#135)
Co-authored-by: Muhammad Atif Ali <atif@coder.com>
1 parent 6b84200 commit a9a58bf

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

lint.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,39 @@ let badExit = false;
1313
// error reports an error to the console and sets badExit to true
1414
// so that the process will exit with a non-zero exit code.
1515
const error = (...data: any[]) => {
16-
console.error(...data);
17-
badExit = true;
18-
}
16+
console.error(...data);
17+
badExit = true;
18+
};
19+
20+
const verifyCodeBlocks = (
21+
tokens: marked.Token[],
22+
res = {
23+
codeIsTF: false,
24+
codeIsHCL: false,
25+
}
26+
) => {
27+
for (const token of tokens) {
28+
// Check in-depth.
29+
if (token.type === "list") {
30+
verifyCodeBlocks(token.items, res);
31+
continue;
32+
}
33+
if (token.type === "list_item") {
34+
verifyCodeBlocks(token.tokens, res);
35+
continue;
36+
}
37+
38+
if (token.type === "code") {
39+
if (token.lang === "tf") {
40+
res.codeIsTF = true;
41+
}
42+
if (token.lang === "hcl") {
43+
res.codeIsHCL = true;
44+
}
45+
}
46+
}
47+
return res;
48+
};
1949

2050
// Ensures that each README has the proper format.
2151
// Exits with 0 if all is good!
@@ -89,6 +119,14 @@ for (const dir of dirs) {
89119
if (!code) {
90120
error(dir.name, "missing example code block after paragraph");
91121
}
122+
123+
const { codeIsTF, codeIsHCL } = verifyCodeBlocks(tokens);
124+
if (!codeIsTF) {
125+
error(dir.name, "missing example tf code block");
126+
}
127+
if (codeIsHCL) {
128+
error(dir.name, "hcl code block should be tf");
129+
}
92130
}
93131

94132
if (badExit) {

0 commit comments

Comments
 (0)