diff options
author | Robert Speicher <rspeicher@gmail.com> | 2021-01-20 13:34:23 -0600 |
---|---|---|
committer | Robert Speicher <rspeicher@gmail.com> | 2021-01-20 13:34:23 -0600 |
commit | 6438df3a1e0fb944485cebf07976160184697d72 (patch) | |
tree | 00b09bfd170e77ae9391b1a2f5a93ef6839f2597 /danger/frozen_string/Dangerfile | |
parent | 42bcd54d971da7ef2854b896a7b34f4ef8601067 (diff) | |
download | gitlab-ce-6438df3a1e0fb944485cebf07976160184697d72.tar.gz |
Add latest changes from gitlab-org/gitlab@13-8-stable-eev13.8.0-rc42
Diffstat (limited to 'danger/frozen_string/Dangerfile')
-rw-r--r-- | danger/frozen_string/Dangerfile | 29 |
1 files changed, 24 insertions, 5 deletions
diff --git a/danger/frozen_string/Dangerfile b/danger/frozen_string/Dangerfile index 7c4c4f2b17c..bc598b16463 100644 --- a/danger/frozen_string/Dangerfile +++ b/danger/frozen_string/Dangerfile @@ -1,15 +1,34 @@ # frozen_string_literal: true FILE_EXTENSION = ".rb" -MAGIC_COMMENT = "# frozen_string_literal: true" +FROZEN_STRING_MAGIC_COMMENT = "# frozen_string_literal: true" +SHEBANG_COMMENT = "#!" def get_files_with_no_magic_comment(files) - files.select do |file| - file.end_with?(FILE_EXTENSION) && - !File.open(file, &:gets)&.start_with?(MAGIC_COMMENT) + files.select do |path| + path.end_with?(FILE_EXTENSION) && + !file_has_frozen_string_magic_comment?(path) end end +def file_has_frozen_string_magic_comment?(path) + File.open(path) do |file| + first_line = file.gets + + line_has_frozen_string_magic_comment?(first_line) || + (line_has_shebang?(first_line) && + line_has_frozen_string_magic_comment?(file.gets)) + end +end + +def line_has_frozen_string_magic_comment?(line) + line&.start_with?(FROZEN_STRING_MAGIC_COMMENT) +end + +def line_has_shebang?(line) + line&.start_with?(SHEBANG_COMMENT) +end + files_to_fix = get_files_with_no_magic_comment(git.added_files) if files_to_fix.any? @@ -20,7 +39,7 @@ if files_to_fix.any? markdown(<<~MARKDOWN) ## Enable Frozen String Literal - The following files should have `#{MAGIC_COMMENT}` on the first line: + The following files should have `#{FROZEN_STRING_MAGIC_COMMENT}` on the first line: * #{files_to_fix.map { |path| "`#{path}`" }.join("\n* ")} MARKDOWN |