File size: 5,854 Bytes
68d7816 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | import { isAbsolute, join, normalize } from 'pathe';
import type { BashSyntaxNode } from '#/app/bashParser/bashParser';
const LISTING_COMMANDS: ReadonlySet<string> = new Set([
'ls',
'tree',
'find',
'dir',
'exa',
'eza',
'lsd',
]);
const TRANSPARENT_WRAPPERS: ReadonlySet<string> = new Set([
'program',
'list',
'pipeline',
'redirected_statement',
]);
const LS_ARG_TAKING_OPTIONS: ReadonlySet<string> = new Set([
'-w',
'--width',
'--sort',
'--block-size',
'-I',
'--ignore',
'--hide',
'--format',
'--time-style',
'--indicator-style',
'--quoting-style',
'-T',
'--tabsize',
]);
const TREE_LIKE_ARG_TAKING_OPTIONS: ReadonlySet<string> = new Set([
'-w',
'--width',
'--sort',
'--block-size',
'-I',
'--ignore',
'--ignore-glob',
'--hide',
'--format',
'--time-style',
'--indicator-style',
'--hyperlink',
'--quoting-style',
'-T',
'--tabsize',
'-L',
'--level',
'--depth',
'-P',
'-o',
'--filelimit',
'--charset',
'--timefmt',
'-s',
]);
const FIND_GLOBAL_OPTIONS: ReadonlySet<string> = new Set(['-H', '-L', '-P']);
const UNSAFE_OPERAND = /[$`*?[\]~]/;
export function extractBashTargetDirs(
root: BashSyntaxNode,
cwd: string,
homeDir: string,
): string[] {
const commands: CollectedCommand[] = [];
collectCommands(root, commands);
const targets: string[] = [];
const seen = new Set<string>();
let base: string | undefined = cwd;
const push = (operand: string): void => {
let dir: string;
if (isAbsolute(operand)) {
dir = normalize(operand);
} else {
const currentBase = base;
if (currentBase === undefined) return;
dir = normalize(join(currentBase, operand));
}
if (seen.has(dir)) return;
seen.add(dir);
targets.push(dir);
};
for (const { node, inPipeline } of commands) {
const { name, args, dropped } = commandNameAndArgs(node);
if (name === undefined) continue;
if (name === 'cd') {
if (inPipeline) continue;
if (dropped) {
base = undefined;
continue;
}
const target = args[0];
if (args.length === 1 && target !== undefined && !target.startsWith('-')) {
if (isAbsolute(target)) {
base = normalize(target);
} else if (base !== undefined) {
base = normalize(join(base, target));
}
} else if (args.length === 0) {
base = homeDir;
} else {
base = undefined;
}
continue;
}
if (!LISTING_COMMANDS.has(name)) continue;
const operands = name === 'find' ? takeLeadingPaths(args) : dropFlags(args, name);
if (operands.length === 0) {
if (!dropped) push('.');
continue;
}
for (const operand of operands) push(operand);
}
return targets;
}
interface CollectedCommand {
readonly node: BashSyntaxNode;
readonly inPipeline: boolean;
}
function collectCommands(node: BashSyntaxNode, out: CollectedCommand[], inPipeline = false): void {
if (node.type === 'command') {
out.push({ node, inPipeline });
return;
}
if (!TRANSPARENT_WRAPPERS.has(node.type)) return;
const nested = inPipeline || node.type === 'pipeline';
for (const child of node.children) collectCommands(child, out, nested);
}
function commandNameAndArgs(command: BashSyntaxNode): {
name: string | undefined;
args: string[];
dropped: boolean;
} {
const nameIndex = command.children.findIndex((child) => child.type === 'command_name');
const nameNode = nameIndex >= 0 ? command.children[nameIndex] : undefined;
const nameWord = nameNode?.children.find((child) => child.isNamed);
const rawName = nameWord === undefined ? undefined : literalText(nameWord);
if (rawName === undefined || rawName.length === 0 || rawName.includes('/')) {
return { name: undefined, args: [], dropped: false };
}
const args: string[] = [];
let dropped = false;
for (const child of command.children.slice(nameIndex + 1)) {
const value = literalText(child);
if (value === undefined) {
dropped = true;
} else if (value.length > 0) {
args.push(value);
}
}
return { name: rawName, args, dropped };
}
function literalText(node: BashSyntaxNode): string | undefined {
switch (node.type) {
case 'word': {
const raw = node.text;
if (UNSAFE_OPERAND.test(raw)) return undefined;
const unescaped = raw.replaceAll(/\\(.)/gs, '$1');
return UNSAFE_OPERAND.test(unescaped) ? undefined : unescaped;
}
case 'number':
return node.text;
case 'raw_string': {
if (node.text.length < 2) return undefined;
const value = node.text.slice(1, -1);
return UNSAFE_OPERAND.test(value) ? undefined : value;
}
case 'string': {
let value = '';
for (const child of node.children) {
if (child.type === 'string_content') {
value += child.text;
} else if (child.isNamed) {
return undefined;
}
}
return UNSAFE_OPERAND.test(value) ? undefined : value;
}
default:
return undefined;
}
}
function dropFlags(args: readonly string[], command: string): string[] {
const argumentTakingOptions =
command === 'ls' || command === 'dir'
? LS_ARG_TAKING_OPTIONS
: TREE_LIKE_ARG_TAKING_OPTIONS;
const out: string[] = [];
for (let i = 0; i < args.length; i += 1) {
const arg = args[i]!;
if (!arg.startsWith('-')) {
out.push(arg);
continue;
}
if (!arg.includes('=') && argumentTakingOptions.has(arg)) {
i += 1;
}
}
return out;
}
function takeLeadingPaths(args: readonly string[]): string[] {
const out: string[] = [];
for (const arg of args) {
if (FIND_GLOBAL_OPTIONS.has(arg) || arg === '--') continue;
if (arg.startsWith('-') || arg === '(' || arg === ')' || arg === '!') break;
out.push(arg);
}
return out;
}
|