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

Support caching with json and compact reporters #1

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
4 changes: 2 additions & 2 deletions lib/erb_lint/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def get(filename, file_content)
begin
cache_file_contents_as_offenses = JSON.parse(
File.read(File.join(CACHE_DIRECTORY, file_checksum))
).map do |offense|
ERBLint::CachedOffense.from_json(offense)
).map do |offense_hash|
ERBLint::CachedOffense.new(offense_hash)
end
rescue Errno::ENOENT
return false
Expand Down
55 changes: 37 additions & 18 deletions lib/erb_lint/cached_offense.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,56 @@
module ERBLint
# A Cached version of an Offense with only essential information represented as strings
class CachedOffense
attr_reader :line_number, :message, :severity
attr_reader(
:message,
:line_number,
:severity,
:column,
:simple_name,
:last_line,
:last_column,
:length,
)

def initialize(message, line_number, severity)
@message = message
@line_number = line_number
@severity = severity
def initialize(params)
params = params.transform_keys(&:to_sym)

@message = params[:message]
@line_number = params[:line_number]
@severity = params[:severity]&.to_sym
@column = params[:column]
@simple_name = params[:simple_name]
@last_line = params[:last_line]
@last_column = params[:last_column]
@length = params[:length]
end

def self.new_from_offense(offense)
new(
offense.message,
offense.line_number.to_s,
offense.severity
{
message: offense.message,
line_number: offense.line_number,
severity: offense.severity,
column: offense.column,
simple_name: offense.simple_name,
last_line: offense.last_line,
last_column: offense.last_column,
length: offense.length,
}
)
end

def to_json_format
def to_h
{
message: message,
line_number: line_number,
severity: severity,
column: column,
simple_name: simple_name,
last_line: last_line,
last_column: last_column,
length: length,
}
end

def self.from_json(parsed_json)
parsed_json.transform_keys!(&:to_sym)
new(
parsed_json[:message],
parsed_json[:line_number],
parsed_json[:severity]&.to_sym
)
end
end
end
2 changes: 1 addition & 1 deletion lib/erb_lint/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def run_using_cache(runner, filename, file_content)
runner.restore_offenses(cache_result_offenses)
else
run_with_corrections(runner, filename, file_content)
cache.set(filename, file_content, runner.offenses.map(&:to_cached_offense_json_format).to_json)
cache.set(filename, file_content, runner.offenses.map(&:to_cached_offense_hash).to_json)
end
end

Expand Down
20 changes: 18 additions & 2 deletions lib/erb_lint/offense.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def initialize(linter, source_range, message, context = nil, severity = nil)
@severity = severity
end

def to_cached_offense_json_format
ERBLint::CachedOffense.new_from_offense(self).to_json_format
def to_cached_offense_hash
ERBLint::CachedOffense.new_from_offense(self).to_h
end

def inspect
Expand Down Expand Up @@ -47,5 +47,21 @@ def line_number
def column
source_range.column
end

def simple_name
linter.class.simple_name
end

def last_line
source_range.last_line
end

def last_column
source_range.last_column
end

def length
source_range.length
end
end
end
8 changes: 4 additions & 4 deletions lib/erb_lint/reporters/json_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ def formatted_offenses(offenses)

def format_offense(offense)
{
linter: offense.linter.class.simple_name,
linter: offense.simple_name,
message: offense.message.to_s,
location: {
start_line: offense.line_number,
start_column: offense.column,
last_line: offense.source_range.last_line,
last_column: offense.source_range.last_column,
length: offense.source_range.length,
last_line: offense.last_line,
last_column: offense.last_column,
length: offense.length,
},
}
end
Expand Down
2 changes: 1 addition & 1 deletion spec/erb_lint/fixtures/cache_file_content
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"message":"Layout/InitialIndentation: Indentation of first line in file detected.","line_number":"1","severity":"convention"},{"message":"Layout/TrailingEmptyLines: Final newline missing.","line_number":"1","severity":"convention"}]
[{"message":"Layout/InitialIndentation: Indentation of first line in file detected.","line_number":"1","severity":"convention","line_number":"1","severity":"convention","column":"3","simple_name":"Rubocop","last_line":"1","last_column":"8","length":"5"},{"message":"Layout/TrailingEmptyLines: Final newline missing.","line_number":"1","severity":"convention","line_number":"1","severity":"convention","column":"3","simple_name":"Rubocop","last_line":"1","last_column":"8","length":"5"}]
22 changes: 8 additions & 14 deletions spec/erb_lint/reporters/json_reporter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,20 @@
message: "Extra space detected where there should be no space.",
line_number: 1,
column: 7,
source_range: instance_double(
BetterHtml::Tokenizer::Location,
last_line: 1,
last_column: 9,
length: 2,
),
linter: ERBLint::Linters::SpaceInHtmlTag.new(nil, ERBLint::LinterConfig.new),
simple_name: "SpaceInHtmlTag",
last_line: 1,
last_column: 9,
length: 2,
),
instance_double(
ERBLint::Offense,
message: "Remove newline before `%>` to match start of tag.",
line_number: 52,
column: 10,
source_range: instance_double(
BetterHtml::Tokenizer::Location,
last_line: 54,
last_column: 10,
length: 10,
),
linter: ERBLint::Linters::ClosingErbTagIndent.new(nil, ERBLint::LinterConfig.new),
simple_name: "ClosingErbTagIndent",
last_line: 54,
last_column: 10,
length: 10,
),
]
end
Expand Down