MCPcopy Index your code
hub / github.com/claude-code-best/claude-code / parseHeredocBodyContent

Function parseHeredocBodyContent

src/utils/bash/bashParser.ts:1925–1983  ·  view source on GitHub ↗
(
  P: ParseState,
  start: number,
  end: number,
)

Source from the content-addressed store, hash-verified

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

Callers 1

parseSimpleCommandFunction · 0.85

Calls 8

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

Tested by

no test coverage detected