Class: SCSSLint::Config
- Inherits:
-
Object
- Object
- SCSSLint::Config
- 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
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#warnings ⇒ Object
readonly
Returns the value of attribute warnings.
Class Method Summary collapse
- .default ⇒ Object
- .linter_name(linter) ⇒ Object
-
.load(file, options = {}) ⇒ Object
Loads a configuration from a file, merging it with the default configuration.
-
.user_file ⇒ Object
Returns the location of the user-wide scss-lint configuration.
Instance Method Summary collapse
-
#==(other) ⇒ true, false
Compares this configuration with another.
- #[](key) ⇒ Object
- #disable_all_linters ⇒ Object
- #disable_linter(linter) ⇒ Object
- #enable_linter(linter) ⇒ Object
- #enabled_linters ⇒ Object
- #exclude_file(file_path) ⇒ Object
- #exclude_patterns ⇒ Object
- #excluded_file?(file_path) ⇒ Boolean
- #excluded_file_for_linter?(file_path, linter) ⇒ Boolean
-
#extend(config) ⇒ SCSSLint::Config
Extend this Config with another configuration.
-
#initialize(options, file = Config.user_file) ⇒ Config
constructor
A new instance of Config.
- #linter_enabled?(linter) ⇒ Boolean
- #linter_options(linter) ⇒ Object
- #load_plugins ⇒ Object
-
#scss_files ⇒ Object
Array.
Constructor Details
Instance Attribute Details
#file ⇒ Object (readonly)
Returns the value of attribute file.
11 12 13 |
# File 'lib/scss_lint/config.rb', line 11 def file @file end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
11 12 13 |
# File 'lib/scss_lint/config.rb', line 11 def @options end |
#warnings ⇒ Object (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
.default ⇒ Object
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, = {}) = (file) config = new(, 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 .fetch(:merge_with_default, true) config = default.extend(config) end config end |
.user_file ⇒ Object
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.
220 221 222 |
# File 'lib/scss_lint/config.rb', line 220 def ==(other) super || @options == other. end |
#[](key) ⇒ Object
212 213 214 |
# File 'lib/scss_lint/config.rb', line 212 def [](key) @options[key] end |
#disable_all_linters ⇒ Object
266 267 268 269 270 |
# File 'lib/scss_lint/config.rb', line 266 def disable_all_linters @options['linters'].values.each 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)['enabled'] = false end |
#enable_linter(linter) ⇒ Object
258 259 260 |
# File 'lib/scss_lint/config.rb', line 258 def enable_linter(linter) (linter)['enabled'] = true end |
#enabled_linters ⇒ Object
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)['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.(file_path) @options['exclude'] ||= [] @options['exclude'] << abs_path end |
#exclude_patterns ⇒ Object
286 287 288 |
# File 'lib/scss_lint/config.rb', line 286 def exclude_patterns @options.fetch('exclude', []) end |
#excluded_file?(file_path) ⇒ Boolean
278 279 280 281 282 283 284 |
# File 'lib/scss_lint/config.rb', line 278 def excluded_file?(file_path) abs_path = File.(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
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.(file_path) (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.
227 228 229 230 231 |
# File 'lib/scss_lint/config.rb', line 227 def extend(config) @options = self.class.send(:smart_merge, @options, config.) @warnings += config.warnings self end |
#linter_enabled?(linter) ⇒ Boolean
254 255 256 |
# File 'lib/scss_lint/config.rb', line 254 def linter_enabled?(linter) ((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['linters'].fetch(self.class.linter_name(linter), {}) ['severity'] ||= @options['severity'] end |
#load_plugins ⇒ Object
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) end # We only want to set defaults for linters introduced via plugins, # otherwise we'll accidentally enable some linters (new_linters) end |
#scss_files ⇒ Object
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 |