|
| 1 | +import { readFile, readdir } from "node:fs/promises"; |
| 2 | + |
| 3 | +const SPEC_DIR = new URL("../spec", import.meta.url).pathname; |
| 4 | + |
| 5 | +process.exitCode = 0; |
| 6 | +const filenames = await readdir(SPEC_DIR); |
| 7 | +for (const filename of filenames) { |
| 8 | + if (!filename.endsWith(".md")) { |
| 9 | + continue; |
| 10 | + } |
| 11 | + const markdown = await readFile(`${SPEC_DIR}/${filename}`, "utf8"); |
| 12 | + |
| 13 | + /** |
| 14 | + * Not strictly 'lines' since we try and group indented things together as if |
| 15 | + * they were one line. Close enough though. |
| 16 | + */ |
| 17 | + const lines = markdown.split(/\n(?=[\S\n]|\s*(?:-|[0-9]+\.) )/); |
| 18 | + |
| 19 | + for (let i = 0, l = lines.length; i < l; i++) { |
| 20 | + const line = lines[i]; |
| 21 | + |
| 22 | + // Check algorithm is consistently formatted |
| 23 | + { |
| 24 | + // Is it an algorithm definition? |
| 25 | + const matches = line.match(/^([a-z0-9A-Z]+)(\s*)\(([^)]*)\)(\s*):(\s*)$/); |
| 26 | + const grammarMatches = |
| 27 | + filename === "Section 2 -- Language.md" && |
| 28 | + line.match(/^([A-Za-z0-9]+) :\s+((\S).*)$/); |
| 29 | + if (matches) { |
| 30 | + const [, algorithmName, ns1, _args, ns2, ns3] = matches; |
| 31 | + if (ns1 || ns2 || ns3) { |
| 32 | + console.log( |
| 33 | + `Bad whitespace in definition of ${algorithmName} in '${filename}':` |
| 34 | + ); |
| 35 | + console.dir(line); |
| 36 | + console.log(); |
| 37 | + process.exitCode = 1; |
| 38 | + } |
| 39 | + if (lines[i + 1] !== "") { |
| 40 | + console.log( |
| 41 | + `No empty space after algorithm ${algorithmName} header in '${filename}'` |
| 42 | + ); |
| 43 | + console.log(); |
| 44 | + process.exitCode = 1; |
| 45 | + } |
| 46 | + for (let j = i + 2; j < l; j++) { |
| 47 | + const step = lines[j]; |
| 48 | + if (!step.match(/^\s*(-|[0-9]+\.) /)) { |
| 49 | + if (step !== "") { |
| 50 | + console.log( |
| 51 | + `Bad algorithm ${algorithmName} step in '${filename}':` |
| 52 | + ); |
| 53 | + console.dir(step); |
| 54 | + console.log(); |
| 55 | + process.exitCode = 1; |
| 56 | + } |
| 57 | + break; |
| 58 | + } |
| 59 | + if (!step.match(/[.:]$/)) { |
| 60 | + console.log( |
| 61 | + `Bad formatting for '${algorithmName}' step (does not end in '.' or ':') in '${filename}':` |
| 62 | + ); |
| 63 | + console.dir(step); |
| 64 | + console.log(); |
| 65 | + process.exitCode = 1; |
| 66 | + } |
| 67 | + if (step.match(/^\s*(-|[0-9]\.)\s+[a-z]/)) { |
| 68 | + console.log( |
| 69 | + `Bad formatting of '${algorithmName}' step (should start with a capital) in '${filename}':` |
| 70 | + ); |
| 71 | + console.dir(step); |
| 72 | + console.log(); |
| 73 | + process.exitCode = 1; |
| 74 | + } |
| 75 | + const trimmedInnerLine = step.replace(/\s+/g, " "); |
| 76 | + if ( |
| 77 | + trimmedInnerLine.match( |
| 78 | + /(?:[rR]eturn|is (?:not )?)(true|false|null)\b/ |
| 79 | + ) && |
| 80 | + !trimmedInnerLine.match(/null or empty/) |
| 81 | + ) { |
| 82 | + console.log( |
| 83 | + `Potential bad formatting of '${algorithmName}' step (true/false/null should be wrapped in curly braces, e.g. '{true}') in '${filename}':` |
| 84 | + ); |
| 85 | + console.dir(step); |
| 86 | + console.log(); |
| 87 | + process.exitCode = 1; |
| 88 | + } |
| 89 | + } |
| 90 | + } else if (grammarMatches) { |
| 91 | + // This is super loosey-goosey |
| 92 | + const [, grammarName, rest] = grammarMatches; |
| 93 | + if (rest.trim() === "one of") { |
| 94 | + // Still grammar, not algorithm |
| 95 | + continue; |
| 96 | + } |
| 97 | + if (rest.trim() === "" && lines[i + 1] !== "") { |
| 98 | + console.log( |
| 99 | + `No empty space after grammar ${grammarName} header in '${filename}'` |
| 100 | + ); |
| 101 | + console.log(); |
| 102 | + process.exitCode = 1; |
| 103 | + } |
| 104 | + if (!lines[i + 2].startsWith("- ")) { |
| 105 | + // Not an algorithm; probably more grammar |
| 106 | + continue; |
| 107 | + } |
| 108 | + for (let j = i + 2; j < l; j++) { |
| 109 | + const step = lines[j]; |
| 110 | + if (!step.match(/^\s*(-|[0-9]+\.) /)) { |
| 111 | + if (step !== "") { |
| 112 | + console.log(`Bad grammar ${grammarName} step in '${filename}':`); |
| 113 | + console.dir(step); |
| 114 | + console.log(); |
| 115 | + process.exitCode = 1; |
| 116 | + } |
| 117 | + break; |
| 118 | + } |
| 119 | + if (!step.match(/[.:]$/)) { |
| 120 | + console.log( |
| 121 | + `Bad formatting for '${grammarName}' step (does not end in '.' or ':') in '${filename}':` |
| 122 | + ); |
| 123 | + console.dir(step); |
| 124 | + console.log(); |
| 125 | + process.exitCode = 1; |
| 126 | + } |
| 127 | + if (step.match(/^\s*(-|[0-9]\.)\s+[a-z]/)) { |
| 128 | + console.log( |
| 129 | + `Bad formatting of '${grammarName}' step (should start with a capital) in '${filename}':` |
| 130 | + ); |
| 131 | + console.dir(step); |
| 132 | + console.log(); |
| 133 | + process.exitCode = 1; |
| 134 | + } |
| 135 | + const trimmedInnerLine = step.replace(/\s+/g, " "); |
| 136 | + if ( |
| 137 | + trimmedInnerLine.match( |
| 138 | + /(?:[rR]eturn|is (?:not )?)(true|false|null)\b/ |
| 139 | + ) && |
| 140 | + !trimmedInnerLine.match(/null or empty/) |
| 141 | + ) { |
| 142 | + console.log( |
| 143 | + `Potential bad formatting of '${grammarName}' step (true/false/null should be wrapped in curly braces, e.g. '{true}') in '${filename}':` |
| 144 | + ); |
| 145 | + console.dir(step); |
| 146 | + console.log(); |
| 147 | + process.exitCode = 1; |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + // Check `- ...:` step is followed by an indent |
| 154 | + { |
| 155 | + const matches = line.match(/^(\s*)- .*:\s*$/); |
| 156 | + if (matches) { |
| 157 | + const indent = matches[1]; |
| 158 | + const nextLine = lines[i + 1]; |
| 159 | + if (!nextLine.startsWith(`${indent} `)) { |
| 160 | + console.log( |
| 161 | + `Lacking indent in '${filename}' following ':' character:` |
| 162 | + ); |
| 163 | + console.dir(line); |
| 164 | + console.dir(nextLine); |
| 165 | + console.log(); |
| 166 | + // TODO: process.exitCode = 1; |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +if (process.exitCode === 0) { |
| 174 | + console.log(`Everything looks okay!`); |
| 175 | +} else { |
| 176 | + console.log(`Please resolve the errors detailed above.`); |
| 177 | +} |
0 commit comments