Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add add_offense method to match what rubocop is doing #69

Merged
merged 2 commits into from
Feb 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions lib/erb_lint/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@ def run(args = ARGV)
"#{enabled_linter_classes.size} #{'autocorrectable ' if autocorrect?}linters..."
puts

runner = ERBLint::Runner.new(file_loader, @config)
lint_files.each do |filename|
begin
run_with_corrections(runner, filename)
run_with_corrections(filename)
rescue => e
puts "Exception occured when processing: #{relative_filename(filename)}"
puts e.message
Expand Down Expand Up @@ -93,16 +92,17 @@ def autocorrect?
@options[:autocorrect]
end

def run_with_corrections(runner, filename)
def run_with_corrections(filename)
file_content = File.read(filename)
offenses = []

runner = ERBLint::Runner.new(file_loader, @config)

7.times do
processed_source = ERBLint::ProcessedSource.new(filename, file_content)
offenses = runner.run(processed_source)
break unless autocorrect? && offenses.any?
runner.run(processed_source)
break unless autocorrect? && runner.offenses.any?

corrector = correct(processed_source, offenses)
corrector = correct(processed_source, runner.offenses)
break if corrector.corrections.empty?
break if processed_source.file_content == corrector.corrected_content

Expand All @@ -115,8 +115,8 @@ def run_with_corrections(runner, filename)
file_content = corrector.corrected_content
end

@stats.found += offenses.size
offenses.each do |offense|
@stats.found += runner.offenses.size
runner.offenses.each do |offense|
puts <<~EOF
#{offense.message}#{' (not autocorrected)'.red if autocorrect?}
In file: #{relative_filename(filename)}:#{offense.line_range.begin}
Expand Down
9 changes: 8 additions & 1 deletion lib/erb_lint/linter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ def support_autocorrect?
end
end

attr_reader :offenses

# Must be implemented by the concrete inheriting class.
def initialize(file_loader, config)
@file_loader = file_loader
@config = config
raise ArgumentError, "expect `config` to be #{self.class.config_schema} instance, "\
"not #{config.class}" unless config.is_a?(self.class.config_schema)
@offenses = []
end

def enabled?
Expand All @@ -45,8 +48,12 @@ def excludes_file?(filename)
@config.excludes_file?(filename)
end

def offenses(_processed_source)
def run(_processed_source)
raise NotImplementedError, "must implement ##{__method__}"
end

def add_offense(source_range, message, context = nil)
@offenses << Offense.new(self, source_range, message, context)
end
end
end
65 changes: 30 additions & 35 deletions lib/erb_lint/linters/allowed_script_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,45 +19,40 @@ class ConfigSchema < LinterConfig
end
self.config_schema = ConfigSchema

def offenses(processed_source)
def run(processed_source)
parser = processed_source.parser
[].tap do |offenses|
parser.nodes_with_type(:tag).each do |tag_node|
tag = BetterHtml::Tree::Tag.from_node(tag_node)
next if tag.closing?
next unless tag.name == 'script'
parser.nodes_with_type(:tag).each do |tag_node|
tag = BetterHtml::Tree::Tag.from_node(tag_node)
next if tag.closing?
next unless tag.name == 'script'

if @config.disallow_inline_scripts?
name_node = tag_node.to_a[1]
offenses << Offense.new(
self,
name_node.loc,
"Avoid using inline `<script>` tags altogether. "\
"Instead, move javascript code into a static file."
)
next
end
if @config.disallow_inline_scripts?
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."
)
next
end

type_attribute = tag.attributes['type']
type_present = type_attribute.present? && type_attribute.value_node.present?
type_attribute = tag.attributes['type']
type_present = type_attribute.present? && type_attribute.value_node.present?

if !type_present && !@config.allow_blank?
name_node = tag_node.to_a[1]
offenses << Offense.new(
self,
name_node.loc,
"Missing a `type=\"text/javascript\"` attribute to `<script>` tag.",
[type_attribute]
)
elsif type_present && !@config.allowed_types.include?(type_attribute.value)
offenses << Offense.new(
self,
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?}."
)
end
if !type_present && !@config.allow_blank?
name_node = tag_node.to_a[1]
add_offense(
name_node.loc,
"Missing a `type=\"text/javascript\"` attribute to `<script>` tag.",
[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?}."
)
end
end
end
Expand Down
13 changes: 5 additions & 8 deletions lib/erb_lint/linters/closing_erb_tag_indent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class ClosingErbTagIndent < Linter
START_SPACES = /\A([[:space:]]*)/m
END_SPACES = /([[:space:]]*)\z/m

def offenses(processed_source)
processed_source.ast.descendants(:erb).each_with_object([]) do |erb_node, offenses|
def run(processed_source)
processed_source.ast.descendants(:erb).each do |erb_node|
_, _, code_node, = *erb_node
code = code_node.children.first

Expand All @@ -22,24 +22,21 @@ def offenses(processed_source)
end_with_newline = end_spaces.include?("\n")

if !start_with_newline && end_with_newline
offenses << Offense.new(
self,
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
offenses << Offense.new(
self,
add_offense(
code_node.loc.end.adjust(begin_pos: -end_spaces.size),
"Insert newline before `%>` to match start of tag.",
"\n"
)
elsif start_with_newline && end_with_newline
current_indent = end_spaces.split("\n", -1).last
if erb_node.loc.column != current_indent.size
offenses << Offense.new(
self,
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
Expand Down
15 changes: 6 additions & 9 deletions lib/erb_lint/linters/deprecated_classes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def initialize(file_loader, config)
@addendum = @config.addendum
end

def offenses(processed_source)
def run(processed_source)
process_nested_offenses(
source: processed_source,
offset: 0,
Expand All @@ -40,20 +40,18 @@ def offenses(processed_source)
private

def process_nested_offenses(source:, offset:, parent_source:)
offenses = []
class_name_with_loc(source).each do |class_name, loc|
range = parent_source.to_source_range(loc).offset(offset)
offenses += generate_errors(class_name, range)
generate_offenses(class_name, range)
end
text_tags_content(source).each do |content_node|
sub_source = ProcessedSource.new(source.filename, content_node.loc.source)
offenses += process_nested_offenses(
process_nested_offenses(
source: sub_source,
offset: offset + content_node.loc.begin_pos,
parent_source: parent_source
)
end
offenses
end

def class_name_with_loc(processed_source)
Expand Down Expand Up @@ -93,13 +91,12 @@ def tag_nodes(processed_source)
processed_source.parser.nodes_with_type(:tag)
end

def generate_errors(class_name, range)
violated_rules(class_name).map do |violated_rule|
def generate_offenses(class_name, range)
violated_rules(class_name).each do |violated_rule|
suggestion = " #{violated_rule[:suggestion]}".rstrip
message = "Deprecated class `%s` detected matching the pattern `%s`.%s #{@addendum}".strip

Offense.new(
self,
add_offense(
range,
format(message, class_name, violated_rule[:class_expr], suggestion)
)
Expand Down
11 changes: 3 additions & 8 deletions lib/erb_lint/linters/erb_safety.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,16 @@ def initialize(file_loader, config)
@config_filename = @config.better_html_config
end

def offenses(processed_source)
offenses = []

parser = BetterHtml::Parser.new(processed_source.source_buffer, template_language: :html)
testers_for(parser).each do |tester|
def run(processed_source)
testers_for(processed_source.parser).each do |tester|
tester.validate
tester.errors.each do |error|
offenses << Offense.new(
self,
add_offense(
error.location,
error.message
)
end
end
offenses
end

private
Expand Down
15 changes: 5 additions & 10 deletions lib/erb_lint/linters/extra_newline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,16 @@ class ExtraNewline < Linter

EXTRA_NEWLINES = /(\n{3,})/m

def offenses(processed_source)
matches = processed_source.file_content.match(EXTRA_NEWLINES)
return [] unless matches
def run(processed_source)
return unless (matches = processed_source.file_content.match(EXTRA_NEWLINES))

offenses = []
matches.captures.each_index do |index|
offenses << Offense.new(
self,
processed_source.to_source_range(
(matches.begin(index) + 2)...matches.end(index)
),
add_offense(
processed_source
.to_source_range((matches.begin(index) + 2)...matches.end(index)),
"Extra blank line detected."
)
end
offenses
end

def autocorrect(_processed_source, offense)
Expand Down
42 changes: 18 additions & 24 deletions lib/erb_lint/linters/final_newline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,43 +16,37 @@ def initialize(file_loader, config)
@new_lines_should_be_present = @config.present?
end

def offenses(processed_source)
def run(processed_source)
file_content = processed_source.file_content

offenses = []
return offenses if file_content.empty?
return if file_content.empty?

match = file_content.match(/(\n+)\z/)
final_newline = match&.captures&.first || ""

if @new_lines_should_be_present && final_newline.size != 1
offenses <<
if final_newline.empty?
Offense.new(
self,
processed_source.to_source_range(file_content.size...file_content.size),
'Missing a trailing newline at the end of the file.',
:insert
)
else
Offense.new(
self,
processed_source.to_source_range(
(file_content.size - final_newline.size + 1)...file_content.size
),
'Remove multiple trailing newline at the end of the file.',
:remove
)
end
if final_newline.empty?
add_offense(
processed_source.to_source_range(file_content.size...file_content.size),
'Missing a trailing newline at the end of the file.',
:insert
)
else
add_offense(
processed_source.to_source_range(
(file_content.size - final_newline.size + 1)...file_content.size
),
'Remove multiple trailing newline at the end of the file.',
:remove
)
end
elsif !@new_lines_should_be_present && !final_newline.empty?
offenses << Offense.new(
self,
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
)
end
offenses
end

def autocorrect(_processed_source, offense)
Expand Down
7 changes: 3 additions & 4 deletions lib/erb_lint/linters/hard_coded_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ConfigSchema < LinterConfig
end
self.config_schema = ConfigSchema

def offenses(processed_source)
def run(processed_source)
hardcoded_strings = processed_source.ast.descendants(:text).each_with_object([]) do |text_node, to_check|
next if javascript?(processed_source, text_node)

Expand All @@ -34,12 +34,11 @@ def offenses(processed_source)
end
end

hardcoded_strings.compact.map do |text_node, offended_str|
hardcoded_strings.compact.each do |text_node, offended_str|
range = find_range(text_node, offended_str)
source_range = processed_source.to_source_range(range)

Offense.new(
self,
add_offense(
source_range,
message(source_range.source)
)
Expand Down
Loading