|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { readFile, readdir, writeFile } from "node:fs/promises"; |
| 4 | +import { extname, resolve } from "node:path"; |
| 5 | +import { pathToFileURL } from "node:url"; |
| 6 | + |
| 7 | +const methods = new Set(["record", "nonEmptyObject"]); |
| 8 | +const ignoredDirectories = new Set([".git", "node_modules"]); |
| 9 | + |
| 10 | +function tokenize(source) { |
| 11 | + const tokens = []; |
| 12 | + let index = 0; |
| 13 | + |
| 14 | + while (index < source.length) { |
| 15 | + const start = index; |
| 16 | + const char = source[index]; |
| 17 | + const next = source[index + 1]; |
| 18 | + |
| 19 | + if (/\s/.test(char)) { |
| 20 | + index += 1; |
| 21 | + } else if (char === "/" && next === "/") { |
| 22 | + index = source.indexOf("\n", index + 2); |
| 23 | + if (index === -1) break; |
| 24 | + } else if (char === "/" && next === "*") { |
| 25 | + index = source.indexOf("*/", index + 2); |
| 26 | + index = index === -1 ? source.length : index + 2; |
| 27 | + } else if (char === "'" || char === "\"" || char === "`") { |
| 28 | + const quote = char; |
| 29 | + index += 1; |
| 30 | + while (index < source.length) { |
| 31 | + if (source[index] === "\\") { |
| 32 | + index += 2; |
| 33 | + } else if (source[index] === quote) { |
| 34 | + index += 1; |
| 35 | + break; |
| 36 | + } else { |
| 37 | + index += 1; |
| 38 | + } |
| 39 | + } |
| 40 | + } else if (/[A-Za-z_$]/.test(char)) { |
| 41 | + index += 1; |
| 42 | + while (index < source.length && /[\w$]/.test(source[index])) index += 1; |
| 43 | + tokens.push({ value: source.slice(start, index), start, end: index }); |
| 44 | + } else { |
| 45 | + index += 1; |
| 46 | + tokens.push({ value: char, start, end: index }); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + return tokens; |
| 51 | +} |
| 52 | + |
| 53 | +function closingBrace(tokens, openingIndex) { |
| 54 | + let depth = 0; |
| 55 | + for (let index = openingIndex; index < tokens.length; index += 1) { |
| 56 | + if (tokens[index].value === "{") depth += 1; |
| 57 | + if (tokens[index].value === "}") depth -= 1; |
| 58 | + if (depth === 0) return index; |
| 59 | + } |
| 60 | + return undefined; |
| 61 | +} |
| 62 | + |
| 63 | +export function lintSource(source) { |
| 64 | + const tokens = tokenize(source); |
| 65 | + const problems = []; |
| 66 | + |
| 67 | + for (let index = 0; index <= tokens.length - 5; index += 1) { |
| 68 | + const [schema, dot, method, openCall] = tokens.slice(index, index + 4); |
| 69 | + if ( |
| 70 | + tokens[index - 1]?.value === "." |
| 71 | + || schema.value !== "s" |
| 72 | + || dot.value !== "." |
| 73 | + || !methods.has(method.value) |
| 74 | + || openCall.value !== "(" |
| 75 | + ) { |
| 76 | + continue; |
| 77 | + } |
| 78 | + |
| 79 | + let objectIndex = index + 4; |
| 80 | + while (tokens[objectIndex]?.value === "(") objectIndex += 1; |
| 81 | + const object = tokens[objectIndex]; |
| 82 | + if (object?.value !== "{") continue; |
| 83 | + |
| 84 | + const closingIndex = closingBrace(tokens, objectIndex); |
| 85 | + const wrapperCount = objectIndex - (index + 4); |
| 86 | + const wrappersClose = Array.from( |
| 87 | + { length: wrapperCount }, |
| 88 | + (_, offset) => tokens[(closingIndex ?? tokens.length) + offset + 1]?.value, |
| 89 | + ).every((value) => value === ")"); |
| 90 | + if (closingIndex !== undefined && wrappersClose) { |
| 91 | + problems.push({ |
| 92 | + start: object.start, |
| 93 | + end: tokens[closingIndex].end, |
| 94 | + message: `Wrap object-valued record fields with s.object(...).`, |
| 95 | + }); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return problems; |
| 100 | +} |
| 101 | + |
| 102 | +export function fixSource(source, problems = lintSource(source)) { |
| 103 | + let fixed = source; |
| 104 | + const edits = problems |
| 105 | + .flatMap(({ start, end }) => [ |
| 106 | + { index: start, text: "s.object(" }, |
| 107 | + { index: end, text: ")" }, |
| 108 | + ]) |
| 109 | + .sort((left, right) => right.index - left.index); |
| 110 | + for (const edit of edits) { |
| 111 | + fixed = `${fixed.slice(0, edit.index)}${edit.text}${fixed.slice(edit.index)}`; |
| 112 | + } |
| 113 | + return fixed; |
| 114 | +} |
| 115 | + |
| 116 | +async function sourceFiles(paths) { |
| 117 | + const files = []; |
| 118 | + for (const path of paths) { |
| 119 | + const entries = await readdir(path, { withFileTypes: true }).catch(() => undefined); |
| 120 | + if (!entries) { |
| 121 | + if (extname(path) === ".ts") files.push(path); |
| 122 | + continue; |
| 123 | + } |
| 124 | + for (const entry of entries) { |
| 125 | + if (entry.isDirectory() && ignoredDirectories.has(entry.name)) continue; |
| 126 | + const child = resolve(path, entry.name); |
| 127 | + if (entry.isDirectory()) files.push(...await sourceFiles([child])); |
| 128 | + else if (extname(entry.name) === ".ts") files.push(child); |
| 129 | + } |
| 130 | + } |
| 131 | + return files; |
| 132 | +} |
| 133 | + |
| 134 | +async function main(argv) { |
| 135 | + const fix = argv.includes("--fix"); |
| 136 | + const paths = argv.filter((arg) => arg !== "--fix").map((path) => resolve(path)); |
| 137 | + if (paths.length === 0) { |
| 138 | + throw new Error("Usage: node skills/rig/eslint/lint.js [--fix] <file-or-directory> [...]"); |
| 139 | + } |
| 140 | + |
| 141 | + let failures = 0; |
| 142 | + for (const file of await sourceFiles(paths)) { |
| 143 | + const source = await readFile(file, "utf8"); |
| 144 | + const problems = lintSource(source); |
| 145 | + if (problems.length === 0) continue; |
| 146 | + if (fix) { |
| 147 | + await writeFile(file, fixSource(source, problems)); |
| 148 | + continue; |
| 149 | + } |
| 150 | + failures += problems.length; |
| 151 | + for (const problem of problems) { |
| 152 | + const line = source.slice(0, problem.start).split("\n").length; |
| 153 | + console.error(`${file}:${line}: ${problem.message}`); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + if (failures > 0) process.exitCode = 1; |
| 158 | +} |
| 159 | + |
| 160 | +const isMain = process.argv[1] && pathToFileURL(resolve(process.argv[1])).href === import.meta.url; |
| 161 | +if (isMain) { |
| 162 | + main(process.argv.slice(2)).catch((error) => { |
| 163 | + console.error(error.message); |
| 164 | + process.exitCode = 1; |
| 165 | + }); |
| 166 | +} |
0 commit comments