Class: SCSSLint::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/scss_lint/config.rb

Overview

Loads and manages application configuration.

Constant Summary collapse

FILE_NAME =
'.scss-lint.yml'.freeze
DEFAULT_FILE =
File.join(SCSS_LINT_HOME, 'config', 'default.yml')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, file = Config.user_file) ⇒ Config

Returns a new instance of Config.



204
205
206
207
208
209
210
# File 'lib/scss_lint/config.rb', line 204

def initialize(options, file = Config.user_file)
  @options = options
  @warnings = []
  @file = file

  validate_linters
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



11
12
13
# File 'lib/scss_lint/config.rb', line 11

def file
  @file
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/scss_lint/config.rb', line 11

def options
  @options
end

#warningsObject (readonly)

Returns the value of attribute warnings.



11
12
13
# File 'lib/scss_lint/config.rb', line 11

def warnings
  @warnings
end

Class Method Details

.defaultObject



14
15
16
# File 'lib/scss_lint/config.rb', line 14

def default
  load(DEFAULT_FILE, merge_with_default: false)
end

.linter_name(linter) ⇒ Object



45
46
47
# File 'lib/scss_lint/config.rb', line 45

def linter_name(linter)
  (linter.is_a?(Class) ? linter : linter.class).simple_name
end

.load(file, options = {}) ⇒ Object

Loads a configuration from a file, merging it with the default configuration.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/scss_lint/config.rb', line 20

def load(file, options = {})
  config_options = load_options_hash_from_file(file)

  config = new(config_options, file)

  # Need to call this before merging with the default configuration so
  # that plugins can override the default configuration while still being
  # overridden by the repo's configuration.
  config.load_plugins

  if options.fetch(:merge_with_default, true)
    config = default.extend(config)
  end

  config
end

.user_fileObject

Returns the location of the user-wide scss-lint configuration.

This needs to be a method instead of a constant so that we can change the user’s home directory in tests.



41
42
43
# File 'lib/scss_lint/config.rb', line 41

def user_file
  File.join(Dir.home, FILE_NAME)
end

Instance Method Details

#==(other) ⇒ true, false

Compares this configuration with another.

Parameters:

Returns:

  • (true, false)


220
221
222
# File 'lib/scss_lint/config.rb', line 220

def ==(other)
  super || @options == other.options
end

#[](key) ⇒ Object



212
213
214
# File 'lib/scss_lint/config.rb', line 212

def [](key)
  @options[key]
end

#disable_all_lintersObject



266
267
268
269
270
# File 'lib/scss_lint/config.rb', line 266

def disable_all_linters
  @options['linters'].each_value do |linter_config|
    linter_config['enabled'] = false
  end
end

#disable_linter(linter) ⇒ Object



262
263
264
# File 'lib/scss_lint/config.rb', line 262

def disable_linter(linter)
  linter_options(linter)['enabled'] = false
end

#enable_linter(linter) ⇒ Object



258
259
260
# File 'lib/scss_lint/config.rb', line 258

def enable_linter(linter)
  linter_options(linter)['enabled'] = true
end

#enabled_lintersObject



248
249
250
251
252
# File 'lib/scss_lint/config.rb', line 248

def enabled_linters
  LinterRegistry.extract_linters_from(@options['linters'].keys).select do |linter|
    linter_options(linter)['enabled']
  end
end

#exclude_file(file_path) ⇒ Object



298
299
300
301
302
303
# File 'lib/scss_lint/config.rb', line 298

def exclude_file(file_path)
  abs_path = File.expand_path(file_path)

  @options['exclude'] ||= []
  @options['exclude'] << abs_path
end

#exclude_patternsObject



286
287
288
# File 'lib/scss_lint/config.rb', line 286

def exclude_patterns
  @options.fetch('exclude', [])
end

#excluded_file?(file_path) ⇒ Boolean

Returns:

  • (Boolean)


278
279
280
281
282
283
284
# File 'lib/scss_lint/config.rb', line 278

def excluded_file?(file_path)
  abs_path = File.expand_path(file_path)

  @options.fetch('exclude', []).any? do |exclusion_glob|
    File.fnmatch(exclusion_glob, abs_path)
  end
end

#excluded_file_for_linter?(file_path, linter) ⇒ Boolean

Returns:

  • (Boolean)


290
291
292
293
294
295
296
# File 'lib/scss_lint/config.rb', line 290

def excluded_file_for_linter?(file_path, linter)
  abs_path = File.expand_path(file_path)

  linter_options(linter).fetch('exclude', []).any? do |exclusion_glob|
    File.fnmatch(exclusion_glob, abs_path)
  end
end

#extend(config) ⇒ SCSSLint::Config

Extend this SCSSLint::Config with another configuration.

Returns:



227
228
229
230
231
# File 'lib/scss_lint/config.rb', line 227

def extend(config)
  @options = self.class.send(:smart_merge, @options, config.options)
  @warnings += config.warnings
  self
end

#linter_enabled?(linter) ⇒ Boolean

Returns:

  • (Boolean)


254
255
256
# File 'lib/scss_lint/config.rb', line 254

def linter_enabled?(linter)
  (linter_options(linter) || {}).fetch('enabled', false)
end

#linter_options(linter) ⇒ Object



272
273
274
275
276
# File 'lib/scss_lint/config.rb', line 272

def linter_options(linter)
  options = @options['linters'].fetch(self.class.linter_name(linter), {})
  options['severity'] ||= @options['severity']
  options
end

#load_pluginsObject



233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/scss_lint/config.rb', line 233

def load_plugins
  previous_linters = LinterRegistry.linters
  plugins = SCSSLint::Plugins.new(self).load
  new_linters = LinterRegistry.linters - previous_linters

  plugins.each do |plugin|
    # Have the plugin options be overrideable by the local configuration
    @options = self.class.send(:smart_merge, plugin.config.options, @options)
  end

  # We only want to set defaults for linters introduced via plugins,
  # otherwise we'll accidentally enable some linters
  ensure_linters_have_default_options(new_linters)
end

#scss_filesObject

Returns Array.

Returns:

  • Array



306
307
308
309
310
311
312
# File 'lib/scss_lint/config.rb', line 306

def scss_files
  if (path = @options['scss_files']) && Array(path).any?
    Array(path).map { |p| Dir[p] }.flatten.uniq
  else
    []
  end
end