Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions syntax/simplify.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,26 @@ func (s *simplifier) inlineSimpleParams(x ArithmExpr) ArithmExpr {
return x
}
if pe.Excl || pe.Length || pe.Width || pe.Slice != nil ||
pe.Repl != nil || pe.Exp != nil || pe.Index != nil {
pe.Repl != nil || pe.Exp != nil {
// A complex parameter expansion can't be simplified.
//
// Note that index expressions can't generally be simplified
// either. It's fine to turn ${a[0]} into a[0], but others like
// a[*] are invalid in many shells including Bash.
return x
}
if pe.Index != nil {
// All index expressions can't be simplified either.
// It's fine to turn ${a[0]} into a[0], but others like
// a[*] or a[@] are invalid in many shells including Bash.
ww, _ := pe.Index.(*Word)
if ww == nil || len(ww.Parts) != 1 {
return x
}
l, _ := ww.Parts[0].(*Lit)
if l == nil || l.Value == "*" || l.Value == "@" {
return x
}
s.modified = true
pe.Short = true
return w
}
s.modified = true
return &Word{Parts: []WordPart{pe.Param}}
}
Expand Down
3 changes: 2 additions & 1 deletion syntax/simplify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ var simplifyTests = [...]simplifyTest{
{"${foo:(1):(2)}", "${foo:1:2}"},
{"a[(1)]=2", "a[1]=2"},
{"$(($a + ${b}))", "$((a + b))"},
{"((${a[0]}))", "((a[0]))"},
{"((${a[foo]}))", "((a[foo]))"},
noSimple("$((${!a} + ${#b}))"),
noSimple("a[$b]=2"),
noSimple("${a[$b]}"),
noSimple("${a[@]}"),
noSimple("((${a[@]}))"),
noSimple("((${a[*]}))"),
noSimple("((${a[0]}))"),
noSimple("(($3 == $#))"),

// test exprs
Expand Down