foxundermoon / vs-shell-format

the shellscript、Dockerfile、properties ...... format extension
MIT License
445 stars 47 forks source link

[BUG] Bash associative array with hyphened key malformed. #275

Open caspertsui opened 2 years ago

caspertsui commented 2 years ago

Hello,

This bug is serious if it's enabled "format on save" feature.

associative_array=(
  [hyphened-key]=value
   ...
)

would be formatted as

associative_array=(
  [hyphened - key]=value
   ...
)

for shell scripts which breaks 1 single key into 3.

Thanks and regards, Alan

meleu commented 2 years ago

This is a known upstream bug caveat.

The shfmt README lists this as a caveat, and suggests:

When indexing Bash associative arrays, always use quotes.

demonstration:

# key surrounded with 'single quotes'
$ echo "array=(['hyphened-key']=value )" | shfmt
array=(['hyphened-key']=value)

# key with "double quotes"
$ echo 'array=(["hyphened-key"]=value )' | shfmt
array=(["hyphened-key"]=value)