Skip to content

Commit

Permalink
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/compiler/crystal/command/format.cr
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ class Crystal::Command
enum Code
FORMAT
SYNTAX
INVALID_BYTE_SEQUENCE
BUG
end
end
@@ -64,6 +65,8 @@ class Crystal::Command
error "formatting '#{result.filename}' produced changes", exit_code: nil
when .syntax?
error "'#{result.filename}' has syntax errors", exit_code: nil
when .invalid_byte_sequence?
error "'#{result.filename}' is not a valid Crystal source file", exit_code: nil
when .bug?
error "there's a bug formatting '#{result.filename}', please report it including the contents of the file: https://github.com/crystal-lang/crystal/issues", exit_code: nil
end
@@ -82,6 +85,11 @@ class Crystal::Command

print result
STDOUT.flush
rescue ex : InvalidByteSequenceError
print "Error: ".colorize.toggle(@color).red.bold
print "source is not a valid Crystal source file: ".colorize.toggle(@color).bold
puts "#{ex.message}"

This comment has been minimized.

Copy link
@Sija

Sija Sep 28, 2016

Contributor

Is string interpolation needed here? puts ex.message might suffice.

exit 1
rescue ex : Crystal::SyntaxException
if @format == "json"
puts ex.to_json
@@ -105,6 +113,11 @@ class Crystal::Command
exit(result == source ? 0 : 1) if check_files

File.write(filename, result)
rescue ex : InvalidByteSequenceError
print "Error: ".colorize.toggle(@color).red.bold
print "file '#{Crystal.relative_filename(filename)}' is not a valid Crystal source file: ".colorize.toggle(@color).bold
puts "#{ex.message}"
exit 1
rescue ex : Crystal::SyntaxException
if @format == "json"
puts ex.to_json
@@ -151,6 +164,14 @@ class Crystal::Command
File.write(filename, result)
STDOUT << "Format".colorize(:green).toggle(@color) << " " << filename << "\n"
end
rescue ex : InvalidByteSequenceError
if check_files
check_files << FormatResult.new(filename, FormatResult::Code::INVALID_BYTE_SEQUENCE)
else
print "Error: ".colorize.toggle(@color).red.bold
print "file '#{Crystal.relative_filename(filename)}' is not a valid Crystal source file: ".colorize.toggle(@color).bold
puts "#{ex.message}"
end
rescue ex : Crystal::SyntaxException
if check_files
check_files << FormatResult.new(filename, FormatResult::Code::SYNTAX)

1 comment on commit a20f45a

@faustinoaq
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@asterite thanks you for fix this issue! 👍 #3178

Please sign in to comment.