summaryrefslogtreecommitdiff
path: root/danger/frozen_string
diff options
context:
space:
mode:
authorgfyoung <gfyoung17@gmail.com>2018-07-22 11:28:17 -0700
committergfyoung <gfyoung17@gmail.com>2018-07-24 02:17:45 -0700
commitdb00a9c6418377526359ed0bd0efd62491f14084 (patch)
tree91d3335c81540876394161b939bf9c3d06e5c25a /danger/frozen_string
parentd4e9cd9de49a9f2b90ab76c5dc63d1515f9fdd69 (diff)
downloadgitlab-ce-db00a9c6418377526359ed0bd0efd62491f14084.tar.gz
Add Dangerfile for frozen_string_literal
Checks that new Ruby files will enable frozen string literal. Partially addresses #47424.
Diffstat (limited to 'danger/frozen_string')
-rw-r--r--danger/frozen_string/Dangerfile27
1 files changed, 27 insertions, 0 deletions
diff --git a/danger/frozen_string/Dangerfile b/danger/frozen_string/Dangerfile
new file mode 100644
index 00000000000..595176d597d
--- /dev/null
+++ b/danger/frozen_string/Dangerfile
@@ -0,0 +1,27 @@
+# frozen_string_literal: true
+
+FILE_EXTENSION = ".rb"
+MAGIC_COMMENT = "# frozen_string_literal: true"
+
+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)
+ end
+end
+
+files_to_check = git.added_files
+files_to_fix = get_files_with_no_magic_comment(files_to_check)
+
+if files_to_fix.any?
+ warn 'This merge request adds files that do not enforce frozen string literal. ' \
+ 'See https://gitlab.com/gitlab-org/gitlab-ce/issues/47424 for more information.'
+
+ markdown(<<~MARKDOWN)
+## Enable Frozen String Literal
+
+The following files should have `#{MAGIC_COMMENT}` in the first line:
+
+* #{files_to_fix.map { |path| "`#{path}`" }.join("\n* ")}
+ MARKDOWN
+end