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.27k stars 345 forks source link

syntax: array globs not parsed correctly #850

Closed philsc closed 2 years ago

philsc commented 2 years ago

The following script is rejected by shfmt:

#!/bin/bash
readonly -a new_patches=( [0-9][0-9][0-9][0-9]-*.patch )

The intent is to glob up all the patch files generated with git format-patch.

We get this error:

$ shfmt foo.sh
foo.sh:2:27: "[x]" must be followed by =

I'm assuming that it thinks we're using associative arrays here.

This is on the latest version as far as I can tell:

$ shfmt -version
v3.4.3
mvdan commented 2 years ago

Yep, this is a known limitation of the parser; see https://github.com/mvdan/sh/issues/558. It is fixable, but not trivially so, as it requires support for ambiguous syntax parsing (e.g. backtracking).

philsc commented 2 years ago

Is there a way via a comment or something to disable formatting on a certain line? Similar to clang-format's // clang-format: off?

mvdan commented 2 years ago

I'm afraid not, but you can split your code between multiple files as an alternative.