audit: add rules for non-ascii character and encoding comment

Closes Homebrew/homebrew#40042.

Signed-off-by: Xu Cheng <xucheng@me.com>
This commit is contained in:
Xu Cheng 2015-05-24 15:59:00 +08:00
parent e4ff0a2f2e
commit 78ccd41251

View File

@ -70,6 +70,14 @@ class FormulaText
/\Z\n/ =~ @text /\Z\n/ =~ @text
end end
def has_non_ascii_character?
/[^\x00-\x7F]/ =~ @text
end
def has_encoding_comment?
/^# (en)?coding: utf-8$/i =~ @text
end
def =~ regex def =~ regex
regex =~ @text regex =~ @text
end end
@ -118,6 +126,14 @@ class FormulaAuditor
problem "'__END__' was found, but 'DATA' is not used" problem "'__END__' was found, but 'DATA' is not used"
end end
if text.has_non_ascii_character? and not text.has_encoding_comment?
problem "Found non-ASCII character: add `# encoding: UTF-8` in the first line"
end
if text.has_encoding_comment? and not text.has_non_ascii_character?
problem "Remove the redundant `# encoding: UTF-8`"
end
unless text.has_trailing_newline? unless text.has_trailing_newline?
problem "File should end with a newline" problem "File should end with a newline"
end end