Class: PlatformosCheck::Check

Inherits:
Object
  • Object
show all
Includes:
JsonHelpers
Defined in:
lib/platformos_check/check.rb

Direct Known Subclasses

HtmlCheck, LiquidCheck, YamlCheck

Constant Summary collapse

SEVERITIES =

The order matters.

%i[
  error
  suggestion
  style
]
SEVERITY_VALUES =
severity: sym

> number

SEVERITIES
.map
.with_index { |sev, i| [sev, i] }
.to_h
CATEGORIES =
%i[
  liquid
  translation
  html
  yaml
  performance
  graphql
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from JsonHelpers

#format_json_parse_error, #pretty_json

Instance Attribute Details

#ignored_patternsObject



128
129
130
# File 'lib/platformos_check/check.rb', line 128

def ignored_patterns
  @ignored_patterns ||= []
end

#offensesObject



86
87
88
# File 'lib/platformos_check/check.rb', line 86

def offenses
  @offenses ||= []
end

#optionsObject

Returns the value of attribute options.



9
10
11
# File 'lib/platformos_check/check.rb', line 9

def options
  @options
end

#platformos_appObject

Returns the value of attribute platformos_app.



9
10
11
# File 'lib/platformos_check/check.rb', line 9

def platformos_app
  @platformos_app
end

Class Method Details

.allObject



35
36
37
# File 'lib/platformos_check/check.rb', line 35

def all
  @all ||= []
end

.can_disable(disableable = nil) ⇒ Object



75
76
77
78
# File 'lib/platformos_check/check.rb', line 75

def can_disable(disableable = nil)
  @can_disable = disableable unless disableable.nil?
  defined?(@can_disable) ? @can_disable : true
end

.categories(*categories) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/platformos_check/check.rb', line 52

def categories(*categories)
  @categories ||= []
  if categories.any?
    unknown_categories = categories.select { |category| !CATEGORIES.include?(category) }
    if unknown_categories.any?
      raise ArgumentError,
            "unknown categories: #{unknown_categories.join(', ')}. Use: #{CATEGORIES.join(', ')}"
    end
    @categories = categories
  end
  @categories
end

.categoryObject



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/platformos_check/check.rb', line 64

def categories(*categories)
  @categories ||= []
  if categories.any?
    unknown_categories = categories.select { |category| !CATEGORIES.include?(category) }
    if unknown_categories.any?
      raise ArgumentError,
            "unknown categories: #{unknown_categories.join(', ')}. Use: #{CATEGORIES.join(', ')}"
    end
    @categories = categories
  end
  @categories
end

.doc(doc = nil) ⇒ Object



66
67
68
69
# File 'lib/platformos_check/check.rb', line 66

def doc(doc = nil)
  @doc = doc if doc
  @doc if defined?(@doc)
end

.docs_url(path) ⇒ Object



71
72
73
# File 'lib/platformos_check/check.rb', line 71

def docs_url(path)
  "https://github.com/Platform-OS/platformos-lsp/blob/master/docs/checks/#{File.basename(path, '.rb')}.md"
end

.severity(severity = nil) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/platformos_check/check.rb', line 39

def severity(severity = nil)
  if severity
    raise ArgumentError, "unknown severity. Use: #{SEVERITIES.join(', ')}" unless SEVERITIES.include?(severity)

    @severity = severity
  end
  @severity if defined?(@severity)
end

.severity_value(severity) ⇒ Object



48
49
50
# File 'lib/platformos_check/check.rb', line 48

def severity_value(severity)
  SEVERITY_VALUES[severity] || -1
end

.single_file(single_file = nil) ⇒ Object



80
81
82
83
# File 'lib/platformos_check/check.rb', line 80

def single_file(single_file = nil)
  @single_file = single_file unless single_file.nil?
  defined?(@single_file) ? @single_file : !method_defined?(:on_end)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



144
145
146
# File 'lib/platformos_check/check.rb', line 144

def ==(other)
  other.is_a?(Check) && code_name == other.code_name
end

#add_offense(message, node: nil, app_file: node&.app_file, markup: nil, line_number: nil, node_markup_offset: 0, &block) ⇒ Object



90
91
92
# File 'lib/platformos_check/check.rb', line 90

def add_offense(message, node: nil, app_file: node&.app_file, markup: nil, line_number: nil, node_markup_offset: 0, &block)
  offenses << Offense.new(check: self, message:, app_file:, node:, markup:, line_number:, node_markup_offset:, correction: block)
end

#can_disable?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/platformos_check/check.rb', line 132

def can_disable?
  self.class.can_disable
end

#categoriesObject



108
109
110
# File 'lib/platformos_check/check.rb', line 108

def categories
  self.class.categories
end

#code_nameObject



116
117
118
# File 'lib/platformos_check/check.rb', line 116

def code_name
  StringHelpers.demodulize(self.class.name)
end

#docObject



112
113
114
# File 'lib/platformos_check/check.rb', line 112

def doc
  self.class.doc
end

#ignore!Object



120
121
122
# File 'lib/platformos_check/check.rb', line 120

def ignore!
  @ignored = true
end

#ignored?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/platformos_check/check.rb', line 124

def ignored?
  defined?(@ignored) && @ignored
end

#severityObject



94
95
96
# File 'lib/platformos_check/check.rb', line 94

def severity
  @severity ||= self.class.severity
end

#severity=(severity) ⇒ Object

Raises:

  • (ArgumentError)


98
99
100
101
102
# File 'lib/platformos_check/check.rb', line 98

def severity=(severity)
  raise ArgumentError, "unknown severity. Use: #{SEVERITIES.join(', ')}" unless SEVERITIES.include?(severity)

  @severity = severity
end

#severity_valueObject



104
105
106
# File 'lib/platformos_check/check.rb', line 104

def severity_value
  SEVERITY_VALUES[severity]
end

#single_file?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/platformos_check/check.rb', line 136

def single_file?
  self.class.single_file
end

#to_sObject



149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/platformos_check/check.rb', line 149

def to_s
  s = +"#{code_name}:\n"
  properties = {
    severity:,
    categories:,
    doc:,
    ignored_patterns:
  }.merge(options)
  properties.each_pair do |name, value|
    s << "  #{name}: #{value}\n" if value
  end
  s
end

#whole_platformos_app?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/platformos_check/check.rb', line 140

def whole_platformos_app?
  !single_file?
end