Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

syntax: simplify unset-or-null with null default to just unset #849

Merged
merged 1 commit into from
May 23, 2022
Merged
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
5 changes: 5 additions & 0 deletions syntax/simplify.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import "bytes"
// Remove redundant quotes [[ "$var" == str ]]
// Merge negations with unary operators [[ ! -n $var ]]
// Use single quotes to shorten literals "\$foo"
// Remove redundant param expansion colons ${foo:-}
func Simplify(n Node) bool {
s := simplifier{}
Walk(n, s.visit)
Expand All @@ -37,6 +38,10 @@ func (s *simplifier) visit(node Node) bool {
x.Index = s.removeParensArithm(x.Index)
// don't inline params - same as above.

if x.Exp != nil && x.Exp.Op == DefaultUnsetOrNull && x.Exp.Word == nil {
s.modified = true
x.Exp.Op = DefaultUnset
}
if x.Slice == nil {
break
}
Expand Down
4 changes: 4 additions & 0 deletions syntax/simplify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ var simplifyTests = [...]simplifyTest{
{"\"fo\\`o\"", "'fo`o'"},
noSimple(`fo"o"bar`),
noSimple(`foo""bar`),

// param expansions
{`${foo:-}`, `${foo-}`},
noSimple(`${foo:-bar}`),
}

func TestSimplify(t *testing.T) {
Expand Down