mvdan / sh

A shell parser, formatter, and interpreter with bash support; includes shfmt
https://pkg.go.dev/mvdan.cc/sh/v3
BSD 3-Clause "New" or "Revised" License
7.35k stars 346 forks source link

Associative array access with concatenated keys without quotes fails to parse #1082

Closed Popkornium18 closed 4 months ago

Popkornium18 commented 4 months ago

This valid bash snippet fails to parse because of the last line. The Workaround is simple (use quotes), but it is valid and I encountered it in the wild, so it should probably parse.

Tree-Sitter and whatever shellcheck uses internally parse the code correctly.

#!/bin/bash
declare -A things=( ["FOO"]=41 ["FOO BAR"]=42 )
declare foo=FOO
declare bar=BAR
echo "${things[$foo]}" # Works
echo "${things["$foo $bar"]}" # Works
echo "${things[$foo $bar]}" # Doesn't work

Running shfmt on this snippet causes an error. I'm using shfmt 3.8.0.

Edit: No, the bash tree-sitter grammar also parses it incorrectly.

mvdan commented 4 months ago

See the caveats section in the README.