|
| 1 | +#!/usr/bin/env node |
| 2 | +import fs from "fs"; |
| 3 | +import path from "path"; |
| 4 | + |
| 5 | +function isOpeningBrace(line) { |
| 6 | + return line.endsWith("{") && !line.includes("}"); |
| 7 | +} |
| 8 | + |
| 9 | +function isTypeDecl(line) { |
| 10 | + return /^(record|variant|enum|flags|resource)\s+/.test(line); |
| 11 | +} |
| 12 | +function isFuncDecl(line) { |
| 13 | + if (line.startsWith("import ") || line.startsWith("export ")) return false; |
| 14 | + return /^[a-zA-Z][\w-]*\s*:\s*func\b/.test(line) || /:\s*func\b/.test(line) || /->/.test(line); |
| 15 | +} |
| 16 | +function isFieldDecl(line) { |
| 17 | + const t = line.trim(); |
| 18 | + return /^[a-zA-Z][a-zA-Z0-9-]*\s*[:,(]/.test(t) || /^[a-zA-Z][a-zA-Z0-9-]*\s*,?\s*$/.test(t); |
| 19 | +} |
| 20 | + |
| 21 | +function formatPackage(line) { |
| 22 | + return line.replace(/\s+/g, " ").replace(/\s*;\s*$/, ";"); |
| 23 | +} |
| 24 | +function formatNamedBlock(line) { |
| 25 | + return line.replace(/\s+/g, " ").replace(/\s*{\s*$/, " {"); |
| 26 | +} |
| 27 | +function formatFunc(line) { |
| 28 | + let f = line; |
| 29 | + f = f.replace(/:func/, ": func"); |
| 30 | + f = f.replace(/:\s*func/, ": func"); |
| 31 | + f = f.replace(/func\s*\(/, "func("); |
| 32 | + f = f.replace(/\)\s*->\s*/, ") -> "); |
| 33 | + f = f.replace(/\)->\s*/, ") -> "); |
| 34 | + f = f.replace(/\)->/, ") -> "); |
| 35 | + f = f.replace(/,\s*/g, ", "); |
| 36 | + f = f.replace(/:\s*/g, ": "); |
| 37 | + f = f.replace(/\s*;\s*$/, ";"); |
| 38 | + return f; |
| 39 | +} |
| 40 | +function formatImportExport(line) { |
| 41 | + let f = line.replace(/^(import|export)\s+/, "$1 "); |
| 42 | + if (f.includes(": func") || f.includes(":func")) { |
| 43 | + f = formatFunc(f); |
| 44 | + } else { |
| 45 | + f = f.replace(/\s*;\s*$/, ";"); |
| 46 | + } |
| 47 | + return f; |
| 48 | +} |
| 49 | +function formatUse(line) { |
| 50 | + let f = line; |
| 51 | + f = f.replace(/^use\s+/, "use "); |
| 52 | + f = f.replace(/\s+as\s+/, " as "); |
| 53 | + f = f.replace(/\s+from\s+/, " from "); |
| 54 | + f = f.replace(/\s*;\s*$/, ";"); |
| 55 | + return f; |
| 56 | +} |
| 57 | +function formatTypeAlias(line) { |
| 58 | + let f = line; |
| 59 | + if (f.startsWith("type ")) f = f.replace(/^type\s+/, "type "); |
| 60 | + f = f.replace(/\s*=\s*/, " = "); |
| 61 | + f = f.replace(/\s*;\s*$/, ";"); |
| 62 | + return f; |
| 63 | +} |
| 64 | +function formatField(line) { |
| 65 | + let f = line; |
| 66 | + f = f.replace(/:\s*/g, ": "); |
| 67 | + f = f.replace(/,\s*/g, ", "); |
| 68 | + f = f.replace(/,\s*$/, ","); |
| 69 | + return f; |
| 70 | +} |
| 71 | + |
| 72 | +function formatWitContent(content, { tabSize = 2, insertSpaces = true } = {}) { |
| 73 | + const lines = content.split(/\r?\n/); |
| 74 | + const out = []; |
| 75 | + let indentLevel = 0; |
| 76 | + const indentUnit = insertSpaces ? " ".repeat(tabSize) : "\t"; |
| 77 | + for (let i = 0; i < lines.length; i++) { |
| 78 | + const raw = lines[i]; |
| 79 | + const trimmed = raw.trim(); |
| 80 | + if (trimmed === "") { |
| 81 | + out.push(""); |
| 82 | + continue; |
| 83 | + } |
| 84 | + if (/^\/\//.test(trimmed)) { |
| 85 | + out.push(indentUnit.repeat(indentLevel) + trimmed); |
| 86 | + continue; |
| 87 | + } |
| 88 | + if (/^\}/.test(trimmed)) indentLevel = Math.max(0, indentLevel - 1); |
| 89 | + let formatted = trimmed; |
| 90 | + if (trimmed.startsWith("package ")) formatted = formatPackage(trimmed); |
| 91 | + else if (trimmed.startsWith("interface ")) formatted = formatNamedBlock(trimmed); |
| 92 | + else if (trimmed.startsWith("world ")) formatted = formatNamedBlock(trimmed); |
| 93 | + else if (isTypeDecl(trimmed)) formatted = formatNamedBlock(trimmed); |
| 94 | + else if (trimmed.startsWith("type ") && trimmed.includes("=")) formatted = formatTypeAlias(trimmed); |
| 95 | + else if (isFuncDecl(trimmed)) formatted = formatFunc(trimmed); |
| 96 | + else if (trimmed.startsWith("import ") || trimmed.startsWith("export ")) |
| 97 | + formatted = formatImportExport(trimmed); |
| 98 | + else if (trimmed.startsWith("use ")) formatted = formatUse(trimmed); |
| 99 | + else if (isFieldDecl(trimmed)) formatted = formatField(trimmed); |
| 100 | + out.push(indentUnit.repeat(indentLevel) + formatted); |
| 101 | + if (isOpeningBrace(trimmed)) indentLevel++; |
| 102 | + } |
| 103 | + return out.join("\n"); |
| 104 | +} |
| 105 | + |
| 106 | +function collect(dir) { |
| 107 | + const acc = []; |
| 108 | + const stack = [dir]; |
| 109 | + while (stack.length) { |
| 110 | + const cur = stack.pop(); |
| 111 | + const ents = fs.readdirSync(cur, { withFileTypes: true }); |
| 112 | + for (const e of ents) { |
| 113 | + const p = path.join(cur, e.name); |
| 114 | + if (e.isDirectory()) stack.push(p); |
| 115 | + else if (e.isFile() && p.endsWith(".wit")) acc.push(p); |
| 116 | + } |
| 117 | + } |
| 118 | + return acc.sort(); |
| 119 | +} |
| 120 | + |
| 121 | +const root = path.resolve("tests"); |
| 122 | +if (!fs.existsSync(root)) { |
| 123 | + console.error("tests directory not found"); |
| 124 | + process.exit(1); |
| 125 | +} |
| 126 | +const files = collect(root); |
| 127 | +let changed = 0; |
| 128 | +for (const f of files) { |
| 129 | + const original = fs.readFileSync(f, "utf8"); |
| 130 | + const formatted = formatWitContent(original, { tabSize: 2, insertSpaces: true }); |
| 131 | + if (formatted !== original) { |
| 132 | + fs.writeFileSync(f, formatted + (formatted.endsWith("\n") ? "" : "\n")); |
| 133 | + changed++; |
| 134 | + console.log("Formatted:", path.relative(process.cwd(), f)); |
| 135 | + } |
| 136 | +} |
| 137 | +console.log(`Done. ${changed} file(s) updated.`); |
0 commit comments