Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

[Bug] tree-sitter-bash parses `$_` / `${_}` as variable_name instead of special_variable_name

Aperta Adatta ai principianti
#4,367 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
Meno di un'ora
Idoneità per principianti
85/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
bash, typescript
Ambito
compilers

Direzione di ricerca

Inizia in src/parser.ts presso parseDollar() e parseExpansion(), dove 0 viene gestito come caso speciale; applica la diff di due righe per trattare anche _ come speciale. Aggiungi le righe del fixture a packages/tree-sitter-bash/test/fixtures/differential/expansions.txt. Esegui pnpm --filter @moonshot-ai/tree-sitter-bash test; fatto significa che la suite differenziale passa con special_variable_name per un _ isolato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

What issue are you seeing?

packages/tree-sitter-bash parses the special parameter $_ as a variable_name, while the reference grammar ([email protected], the parser this package is differentially compared against) parses a lone _ as a special_variable_name. So ourDump(source) !== referenceDump(source) for input the package claims byte-identical tree parity on.

echo $_:

ours:
program [0,7] "echo $_"
  command [0,7] "echo $_"
    command_name [0,4] "echo"
      word [0,4] "echo"
    simple_expansion [5,7] "$_"
      ($) [5,6] "$"
      variable_name [6,7] "_"

reference:
program [0,7] "echo $_"
  command [0,7] "echo $_"
    command_name [0,4] "echo"
      word [0,4] "echo"
    simple_expansion [5,7] "$_"
      ($) [5,6] "$"
      special_variable_name [6,7] "_"

Same divergence for echo ${_}, echo ${_#x}, "${_}", ${_:-d}, ${_=x}: the _ child is variable_name here, special_variable_name in the reference.

Not affected (both sides agree): $?, $#, $-, $!, $@, $*, $$, $0, $1; and $_a, ${_a}, $__, which are variable_name on both sides (the \w+ rule wins once another word character follows).

What steps can reproduce the bug?
  1. Append a fixture to packages/tree-sitter-bash/test/fixtures/differential/expansions.txt:
@match: echo $_ and ${_} are special parameters, $_a and ${_a} are not
echo $_ ${_} ${_#x} $_a ${_a}
===
  1. pnpm --filter @moonshot-ai/tree-sitter-bash test

The differential suite fails on that fixture with the diff above (echo $_a and echo ${_a} still match).

What is the expected behavior?

A lone _ after $, and a lone _ as an expansion body, should produce special_variable_name "_" — matching _special_variable_name in the reference grammar:

_special_variable_name: $ => alias(choice('*', '@', '?', '!', '#', '-', '$', '0', '_'), $.special_variable_name),
_simple_variable_name: $ => alias(/\w+/, $.variable_name),
Additional information

Root cause: src/grammar.ts already lists _ in SPECIAL_VARIABLES (['@', '*', '#', '?', '-', '$', '!', '0', '_']), but SPECIAL_VARIABLE_CHARS filters word characters out of it (they are normally matched by the \w+ rule), so the only place a lone word-character special parameter is recognized explicitly is:

  • src/parser.ts, parseDollar() — if (j === i + 2 && next === '0')
  • src/parser.ts, parseExpansion() — const special = nameEnd === j + 1 && this.source[j] === '0';

Both special-case 0 but not _. Proposed fix (two lines, plus comment wording):

-      if (j === i + 2 && next === '0') {
+      if (j === i + 2 && (next === '0' || next === '_')) {
...
-      const special = nameEnd === j + 1 && this.source[j] === '0';
+      const special = nameEnd === j + 1 && (this.source[j] === '0' || this.source[j] === '_');

With that fix and the fixtures above: pnpm --filter @moonshot-ai/tree-sitter-bash test passes (6 files, 868 tests, 677 of them differential), typecheck is clean, oxlint --type-aware reports nothing.

Scope/impact: no user-visible behavior changes — nothing in packages/agent-core-v2 branches on special_variable_name vs variable_name (the permission policies key on command / command_name / string_content, bashTargets.ts on program / list / pipeline / redirected_statement). This is a fidelity bug against the package's stated contract of matching the reference tree byte for byte. @moonshot-ai/tree-sitter-bash is private, so no changeset is added.

Reproduced on main @ 7ccac2b on Windows 11 x64 with Node 24.21.0 / pnpm 10.33.0.

I'd like to submit the PR for this one — please /approve if that is welcome.

Contribution
  • I am willing to submit a PR for this bug fix myself (please wait for maintainer approval in this issue first)
Lingua principale
TypeScript
Stelle
8k
Fork
1.3k
Merge medio
1g 2h
PR unite (30g)
115

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di MoonshotAI/kimi-code

Tutte le issue di MoonshotAI/kimi-code

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.