@agent-corpus/policy-manager 0.5.0 → 0.6.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 +54 -10
- package/dist/adapters.d.ts +3 -1
- package/dist/adapters.js +18 -4
- package/dist/audit.js +16 -2
- package/dist/cli.js +10 -3
- package/dist/config.js +7 -1
- package/dist/created.d.ts +44 -0
- package/dist/created.js +236 -0
- package/dist/evidence.d.ts +53 -1
- package/dist/evidence.js +201 -6
- package/dist/flow.d.ts +2 -1
- package/dist/flow.js +856 -76
- package/dist/index.d.ts +3 -2
- package/dist/index.js +3 -2
- package/dist/permissive.d.ts +11 -3
- package/dist/permissive.js +648 -28
- package/dist/policy.d.ts +3 -1
- package/dist/policy.js +78 -35
- package/dist/principle.d.ts +8 -0
- package/dist/principle.js +8 -0
- package/dist/profile.d.ts +8 -0
- package/dist/profile.js +26 -0
- package/dist/recoverable.d.ts +41 -0
- package/dist/recoverable.js +391 -0
- package/dist/script.d.ts +35 -1
- package/dist/script.js +215 -17
- package/dist/service.js +2 -2
- package/dist/session.d.ts +4 -1
- package/dist/session.js +44 -15
- package/dist/types.d.ts +9 -3
- package/dist/types.js +12 -1
- package/package.json +2 -2
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
import { lstatSync } from "node:fs";
|
|
3
|
+
import { basename, dirname, join, relative, resolve, sep } from "node:path";
|
|
4
|
+
import { createdBySession } from "./created.js";
|
|
5
|
+
import { canonical, inside, isProtectedPath, protectedPath, sensitive } from "./evidence.js";
|
|
6
|
+
import { parseScript } from "./script.js";
|
|
7
|
+
import { parseShell } from "./shell.js";
|
|
8
|
+
/**
|
|
9
|
+
* Deletions and resets that Git can undo: tracked content that is unmodified against HEAD, in a
|
|
10
|
+
* linked worktree on a feature branch. Evidence is read from Git at decision time and never cached.
|
|
11
|
+
* Any doubt returns false, which leaves the caller's human decision in place.
|
|
12
|
+
*/
|
|
13
|
+
const INERT = new Set(["cd", "pwd", "echo", "ls", "true"]);
|
|
14
|
+
const DESTRUCTIVE_GIT = new Set(["rm", "reset", "checkout", "restore"]);
|
|
15
|
+
/**
|
|
16
|
+
* Evidence holds only when nothing else in the script can change the files first (`mv u t; rm t`).
|
|
17
|
+
* The script may contain these deletions plus `cd` and a few inert commands, without redirections,
|
|
18
|
+
* assignments, wrappers, variables or substitutions.
|
|
19
|
+
*/
|
|
20
|
+
/** Words of a plain simple command: no assignment, redirection, variable or substitution. */
|
|
21
|
+
function plainWords(command) {
|
|
22
|
+
if (command?.kind !== "simple" || command.assigns.length || command.redirects.length)
|
|
23
|
+
return undefined;
|
|
24
|
+
const text = (word) => word.subs.length || !word.parts.every((part) => typeof part === "string")
|
|
25
|
+
? undefined
|
|
26
|
+
: word.parts.join("");
|
|
27
|
+
const words = command.argv.map(text);
|
|
28
|
+
return words.every((word) => word !== undefined) ? words : undefined;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The one directory a script deletes in when its whole effect is `rm` inside linked feature
|
|
32
|
+
* worktrees, or undefined. The script may hold only `rm` and inert commands, with no redirection,
|
|
33
|
+
* assignment, variable or substitution, and at most a leading `cd <dir>` whose list is joined by
|
|
34
|
+
* `&&` alone: if the cd fails nothing else runs. Every `rm` must be a worktree deletion, so one
|
|
35
|
+
* classifier verdict on the first cannot carry an unjudged later command.
|
|
36
|
+
*/
|
|
37
|
+
export function deletionScriptCwd(request) {
|
|
38
|
+
const text = request.input.command ?? request.input.cmd;
|
|
39
|
+
const script = typeof text === "string" ? parseScript(text) : undefined;
|
|
40
|
+
if (!script?.items.length)
|
|
41
|
+
return undefined;
|
|
42
|
+
let cwd = request.cwd;
|
|
43
|
+
const deletions = [];
|
|
44
|
+
for (const [i, item] of script.items.entries()) {
|
|
45
|
+
if (item.background)
|
|
46
|
+
return undefined;
|
|
47
|
+
const nodes = [item.node.first, ...item.node.rest.map((r) => r.node)];
|
|
48
|
+
for (const [j, node] of nodes.entries()) {
|
|
49
|
+
if (node.negated || node.stages.length !== 1)
|
|
50
|
+
return undefined;
|
|
51
|
+
const [program, ...args] = plainWords(node.stages[0]) ?? [];
|
|
52
|
+
if (program === "cd") {
|
|
53
|
+
const only = i === 0 && j === 0 && script.items.length === 1;
|
|
54
|
+
const anded = item.node.rest.every((r) => r.op === "&&");
|
|
55
|
+
if (!only || !anded || args.length !== 1 || /^[~-]/.test(args[0]))
|
|
56
|
+
return undefined;
|
|
57
|
+
cwd = resolve(cwd, args[0]);
|
|
58
|
+
}
|
|
59
|
+
else if (program === "rm")
|
|
60
|
+
deletions.push(args);
|
|
61
|
+
else if (!program || !INERT.has(program))
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return deletions.length && deletions.every((args) => worktreeDeletion(cwd, args))
|
|
66
|
+
? cwd
|
|
67
|
+
: undefined;
|
|
68
|
+
}
|
|
69
|
+
export function soleEffect(request) {
|
|
70
|
+
const command = request.input.command ?? request.input.cmd;
|
|
71
|
+
if (typeof command !== "string")
|
|
72
|
+
return false;
|
|
73
|
+
const pipelines = parseShell(command);
|
|
74
|
+
if (!pipelines)
|
|
75
|
+
return false;
|
|
76
|
+
return pipelines.every((stages) => stages.length === 1 &&
|
|
77
|
+
stages.every((c) => !c.env.length &&
|
|
78
|
+
!c.redirects.length &&
|
|
79
|
+
!c.subs.length &&
|
|
80
|
+
c.stdin === undefined &&
|
|
81
|
+
(INERT.has(c.argv[0] ?? "") ||
|
|
82
|
+
c.argv[0] === "rm" ||
|
|
83
|
+
(c.argv[0] === "git" && DESTRUCTIVE_GIT.has(c.argv[1] ?? "")))));
|
|
84
|
+
}
|
|
85
|
+
/** Like evidence.git, with room for whole-repository listings. */
|
|
86
|
+
function git(cwd, args, raw = false) {
|
|
87
|
+
const out = execFileSync("git", ["-c", "core.fsmonitor=false", "-C", cwd, ...args], {
|
|
88
|
+
encoding: "utf8",
|
|
89
|
+
timeout: 5000,
|
|
90
|
+
maxBuffer: 64 * 1024 * 1024,
|
|
91
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
92
|
+
env: Object.fromEntries(Object.entries(process.env).filter(([key]) => !key.startsWith("GIT_"))),
|
|
93
|
+
});
|
|
94
|
+
// Porcelain status starts with a meaningful space (` M`), so -z output is never trimmed.
|
|
95
|
+
return raw ? out : out.trim();
|
|
96
|
+
}
|
|
97
|
+
/** The linked worktree on a feature branch that contains `dir`, or undefined. */
|
|
98
|
+
function featureWorktree(dir) {
|
|
99
|
+
const root = canonical(git(dir, ["rev-parse", "--show-toplevel"]));
|
|
100
|
+
const gitDir = canonical(git(dir, ["rev-parse", "--absolute-git-dir"]));
|
|
101
|
+
const common = canonical(resolve(dir, git(dir, ["rev-parse", "--git-common-dir"])));
|
|
102
|
+
const branch = git(dir, ["branch", "--show-current"]);
|
|
103
|
+
if (gitDir === common || !branch || /^(?:main|master)$/.test(branch))
|
|
104
|
+
return undefined;
|
|
105
|
+
let fallback = "";
|
|
106
|
+
try {
|
|
107
|
+
fallback = git(dir, ["symbolic-ref", "--short", "refs/remotes/origin/HEAD"]);
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
/* no remote default recorded */
|
|
111
|
+
}
|
|
112
|
+
if (fallback && fallback.replace(/^origin\//, "") === branch)
|
|
113
|
+
return undefined;
|
|
114
|
+
return { root };
|
|
115
|
+
}
|
|
116
|
+
function status(root, paths, untracked) {
|
|
117
|
+
const out = git(root, [
|
|
118
|
+
"--literal-pathspecs",
|
|
119
|
+
"status",
|
|
120
|
+
"--porcelain=v1",
|
|
121
|
+
"-z",
|
|
122
|
+
"--ignore-submodules=none",
|
|
123
|
+
untracked ? "--untracked-files=all" : "--untracked-files=no",
|
|
124
|
+
...(untracked ? ["--ignored=matching"] : []),
|
|
125
|
+
"--",
|
|
126
|
+
...paths,
|
|
127
|
+
], true);
|
|
128
|
+
const fields = out.split("\0").filter(Boolean);
|
|
129
|
+
const entries = [];
|
|
130
|
+
for (let i = 0; i < fields.length; i++) {
|
|
131
|
+
const field = fields[i];
|
|
132
|
+
const xy = field.slice(0, 2);
|
|
133
|
+
entries.push({ xy, path: field.slice(3) });
|
|
134
|
+
// Renames and copies carry the original path in the next field.
|
|
135
|
+
if (/[RC]/.test(xy))
|
|
136
|
+
i++;
|
|
137
|
+
}
|
|
138
|
+
return entries;
|
|
139
|
+
}
|
|
140
|
+
/** Index flags that hide modifications from status: assume-unchanged, skip-worktree, unmerged. */
|
|
141
|
+
function hiddenFlags(root, paths) {
|
|
142
|
+
const flags = git(root, ["--literal-pathspecs", "ls-files", "-v", "-z", "--", ...paths]);
|
|
143
|
+
return flags.split("\0").some((line) => line && (/^[a-z]/.test(line) || /^[SM] /.test(line)));
|
|
144
|
+
}
|
|
145
|
+
/** Hidden flags, or a submodule anywhere under the paths. */
|
|
146
|
+
function hiddenState(root, paths) {
|
|
147
|
+
if (hiddenFlags(root, paths))
|
|
148
|
+
return true;
|
|
149
|
+
const stage = git(root, ["--literal-pathspecs", "ls-files", "-s", "-z", "--", ...paths]);
|
|
150
|
+
if (stage.split("\0").some((line) => line.startsWith("160000 ")))
|
|
151
|
+
return true;
|
|
152
|
+
const head = git(root, ["--literal-pathspecs", "ls-tree", "-r", "-z", "HEAD", "--", ...paths]);
|
|
153
|
+
return head.split("\0").some((line) => line.startsWith("160000 "));
|
|
154
|
+
}
|
|
155
|
+
function tracked(root, path) {
|
|
156
|
+
return git(root, ["--literal-pathspecs", "ls-files", "-z", "--", path]) !== "";
|
|
157
|
+
}
|
|
158
|
+
// zsh expands a leading `~` and `=name` (the path of command `name`); `-` is an option and `:`
|
|
159
|
+
// a Git pathspec magic. `./=name` stays a plain file.
|
|
160
|
+
const PLAIN_OPERAND = /^[^~:=-]/;
|
|
161
|
+
/**
|
|
162
|
+
* Resolves an operand to what the command removes, inside a feature worktree, or undefined.
|
|
163
|
+
* Parent symlinks are followed; a final symlink is the link itself unless a trailing slash
|
|
164
|
+
* follows it.
|
|
165
|
+
*/
|
|
166
|
+
function locate(cwd, operand) {
|
|
167
|
+
if (!operand || !PLAIN_OPERAND.test(operand) || /[*?[\]\\]/.test(operand))
|
|
168
|
+
return undefined;
|
|
169
|
+
const parts = operand.split("/");
|
|
170
|
+
const trailing = parts.at(-1) === "";
|
|
171
|
+
const segments = parts.filter((part, index) => part !== "" && !(index === 0 && part === "."));
|
|
172
|
+
if (!segments.length || segments.some((part) => part === "." || part === ".."))
|
|
173
|
+
return undefined;
|
|
174
|
+
const lexical = resolve(cwd, operand);
|
|
175
|
+
if (sensitive(lexical) || isProtectedPath(lexical))
|
|
176
|
+
return undefined;
|
|
177
|
+
const target = trailing
|
|
178
|
+
? canonical(lexical)
|
|
179
|
+
: join(canonical(dirname(lexical)), basename(lexical));
|
|
180
|
+
if (sensitive(target) || isProtectedPath(target))
|
|
181
|
+
return undefined;
|
|
182
|
+
const parent = canonical(dirname(target));
|
|
183
|
+
const tree = featureWorktree(parent);
|
|
184
|
+
if (!tree || !inside(tree.root, target) || target === tree.root)
|
|
185
|
+
return undefined;
|
|
186
|
+
const rel = relative(tree.root, target).split(sep).join("/");
|
|
187
|
+
if (!rel || protectedPath(rel))
|
|
188
|
+
return undefined;
|
|
189
|
+
return { root: tree.root, rel, target, trailing };
|
|
190
|
+
}
|
|
191
|
+
/** A deletion operand that names a credential or protected path, as written or resolved. */
|
|
192
|
+
export function deletesProtected(cwd, args) {
|
|
193
|
+
return args
|
|
194
|
+
.filter((word) => !word.startsWith("-"))
|
|
195
|
+
.some((word) => {
|
|
196
|
+
try {
|
|
197
|
+
const lexical = resolve(cwd, word);
|
|
198
|
+
const real = canonical(lexical);
|
|
199
|
+
return [lexical, real].some((path) => sensitive(path) || isProtectedPath(path));
|
|
200
|
+
}
|
|
201
|
+
catch {
|
|
202
|
+
return false;
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
function exists(path) {
|
|
207
|
+
try {
|
|
208
|
+
lstatSync(path);
|
|
209
|
+
return true;
|
|
210
|
+
}
|
|
211
|
+
catch {
|
|
212
|
+
return false;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
/** Operands and -r of an `rm` with only -f, -r, -R, -d and -v, or undefined. */
|
|
216
|
+
function rmOperands(args) {
|
|
217
|
+
const operands = [];
|
|
218
|
+
let options = true;
|
|
219
|
+
let recursive = false;
|
|
220
|
+
for (const word of args) {
|
|
221
|
+
if (options && word === "--")
|
|
222
|
+
options = false;
|
|
223
|
+
else if (options && word.startsWith("-")) {
|
|
224
|
+
// BSD rm stops at the first operand, so a later `-x` is a file name the checks never saw.
|
|
225
|
+
if (operands.length)
|
|
226
|
+
return undefined;
|
|
227
|
+
if (!/^-[frRdv]+$/.test(word) &&
|
|
228
|
+
!["--force", "--recursive", "--dir", "--verbose"].includes(word))
|
|
229
|
+
return undefined;
|
|
230
|
+
if (/^-[a-zA-Z]*[rR]/.test(word) || word === "--recursive")
|
|
231
|
+
recursive = true;
|
|
232
|
+
}
|
|
233
|
+
else
|
|
234
|
+
operands.push(word);
|
|
235
|
+
}
|
|
236
|
+
return operands.length ? { operands, recursive } : undefined;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* `rm` that removes only tracked content identical to HEAD (no untracked, ignored, modified or
|
|
240
|
+
* staged path under any operand, no submodule): "tracked". Or, without -r, also untracked files
|
|
241
|
+
* this session created and nothing has changed since: "session". Otherwise undefined.
|
|
242
|
+
*/
|
|
243
|
+
export function rmRecoverable(cwd, args) {
|
|
244
|
+
const parsed = rmOperands(args);
|
|
245
|
+
if (!parsed)
|
|
246
|
+
return undefined;
|
|
247
|
+
let kind = "tracked";
|
|
248
|
+
try {
|
|
249
|
+
for (const operand of parsed.operands) {
|
|
250
|
+
const found = locate(cwd, operand);
|
|
251
|
+
if (!found || !exists(found.target))
|
|
252
|
+
return undefined;
|
|
253
|
+
if (!tracked(found.root, found.rel)) {
|
|
254
|
+
if (parsed.recursive || found.trailing || !createdBySession(found.target))
|
|
255
|
+
return undefined;
|
|
256
|
+
kind = "session";
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
if (status(found.root, [found.rel], true).length)
|
|
260
|
+
return undefined;
|
|
261
|
+
if (hiddenState(found.root, [found.rel]))
|
|
262
|
+
return undefined;
|
|
263
|
+
}
|
|
264
|
+
return kind;
|
|
265
|
+
}
|
|
266
|
+
catch {
|
|
267
|
+
return undefined;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* An `rm` whose every operand is a plain path inside a linked feature worktree (not the worktree
|
|
272
|
+
* itself, nothing protected): local file deletion the classifier may judge. Anything else that is
|
|
273
|
+
* destructive stays a person's decision.
|
|
274
|
+
*/
|
|
275
|
+
export function worktreeDeletion(cwd, args) {
|
|
276
|
+
const parsed = rmOperands(args);
|
|
277
|
+
try {
|
|
278
|
+
return !!parsed && parsed.operands.every((operand) => locate(cwd, operand) !== undefined);
|
|
279
|
+
}
|
|
280
|
+
catch {
|
|
281
|
+
return false;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* `git rm -f`: the same evidence as rm, but a path already deleted from the working tree is fine,
|
|
286
|
+
* since only the index changes.
|
|
287
|
+
*/
|
|
288
|
+
export function gitRmRecoverable(cwd, rest) {
|
|
289
|
+
const paths = [];
|
|
290
|
+
let options = true;
|
|
291
|
+
for (const word of rest) {
|
|
292
|
+
if (options && word === "--")
|
|
293
|
+
options = false;
|
|
294
|
+
else if (options && word.startsWith("-")) {
|
|
295
|
+
if (!/^-[frqn]+$/.test(word) &&
|
|
296
|
+
!["--force", "--quiet", "--cached", "--dry-run", "--ignore-unmatch"].includes(word))
|
|
297
|
+
return false;
|
|
298
|
+
}
|
|
299
|
+
else
|
|
300
|
+
paths.push(word);
|
|
301
|
+
}
|
|
302
|
+
if (!paths.length)
|
|
303
|
+
return false;
|
|
304
|
+
try {
|
|
305
|
+
for (const path of paths) {
|
|
306
|
+
const found = locate(cwd, path);
|
|
307
|
+
if (!found || !tracked(found.root, found.rel))
|
|
308
|
+
return false;
|
|
309
|
+
if (status(found.root, [found.rel], true).some((e) => e.xy !== " D"))
|
|
310
|
+
return false;
|
|
311
|
+
if (hiddenState(found.root, [found.rel]))
|
|
312
|
+
return false;
|
|
313
|
+
}
|
|
314
|
+
return true;
|
|
315
|
+
}
|
|
316
|
+
catch {
|
|
317
|
+
return false;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* `git checkout [--] <paths>` and `git restore [-W|-SW] <paths>`: the working tree loses nothing
|
|
322
|
+
* when it already matches the source (index, or HEAD for --staged --worktree), or the file is only
|
|
323
|
+
* missing from it.
|
|
324
|
+
*/
|
|
325
|
+
export function restoreRecoverable(cwd, paths, fromHead) {
|
|
326
|
+
if (!paths.length)
|
|
327
|
+
return false;
|
|
328
|
+
try {
|
|
329
|
+
for (const path of paths) {
|
|
330
|
+
if (path === ".") {
|
|
331
|
+
const here = canonical(cwd);
|
|
332
|
+
const tree = featureWorktree(here);
|
|
333
|
+
if (!tree)
|
|
334
|
+
return false;
|
|
335
|
+
const rel = relative(tree.root, here).split(sep).join("/");
|
|
336
|
+
if (protectedPath(rel) || sensitive(here) || !check(tree.root, rel || ".", fromHead))
|
|
337
|
+
return false;
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
const found = locate(cwd, path);
|
|
341
|
+
if (!found || !check(found.root, found.rel, fromHead))
|
|
342
|
+
return false;
|
|
343
|
+
}
|
|
344
|
+
return true;
|
|
345
|
+
}
|
|
346
|
+
catch {
|
|
347
|
+
return false;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
function check(root, rel, fromHead) {
|
|
351
|
+
if (!tracked(root, rel))
|
|
352
|
+
return false;
|
|
353
|
+
const allowed = (xy) => (fromHead ? xy === " D" : xy[1] === " " || xy[1] === "D");
|
|
354
|
+
if (!status(root, [rel], false).every((e) => allowed(e.xy) && !e.xy.includes("U")))
|
|
355
|
+
return false;
|
|
356
|
+
// Without --recurse-submodules these leave a submodule's own files alone.
|
|
357
|
+
return !hiddenFlags(root, [rel]);
|
|
358
|
+
}
|
|
359
|
+
const REVISION = /^[A-Za-z0-9][A-Za-z0-9/_.~^-]*$/;
|
|
360
|
+
/**
|
|
361
|
+
* `git reset --hard [<commit>]` with no uncommitted tracked change, every commit it drops from the
|
|
362
|
+
* branch still on a remote-tracking ref, and no untracked or ignored file where the target writes.
|
|
363
|
+
*/
|
|
364
|
+
export function resetRecoverable(cwd, target = "HEAD") {
|
|
365
|
+
if (!REVISION.test(target))
|
|
366
|
+
return false;
|
|
367
|
+
try {
|
|
368
|
+
const tree = featureWorktree(canonical(cwd));
|
|
369
|
+
if (!tree)
|
|
370
|
+
return false;
|
|
371
|
+
const { root } = tree;
|
|
372
|
+
if (status(root, [], false).length || hiddenFlags(root, []))
|
|
373
|
+
return false;
|
|
374
|
+
const commit = git(root, ["rev-parse", "--verify", "--end-of-options", `${target}^{commit}`]);
|
|
375
|
+
if (git(root, ["rev-list", "--count", "HEAD", "--not", commit, "--remotes"]) !== "0")
|
|
376
|
+
return false;
|
|
377
|
+
const written = git(root, ["diff", "--name-only", "--no-renames", "-z", "HEAD", commit])
|
|
378
|
+
.split("\0")
|
|
379
|
+
.filter(Boolean);
|
|
380
|
+
if (!written.length)
|
|
381
|
+
return true;
|
|
382
|
+
if (written.some((path) => protectedPath(path)))
|
|
383
|
+
return false;
|
|
384
|
+
const loose = status(root, [], true).map((e) => e.path.replace(/\/$/, ""));
|
|
385
|
+
return !written.some((path) => loose.some((u) => path === u || path.startsWith(`${u}/`) || u.startsWith(`${path}/`)));
|
|
386
|
+
}
|
|
387
|
+
catch {
|
|
388
|
+
return false;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
//# sourceMappingURL=recoverable.js.map
|
package/dist/script.d.ts
CHANGED
|
@@ -1,8 +1,29 @@
|
|
|
1
1
|
/** Stands for the unknown output of an expansion inside a word. */
|
|
2
2
|
export declare const SUBSTITUTION = "\uE000";
|
|
3
|
+
/** `${X:-w}`, `${X:+w}`, `${#X}`, `${X#p}`/`${X##p}`/`${X%p}`/`${X%%p}` and `${X:o:l}`. */
|
|
4
|
+
export type ParamOp = {
|
|
5
|
+
kind: "default";
|
|
6
|
+
word: string;
|
|
7
|
+
} | {
|
|
8
|
+
kind: "alternate";
|
|
9
|
+
word: string;
|
|
10
|
+
} | {
|
|
11
|
+
kind: "length";
|
|
12
|
+
} | {
|
|
13
|
+
kind: "strip";
|
|
14
|
+
suffix: boolean;
|
|
15
|
+
longest: boolean;
|
|
16
|
+
pattern: string;
|
|
17
|
+
} | {
|
|
18
|
+
kind: "slice";
|
|
19
|
+
offset: number;
|
|
20
|
+
length?: number;
|
|
21
|
+
};
|
|
3
22
|
export interface VarRef {
|
|
23
|
+
/** A variable name, or a positional parameter (`1`..`9`, `@`, `*`). */
|
|
4
24
|
name: string;
|
|
5
25
|
quoted: boolean;
|
|
26
|
+
op?: ParamOp;
|
|
6
27
|
}
|
|
7
28
|
export interface UnknownPart {
|
|
8
29
|
unknown: true;
|
|
@@ -57,6 +78,8 @@ export interface ForNode {
|
|
|
57
78
|
}
|
|
58
79
|
export interface WhileNode {
|
|
59
80
|
kind: "while";
|
|
81
|
+
/** `until`: the body runs while the condition fails. */
|
|
82
|
+
until?: boolean;
|
|
60
83
|
cond: ListNode;
|
|
61
84
|
body: ListNode;
|
|
62
85
|
redirects: Redir[];
|
|
@@ -79,7 +102,13 @@ export interface CaseNode {
|
|
|
79
102
|
}>;
|
|
80
103
|
redirects: Redir[];
|
|
81
104
|
}
|
|
82
|
-
|
|
105
|
+
/** `name() { ...; }` or `name() ( ... )`: defines the function, runs nothing. */
|
|
106
|
+
export interface FunctionNode {
|
|
107
|
+
kind: "function";
|
|
108
|
+
name: string;
|
|
109
|
+
body: Group | Subshell;
|
|
110
|
+
}
|
|
111
|
+
export type Command = Simple | Subshell | Group | ForNode | WhileNode | IfNode | CaseNode | FunctionNode;
|
|
83
112
|
export interface PipelineNode {
|
|
84
113
|
stages: Command[];
|
|
85
114
|
negated: boolean;
|
|
@@ -97,5 +126,10 @@ export interface ListNode {
|
|
|
97
126
|
background: boolean;
|
|
98
127
|
}>;
|
|
99
128
|
}
|
|
129
|
+
/**
|
|
130
|
+
* Applies a parameter operation to a value that is set and not empty, which is the only kind of
|
|
131
|
+
* value the evaluator knows.
|
|
132
|
+
*/
|
|
133
|
+
export declare function applyParamOp(value: string, op: ParamOp | undefined): string;
|
|
100
134
|
export declare function parseScript(command: string): ListNode | undefined;
|
|
101
135
|
//# sourceMappingURL=script.d.ts.map
|