Skip to content

Commit 18a9392

Browse files
committed
Copied erblint-disable script from dotcom to this gem, exposed via spec.executables.
1 parent cb685d7 commit 18a9392

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

bin/erblint-disable

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
require "json"
5+
6+
# Accepts comma-separated args with simple rule name
7+
# e.g. script/erblint-disable SomeRule1,SomeRule2
8+
# e.g. script/erblint-disable GitHub::Accessibility::Rule1,SomeRule2
9+
rules = ARGV[0]
10+
11+
rules_array = rules.split(",")
12+
rules_map = {}
13+
rules_array.each do |rule|
14+
rule_underscored = rule.to_s.gsub(/::/, "/").
15+
gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
16+
gsub(/([a-z\d])([A-Z])/, '\1_\2').
17+
tr("-", "_").
18+
downcase.
19+
gsub("git_hub", "github") # corrects rule names with `GitHub` name to align with erb-lint expectations
20+
rules_map[rule] = rule_underscored
21+
end
22+
23+
rules_map.each do |disable_comment_name, command_line_name|
24+
output = `bin/erblint --format json --enable-linters #{command_line_name} app/views app/components packages/**/app/components app/forms/**/`
25+
hashed_output = JSON.parse(output)
26+
hashed_output["files"].each do |file|
27+
path = file["path"]
28+
offenses = file["offenses"]
29+
line_numbers = offenses.map do |offense|
30+
offense["location"]["last_line"]
31+
end
32+
File.open(path, "r+") do |file|
33+
lines = file.each_line.to_a
34+
line_numbers.each do |line_number|
35+
line = lines[line_number - 1]
36+
unless line.match?(/erblint:disable (?<rules>.*#{disable_comment_name}).*/)
37+
existing_disable = line.match(/(?<=# erblint:disable)(.*) (?=%>)/)
38+
if existing_disable
39+
existing_disable_string = existing_disable.captures[0]
40+
add_new_disable = "#{existing_disable_string}, #{disable_comment_name}"
41+
lines[line_number - 1] = lines[line_number - 1].gsub(existing_disable_string, add_new_disable)
42+
else
43+
lines[line_number - 1] = lines[line_number - 1].gsub("\n", "") + "<%# erblint:disable #{disable_comment_name} %>\n"
44+
end
45+
end
46+
end
47+
file.rewind
48+
file.write(lines.join)
49+
end
50+
end
51+
end
52+
53+
exit 0

erblint-github.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ Gem::Specification.new do |s|
2424
s.add_development_dependency "rubocop", "= 1.54.2"
2525
s.add_development_dependency "rubocop-github", "~> 0.20.0"
2626
s.metadata["rubygems_mfa_required"] = "true"
27+
28+
s.executables << "erblint-disable"
2729
end

0 commit comments

Comments
 (0)