Skip to content

Commit

Permalink
Autocorrect all rubocop violations
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelfranca committed Jun 15, 2022
1 parent 032d886 commit f4866cf
Show file tree
Hide file tree
Showing 27 changed files with 53 additions and 31 deletions.
6 changes: 3 additions & 3 deletions erb_lint.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ Gem::Specification.new do |s|
"allowed_push_host" => "https://rubygems.org",
}

s.add_dependency("activesupport")
s.add_dependency("better_html", "~> 1.0.7")
s.add_dependency("html_tokenizer")
s.add_dependency("rubocop")
s.add_dependency("parser", ">= 2.7.1.4")
s.add_dependency("activesupport")
s.add_dependency("smart_properties")
s.add_dependency("rainbow")
s.add_dependency("rubocop")
s.add_dependency("smart_properties")

s.add_development_dependency("rspec")
s.add_development_dependency("rubocop")
Expand Down
2 changes: 1 addition & 1 deletion lib/erb_lint/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def option_parser

def format_options_help
"Report offenses in the given format: "\
"(#{Reporter.available_formats.join(", ")}) (default: multiline)"
"(#{Reporter.available_formats.join(", ")}) (default: multiline)"
end

def invalid_format_error_message(given_format)
Expand Down
2 changes: 2 additions & 0 deletions lib/erb_lint/linter_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def initialize(config = {})
if (extra_keys = given_keys - allowed_keys).any?
raise Error, "Given key is not allowed: #{extra_keys.join(", ")}"
end

super(config)
rescue SmartProperties::InitializationError => e
raise Error, "The following properties are required to be set: #{e.properties}"
Expand All @@ -40,6 +41,7 @@ def [](name)
unless self.class.properties.key?(name)
raise Error, "No such property: #{name}"
end

super
end

Expand Down
7 changes: 4 additions & 3 deletions lib/erb_lint/linters/allowed_script_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def run(processed_source)
add_offense(
name_node.loc,
"Avoid using inline `<script>` tags altogether. "\
"Instead, move javascript code into a static file."
"Instead, move javascript code into a static file."
)
next
end
Expand All @@ -50,15 +50,16 @@ def run(processed_source)
add_offense(
type_attribute.loc,
"Avoid using #{type_attribute.value.inspect} as type for `<script>` tag. "\
"Must be one of: #{@config.allowed_types.join(", ")}"\
"#{" (or no type attribute)" if @config.allow_blank?}."
"Must be one of: #{@config.allowed_types.join(", ")}"\
"#{" (or no type attribute)" if @config.allow_blank?}."
)
end
end
end

def autocorrect(_processed_source, offense)
return unless offense.context

lambda do |corrector|
type_attribute, = *offense.context
if type_attribute.nil?
Expand Down
1 change: 1 addition & 0 deletions lib/erb_lint/linters/deprecated_classes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def class_name_with_loc(processed_source)
tags(processed_source).each do |tag|
class_value = tag.attributes["class"]&.value
next unless class_value

class_value.split(" ").each do |class_name|
yielder.yield(class_name, tag.loc)
end
Expand Down
3 changes: 3 additions & 0 deletions lib/erb_lint/linters/hard_coded_string.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

require "set"
require "better_html/tree/tag"
require "active_support/core_ext/string/inflections"
Expand Down Expand Up @@ -83,6 +84,7 @@ def autocorrect(processed_source, offense)
string = offense.source_range.source
return unless (klass = load_corrector)
return unless string.strip.length > 1

node = ::RuboCop::AST::StrNode.new(:str, [string])
corrector = klass.new(node, processed_source.filename, corrector_i18n_load_path, offense.source_range)
corrector.autocorrect(tag_start: "<%= ", tag_end: " %>")
Expand All @@ -100,6 +102,7 @@ def check_string?(str)
def load_corrector
corrector_name = @config["corrector"].fetch("name") { raise MissingCorrector }
raise ForbiddenCorrector unless ALLOWED_CORRECTORS.include?(corrector_name)

require @config["corrector"].fetch("path") { raise MissingCorrector }

corrector_name.safe_constantize
Expand Down
4 changes: 3 additions & 1 deletion lib/erb_lint/linters/no_javascript_tag_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def run(processed_source)
indicator_node, _, code_node, _ = *erb_node
indicator = indicator_node&.loc&.source
next if indicator == "#"

source = code_node.loc.source

ruby_node =
Expand All @@ -31,13 +32,14 @@ def run(processed_source)
nil
end
next unless ruby_node

send_node = ruby_node.descendants(:send).first
next unless send_node&.method_name?(:javascript_tag)

add_offense(
erb_node.loc,
"Avoid using 'javascript_tag do' as it confuses tests "\
"that validate html, use inline <script> instead",
"that validate html, use inline <script> instead",
[erb_node, send_node]
)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/erb_lint/linters/require_input_autocomplete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def find_html_input_tags(parser)
add_offense(
tag_node.to_a[1].loc,
"Input tag is missing an autocomplete attribute. If no "\
"autocomplete behaviour is desired, use the value `off` or `nope`.",
"autocomplete behaviour is desired, use the value `off` or `nope`.",
[autocomplete_attribute]
)
end
Expand Down Expand Up @@ -97,7 +97,7 @@ def find_rails_helper_input_tags(parser)
add_offense(
erb_node.loc,
"Input field helper is missing an autocomplete attribute. If no "\
"autocomplete behaviour is desired, use the value `off` or `nope`.",
"autocomplete behaviour is desired, use the value `off` or `nope`.",
[erb_node, send_node]
)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/erb_lint/linters/require_script_nonce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ def find_rails_helper_script_tags(parser)

def tag_helper?(send_node)
send_node&.method_name?(:javascript_tag) ||
send_node&.method_name?(:javascript_include_tag) ||
send_node&.method_name?(:javascript_pack_tag)
send_node&.method_name?(:javascript_include_tag) ||
send_node&.method_name?(:javascript_pack_tag)
end

def code_comment?(indicator_node)
Expand Down
1 change: 1 addition & 0 deletions lib/erb_lint/linters/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def run(processed_source)
if ::RuboCop::Version::STRING.to_f >= 0.87
def autocorrect(_processed_source, offense)
return unless offense.context

rubocop_correction = offense.context[:rubocop_correction]
return unless rubocop_correction

Expand Down
1 change: 1 addition & 0 deletions lib/erb_lint/linters/self_closing_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def run(processed_source)
end

next unless @config.enforced_style == :never && tag.self_closing?

end_solidus = tag_node.children.last
add_offense(
end_solidus.loc,
Expand Down
9 changes: 5 additions & 4 deletions lib/erb_lint/linters/space_around_erb_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,23 @@ def run(processed_source)
indicator_node, ltrim, code_node, rtrim = *erb_node
indicator = indicator_node&.loc&.source
next if indicator == "#" || indicator == "%"

code = code_node.children.first

start_spaces = code.match(START_SPACES)&.captures&.first || ""
if start_spaces.size != 1 && !start_spaces.include?("\n")
add_offense(
code_node.loc.resize(start_spaces.size),
"Use 1 space after `<%#{indicator}#{ltrim&.loc&.source}` "\
"instead of #{start_spaces.size} space#{"s" if start_spaces.size > 1}.",
"instead of #{start_spaces.size} space#{"s" if start_spaces.size > 1}.",
" "
)
elsif start_spaces.count("\n") > 1
lines = start_spaces.split("\n", -1)
add_offense(
code_node.loc.resize(start_spaces.size),
"Use 1 newline after `<%#{indicator&.loc&.source}#{ltrim&.loc&.source}` "\
"instead of #{start_spaces.count("\n")}.",
"instead of #{start_spaces.count("\n")}.",
"#{lines.first}\n#{lines.last}"
)
end
Expand All @@ -41,15 +42,15 @@ def run(processed_source)
add_offense(
code_node.loc.end.adjust(begin_pos: -end_spaces.size),
"Use 1 space before `#{rtrim&.loc&.source}%>` "\
"instead of #{end_spaces.size} space#{"s" if start_spaces.size > 1}.",
"instead of #{end_spaces.size} space#{"s" if start_spaces.size > 1}.",
" "
)
elsif end_spaces.count("\n") > 1
lines = end_spaces.split("\n", -1)
add_offense(
code_node.loc.end.adjust(begin_pos: -end_spaces.size),
"Use 1 newline before `#{rtrim&.loc&.source}%>` "\
"instead of #{end_spaces.count("\n")}.",
"instead of #{end_spaces.count("\n")}.",
"#{lines.first}\n#{lines.last}"
)
end
Expand Down
1 change: 1 addition & 0 deletions lib/erb_lint/linters/space_in_html_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def process_attributes(processed_source, attributes)
no_space(processed_source, equal.loc.end_pos...value.loc.begin_pos) if equal && value

next if index >= attributes.children.size - 1

next_attribute = attributes.children[index + 1]

single_space_or_newline(
Expand Down
1 change: 1 addition & 0 deletions lib/erb_lint/offense.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def initialize(linter, source_range, message, context = nil, severity = nil)
unless source_range.is_a?(Parser::Source::Range)
raise ArgumentError, "expected Parser::Source::Range for arg 2"
end

@linter = linter
@source_range = source_range
@message = message
Expand Down
2 changes: 2 additions & 0 deletions lib/erb_lint/reporter.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

require "active_support/core_ext/class"
require "active_support/core_ext/module/delegation"

Expand Down Expand Up @@ -34,6 +35,7 @@ def show; end
private

attr_reader :stats, :autocorrect

delegate :processed_files, to: :stats
end
end
1 change: 1 addition & 0 deletions lib/erb_lint/reporters/multiline_reporter.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

require_relative "compact_reporter"

module ERBLint
Expand Down
1 change: 1 addition & 0 deletions lib/erb_lint/runner_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def for_linter(klass)
end
linter_klass = LinterRegistry.find_by_name(klass_name)
raise Error, "#{klass_name}: linter not found (is it loaded?)" unless linter_klass

linter_klass.config_schema.new(config_hash_for_linter(klass_name))
end

Expand Down
1 change: 1 addition & 0 deletions lib/erb_lint/runner_config_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def resolve_inheritance(hash, file_loader)
base_configs(file_loader, inherited_files).reverse_each do |base_config|
base_config.each do |k, v|
next unless v.is_a?(Hash)

v = v.deep_merge(hash[k]) if hash.key?(k)
hash[k] = v
end
Expand Down
1 change: 1 addition & 0 deletions lib/erb_lint/stats.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

module ERBLint
class Stats
attr_accessor :ignored,
Expand Down
1 change: 1 addition & 0 deletions lib/erb_lint/utils/block_map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ def parser

def find_entry(range)
return unless range

@entries.find do |entry|
entry.contains_ruby_range?(Range.new(range.begin_pos, range.end_pos))
end
Expand Down
2 changes: 1 addition & 1 deletion lib/erb_lint/utils/offset_corrector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def to_range(node_or_range)
else
raise TypeError,
"Expected a Parser::Source::Range, Comment or " \
"Rubocop::AST::Node, got #{node_or_range.class}"
"Rubocop::AST::Node, got #{node_or_range.class}"
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/erb_lint/utils/ruby_to_erb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def html_options_to_tag_attributes(hash_node)

def ruby_to_erb(node, indicator = nil, &block)
return node if node.nil? || node.is_a?(String)

case node.type
when :str, :sym
s = node.children.first.to_s
Expand Down
4 changes: 2 additions & 2 deletions spec/erb_lint/linters/allowed_script_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
expect(subject).to(eq([
build_offense(8..29,
"Avoid using \"text/yavascript\" as type for `<script>` tag. "\
"Must be one of: text/javascript (or no type attribute)."),
"Must be one of: text/javascript (or no type attribute)."),
]))
end
end
Expand All @@ -41,7 +41,7 @@
expect(subject).to(eq([
build_offense(1..6,
"Avoid using inline `<script>` tags altogether. "\
"Instead, move javascript code into a static file."),
"Instead, move javascript code into a static file."),
]))
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/erb_lint/linters/no_javascript_tag_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def build_offense(range)
linter,
processed_source.to_source_range(range),
"Avoid using 'javascript_tag do' as it confuses tests "\
"that validate html, use inline <script> instead"
"that validate html, use inline <script> instead"
)
end
end
8 changes: 4 additions & 4 deletions spec/erb_lint/linters/require_input_autocomplete_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
end
let(:html_message) do
"Input tag is missing an autocomplete attribute. If no autocomplete behaviour "\
"is desired, use the value `off` or `nope`."
"is desired, use the value `off` or `nope`."
end
let(:form_helper_message) do
"Input field helper is missing an autocomplete attribute. If no autocomplete "\
"behaviour is desired, use the value `off` or `nope`."
"behaviour is desired, use the value `off` or `nope`."
end
before { linter.run(processed_source) }

Expand Down Expand Up @@ -68,7 +68,7 @@
s + "<%= " + helper.to_s + ' autocomplete: "foo" do %>'
end
}
FILE
FILE

it { expect(subject).to(eq([])) }
end
Expand All @@ -81,7 +81,7 @@
s + "<%= " + helper.to_s + " do %>"
end
}
FILE
FILE

it do
expect(subject).to(eq([
Expand Down
Loading

0 comments on commit f4866cf

Please sign in to comment.