Skip to content

Commit

Permalink
Quote dollar signs when not by themself (#31)
Browse files Browse the repository at this point in the history
Fix up escaping of `$`; revise tests to support.
---------

Signed-off-by: Bolun Thompson <[email protected]>
  • Loading branch information
BolunThompson authored Dec 9, 2024
1 parent a39be0e commit b7d9236
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 24 deletions.
9 changes: 1 addition & 8 deletions libdash/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,14 +408,7 @@ def string_of_arg (args, quote_mode=UNQUOTED):
text = []
while i < len(args):
c = string_of_arg_char(args[i], quote_mode=quote_mode)

# dash will parse '$?' as
# [(C, '$'), (E, '?')]
# but we don't normally want to escape ?
#
# so we check up after the fact: if the character after $ is escaped,
# we'll escape the $, too
if c == "$" and (i+1 < len(args)) and args[i+1][0] == "E":
if c == "$" and (i+1 < len(args)):
c = "\\$"
text.append(c)

Expand Down
2 changes: 1 addition & 1 deletion ocaml/ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ and string_of_arg ?quote_mode:(quote_mode=QUnquoted) = function
| [] -> ""
| c :: a ->
let char = string_of_arg_char ~quote_mode c in
if char = "$" && next_is_escaped a
if char = "$" && a <> []
then "\\$" ^ string_of_arg ~quote_mode a
else char ^ string_of_arg ~quote_mode a

Expand Down
40 changes: 25 additions & 15 deletions test/round_trip.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ fi
p=$1
tgt=$2

two_roundtrips() {
[ "$(head -n1 "$tgt")" != '# TEST: single roundtrip' ]
}

orig=$(mktemp)

"$p" "$tgt" >"$orig"
Expand All @@ -31,25 +35,31 @@ then
echo "PASS '$tgt'"
exit 0
else
# try one more time around the loop
rtrt=$(mktemp)

"$p" "$rt" >"$rtrt"
if [ "$?" -ne 0 ]
if two_roundtrips
then
echo "RT_ABORT_3: '$tgt' -> '$orig' -> '$rt' -> '$rtrt'"
exit 5
fi
# try one more time around the loop
rtrt=$(mktemp)

if diff -b "$rt" "$rtrt" >/dev/null
then
echo "PASS '$tgt' (two runs to fixpoint)"
exit 0
"$p" "$rt" >"$rtrt"
if [ "$?" -ne 0 ]
then
echo "RT_ABORT_3: '$tgt' -> '$orig' -> '$rt' -> '$rtrt'"
exit 5
fi

if diff -b "$rt" "$rtrt" >/dev/null
then
echo "PASS '$tgt' (two runs to fixpoint)"
exit 0
fi
fi

echo "FAIL: '$tgt' first time"
diff -ub "$orig" "$rt"
echo ">>> '$tgt' second time"
diff -ub "$rt" "$rtrt"
if two_roundtrips
then
echo ">>> '$tgt' second time"
diff -ub "$rt" "$rtrt"
fi
exit 1
fi
4 changes: 4 additions & 0 deletions test/tests/single_quoted_dollar_sign.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# TEST: single roundtrip

echo '$1'
echo $ a

0 comments on commit b7d9236

Please sign in to comment.