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

Revert "Formatter: Escape non-printable characters in literals" #11603

Merged
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
43 changes: 2 additions & 41 deletions spec/compiler/formatter/formatter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -450,49 +450,10 @@ describe Crystal::Formatter do
assert_format "__DIR__", "__DIR__"
assert_format "__LINE__", "__LINE__"

assert_format %("\\0\\"\#\a\b\r\n\t\v\f\e\\xFF"), %("\\0\\"\#\\a\\b\\r\n\t\\v\\f\\e\\xFF")
assert_format %("\u00AD\uF8FF\u202A"), %("\\u00AD\\uF8FF\\u202A")
assert_format %("\\0\\"\#\a\b\r\n\t\v\f\e\\xFF\#{nil}"), %("\\0\\"\#\\a\\b\\r\n\t\\v\\f\\e\\xFF\#{nil}")
assert_format %("\u00AD\uF8FF\u202A\#{nil}"), %("\\u00AD\\uF8FF\\u202A\#{nil}")
assert_format %(:"\\0\\"\#\a\b\r\n\t\v\f\e\\xFF"), %(:"\\0\\"\#\\a\\b\\r\n\t\\v\\f\\e\\xFF")
assert_format %(:"\u00AD\uF8FF\u202A"), %(:"\\u00AD\\uF8FF\\u202A")
assert_format %(<<-HERE\n\\0"\#\a\b\r\n\t\v\f\e\\xFF\nHERE), %(<<-HERE\n\\0"\#\\a\\b\\r\n\t\\v\\f\\e\\xFF\nHERE)
assert_format %(<<-HERE\n\u00AD\uF8FF\u202A\nHERE), %(<<-HERE\n\\u00AD\\uF8FF\\u202A\nHERE)
assert_format %q("\\0\\\"\#\a\b\n\r\t\v\f\e\\xFF")
assert_format %q("\\\"\#\a\b\n\r\t\v\f\e")
assert_format %q("\a\c\b\d"), %q("\ac\bd")
assert_format %q("\\0\\\"\#\a\b\n\r\t#{foo}\v\f\e\\xFF")
assert_format %q("\\\"\#\a\b\n\r\t#{foo}\v\f\e")
assert_format %q("\a\c#{foo}\b\d"), %q("\ac#{foo}\bd")
assert_format %(%w(\\0\\"\#\a\b\r\n\t\v\f\e\\xFF)), %(%w(\\0\\"\#\\a\\b \\e\\xFF))
assert_format %(%w(\u00AD\uF8FF\u202A)), %(%w(\\u00AD\\uF8FF\\u202A))
assert_format %(%i(\\0\\"\#\a\b\r\n\t\v\f\e\\xFF)), %(%i(\\0\\"\#\\a\\b \\e\\xFF))
assert_format %(%i(\u00AD\uF8FF\u202A)), %(%i(\\u00AD\\uF8FF\\u202A))

assert_format %("\\u0061\\u{61}\\u0009\\u{9}")

assert_format "'\\''"
assert_format "'\\0'"
assert_format "'\\u0000'"
assert_format "'\\\\'"
assert_format "'\a'", "'\\a'"
assert_format "'\b'", "'\\b'"
assert_format "'\r'", "'\\r'"
assert_format "'\n'", "'\\n'"
assert_format "'\t'", "'\\t'"
assert_format "'\v'", "'\\v'"
assert_format "'\f'", "'\\f'"
assert_format "'\e'", "'\\e'"
assert_format "'\u00AD'", "'\\u00AD'"
assert_format "'\uF8FF'", "'\\uF8FF'"
assert_format "'\u202A'", "'\\u202A'"
assert_format "'青'"
assert_format "'\\u9752'"
assert_format "'\\u9752'"
assert_format "'\u{110BD}'", "'\\u{110BD}'"
assert_format "'\u{1F48E}'", "'\u{1F48E}'"
assert_format "'\\u0061'"
assert_format "'\\u{61}'"
assert_format "'\\u0009'"
assert_format "'\\u{9}'"

assert_format %("\#{foo = 1\n}"), %("\#{foo = 1}")
assert_format %("\#{\n foo = 1\n}")
Expand Down
54 changes: 9 additions & 45 deletions src/compiler/crystal/tools/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -432,22 +432,15 @@ module Crystal

def visit(node : CharLiteral)
check :CHAR
if @token.raw[1].printable?
# Keep original format when printable.
# This makes sure that printable representations of non-printable characters stay unmodified.
# Example: `'\u0009'` must not be converted to `'\t'`.
write @token.raw
else
write node.value.inspect
end
write @token.raw
next_token

false
end

def visit(node : SymbolLiteral)
check :SYMBOL
write_string @token
write @token.raw
next_token

false
Expand All @@ -467,39 +460,6 @@ module Crystal
false
end

def write_string(token)
string = if token.invalid_escape
token.value.as(String)
else
token.raw
end

write escape_nonprintable(string)
end

# Escapes non-printable characters in *string*.
# This is similar to `String#inspect` except `\n` and `\t` characters are
# printed literally. These control characters should not be escaped to keep
# the code formating intact.
def escape_nonprintable(string)
String.build do |io|
string.each_char do |char|
case char
when '\a' then io << "\\a"
when '\b' then io << "\\b"
when '\e' then io << "\\e"
when '\v' then io << "\\v"
when '\f' then io << "\\f"
when '\r' then io << "\\r"
when '\n', '\t', .printable?
io << char
else
char.unicode_escape(io)
end
end
end
end

def visit(node : StringLiteral)
column = @column

Expand All @@ -518,7 +478,11 @@ module Crystal
while true
case @token.type
when :STRING
write_string @token
if @token.invalid_escape
write @token.value
else
write @token.raw
end
next_string_token
when :INTERPOLATION_START
# This is the case of #{__DIR__}
Expand Down Expand Up @@ -633,7 +597,7 @@ module Crystal
else
loop do
check :STRING
write_string @token
write @token.invalid_escape ? @token.value : @token.raw
next_string_token

# On heredoc, pieces of contents are combined due to removing indentation.
Expand Down Expand Up @@ -806,7 +770,7 @@ module Crystal
case @token.type
when :STRING
write " " unless first || has_space_newline
write_string @token
write @token.raw
first = false
when :STRING_ARRAY_END
write @token.raw
Expand Down