Class: CC::Engine::Issue

Inherits:
Object
  • Object
show all
Defined in:
lib/cc/engine/issue.rb

Overview

Transforms the HamlLint JSON format to the one expected by Code Climate

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(linter_name:, location:, message:, path:, root: '', severity:) ⇒ Issue

Instantiates a new Code Climate issue

rubocop:disable Metrics/ParameterLists

Examples:

issue = CC::Engine::Issue.new(
  linter_name: "AltText",
  location: {line: 42},
  message: "This is a problem",
  path: "a.haml",
  root: "/tmp",
  severity: "error"
)

Parameters:

  • linter_name (String)

    the name of the linter that threw the error

  • location (Hash)

    a container with the line of the issue

  • message (String)

    the description of the issue

  • path (String)

    the path to the file where the issue was detected

  • root (String) (defaults to: '')

    the root path of the analysis

  • severity (String)

    the severity of the issue in HamlLint terminology


37
38
39
40
41
42
# File 'lib/cc/engine/issue.rb', line 37

def initialize(linter_name:, location:, message:, path:, root: '', severity:)
  @linter = linter_name
  @description = message
  @location = Location.from_haml_lint(location: location, path: path, root: root)
  @severity = Severity.from_haml_lint(severity)
end

Instance Attribute Details

#descriptionString (readonly)

The description of the issue

Examples:

issue = CC::Engine::Issue.new(
  linter_name: "AltText",
  location: {line: 42},
  message: "This is a problem",
  path: "a.haml",
  root: "/tmp",
  severity: "error"
)
issue.description  #=> "This is a problem"

Returns:

  • (String)

59
60
61
# File 'lib/cc/engine/issue.rb', line 59

def description
  @description
end

#locationCC::Engine::Location (readonly)

The location of the issue in the file

Examples:

issue = CC::Engine::Issue.new(
  linter_name: "AltText",
  location: {line: 42},
  message: "This is a problem",
  path: "a.haml",
  root: "/tmp",
  severity: "error"
)
issue.location

Returns:


76
77
78
# File 'lib/cc/engine/issue.rb', line 76

def location
  @location
end

#severityString (readonly)

The severity of the issue in Code Climate terminology

Examples:

issue = CC::Engine::Issue.new(
  linter_name: "AltText",
  location: {line: 42},
  message: "This is a problem",
  path: "a.haml",
  root: "/tmp",
  severity: "error"
)
issue.severity  #=> "critical"

Returns:

  • (String)

93
94
95
# File 'lib/cc/engine/issue.rb', line 93

def severity
  @severity
end

Instance Method Details

#categoriesCC::Engine::Categories

The categories of the issue

Examples:

issue = CC::Engine::Issue.new(
  linter_name: "AltText",
  location: {line: 42},
  message: "This is a problem",
  path: "a.haml",
  root: "/tmp",
  severity: "error"
)
issue.category  #=> ["Compability"]

Returns:


110
111
112
# File 'lib/cc/engine/issue.rb', line 110

def categories
  @categories ||= Categories.new(linter)
end

#check_nameString

The name of the check that caught the issue

Examples:

issue = CC::Engine::Issue.new(
  linter_name: "AltText",
  location: {line: 42},
  message: "This is a problem",
  path: "a.haml",
  root: "/tmp",
  severity: "error"
)
issue.check_name  #=> "HamlLint/AltText"

Returns:

  • (String)

129
130
131
# File 'lib/cc/engine/issue.rb', line 129

def check_name
  "HamlLint/#{linter}"
end

#contentCC::Engine::Content

The content for an issue

Examples:

issue = CC::Engine::Issue.new(
  linter_name: "AltText",
  location: {line: 42},
  message: "This is a problem",
  path: "a.haml",
  root: "/tmp",
  severity: "error"
)
issue.content

Returns:


148
149
150
# File 'lib/cc/engine/issue.rb', line 148

def content
  @content ||= Content.new(linter)
end

#fingerprintCC::Engine::Fingerprint

A unique identifier for overridable issues

Examples:

issue = CC::Engine::Issue.new(
  linter_name: "AltText",
  location: {line: 42},
  message: "This is a problem",
  path: "a.haml",
  root: "/tmp",
  severity: "error"
)
issue.fingerprint

Returns:


167
168
169
# File 'lib/cc/engine/issue.rb', line 167

def fingerprint
  @fingerprint ||= Fingerprint.new(location.path, linter, description)
end

#pointsCC::Engine::RemediationPoints

The difficulty to fix an issue

Examples:

issue = CC::Engine::Issue.new(
  linter_name: "AltText",
  location: {line: 42},
  message: "This is a problem",
  path: "a.haml",
  root: "/tmp",
  severity: "error"
)
issue.points

Returns:


186
187
188
# File 'lib/cc/engine/issue.rb', line 186

def points
  @points ||= RemediationPoints.new(linter)
end

#to_hHash

Converts the issue into a Hash

Examples:

issue = CC::Engine::Issue.new(
  linter_name: "AltText",
  location: {line: 42},
  message: "This is a problem",
  path: "a.haml",
  root: "/tmp",
  severity: "error"
)
issue.to_h
#=> {
  type: "issue",
  check_name: "HamlLint/AltText",
  description: "This is a problem",
  location: <#CC::Engine::Location>,
  severity: "critical",
  categories: ["Compatibility"],
  content: {body: "..."},
  fingerprint: <#CC::Engine::Fingerprint>,
  remediation_points: 50000
}

Returns:

  • (Hash)

216
217
218
219
220
221
222
# File 'lib/cc/engine/issue.rb', line 216

def to_h
  extract_fields(:type, :check_name, :description, :location, :severity).tap do |hash|
    %i[categories content fingerprint points].each do |field|
      hash[field] = __send__(field) unless __send__(field).empty?
    end
  end
end

#to_jsonString

Converts the issue into a JSON document

Examples:

issue = CC::Engine::Issue.new(
  linter_name: "AltText",
  location: {line: 42},
  message: "This is a problem",
  path: "a.haml",
  root: "/tmp",
  severity: "error"
)
issue.to_json
#=> {
  "type": "issue",
  "check_name": "HamlLint/AltText",
  "description": "This is a problem",
  "location": {
    "path": "a.haml",
    "lines": {
      "begin": 42,
      "end": 42
    }
  },
  "severity": "critical",
  "categories": ["Compatibility"],
  "content": "...",
  "fingerprint": "...",
  "remediation_points": 50000
}

Returns:

  • (String)

256
257
258
# File 'lib/cc/engine/issue.rb', line 256

def to_json(*)
  to_h.to_json
end

#typeString

The type of the issue in Code Climate's terminology

Examples:

issue = CC::Engine::Issue.new(
  linter_name: "AltText",
  location: {line: 42},
  message: "This is a problem",
  path: "a.haml",
  root: "/tmp",
  severity: "error"
)
issue.type  #=> "issue"

Returns:

  • (String)

275
276
277
# File 'lib/cc/engine/issue.rb', line 275

def type
  'issue'
end