refactor(core): remove duplicated logic in the shell scanner (#61177)

This commit is contained in:
Aiden Cline
2026-11-01 21:17:38 -06:00
committed by GitHub
Unverified
parent 756d7dac45
commit 00e97c71d9
3 changed files with 63 additions and 91 deletions
+1 -3
View File
@@ -10,8 +10,6 @@ import { Wildcard } from "../util/wildcard.js"
type Part = { type: string; text: string; raw: string; unquotedTilde?: boolean }
const CWD = new Set(["cd", "chdir", "popd", "pushd", "push-location", "set-location"])
const POWERSHELL_PATH_FLAG_RE =
/^-(?:p(?:ath|at|a)?|lp|l(?:i(?:t(?:e(?:r(?:a(?:l(?:p(?:a(?:t(?:h)?)?)?)?)?)?)?)?)?)?|psp(?:a(?:t(?:h)?)?)?):?$/i
const POWERSHELL_LITERAL_FLAG_RE = /^-(?:lp|l(?:i(?:t(?:e(?:r(?:a(?:l(?:p(?:a(?:t(?:h)?)?)?)?)?)?)?)?)?)?|psp(?:a(?:t(?:h)?)?)?):?$/i
const POWERSHELL_STACK_FLAG_RE = /^-(?:stack(?:name|nam|na|n)?|st):?$/i
@@ -504,8 +502,8 @@ function directoryArgs(command: Part[], powershell: boolean, cwd: string, shell:
complete = false
return { directories, complete }
}
pathFlag = POWERSHELL_PATH_FLAG_RE.test(part.text)
literalFlag = POWERSHELL_LITERAL_FLAG_RE.test(part.text)
pathFlag = literalFlag || /^-p(?:ath|at|a)?:?$/i.test(part.text)
continue
}
sawOperand = true
+61 -87
View File
@@ -96,10 +96,16 @@ const TOKEN_RE = /[A-Za-z_][A-Za-z0-9_]*(?=(?:\\\n)*(?:[ \t\n;|&()<>]|$))/y
const BRACE_CLOSE_AHEAD_RE = /(?:\\\n)*(?:[ \t\n;&|()<>]|$)/y
const SPACE_CONTINUATION_AHEAD_RE = /(?:\\\n)*[ \t\n]/y
const NEGATION_AHEAD_RE = /(?:\\\n)*[ \t\n(]/y
const COPROC_AHEAD_RE =
/coproc(?:[ \t]|\\\n)+(?:--(?:[ \t]|\\\n)+)?(?:[A-Za-z_][A-Za-z0-9_]*(?:[ \t]|\\\n)+)?(?=(?:!(?:[ \t]|\\\n)*)*(?:[{(]|\[\[(?:\\\n)*[ \t\n]|(?:if|while|until|for|select|case|repeat)\b))/y
const TIME_AHEAD_RE =
/time(?:[ \t]|\\\n)+(?:-p(?:[ \t]|\\\n)+)?(?:--(?:[ \t]|\\\n)+)?(?=(?:!(?:[ \t]|\\\n)*)*(?:[{(]|\[\[(?:\\\n)*[ \t\n]|(?:if|while|until|for|select|case|repeat)\b))/y
// Ahead of a coproc or time prefix: optional negations, then a compound command.
const COMPOUND_AHEAD = String.raw`(?=(?:!(?:[ \t]|\\\n)*)*(?:[{(]|\[\[(?:\\\n)*[ \t\n]|(?:if|while|until|for|select|case|repeat)\b))`
const COPROC_AHEAD_RE = new RegExp(
String.raw`coproc(?:[ \t]|\\\n)+(?:--(?:[ \t]|\\\n)+)?(?:[A-Za-z_][A-Za-z0-9_]*(?:[ \t]|\\\n)+)?` + COMPOUND_AHEAD,
"y",
)
const TIME_AHEAD_RE = new RegExp(
String.raw`time(?:[ \t]|\\\n)+(?:-p(?:[ \t]|\\\n)+)?(?:--(?:[ \t]|\\\n)+)?` + COMPOUND_AHEAD,
"y",
)
// [function] name [()] then blanks, continuations, and comments before the body.
const FUNCTION_HEAD_RE =
/(function[ \t](?:[ \t]|\\\n)*)?([A-Za-z_][\w.:+@%-]*)?(?:[ \t]|\\\n)*(\([ \t]*\))?((?:[ \t\n]|\\\n|#[^\n]*\n)*)/y
@@ -375,6 +381,7 @@ function finishBashCommand(state: BashState, boundary = false, newline = false)
const name = state.commandWordIndex
const inHeader = bashInHeader(state)
if (name >= 0 && !state.words[name]) state.invalid ??= "invalid-structure"
let owner: Command | undefined
if (name >= 0 && !inHeader) {
const resource = state.input.slice(state.commandStart, state.wordEnd).replace(/^[ \t\n]+|[ \t\n]+$/g, "")
const command: Command = {
@@ -429,14 +436,7 @@ function finishBashCommand(state: BashState, boundary = false, newline = false)
if (BASH_BINDING_BUILTINS.has(builtinName ?? "")) {
for (const heredoc of state.heredocs) heredoc.binding ??= true
}
for (const heredoc of state.heredocs) {
if (heredoc.owned) continue
heredoc.owned = true
if (newline) {
heredoc.command = command
heredoc.start = state.commandStart
}
}
owner = command
} else if (
name < 0 &&
!inHeader &&
@@ -459,17 +459,16 @@ function finishBashCommand(state: BashState, boundary = false, newline = false)
redirectPrefixEnd: prefixEnd,
}
state.commands.push(command)
for (const heredoc of state.heredocs) {
if (heredoc.owned) continue
heredoc.owned = true
if (newline) {
heredoc.command = command
heredoc.start = state.commandStart
}
}
owner = command
}
}
for (const heredoc of state.heredocs) heredoc.owned = true
for (const heredoc of state.heredocs) {
if (owner && newline && !heredoc.owned) {
heredoc.command = owner
heredoc.start = state.commandStart
}
heredoc.owned = true
}
if (boundary && !state.words.length && !state.hasRedirect && !state.compoundEnd && !inHeader)
state.invalid ??= "invalid-structure"
if (state.words.length > (state.ampersandRedirectWords ?? Infinity)) state.invalid ??= "invalid-redirect"
@@ -550,7 +549,7 @@ function scanBash(
(close === "}" || close === "nofork") &&
state.structures.length === 0 &&
(heredocs !== undefined || state.heredocs.length === 0) &&
(close === "nofork" || ((BRACE_CLOSE_AHEAD_RE.lastIndex = index + 1), BRACE_CLOSE_AHEAD_RE.test(input)))
(close === "nofork" || testAt(BRACE_CLOSE_AHEAD_RE, input, index + 1))
)
return closeBashList(state, index)
if (char === "}")
@@ -671,7 +670,7 @@ function scanBash(
context.dialect === "zsh" &&
state.wordStarted &&
!inCasePattern &&
((ZSH_GLOB_QUALIFIER_RE.lastIndex = index), ZSH_GLOB_QUALIFIER_RE.test(input))
testAt(ZSH_GLOB_QUALIFIER_RE, input, index)
) {
const end = ZSH_GLOB_QUALIFIER_RE.lastIndex - 1
bashAppend(state, input.slice(index, end + 1))
@@ -860,27 +859,23 @@ function scanBashCommandStart(
// Zsh permits a sublist or brace group directly after the value list, without do/done.
structure.phase = "do"
structure.parenthesized = true
DO_AHEAD_RE.lastIndex = end + 1
if (!DO_AHEAD_RE.test(input)) state.structures.pop()
if (!testAt(DO_AHEAD_RE, input, end + 1)) state.structures.pop()
return end
}
if (structure?.kind === "repeat" && structure.phase === "do" && char !== "\n" && char !== "#" && char !== ";") {
TOKEN_RE.lastIndex = index
if (TOKEN_RE.exec(input)?.[0] !== "do") state.structures.pop()
if (matchAt(TOKEN_RE, input, index)?.[0] !== "do") state.structures.pop()
}
const keywordStep = scanBashKeyword(state, index, depth, context)
if (keywordStep !== undefined) return keywordStep
if (structure?.kind === "for" && structure.phase === "do" && char !== "\n" && char !== "#" && char !== ";") {
SPACE_CONTINUATION_AHEAD_RE.lastIndex = index + 1
if (char !== "{" || !SPACE_CONTINUATION_AHEAD_RE.test(input)) return { kind: "opaque", reason: "compound-command" }
if (char !== "{" || !testAt(SPACE_CONTINUATION_AHEAD_RE, input, index + 1)) return { kind: "opaque", reason: "compound-command" }
state.structures.pop()
}
const atStart = atCommandStart(state)
const functionHead = atStart && char === "(" ? bashFunctionHeadLength(input, index) : 0
if (functionHead) return index + functionHead - 1
if (atStart && char === "!") {
NEGATION_AHEAD_RE.lastIndex = index + 1
if (NEGATION_AHEAD_RE.test(input)) return index
if (testAt(NEGATION_AHEAD_RE, input, index + 1)) return index
}
const forHeader = structure?.kind === "for" && structure.phase === "header"
const compoundOpen =
@@ -916,7 +911,7 @@ function scanBashCommandStart(
if (
atStart &&
(char === "(" ||
(char === "{" && ((SPACE_CONTINUATION_AHEAD_RE.lastIndex = index + 1), SPACE_CONTINUATION_AHEAD_RE.test(input))))
(char === "{" && testAt(SPACE_CONTINUATION_AHEAD_RE, input, index + 1)))
) {
const group = scanBash(input, index + 1, depth + 1, context, char === "{" ? "}" : ")", state.heredocs)
if (group.kind === "opaque") return group
@@ -929,7 +924,7 @@ function scanBashCommandStart(
if (
atStart &&
compoundOpen === "[[" &&
((SPACE_CONTINUATION_AHEAD_RE.lastIndex = index + 2), SPACE_CONTINUATION_AHEAD_RE.test(input))
testAt(SPACE_CONTINUATION_AHEAD_RE, input, index + 2)
) {
const end = scanBashConditional(input, index + 2, depth, context, state.commands)
if (typeof end === "object") return end
@@ -951,8 +946,7 @@ function scanBashKeyword(
const char = input[index]
if (!((char >= "A" && char <= "Z") || (char >= "a" && char <= "z") || char === "_")) return undefined
const structure = state.structures.at(-1)
TOKEN_RE.lastIndex = index
const token = TOKEN_RE.exec(input)?.[0]
const token = matchAt(TOKEN_RE, input, index)?.[0]
const end = index + (token?.length ?? 0) - 1
if (structure?.kind === "case" && structure.phase === "header" && token === "in") {
if (state.words.length !== 1 || state.hasRedirect) return { kind: "opaque", reason: "compound-command" }
@@ -1064,26 +1058,17 @@ function scanBashKeyword(
state.dangling = false
return end
}
if (token === "coproc") {
COPROC_AHEAD_RE.lastIndex = index
const coprocMatch = COPROC_AHEAD_RE.exec(input)?.[0]
if (coprocMatch) return index + coprocMatch.length - 1
}
if (token === "time") {
TIME_AHEAD_RE.lastIndex = index
const timeMatch = TIME_AHEAD_RE.exec(input)?.[0]
if (timeMatch) return index + timeMatch.length - 1
}
const prefix = token === "coproc" ? COPROC_AHEAD_RE : token === "time" ? TIME_AHEAD_RE : undefined
const prefixMatch = prefix && matchAt(prefix, input, index)?.[0]
if (prefixMatch) return index + prefixMatch.length - 1
return undefined
}
function bashFunctionHeadLength(input: string, start: number) {
FUNCTION_HEAD_RE.lastIndex = start
const head = FUNCTION_HEAD_RE.exec(input)
const head = matchAt(FUNCTION_HEAD_RE, input, start)
if (!head || (head[1] ? !head[2] : !head[3] || BASH_NON_FUNCTION_KEYWORDS.has(head[2] ?? ""))) return 0
FUNCTION_BODY_RE.lastIndex = start + head[0].length
// Blanks, comments, and newlines before the body stay for the caller, where newlines start heredoc bodies.
return FUNCTION_BODY_RE.test(input) ? head[0].length - head[4].length : 0
return testAt(FUNCTION_BODY_RE, input, start + head[0].length) ? head[0].length - head[4].length : 0
}
// Word text is unquoted. Quoted text follows double-quote rules. Arithmetic text, including subscripts,
@@ -1136,15 +1121,13 @@ function scanBashUnit(
char === "$" &&
context.dialect !== "bash" &&
(mode === "word" || mode === "quoted") &&
((ZSH_SUBSCRIPT_RE.lastIndex = index), ZSH_SUBSCRIPT_RE.test(input))
testAt(ZSH_SUBSCRIPT_RE, input, index)
) {
const bracket = ZSH_SUBSCRIPT_RE.lastIndex
const subscript = bashText()
const span = mode === "quoted" ? BASH_SPANS.quotedSubscript : BASH_SPANS.subscript
const end = scanBashSpan(input, bracket, depth, context, commands, span, false, subscript)
const end = scanBashSubscript(input, bracket, depth, context, commands, span, subscript)
if (typeof end === "object") return end
context.sinkBracket = true
if (bashEvaluatesExpansion(subscript, "arithmetic", context)) return { kind: "opaque", reason: "dynamic-execution" }
if (text) text.word += `${input.slice(index, bracket)}${subscript.word}]`
return end
}
@@ -1156,8 +1139,7 @@ function scanBashUnit(
: undefined
if (typeof opener === "object") return opener
if (opener === "$$") {
LINE_CONTINUATION_RE.lastIndex = index + 2
const quoteStart = LINE_CONTINUATION_RE.exec(input) ? LINE_CONTINUATION_RE.lastIndex : index + 2
const quoteStart = matchAt(LINE_CONTINUATION_RE, input, index + 2) ? LINE_CONTINUATION_RE.lastIndex : index + 2
if (input[quoteStart] === "'") {
const nextQuote = input.indexOf("'", quoteStart + 1)
if (nextQuote < 0) return { kind: "opaque", reason: "unterminated-quote" }
@@ -1224,13 +1206,11 @@ function zshEvaluates(input: string, index: number) {
let at = index + 1
if (input.startsWith("{\\\n", at)) return true
if (input[at] === "{" && input[at + 1] === "(") {
ZSH_SAFE_FLAGS_RE.lastIndex = index
if (!ZSH_SAFE_FLAGS_RE.test(input)) return true
if (!testAt(ZSH_SAFE_FLAGS_RE, input, index)) return true
at = ZSH_SAFE_FLAGS_RE.lastIndex
}
if (input[at] === "{") at++
ZSH_GLOB_MODIFIER_RE.lastIndex = at
return ZSH_GLOB_MODIFIER_RE.test(input)
return testAt(ZSH_GLOB_MODIFIER_RE, input, at)
}
function bashAppend(text: BashText, value: string) {
@@ -1280,8 +1260,8 @@ function scanBashSubscript(
context: BashContext,
commands: Command[],
span: BashSpan = BASH_SPANS.subscript,
text = bashText(),
): number | Opaque {
const text = bashText()
const end = scanBashSpan(input, start, depth, context, commands, span, false, text)
if (typeof end === "number") {
context.sinkBracket = true
@@ -1371,8 +1351,7 @@ function scanBashDollarOrBacktick(
if (end !== undefined) return end
}
if (input.startsWith("$(", start)) return scanBashNested(input, start + 2, depth, context, commands, ")", text)
NOFORK_OPEN_RE.lastIndex = start
const nofork = NOFORK_OPEN_RE.exec(input)
const nofork = matchAt(NOFORK_OPEN_RE, input, start)
if (nofork) return scanBashNested(input, start + nofork[0].length, depth, context, commands, "nofork", text)
if (input.startsWith("${", start)) {
// Literal text in parameter words reaches the enclosing word's value.
@@ -1383,7 +1362,7 @@ function scanBashDollarOrBacktick(
/^(?:\\\n)*!/.test(input.slice(start + 2)) ||
(context.dialect !== "bash" &&
input[start + 2] === "(" &&
((ZSH_SAFE_FLAGS_RE.lastIndex = start), ZSH_SAFE_FLAGS_RE.test(input)) &&
testAt(ZSH_SAFE_FLAGS_RE, input, start) &&
input.slice(start + 3, ZSH_SAFE_FLAGS_RE.lastIndex - 1).replace(ZSH_QUOTED_FLAG_ARG_RE, "").includes("P"))
if (indirect) {
context.variableSink = true
@@ -1473,11 +1452,9 @@ function scanBashParameter(
quoted: boolean,
text: BashText,
): number | Opaque {
PARAMETER_PREFIX_RE.lastIndex = start
const nameStart = PARAMETER_PREFIX_RE.exec(input) ? PARAMETER_PREFIX_RE.lastIndex : start
const nameStart = matchAt(PARAMETER_PREFIX_RE, input, start) ? PARAMETER_PREFIX_RE.lastIndex : start
// The operands of ${name:offset:length} are arithmetic, and ${name:=word} or ${name=word} binds a variable.
PARAMETER_NAME_RE.lastIndex = nameStart
let colon = PARAMETER_NAME_RE.test(input) ? PARAMETER_NAME_RE.lastIndex : -1
let colon = testAt(PARAMETER_NAME_RE, input, nameStart) ? PARAMETER_NAME_RE.lastIndex : -1
const name = colon > nameStart ? input.slice(nameStart, colon).replace(/\\\n/g, "") : ""
if (
context.dialect !== "bash" &&
@@ -1514,8 +1491,7 @@ function scanBashParameter(
}
// Bash ${name@P} evaluates the value as prompt text.
if (char === "@") {
PARAMETER_PROMPT_RE.lastIndex = index
if (PARAMETER_PROMPT_RE.test(input)) return { kind: "opaque", reason: "dynamic-execution" }
if (testAt(PARAMETER_PROMPT_RE, input, index)) return { kind: "opaque", reason: "dynamic-execution" }
}
// Zsh globs an unquoted parameter's words, where a parenthesized group can be a glob qualifier.
if (!quoted && context.dialect !== "bash" && char === "(" && index > start)
@@ -1538,8 +1514,7 @@ function scanBashParameter(
if (quoted && unit !== undefined && input.startsWith("$(", index) && input.slice(index, unit + 1).includes("}"))
return { kind: "opaque", reason: "command-substitution" }
if (subscript || (index === nameStart && input.startsWith("${", index) && unit !== undefined)) {
LINE_CONTINUATION_RE.lastIndex = Number(unit) + 1
colon = LINE_CONTINUATION_RE.exec(input) ? LINE_CONTINUATION_RE.lastIndex : Number(unit) + 1
colon = matchAt(LINE_CONTINUATION_RE, input, Number(unit) + 1) ? LINE_CONTINUATION_RE.lastIndex : Number(unit) + 1
}
if (unit !== undefined) index = unit
else bashAppend(text, char)
@@ -1577,8 +1552,7 @@ function scanBashConditional(
const close = !wordStarted && char === "]" ? bashOperator(input, index, ["]]"]) : undefined
if (typeof close === "object") return close
if (close) {
BRACE_CLOSE_AHEAD_RE.lastIndex = index + 2
if (BRACE_CLOSE_AHEAD_RE.test(input)) {
if (testAt(BRACE_CLOSE_AHEAD_RE, input, index + 2)) {
if (parenDepth !== 0) return { kind: "opaque", reason: "invalid-structure" }
// -v names a variable, -t in Zsh evaluates the descriptor, and arithmetic comparisons evaluate both operands.
const evaluated = words.some(
@@ -1677,7 +1651,7 @@ function scanBashArrayOrPattern(
wordStarted &&
char === "(" &&
context.dialect === "zsh" &&
((ZSH_GLOB_QUALIFIER_RE.lastIndex = index), ZSH_GLOB_QUALIFIER_RE.test(input))
testAt(ZSH_GLOB_QUALIFIER_RE, input, index)
) {
const end = ZSH_GLOB_QUALIFIER_RE.lastIndex - 1
if (text) text.literal += input.slice(index, end + 1)
@@ -1890,6 +1864,8 @@ function scanPowerShellList(
let dangling = false
let invocation = false
const previousWord = () => (started ? word : (words.at(-1) ?? ""))
const afterMemberOperator = () => indexable === "member" && /[.:]$/.test(previousWord())
const resetMember = () => {
indexable = false
memberChainStart = undefined
@@ -2010,7 +1986,7 @@ function scanPowerShellList(
if (char === "'" || char === '"') {
if (!started && words.length === 0 && !invocation) expression = true
quote = char
const dynamicMember = indexable === "member" && /[.:]$/.test(started ? word : (words.at(-1) ?? ""))
const dynamicMember = afterMemberOperator()
standalone = atBoundary || (expression && !redirectTarget) || dynamicMember
if (dynamicMember) hasMemberAccess = true
else if (standalone) {
@@ -2049,7 +2025,7 @@ function scanPowerShellList(
continue
}
if (char === "<" && input[index + 1] === "#" && (atBoundary || inExpression || indexable === "member")) {
const memberOperator = indexable === "member" && /[.:]$/.test(started ? word : (words.at(-1) ?? ""))
const memberOperator = afterMemberOperator()
if (started) finishWord(index)
const end = input.indexOf("#>", index + 2)
if (end < 0) return { kind: "opaque", reason: "invalid-structure" }
@@ -2063,8 +2039,8 @@ function scanPowerShellList(
indexable === "member" ||
(inExpression && !(close === "]" && /^[\p{L}\p{Nl}_][\p{L}\p{Nl}\d_.#+`\\]*$/u.test(word))))
) {
const prev = started ? word : (words.at(-1) ?? "")
const memberOperator = indexable === "member" && /[.:]$/.test(prev)
const prev = previousWord()
const memberOperator = afterMemberOperator()
if ((clause && clause !== "member") || prev.endsWith(",") || memberOperator) finishWord(index)
else if (words.length || started || invocation) finishCommand(index)
statementHead = !dangling
@@ -2187,7 +2163,7 @@ function scanPowerShellList(
const end = powerShellBracedVariable(input, index, skipped)
if (end === undefined) return { kind: "opaque", reason: "invalid-structure" }
if (!started && !words.length && !invocation && hash !== "switch") expression = true
const dynamicMember = indexable === "member" && /[.:]$/.test(started ? word : (words.at(-1) ?? ""))
const dynamicMember = afterMemberOperator()
if (dynamicMember) {
indexable = "closed"
hasMemberAccess = true
@@ -2218,8 +2194,7 @@ function scanPowerShellList(
if (opener !== undefined) {
const tokenIndex = index
const openChar = input[opener]
const prevMemberWord = started ? word : (words.at(-1) ?? "")
const dynamicMemberOpener = indexable === "member" && /[.:]$/.test(prevMemberWord)
const dynamicMemberOpener = afterMemberOperator()
const priorIndexable = Boolean(indexable)
const isMethodCall =
char === "(" &&
@@ -2303,7 +2278,7 @@ function scanPowerShellList(
continue
}
if (/\s/.test(char) && char !== "\n" && char !== "\r") {
const memberOperator = indexable === "member" && /[.:]$/.test(started ? word : (words.at(-1) ?? ""))
const memberOperator = afterMemberOperator()
finishWord(index)
if (!memberOperator) resetMember()
continue
@@ -2324,8 +2299,8 @@ function scanPowerShellList(
)
return { kind: "opaque", reason: "invalid-structure" }
if (separator === "\n" || separator === "\r" || separator === "\r\n") {
const prev = started ? word : (words.at(-1) ?? "")
const memberOperator = indexable === "member" && /[.:]$/.test(prev)
const prev = previousWord()
const memberOperator = afterMemberOperator()
if ((clause && clause !== "member") || prev.endsWith(",") || memberOperator) {
finishWord(index)
index += separator.length - 1
@@ -2346,10 +2321,9 @@ function scanPowerShellList(
}
if (!started && !words.length && !invocation && hash !== "switch" && testAt(POWERSHELL_EXPRESSION_RE, input, index))
expression = true
const prevMember = started ? word : (words.at(-1) ?? "")
if ((atBoundary || (indexable === "member" && /[.:]$/.test(prevMember))) && char === "$") {
if ((atBoundary || afterMemberOperator()) && char === "$") {
if (testAt(POWERSHELL_INDEXABLE_VAR_RE, input, index + 1)) {
if (indexable === "member" && /[.:]$/.test(prevMember)) hasMemberAccess = true
if (afterMemberOperator()) hasMemberAccess = true
else {
memberChainStart = index
hasMemberAccess = false
+1 -1
View File
@@ -170,7 +170,7 @@ export function args(file: string, command: string) {
// Resolve afresh so removing a shell does not leave terminals using a stale cached path.
export function environment(bin?: string, filter?: { compatible?: boolean }) {
return select(process.env.SHELL, undefined, filter, bin) ?? fallback(bin)
return select(process.env.SHELL, undefined, filter, bin)
}
let defaultConfigured: { bin?: string; value: string } | undefined