Skip to content

Use Mutex in JSONFormatter #494

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

Merged
merged 3 commits into from
Nov 17, 2024
Merged
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
39 changes: 20 additions & 19 deletions src/ameba/formatter/json_formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ module Ameba::Formatter
#
# Example:
#
# ```
# ```json
# {
# "metadata": {
# "ameba_version": "x.x.x",
# "ameba_version": "x.x.x",
# "crystal_version": "x.x.x",
# },
# "sources": [
Expand All @@ -17,55 +17,54 @@ module Ameba::Formatter
# {
# "location": {
# "column": 7,
# "line": 17,
# "line": 17,
# },
# "end_location": {
# "column": 20,
# "line": 17,
# "line": 17,
# },
# "message": "Useless assignment to variable `a`",
# "message": "Useless assignment to variable `a`",
# "rule_name": "UselessAssign",
# "severity": "Convention",
# "severity": "Convention",
# },
# {
# "location": {
# "column": 7,
# "line": 18,
# "line": 18,
# },
# "end_location": {
# "column": 8,
# "line": 18,
# "line": 18,
# },
# "message": "Useless assignment to variable `a`",
# "message": "Useless assignment to variable `a`",
# "rule_name": "UselessAssign",
# },
# {
# "location": {
# "column": 7,
# "line": 19,
# "line": 19,
# },
# "end_location": {
# "column": 9,
# "line": 19,
# "line": 19,
# },
# "message": "Useless assignment to variable `a`",
# "message": "Useless assignment to variable `a`",
# "rule_name": "UselessAssign",
# "severity": "Convention",
# "severity": "Convention",
# },
# ],
# "path": "src/ameba/formatter/json_formatter.cr",
# },
# ],
# "summary": {
# "issues_count": 3,
# "issues_count": 3,
# "target_sources_count": 1,
# },
# }
# ```
class JSONFormatter < BaseFormatter
def initialize(@output = STDOUT)
@result = AsJSON::Result.new
end
@result = AsJSON::Result.new
@mutex = Mutex.new

def started(sources)
@result.summary.target_sources_count = sources.size
Expand All @@ -85,10 +84,12 @@ module Ameba::Formatter
issue.end_location,
issue.message
)
@result.summary.issues_count += 1
end

@result.sources << json_source
@mutex.synchronize do
@result.summary.issues_count += json_source.issues.size
@result.sources << json_source
end
end

def finished(sources)
Expand Down