MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / parseHeredocBodyContent

Function parseHeredocBodyContent

source code/utils/bash/bashParser.ts:1931–1989  ·  view source on GitHub ↗
(
  P: ParseState,
  start: number,
  end: number,
)

Source from the content-addressed store, hash-verified

1929}
1930
1931function parseHeredocBodyContent(
1932 P: ParseState,
1933 start: number,
1934 end: number,
1935): TsNode[] {
1936 // Parse expansions inside an unquoted heredoc body.
1937 const saved = saveLex(P.L)
1938 // Position lexer at body start
1939 restoreLexToByte(P, start)
1940 const out: TsNode[] = []
1941 let contentStart = P.L.b
1942 // tree-sitter-bash's heredoc_body rule hides the initial text segment
1943 // (_heredoc_body_beginning) — only content AFTER the first expansion is
1944 // emitted as heredoc_content. Track whether we've seen an expansion yet.
1945 let sawExpansion = false
1946 while (P.L.b < end) {
1947 const c = peek(P.L)
1948 // Backslash escapes suppress expansion: \$ \` stay literal in heredoc.
1949 if (c === '\\') {
1950 const nxt = peek(P.L, 1)
1951 if (nxt === '$' || nxt === '`' || nxt === '\\') {
1952 advance(P.L)
1953 advance(P.L)
1954 continue
1955 }
1956 advance(P.L)
1957 continue
1958 }
1959 if (c === '$' || c === '`') {
1960 const preB = P.L.b
1961 const exp = parseDollarLike(P)
1962 // Bare `$` followed by non-name (e.g. `$'` in a regex) returns a lone
1963 // '$' leaf, not an expansion — treat as literal content, don't split.
1964 if (
1965 exp &&
1966 (exp.type === 'simple_expansion' ||
1967 exp.type === 'expansion' ||
1968 exp.type === 'command_substitution' ||
1969 exp.type === 'arithmetic_expansion')
1970 ) {
1971 if (sawExpansion && preB > contentStart) {
1972 out.push(mk(P, 'heredoc_content', contentStart, preB, []))
1973 }
1974 out.push(exp)
1975 contentStart = P.L.b
1976 sawExpansion = true
1977 }
1978 continue
1979 }
1980 advance(P.L)
1981 }
1982 // Only emit heredoc_content children if there were expansions — otherwise
1983 // the heredoc_body is a leaf node (tree-sitter convention).
1984 if (sawExpansion) {
1985 out.push(mk(P, 'heredoc_content', contentStart, end, []))
1986 }
1987 restoreLex(P.L, saved)
1988 return out

Callers 1

parseSimpleCommandFunction · 0.85

Calls 7

saveLexFunction · 0.85
restoreLexToByteFunction · 0.85
advanceFunction · 0.85
parseDollarLikeFunction · 0.85
mkFunction · 0.85
restoreLexFunction · 0.85
peekFunction · 0.70

Tested by

no test coverage detected