@remylagerweij/vue-doctor 2.1.2 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/dist/agent-hook-run-C6TGHuls.js +141 -0
- package/dist/changed-lines-B1gWj36s.js +109 -0
- package/dist/cli.js +465 -55
- package/dist/constants-Il8yYQQ1.js +14 -0
- package/dist/{diagnose-aMh5eWWF.js → diagnose-BWzFVcW1.js} +73 -89
- package/dist/diagnose-BmcWVOdC.js +2 -0
- package/dist/{discover-project-Bfc9urYw.js → discover-project-BhWONopi.js} +1 -1
- package/dist/{discover-project-DdngEZRq.js → discover-project-CxUVNRN2.js} +2 -13
- package/dist/gate-DPygx02J.js +58 -0
- package/dist/get-diff-files-BmOTftg1.js +187 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +82 -14
- package/dist/logger-lb8ODdY1.js +81 -0
- package/dist/{model-eTvTct_Z.js → model-D_iIG_Kz.js} +10 -1
- package/dist/prompts-B6KyXw4n.js +2 -0
- package/dist/prompts-CeGonHoj.js +33 -0
- package/dist/rules-DcjyLWUi.js +118 -0
- package/dist/{runner-DfuzuZYa.js → runner-4Wnz5goR.js} +2 -1
- package/dist/{schema-Khrhwl9M.js → schema-BMBp1hXu.js} +2 -1
- package/dist/{schema-DHV0VPmo.js → schema-C4ucbXIS.js} +2 -1
- package/dist/{server--meyU6b-.js → server-Bhj9foBP.js} +17 -5
- package/dist/vue-doctor-plugin.d.ts +12 -0
- package/dist/vue-doctor-plugin.js +29 -1
- package/package.json +5 -5
- package/schema/report.schema.json +49 -0
- package/schema/vue-doctor.schema.json +4 -0
- package/dist/diagnose-20sE9_pC.js +0 -2
- package/dist/rules-D5a-iMzD.js +0 -318
package/README.md
CHANGED
|
@@ -331,6 +331,8 @@ console.log(`Total Findings: ${result.diagnostics.length}`);
|
|
|
331
331
|
- **[Official Documentation](https://remylagerweij.github.io/vue-doctor/)**
|
|
332
332
|
- **[Full Rules Catalog](https://remylagerweij.github.io/vue-doctor/rules/)**
|
|
333
333
|
- **[CI & GitHub PR Guide](https://remylagerweij.github.io/vue-doctor/guide/ci)**
|
|
334
|
+
- **[Editor Integration (Oxlint / ESLint plugin)](https://remylagerweij.github.io/vue-doctor/guide/editor)**
|
|
335
|
+
- **[Pre-commit Hooks](https://remylagerweij.github.io/vue-doctor/guide/pre-commit)**
|
|
334
336
|
- **[AI Agents Playbook](https://remylagerweij.github.io/vue-doctor/guide/agents)**
|
|
335
337
|
- **[Model Context Protocol (MCP) Guide](https://remylagerweij.github.io/vue-doctor/guide/mcp)**
|
|
336
338
|
- **[CLI Reference](https://remylagerweij.github.io/vue-doctor/reference/cli)**
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { spawnSync } from "node:child_process";
|
|
3
|
+
import crypto from "node:crypto";
|
|
4
|
+
import fs from "node:fs";
|
|
5
|
+
import os from "node:os";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { r as getDiffInfo, t as filterSourceFiles } from "./get-diff-files-BmOTftg1.js";
|
|
8
|
+
// src/commands/agent-hook-run.ts
|
|
9
|
+
/**
|
|
10
|
+
* Runtime of the end-of-turn hooks (`vue-doctor agents hook <claude|cursor>`), which the agent
|
|
11
|
+
* runs when it is about to finish a turn. Contract:
|
|
12
|
+
* - Fast: only the files changed since HEAD are scanned, without dead-code analysis or network,
|
|
13
|
+
* and the scan is killed after SCAN_TIMEOUT_MS. No changed source files means no scan at all.
|
|
14
|
+
* - Fail open: any problem (no git, scan failure, bad input, timeout) prints nothing and exits 0,
|
|
15
|
+
* so the hook can never trap the agent.
|
|
16
|
+
* - Cannot loop: the agent's own loop flag (`stop_hook_active` for Claude Code, `loop_count` for
|
|
17
|
+
* Cursor) makes the hook stay silent, and the same set of findings is reported at most once
|
|
18
|
+
* per session, so an agent that stops again without fixing is not nagged.
|
|
19
|
+
*/
|
|
20
|
+
const SCAN_TIMEOUT_MS = 45e3;
|
|
21
|
+
const MAX_STDIN_BYTES = 1048576;
|
|
22
|
+
const SCAN_MAX_BUFFER_BYTES = 67108864;
|
|
23
|
+
/** What the hook prints to stdout (the agent reads it); `null` means print nothing. */
|
|
24
|
+
const runAgentHook = (options) => {
|
|
25
|
+
try {
|
|
26
|
+
const { agent, input } = options;
|
|
27
|
+
if ((options.env ?? process.env)["VUE_DOCTOR_AGENT_HOOK"] === "off") return null;
|
|
28
|
+
if (agent === "claude" && input.stop_hook_active === true) return null;
|
|
29
|
+
if (agent === "cursor" && ((input.loop_count ?? 0) >= 1 || input.status !== void 0 && input.status !== "completed")) return null;
|
|
30
|
+
const directory = agent === "cursor" ? path.resolve(input.workspace_roots?.[0] ?? input.cwd ?? options.cwd) : resolveClaudeDirectory(input.cwd ?? options.cwd, options.env ?? process.env);
|
|
31
|
+
const diff = getDiffInfo(directory, "HEAD");
|
|
32
|
+
if (diff.status !== "ok" || filterSourceFiles(diff.changedFiles).length === 0) return null;
|
|
33
|
+
const reportText = (options.runScan ?? runScanProcess)(directory);
|
|
34
|
+
if (!reportText) return null;
|
|
35
|
+
const findings = parseFindings(reportText);
|
|
36
|
+
if (findings.length === 0) return null;
|
|
37
|
+
const sessionId = input.session_id ?? input.conversation_id ?? "default";
|
|
38
|
+
if (!recordIfNew(options.stateDirectory ?? os.tmpdir(), directory, sessionId, findings)) return null;
|
|
39
|
+
const message = formatMessage(findings);
|
|
40
|
+
return agent === "claude" ? JSON.stringify({
|
|
41
|
+
decision: "block",
|
|
42
|
+
reason: message
|
|
43
|
+
}) : JSON.stringify({ followup_message: message });
|
|
44
|
+
} catch {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Claude Code's `cwd` follows the agent's `cd`s, so it can sit in a subfolder with no package.json.
|
|
50
|
+
* Prefer the project directory Claude Code exports for hooks; otherwise walk up to the nearest
|
|
51
|
+
* package.json.
|
|
52
|
+
*/
|
|
53
|
+
const resolveClaudeDirectory = (cwd, env) => {
|
|
54
|
+
const projectDir = env.CLAUDE_PROJECT_DIR;
|
|
55
|
+
if (projectDir && fs.existsSync(projectDir)) return path.resolve(projectDir);
|
|
56
|
+
let current = path.resolve(cwd);
|
|
57
|
+
for (;;) {
|
|
58
|
+
if (fs.existsSync(path.join(current, "package.json"))) return current;
|
|
59
|
+
const parent = path.dirname(current);
|
|
60
|
+
if (parent === current) return path.resolve(cwd);
|
|
61
|
+
current = parent;
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
/** Runs this same CLI on the changed files. Returns the JSON report, or null on timeout or failure. */
|
|
65
|
+
const runScanProcess = (directory) => {
|
|
66
|
+
const entry = process.argv[1];
|
|
67
|
+
if (!entry) return null;
|
|
68
|
+
const result = spawnSync(process.execPath, [
|
|
69
|
+
entry,
|
|
70
|
+
directory,
|
|
71
|
+
"--diff",
|
|
72
|
+
"HEAD",
|
|
73
|
+
"--format",
|
|
74
|
+
"json",
|
|
75
|
+
"--no-timestamp",
|
|
76
|
+
"--no-dead-code",
|
|
77
|
+
"--offline",
|
|
78
|
+
"--no-color",
|
|
79
|
+
"--quiet",
|
|
80
|
+
"--yes"
|
|
81
|
+
], {
|
|
82
|
+
encoding: "utf8",
|
|
83
|
+
stdio: [
|
|
84
|
+
"ignore",
|
|
85
|
+
"pipe",
|
|
86
|
+
"ignore"
|
|
87
|
+
],
|
|
88
|
+
timeout: SCAN_TIMEOUT_MS,
|
|
89
|
+
maxBuffer: SCAN_MAX_BUFFER_BYTES
|
|
90
|
+
});
|
|
91
|
+
if (result.error || typeof result.stdout !== "string" || result.stdout.length === 0) return null;
|
|
92
|
+
return result.stdout;
|
|
93
|
+
};
|
|
94
|
+
const parseFindings = (reportText) => {
|
|
95
|
+
const all = (JSON.parse(reportText).projects ?? []).flatMap((project) => project.findings ?? []);
|
|
96
|
+
return [...all.filter((finding) => finding.severity === "error"), ...all.filter((finding) => finding.severity !== "error")];
|
|
97
|
+
};
|
|
98
|
+
const findingKey = (finding) => finding.fingerprint ?? `${finding.ruleId}:${finding.file}:${finding.line}`;
|
|
99
|
+
/** Returns true (and remembers the set) when this session has not been told about exactly these findings yet. */
|
|
100
|
+
const recordIfNew = (stateDirectory, directory, sessionId, findings) => {
|
|
101
|
+
const digest = (value) => crypto.createHash("sha256").update(value).digest("hex");
|
|
102
|
+
const stateFile = path.join(stateDirectory, `vue-doctor-agent-hook-${digest(`${directory}\0${sessionId}`).slice(0, 16)}.json`);
|
|
103
|
+
const current = digest(findings.map(findingKey).sort().join("\n"));
|
|
104
|
+
try {
|
|
105
|
+
if (JSON.parse(fs.readFileSync(stateFile, "utf8")).reported === current) return false;
|
|
106
|
+
} catch {}
|
|
107
|
+
try {
|
|
108
|
+
fs.writeFileSync(stateFile, JSON.stringify({ reported: current }), {
|
|
109
|
+
encoding: "utf8",
|
|
110
|
+
mode: 384
|
|
111
|
+
});
|
|
112
|
+
} catch {}
|
|
113
|
+
return true;
|
|
114
|
+
};
|
|
115
|
+
const formatMessage = (findings) => {
|
|
116
|
+
const shown = findings.slice(0, 10);
|
|
117
|
+
const lines = shown.map((finding) => {
|
|
118
|
+
return `- ${finding.line > 0 ? `${finding.file}:${finding.line}` : finding.file} [${finding.severity}] ${finding.ruleId}: ${finding.message}`;
|
|
119
|
+
});
|
|
120
|
+
const more = findings.length > shown.length ? `\n...and ${findings.length - shown.length} more.` : "";
|
|
121
|
+
return [`Vue Doctor found ${findings.length} issue${findings.length === 1 ? "" : "s"} in your uncommitted files (changes since HEAD):`, ...lines].join("\n") + `${more}\nFix the errors first (\`npx @remylagerweij/vue-doctor explain <ruleId>\` shows remediation guidance, \`--fix\` applies safe codemods). If a finding is intentional, say so briefly and finish. This check reports a given set of findings only once.`;
|
|
122
|
+
};
|
|
123
|
+
/** Reads the hook payload from stdin (bounded) and parses it as JSON; resolves to `{}` when stdin is a terminal, too large, not a JSON object, or unreadable. */
|
|
124
|
+
const readHookInput = async (stream = process.stdin) => {
|
|
125
|
+
if (stream.isTTY) return {};
|
|
126
|
+
const chunks = [];
|
|
127
|
+
let size = 0;
|
|
128
|
+
try {
|
|
129
|
+
for await (const chunk of stream) {
|
|
130
|
+
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk));
|
|
131
|
+
size += buffer.length;
|
|
132
|
+
if (size > MAX_STDIN_BYTES) return {};
|
|
133
|
+
chunks.push(buffer);
|
|
134
|
+
}
|
|
135
|
+
const parsed = JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
136
|
+
return typeof parsed === "object" && parsed !== null ? parsed : {};
|
|
137
|
+
} catch {
|
|
138
|
+
return {};
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
export { readHookInput, runAgentHook };
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// src/utils/changed-lines.ts
|
|
3
|
+
const HUNK_HEADER = /^@@ -\d+(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
|
|
4
|
+
const C_ESCAPES = {
|
|
5
|
+
a: 7,
|
|
6
|
+
b: 8,
|
|
7
|
+
t: 9,
|
|
8
|
+
n: 10,
|
|
9
|
+
v: 11,
|
|
10
|
+
f: 12,
|
|
11
|
+
r: 13,
|
|
12
|
+
"\"": 34,
|
|
13
|
+
"\\": 92
|
|
14
|
+
};
|
|
15
|
+
/** Undoes git's C-style quoting of a path (`"a\tb\303\251"`), decoding octal escapes as UTF-8. */
|
|
16
|
+
const unquoteGitPath = (quoted) => {
|
|
17
|
+
if (!quoted.startsWith("\"") || !quoted.endsWith("\"") || quoted.length < 2) return quoted;
|
|
18
|
+
const body = quoted.slice(1, -1);
|
|
19
|
+
const bytes = [];
|
|
20
|
+
for (let index = 0; index < body.length; index++) {
|
|
21
|
+
if (body[index] !== "\\") {
|
|
22
|
+
const character = String.fromCodePoint(body.codePointAt(index) ?? 0);
|
|
23
|
+
bytes.push(...Buffer.from(character, "utf-8"));
|
|
24
|
+
index += character.length - 1;
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
const next = body[index + 1] ?? "";
|
|
28
|
+
const octal = /^[0-7]{3}/.exec(body.slice(index + 1));
|
|
29
|
+
if (octal) {
|
|
30
|
+
bytes.push(Number.parseInt(octal[0], 8));
|
|
31
|
+
index += 3;
|
|
32
|
+
} else if (next in C_ESCAPES) {
|
|
33
|
+
bytes.push(C_ESCAPES[next]);
|
|
34
|
+
index += 1;
|
|
35
|
+
} else bytes.push(92);
|
|
36
|
+
}
|
|
37
|
+
return Buffer.from(bytes).toString("utf-8");
|
|
38
|
+
};
|
|
39
|
+
/** Path of a `+++ ` header line, without the `b/` prefix; `null` for a deleted file (`/dev/null`). */
|
|
40
|
+
const parseNewPath = (header) => {
|
|
41
|
+
let raw = header.slice(4);
|
|
42
|
+
if (!raw.startsWith("\"") && raw.endsWith(" ")) raw = raw.slice(0, -1);
|
|
43
|
+
if (raw === "/dev/null") return null;
|
|
44
|
+
const unquoted = unquoteGitPath(raw);
|
|
45
|
+
return unquoted.startsWith("b/") ? unquoted.slice(2) : unquoted;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Parses `git diff --unified=0 --src-prefix=a/ --dst-prefix=b/` output into the changed lines on
|
|
49
|
+
* the new side. Hunk bodies are skipped by their line counts, so content that looks like a header
|
|
50
|
+
* (an added line `++ x` shows as `+++ x`) is never taken for one. Deleted files, binary files and
|
|
51
|
+
* pure deletions contribute nothing; new files are `"all"`.
|
|
52
|
+
*/
|
|
53
|
+
const parseUnifiedDiff = (diffOutput) => {
|
|
54
|
+
const changed = {};
|
|
55
|
+
let currentPath = null;
|
|
56
|
+
let isNewFile = false;
|
|
57
|
+
let remainingOld = 0;
|
|
58
|
+
let remainingNew = 0;
|
|
59
|
+
for (const line of diffOutput.split("\n")) {
|
|
60
|
+
if (remainingOld > 0 || remainingNew > 0) {
|
|
61
|
+
const marker = line[0];
|
|
62
|
+
if (marker === "\\") continue;
|
|
63
|
+
if (marker === "-" && remainingOld > 0) {
|
|
64
|
+
remainingOld--;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
if (marker === "+" && remainingNew > 0) {
|
|
68
|
+
remainingNew--;
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
if (marker === " " && remainingOld > 0 && remainingNew > 0) {
|
|
72
|
+
remainingOld--;
|
|
73
|
+
remainingNew--;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
remainingOld = 0;
|
|
77
|
+
remainingNew = 0;
|
|
78
|
+
}
|
|
79
|
+
if (line.startsWith("diff --git ")) {
|
|
80
|
+
currentPath = null;
|
|
81
|
+
isNewFile = false;
|
|
82
|
+
} else if (line.startsWith("new file mode ")) isNewFile = true;
|
|
83
|
+
else if (line.startsWith("+++ ")) {
|
|
84
|
+
currentPath = parseNewPath(line);
|
|
85
|
+
if (currentPath !== null) changed[currentPath] = isNewFile ? "all" : changed[currentPath] ?? [];
|
|
86
|
+
} else if (line.startsWith("@@ ")) {
|
|
87
|
+
const match = HUNK_HEADER.exec(line);
|
|
88
|
+
if (!match) continue;
|
|
89
|
+
remainingOld = match[1] === void 0 ? 1 : Number(match[1]);
|
|
90
|
+
const start = Number(match[2]);
|
|
91
|
+
const count = match[3] === void 0 ? 1 : Number(match[3]);
|
|
92
|
+
remainingNew = count;
|
|
93
|
+
const ranges = currentPath === null ? void 0 : changed[currentPath];
|
|
94
|
+
if (count > 0 && Array.isArray(ranges)) ranges.push({
|
|
95
|
+
start,
|
|
96
|
+
end: start + count - 1
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return changed;
|
|
101
|
+
};
|
|
102
|
+
/** Whether `line` of `filePath` is a changed line; files that are not keys have none. */
|
|
103
|
+
const isChangedLine = (changedLines, filePath, line) => {
|
|
104
|
+
const ranges = changedLines[filePath];
|
|
105
|
+
if (ranges === void 0) return false;
|
|
106
|
+
if (ranges === "all") return true;
|
|
107
|
+
return ranges.some((range) => line >= range.start && line <= range.end);
|
|
108
|
+
};
|
|
109
|
+
export { parseUnifiedDiff as n, isChangedLine as t };
|