Skip to content

Commit

Permalink
Run bundle exec rubocop autocorrect
Browse files Browse the repository at this point in the history
  • Loading branch information
george-ma committed Mar 19, 2024
1 parent f12c57a commit 35965ee
Show file tree
Hide file tree
Showing 64 changed files with 324 additions and 239 deletions.
4 changes: 2 additions & 2 deletions lib/erb_lint/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def get(filename, file_content)
file_checksum = checksum(filename, file_content)
begin
cache_file_contents_as_offenses = JSON.parse(
File.read(File.join(@cache_dir, file_checksum))
File.read(File.join(@cache_dir, file_checksum)),
).map do |offense_hash|
ERBLint::CachedOffense.new(offense_hash)
end
Expand Down Expand Up @@ -76,7 +76,7 @@ def checksum(filename, file_content)
mode = File.stat(filename).mode

digester.update(
"#{mode}#{config.to_hash}#{ERBLint::VERSION}#{file_content}"
"#{mode}#{config.to_hash}#{ERBLint::VERSION}#{file_content}",
)
digester.hexdigest
rescue Errno::ENOENT
Expand Down
2 changes: 1 addition & 1 deletion lib/erb_lint/cached_offense.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def self.new_from_offense(offense)
last_line: offense.last_line,
last_column: offense.last_column,
length: offense.length,
}
},
)
end

Expand Down
16 changes: 10 additions & 6 deletions lib/erb_lint/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def run(args = ARGV)
rescue => e
@stats.exceptions += 1
puts "Exception occurred when processing: #{relative_filename(filename)}"
puts "If this file cannot be processed by erb-lint, "\
puts "If this file cannot be processed by erb-lint, " \
"you can exclude it in your configuration file."
puts e.message
puts Rainbow(e.backtrace.join("\n")).red
Expand Down Expand Up @@ -303,7 +303,7 @@ def runner_config_override
ERBLint::LinterRegistry.linters.map do |klass|
linters[klass.simple_name] = { "enabled" => enabled_linter_classes.include?(klass) }
end
end
end,
)
end

Expand Down Expand Up @@ -348,8 +348,12 @@ def option_parser
@options[:clear_cache] = config
end

opts.on("--enable-linters LINTER[,LINTER,...]", Array,
"Only use specified linter", "Known linters are: #{known_linter_names.join(", ")}") do |linters|
opts.on(
"--enable-linters LINTER[,LINTER,...]",
Array,
"Only use specified linter",
"Known linters are: #{known_linter_names.join(", ")}",
) do |linters|
linters.each do |linter|
unless known_linter_names.include?(linter)
failure!("#{linter}: not a valid linter name (#{known_linter_names.join(", ")})")
Expand Down Expand Up @@ -382,7 +386,7 @@ def option_parser
opts.on(
"-sFILE",
"--stdin FILE",
"Pipe source from STDIN. Takes the path to be used to check which rules to apply."
"Pipe source from STDIN. Takes the path to be used to check which rules to apply.",
) do |file|
@options[:stdin] = [file]
end
Expand All @@ -398,7 +402,7 @@ def option_parser
end

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

Expand Down
2 changes: 1 addition & 1 deletion lib/erb_lint/linter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def support_autocorrect?
def initialize(file_loader, config)
@file_loader = file_loader
@config = config
raise ArgumentError, "expect `config` to be #{self.class.config_schema} instance, "\
raise ArgumentError, "expect `config` to be #{self.class.config_schema} instance, " \
"not #{config.class}" unless config.is_a?(self.class.config_schema)
@offenses = []
end
Expand Down
15 changes: 8 additions & 7 deletions lib/erb_lint/linters/allowed_script_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class AllowedScriptType < Linter
include LinterRegistry

class ConfigSchema < LinterConfig
property :allowed_types, accepts: array_of?(String),
property :allowed_types,
accepts: array_of?(String),
default: -> { ["text/javascript"] }
property :allow_blank, accepts: [true, false], default: true, reader: :allow_blank?
property :disallow_inline_scripts, accepts: [true, false], default: false, reader: :disallow_inline_scripts?
Expand All @@ -30,8 +31,8 @@ def run(processed_source)
name_node = tag_node.to_a[1]
add_offense(
name_node.loc,
"Avoid using inline `<script>` tags altogether. "\
"Instead, move javascript code into a static file."
"Avoid using inline `<script>` tags altogether. " \
"Instead, move javascript code into a static file.",
)
next
end
Expand All @@ -44,14 +45,14 @@ def run(processed_source)
add_offense(
name_node.loc,
"Missing a `type=\"text/javascript\"` attribute to `<script>` tag.",
[type_attribute]
[type_attribute],
)
elsif type_present && !@config.allowed_types.include?(type_attribute.value)
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?}."
"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?}.",
)
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/erb_lint/linters/closing_erb_tag_indent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@ def run(processed_source)
add_offense(
code_node.loc.end.adjust(begin_pos: -end_spaces.size),
"Remove newline before `%>` to match start of tag.",
" "
" ",
)
elsif start_with_newline && !end_with_newline
add_offense(
code_node.loc.end.adjust(begin_pos: -end_spaces.size),
"Insert newline before `%>` to match start of tag.",
"\n"
"\n",
)
elsif start_with_newline && end_with_newline
current_indent = end_spaces.split("\n", -1).last
if erb_node.loc.column != current_indent.size
add_offense(
code_node.loc.end.adjust(begin_pos: -current_indent.size),
"Indent `%>` on column #{erb_node.loc.column} to match start of tag.",
" " * erb_node.loc.column
" " * erb_node.loc.column,
)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/erb_lint/linters/comment_syntax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def run(processed_source)

add_offense(
source_range,
<<~EOF.chomp
<<~EOF.chomp,
Bad ERB comment syntax. Should be #{correct_erb_tag} without a space between.
Leaving a space between ERB tags and the Ruby comment character can cause parser errors.
EOF
Expand Down
4 changes: 2 additions & 2 deletions lib/erb_lint/linters/deprecated_classes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def process_nested_offenses(source:, offset:, parent_source:)
process_nested_offenses(
source: sub_source,
offset: offset + content_node.loc.begin_pos,
parent_source: parent_source
parent_source: parent_source,
)
end
end
Expand Down Expand Up @@ -99,7 +99,7 @@ def generate_offenses(class_name, range)

add_offense(
range,
format(message, class_name, violated_rule[:class_expr], suggestion)
format(message, class_name, violated_rule[:class_expr], suggestion),
)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/erb_lint/linters/erb_safety.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def run(processed_source)
tester.errors.each do |error|
add_offense(
error.location,
error.message
error.message,
)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/erb_lint/linters/extra_newline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def run(processed_source)
add_offense(
processed_source
.to_source_range((matches.begin(index) + 2)...matches.end(index)),
"Extra blank line detected."
"Extra blank line detected.",
)
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/erb_lint/linters/final_newline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@ def run(processed_source)
add_offense(
processed_source.to_source_range(file_content.size...file_content.size),
"Missing a trailing newline at the end of the file.",
:insert
:insert,
)
else
add_offense(
processed_source.to_source_range(
(file_content.size - final_newline.size + 1)...file_content.size
(file_content.size - final_newline.size + 1)...file_content.size,
),
"Remove multiple trailing newline at the end of the file.",
:remove
:remove,
)
end
elsif !@new_lines_should_be_present && !final_newline.empty?
add_offense(
processed_source.to_source_range(match.begin(0)...match.end(0)),
"Remove #{final_newline.size} trailing newline at the end of the file.",
:remove
:remove,
)
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/erb_lint/linters/hard_coded_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def run(processed_source)

add_offense(
source_range,
message(source_range.source)
message(source_range.source),
)
end
end
Expand All @@ -84,7 +84,7 @@ def find_range(node, str)
def autocorrect(processed_source, offense)
string = offense.source_range.source
return unless (klass = load_corrector)
return unless string.strip.length > 1
return if 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)
Expand Down
10 changes: 6 additions & 4 deletions lib/erb_lint/linters/no_javascript_tag_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ def run(processed_source)

add_offense(
erb_node.loc,
"Avoid using 'javascript_tag do' as it confuses tests "\
"Avoid using 'javascript_tag do' as it confuses tests " \
"that validate html, use inline <script> instead",
[erb_node, send_node]
[erb_node, send_node],
)
end
end
Expand Down Expand Up @@ -82,8 +82,10 @@ def correct_offense(processed_source, offense, corrector)
corrector.replace(end_node.loc, end_content)
elsif script_content
script_content = "\n//<![CDATA[\n#{script_content}\n//]]>\n" if @config.correction_style == :cdata
corrector.replace(begin_node.loc,
"<script#{arguments}>#{script_content}</script>")
corrector.replace(
begin_node.loc,
"<script#{arguments}>#{script_content}</script>",
)
end
rescue Utils::RubyToERB::Error, Utils::BlockMap::ParseError
nil
Expand Down
6 changes: 4 additions & 2 deletions lib/erb_lint/linters/no_unused_disable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ def run(processed_source, offenses)

disabled_rules_and_line_number.each do |rule, line_numbers|
line_numbers.each do |line_number|
add_offense(processed_source.source_buffer.line_range(line_number),
"Unused erblint:disable comment for #{rule}")
add_offense(
processed_source.source_buffer.line_range(line_number),
"Unused erblint:disable comment for #{rule}",
)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/erb_lint/linters/parser_errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def run(processed_source)
processed_source.parser.parser_errors.each do |error|
add_offense(
error.loc,
"#{error.message} (at #{error.loc.source})"
"#{error.message} (at #{error.loc.source})",
)
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/erb_lint/linters/partial_instance_variable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ def run(processed_source)

add_offense(
processed_source.to_source_range(
processed_source.file_content =~ instance_variable_regex..processed_source.file_content.size
processed_source.file_content =~ instance_variable_regex..processed_source.file_content.size,
),
"Instance variable detected in partial."
"Instance variable detected in partial.",
)
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/erb_lint/linters/require_input_autocomplete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ def find_html_input_tags(parser)

add_offense(
tag_node.to_a[1].loc,
"Input tag is missing an autocomplete attribute. If no "\
"Input tag is missing an autocomplete attribute. If no " \
"autocomplete behaviour is desired, use the value `off` or `nope`.",
[autocomplete_attribute]
[autocomplete_attribute],
)
end
end
Expand Down Expand Up @@ -96,9 +96,9 @@ def find_rails_helper_input_tags(parser)

add_offense(
erb_node.loc,
"Input field helper is missing an autocomplete attribute. If no "\
"Input field helper is missing an autocomplete attribute. If no " \
"autocomplete behaviour is desired, use the value `off` or `nope`.",
[erb_node, send_node]
[erb_node, send_node],
)
end
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 @@ -29,7 +29,7 @@ def find_html_script_tags(parser)
add_offense(
tag_node.to_a[1].loc,
"Missing a nonce attribute. Use request.content_security_policy_nonce",
[nonce_attribute]
[nonce_attribute],
)
end
end
Expand Down Expand Up @@ -67,7 +67,7 @@ def find_rails_helper_script_tags(parser)
add_offense(
erb_node.loc,
"Missing a nonce attribute. Use nonce: true",
[erb_node, send_node]
[erb_node, send_node],
)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/erb_lint/linters/right_trim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def run(processed_source)

add_offense(
trim_node.loc,
"Prefer #{@config.enforced_style}%> instead of #{trim_node.loc.source}%> for trimming on the right."
"Prefer #{@config.enforced_style}%> instead of #{trim_node.loc.source}%> for trimming on the right.",
)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/erb_lint/linters/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def rubocop_processed_source(content, filename)
source = ::RuboCop::ProcessedSource.new(
content,
@rubocop_config.target_ruby_version,
filename
filename,
)
if ::RuboCop::Version::STRING.to_f >= 1.38
registry = RuboCop::Cop::Registry.global
Expand Down
Loading

0 comments on commit 35965ee

Please sign in to comment.