Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482/** Work-item builders for `unbash` analyzer traversal. @module */
import type { AssignmentPrefix as UnbashAssignmentPrefix, Case as UnbashCase, Command as UnbashCommand, SyntaxNode as UnbashNode, ParameterExpansionPart as UnbashParameterExpansionPart, Redirection as UnbashRedirect, Statement as UnbashStatement, Word as UnbashWord,} from 'unbash';import type { ForeignBorrowed, } from '@monochromatic-dev/ownership-marker-foreign-borrowed/ts';import type { ShellCommandContext, } from './types.ts';import { EMPTY_REDIRECTS, type WorkItem,} from './internal-types.ts';
/** Build one node work item. @param node - node to enqueue @param context - execution context inherited by node @returns node work item with no inherited redirects @example ```ts nodeWorkItem({ node, context }); ``` */function nodeWorkItem( { node, context, }: { readonly node: ForeignBorrowed<UnbashNode>; readonly context: ShellCommandContext; },): WorkItem { return { kind: 'node', node, redirects: EMPTY_REDIRECTS, context, };}
/** Build node work items in source order. @param nodes - nodes to enqueue @param context - execution context inherited by nodes @returns work items in source order @example ```ts nodeWorkItems({ nodes: pipeline.commands, context }); ``` */function nodeWorkItems( { nodes, context, }: { readonly nodes: readonly ForeignBorrowed<UnbashNode>[]; readonly context: ShellCommandContext; },): WorkItem[] { return nodes.map(function nodeToWorkItem(node,): WorkItem { return nodeWorkItem({ node, context, },); },);}
/** Build statement work items in source order. @param statements - statements to enqueue @param context - execution context inherited by statements @returns work items in source order @example ```ts statementWorkItems({ statements: script.commands, context }); ``` */function statementWorkItems( { statements, context, }: { readonly statements: readonly ForeignBorrowed<UnbashStatement>[]; readonly context: ShellCommandContext; },): WorkItem[] { return statements.map(function statementToWorkItem(statement,): WorkItem { return nodeWorkItem({ node: statement, context, },); },);}
/** Build one word work item. @param word - word to enqueue @param context - execution context inherited by word @returns singleton word work item list @example ```ts wordWorkItems({ word, context }); ``` */function wordWorkItems( { word, context, }: { readonly word: ForeignBorrowed<UnbashWord>; readonly context: ShellCommandContext; },): WorkItem[] { return [{ kind: 'word', word, context, },];}
/** Build word work items in source order. @param words - words to enqueue @param context - execution context inherited by words @returns work items in source order @example ```ts wordsWorkItems({ words: command.suffix, context }); ``` */function wordsWorkItems( { words, context, }: { readonly words: readonly ForeignBorrowed<UnbashWord>[]; readonly context: ShellCommandContext; },): WorkItem[] { return words.flatMap(function wordToWorkItems(word,): WorkItem[] { return wordWorkItems({ word, context, },); },);}
/** Build nested word items from parameter expansion. @param part - parameter expansion part @param context - execution context inherited by nested words @returns word work items in source order @example ```ts parameterWordItems({ part, context }); ``` */function parameterWordItems( { part, context, }: { readonly part: ForeignBorrowed<UnbashParameterExpansionPart>; readonly context: ShellCommandContext; },): WorkItem[] { /** Index substitutions execute independently of an optional parameter operation. */ const indexItems = part.index === undefined ? [] : wordWorkItems({ word: part.index, context, },); /** Discriminated operation contains only the words belonging to that expansion form. */ const {operation} = part; if (operation === undefined) return indexItems; if ('operand' in operation) { return [ ...indexItems, ...(operation.operand === undefined ? [] : wordWorkItems({ word: operation.operand, context, },)), ]; } if (operation.type === 'Slice') { return [ ...indexItems, ...wordWorkItems({ word: operation.offset, context, },), ...(operation.length === undefined ? [] : wordWorkItems({ word: operation.length, context, },)), ]; } if (operation.type === 'Replace') { return [ ...indexItems, ...wordWorkItems({ word: operation.pattern, context, },), ...wordWorkItems({ word: operation.replacement, context, },), ]; } return indexItems;}
/** Build assignment value word items. @param assignments - assignment prefixes to scan @param context - execution context inherited by assignment words @returns word work items in source order @example ```ts assignmentWordItems({ assignments: command.prefix, context }); ``` */function assignmentWordItems( { assignments, context, }: { readonly assignments: readonly ForeignBorrowed<UnbashAssignmentPrefix>[]; readonly context: ShellCommandContext; },): WorkItem[] { return assignments.flatMap(function assignmentWords(assignment,): WorkItem[] { return [ ...(assignment.index === undefined ? [] : wordWorkItems({ word: assignment.index, context, },)), ...wordsWorkItems({ words: assignment.value .type === 'ArrayValue' ? assignment.value .elements : [assignment.value,], context, },), ]; },);}
/** Build work items for words attached to redirects. @param redirects - redirects whose words should be scanned @param context - execution context inherited by redirect words @returns word work items in source order @example ```ts redirectWordItems({ redirects, context }); ``` */function redirectWordItems( { redirects, context, }: { readonly redirects: readonly ForeignBorrowed<UnbashRedirect>[]; readonly context: ShellCommandContext; },): WorkItem[] { return redirects.flatMap(function redirectWords(redirect,): WorkItem[] { if (redirect.type === 'HereDoc') { if (redirect.body .parts === undefined) return []; return [{ kind: 'parts', parts: redirect.body .parts, context, },]; } return redirect.target === undefined ? [] : wordWorkItems({ word: redirect.target, context, },); },);}
/** Build redirect-only work item. @param redirects - redirects to surface after child commands @param context - execution context inherited by redirect item @returns singleton redirect item when redirects exist @example ```ts redirectWorkItems({ redirects, context }); ``` */function redirectWorkItems( { redirects, context, }: { readonly redirects: readonly ForeignBorrowed<UnbashRedirect>[]; readonly context: ShellCommandContext; },): WorkItem[] { if (redirects.length === 0) return []; return [{ kind: 'redirects', redirects, context, },];}
/** Build work items for words attached to command. @param command - command whose words should be scanned @param redirects - inherited redirects to scan with command redirects @param context - execution context inherited by command words @returns word work items in source order @example ```ts commandWordItems({ command, redirects: [], context }); ``` */function commandWordItems( { command, redirects, context, }: { readonly command: ForeignBorrowed<UnbashCommand>; readonly redirects: readonly ForeignBorrowed<UnbashRedirect>[]; readonly context: ShellCommandContext; },): WorkItem[] { return [ ...assignmentWordItems({ assignments: command.prefix .filter(function isAssignment(prefix,) { return prefix.type === 'Assignment'; },), context, },), ...(command.name === undefined ? [] : wordWorkItems({ word: command.name, context, })), ...command.args .flatMap(function argumentItems(argument,): WorkItem[] { if (argument.type === 'Assignment') return assignmentWordItems({ assignments: [argument,], context, },); return wordWorkItems({ word: argument, context, },); },), ...redirectWordItems({ redirects: [ ...command.redirects, ...redirects, ], context, },), ];}
/** Build words and body commands from case statement. @param node - case node to scan @param context - execution context inherited by case items @returns work items in source order @example ```ts caseItemWorkItems({ node, context }); ``` */function caseItemWorkItems( { node, context, }: { readonly node: ForeignBorrowed<UnbashCase>; readonly context: ShellCommandContext; },): WorkItem[] { return node.items .flatMap(function caseItemWords( item: ForeignBorrowed<UnbashCase['items'][number]>, ): WorkItem[] { return [ ...wordsWorkItems({ words: item.pattern, context, },), nodeWorkItem({ node: item.body, context, },), ]; },);}
export { caseItemWorkItems, commandWordItems, nodeWorkItem, nodeWorkItems, parameterWordItems, redirectWordItems, redirectWorkItems, statementWorkItems, wordWorkItems, wordsWorkItems,};