debian-mirror-gitlab/danger/frozen_string/Dangerfile

48 lines
1.3 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
FILE_EXTENSION = ".rb"
2021-03-08 18:12:59 +05:30
FROZEN_STRING_MAGIC_COMMENT = "# frozen_string_literal: true"
SHEBANG_COMMENT = "#!"
2018-11-18 11:00:15 +05:30
def get_files_with_no_magic_comment(files)
2021-03-08 18:12:59 +05:30
files.select do |path|
path.end_with?(FILE_EXTENSION) &&
!file_has_frozen_string_magic_comment?(path)
2018-11-18 11:00:15 +05:30
end
end
2021-03-08 18:12:59 +05:30
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
2018-11-18 11:00:15 +05:30
files_to_fix = get_files_with_no_magic_comment(git.added_files)
if files_to_fix.any?
warn 'This merge request adds files that do not enforce frozen string literal. ' \
2019-12-04 20:38:33 +05:30
'See https://gitlab.com/gitlab-org/gitlab-foss/issues/47424 for more information.'
2018-11-18 11:00:15 +05:30
2019-12-04 20:38:33 +05:30
if GitlabDanger.new(helper.gitlab_helper).ci?
markdown(<<~MARKDOWN)
## Enable Frozen String Literal
2018-11-18 11:00:15 +05:30
2021-03-08 18:12:59 +05:30
The following files should have `#{FROZEN_STRING_MAGIC_COMMENT}` on the first line:
2018-11-18 11:00:15 +05:30
2019-12-04 20:38:33 +05:30
* #{files_to_fix.map { |path| "`#{path}`" }.join("\n* ")}
MARKDOWN
end
2018-11-18 11:00:15 +05:30
end