diff --git a/.rubocop.yml b/.rubocop.yml index 7ebac9c6..f4449f43 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -50,11 +50,20 @@ HandleExceptions: IfUnlessModifier: Enabled: false +Layout/AlignHash: + Enabled: false + +Layout/ClosingHeredocIndentation: + Enabled: false + +Layout/EmptyLineAfterGuardClause: + Enabled: false + Layout/ExtraSpacing: Exclude: - '*.gemspec' -Layout/IndentArray: +Layout/IndentFirstArrayElement: Enabled: false Layout/IndentHeredoc: @@ -70,6 +79,13 @@ LineLength: Lint/InterpolationCheck: Enabled: false +Lint/Void: + Exclude: + - 'spec/**/*_spec.rb' + +Naming/UncommunicativeMethodParamName: + Enabled: false + MethodLength: Max: 20 @@ -119,7 +135,10 @@ Style/NumericPredicate: Style/TrailingCommaInArguments: Enabled: false -Style/TrailingCommaInLiteral: +Style/TrailingCommaInArrayLiteral: + Enabled: false + +Style/TrailingCommaInHashLiteral: Enabled: false Style/FrozenStringLiteralComment: diff --git a/Gemfile b/Gemfile index 5be00fcd..0b61542a 100644 --- a/Gemfile +++ b/Gemfile @@ -8,6 +8,6 @@ gem 'rspec', '~> 3.7' gem 'overcommit', '~> 0.41.0' # Pin tool versions (which are executed by Overcommit) for Travis builds -gem 'rubocop', '~> 0.52.1' +gem 'rubocop', '~> 0.68' gem 'coveralls', require: false diff --git a/lib/scss_lint/cli.rb b/lib/scss_lint/cli.rb index 5bd6f06d..2f5a6057 100644 --- a/lib/scss_lint/cli.rb +++ b/lib/scss_lint/cli.rb @@ -31,8 +31,8 @@ def initialize(logger) def run(args) options = SCSSLint::Options.new.parse(args) act_on_options(options) - rescue StandardError => ex - handle_runtime_exception(ex, options) + rescue StandardError => e + handle_runtime_exception(e, options) end private @@ -205,9 +205,9 @@ def load_required_paths(options) Array(options[:required_paths]).each do |path| require path end - rescue LoadError => ex + rescue LoadError => e raise SCSSLint::Exceptions::RequiredLibraryMissingError, - "Required library not found: #{ex.message}" + "Required library not found: #{e.message}" end def load_reporters(options) diff --git a/lib/scss_lint/config.rb b/lib/scss_lint/config.rb index 1cff0595..8da7db9e 100644 --- a/lib/scss_lint/config.rb +++ b/lib/scss_lint/config.rb @@ -64,9 +64,9 @@ def load_options_hash_from_file(file) else {} end - rescue StandardError => ex + rescue StandardError => e raise SCSSLint::Exceptions::InvalidConfiguration, - "Invalid configuration: #{ex.message}" + "Invalid configuration: #{e.message}" end options = convert_single_options_to_arrays(options) @@ -94,8 +94,6 @@ def convert_single_options_to_arrays(options) # Merge options from wildcard linters into individual linter configs def merge_wildcard_linter_options(options) options = options.dup - - # rubocop:disable Performance/HashEachMethods (FALSE POSITIVE) # Cannot use `each_key` because the cycle adds new keys during iteration options.fetch('linters', {}).keys.each do |class_name| next unless class_name.include?('*') @@ -103,7 +101,6 @@ def merge_wildcard_linter_options(options) wildcard_options = options['linters'].delete(class_name) apply_options_to_matching_linters(class_name, options, wildcard_options) end - # rubocop:enable Performance/HashEachMethods options end diff --git a/lib/scss_lint/engine.rb b/lib/scss_lint/engine.rb index c9c8b4cb..d6a780e3 100644 --- a/lib/scss_lint/engine.rb +++ b/lib/scss_lint/engine.rb @@ -34,12 +34,12 @@ def initialize(options = {}) .lines @tree = @engine.to_tree find_any_control_commands - rescue Encoding::UndefinedConversionError, Sass::SyntaxError, ArgumentError => error - if error.is_a?(Encoding::UndefinedConversionError) || - error.message.match(/invalid.*(byte sequence|character)/i) + rescue Encoding::UndefinedConversionError, Sass::SyntaxError, ArgumentError => e + if e.is_a?(Encoding::UndefinedConversionError) || + e.message.match(/invalid.*(byte sequence|character)/i) raise FileEncodingError, - "Unable to parse SCSS file: #{error}", - error.backtrace + "Unable to parse SCSS file: #{e}", + e.backtrace else raise end diff --git a/lib/scss_lint/linter/chained_classes.rb b/lib/scss_lint/linter/chained_classes.rb index 36489015..629bb5f7 100644 --- a/lib/scss_lint/linter/chained_classes.rb +++ b/lib/scss_lint/linter/chained_classes.rb @@ -6,7 +6,7 @@ class Linter::ChainedClasses < Linter def visit_sequence(sequence) line_offset = 0 sequence.members.each do |member| - line_offset += 1 if member =~ /\n/ # rubocop:disable Performance/RegexpMatch + line_offset += 1 if member =~ /\n/ next unless chained_class?(member) add_lint(member.line + line_offset, 'Prefer using a distinct class over chained classes ' \ diff --git a/lib/scss_lint/linter/color_variable.rb b/lib/scss_lint/linter/color_variable.rb index d25c3e62..c0fe8872 100644 --- a/lib/scss_lint/linter/color_variable.rb +++ b/lib/scss_lint/linter/color_variable.rb @@ -18,13 +18,10 @@ def visit_script_color(node) def visit_script_string(node) return if literal_string?(node) - - # rubocop:disable Performance/HashEachMethods (FALSE POSITIVE v0.50.0) remove_quoted_strings(node.value) .scan(/(^|\s)(#[a-f0-9]+|[a-z]+)(?=\s|$)/i) .select { |_, word| color?(word) } .each { |_, color| record_lint(node, color) } - # rubocop:enable Performance/HashEachMethods end def visit_script_funcall(node) diff --git a/lib/scss_lint/linter/disable_linter_reason.rb b/lib/scss_lint/linter/disable_linter_reason.rb index 4b10faf8..94116fbc 100644 --- a/lib/scss_lint/linter/disable_linter_reason.rb +++ b/lib/scss_lint/linter/disable_linter_reason.rb @@ -48,7 +48,7 @@ def visit_command_comment(node) (?disable)\s+ (?.*?) \s*(?:\*/|\n) # Comment end marker or end of line - }x + }x.freeze def comment_lines(node) node.value.join.split("\n") diff --git a/lib/scss_lint/linter/hex_length.rb b/lib/scss_lint/linter/hex_length.rb index efbb66c6..2efadd6b 100644 --- a/lib/scss_lint/linter/hex_length.rb +++ b/lib/scss_lint/linter/hex_length.rb @@ -4,7 +4,7 @@ module SCSSLint class Linter::HexLength < Linter include LinterRegistry - HEX_REGEX = /(#(\h{3}|\h{6}))(?!\h)/ + HEX_REGEX = /(#(\h{3}|\h{6}))(?!\h)/.freeze def visit_script_color(node) return unless hex = source_from_range(node.source_range)[HEX_REGEX, 1] diff --git a/lib/scss_lint/linter/hex_notation.rb b/lib/scss_lint/linter/hex_notation.rb index e0fe1285..0f4dab2e 100644 --- a/lib/scss_lint/linter/hex_notation.rb +++ b/lib/scss_lint/linter/hex_notation.rb @@ -3,7 +3,7 @@ module SCSSLint class Linter::HexNotation < Linter include LinterRegistry - HEX_REGEX = /(#(\h{3}|\h{6}))(?!\h)/ + HEX_REGEX = /(#(\h{3}|\h{6}))(?!\h)/.freeze def visit_script_color(node) return unless hex = source_from_range(node.source_range)[HEX_REGEX, 1] diff --git a/lib/scss_lint/linter/hex_validation.rb b/lib/scss_lint/linter/hex_validation.rb index 8d594b6a..462e4aff 100644 --- a/lib/scss_lint/linter/hex_validation.rb +++ b/lib/scss_lint/linter/hex_validation.rb @@ -13,7 +13,7 @@ def visit_script_string(node) private - HEX_REGEX = /(#(\h{3}|\h{6}|\h{8}))(?!\h)/ + HEX_REGEX = /(#(\h{3}|\h{6}|\h{8}))(?!\h)/.freeze def check_hex(hex, node) return if HEX_REGEX.match?(hex) diff --git a/lib/scss_lint/linter/leading_zero.rb b/lib/scss_lint/linter/leading_zero.rb index 45c9c804..3214e685 100644 --- a/lib/scss_lint/linter/leading_zero.rb +++ b/lib/scss_lint/linter/leading_zero.rb @@ -22,7 +22,7 @@ def visit_script_number(node) private - NUMBER_WITH_LEADING_ZERO_REGEX = /^-?(0?\.\d+)/ + NUMBER_WITH_LEADING_ZERO_REGEX = /^-?(0?\.\d+)/.freeze CONVENTIONS = { 'exclude_zero' => { diff --git a/lib/scss_lint/linter/length_variable.rb b/lib/scss_lint/linter/length_variable.rb index f11023cb..7ba2f984 100644 --- a/lib/scss_lint/linter/length_variable.rb +++ b/lib/scss_lint/linter/length_variable.rb @@ -23,7 +23,7 @@ class Linter::LengthVariable < Linter (?:#{LENGTH_UNITS.join('|')}) # unit! ) (?:$|[\s+\-/*()]) # math or space separated, or end of string - }x + }x.freeze def visit_prop(node) return if allowed_prop?(node) diff --git a/lib/scss_lint/linter/property_units.rb b/lib/scss_lint/linter/property_units.rb index 0e7a389f..cbc3640a 100644 --- a/lib/scss_lint/linter/property_units.rb +++ b/lib/scss_lint/linter/property_units.rb @@ -15,7 +15,7 @@ class Linter::PropertyUnits < Linter ) ([a-z%]+) # [1: units] letters or percent sign, e.g. px or % ) - /ix + /ix.freeze def visit_root(_node) @globally_allowed_units = config['global'].to_set diff --git a/lib/scss_lint/linter/selector_depth.rb b/lib/scss_lint/linter/selector_depth.rb index 887799de..71251373 100644 --- a/lib/scss_lint/linter/selector_depth.rb +++ b/lib/scss_lint/linter/selector_depth.rb @@ -47,7 +47,7 @@ def sequence_depth(sequence, current_depth) # combinator, as these "combine" simple sequences such that they do not # increase depth. depth = simple_sequences.size - - separators.count { |item| item == '~' || item == '+' } + separators.count { |item| %w[~ +].include?(item) } depth += if parent_selectors > 0 diff --git a/lib/scss_lint/linter/shorthand.rb b/lib/scss_lint/linter/shorthand.rb index 75d691a7..fe34b611 100644 --- a/lib/scss_lint/linter/shorthand.rb +++ b/lib/scss_lint/linter/shorthand.rb @@ -64,7 +64,7 @@ def check_script_literal(prop, literal) (\S+\s+\S+(\s+\S+){0,2}) # Two to four values separated by spaces (\s+!\w+)? # Ignore `!important` priority overrides \z - /x + /x.freeze # @param prop [String] # @param script_string [Sass::Script::Value::String] diff --git a/lib/scss_lint/linter/space_between_parens.rb b/lib/scss_lint/linter/space_between_parens.rb index 47209fa9..ef0cfc4e 100644 --- a/lib/scss_lint/linter/space_between_parens.rb +++ b/lib/scss_lint/linter/space_between_parens.rb @@ -29,7 +29,7 @@ def feel_for_parens_and_check_node(node) private - TRAILING_WHITESPACE = /\s*$/ + TRAILING_WHITESPACE = /\s*$/.freeze def check(node, source) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength @spaces = config['spaces'] diff --git a/lib/scss_lint/linter/string_quotes.rb b/lib/scss_lint/linter/string_quotes.rb index 20cdf5c6..ba74418e 100644 --- a/lib/scss_lint/linter/string_quotes.rb +++ b/lib/scss_lint/linter/string_quotes.rb @@ -45,7 +45,7 @@ def check_quotes(node, source) \s*\)?\s*;?\s* # Sometimes the Sass parser includes a trailing ) or ; (//.*)? # Exclude any trailing comments that might have snuck in \z - }x + }x.freeze def extract_string_without_quotes(source) return unless match = STRING_WITHOUT_QUOTES_REGEX.match(source) diff --git a/lib/scss_lint/linter/trailing_zero.rb b/lib/scss_lint/linter/trailing_zero.rb index 82fa4aca..9a793a08 100644 --- a/lib/scss_lint/linter/trailing_zero.rb +++ b/lib/scss_lint/linter/trailing_zero.rb @@ -22,7 +22,7 @@ def visit_script_number(node) private - FRACTIONAL_DIGIT_REGEX = /^-?(\d*\.\d+)/ + FRACTIONAL_DIGIT_REGEX = /^-?(\d*\.\d+)/.freeze def check_for_trailing_zeros(node, original_number) return unless match = /^(\d*\.(?:[0-9]*[1-9]|[1-9])*)0+$/.match(original_number) diff --git a/lib/scss_lint/linter/unnecessary_mantissa.rb b/lib/scss_lint/linter/unnecessary_mantissa.rb index b9b08c1e..e0ac8f74 100644 --- a/lib/scss_lint/linter/unnecessary_mantissa.rb +++ b/lib/scss_lint/linter/unnecessary_mantissa.rb @@ -35,7 +35,7 @@ def visit_script_number(node) (?\d+) (?\w*) )\b - /ix + /ix.freeze MESSAGE_FORMAT = '`%s` should be written without the mantissa as `%s%s`'.freeze diff --git a/lib/scss_lint/linter/url_format.rb b/lib/scss_lint/linter/url_format.rb index dc1b1ab0..f7f7989d 100644 --- a/lib/scss_lint/linter/url_format.rb +++ b/lib/scss_lint/linter/url_format.rb @@ -49,8 +49,8 @@ def check_url(url, node) if uri.scheme || uri.host add_lint(node, "URL `#{url}` should not contain protocol or domain") end - rescue URI::Error => ex - add_lint(node, "Invalid URL `#{url}`: #{ex}") + rescue URI::Error => e + add_lint(node, "Invalid URL `#{url}`: #{e}") end end end diff --git a/lib/scss_lint/linter/zero_unit.rb b/lib/scss_lint/linter/zero_unit.rb index 51498211..c702077f 100644 --- a/lib/scss_lint/linter/zero_unit.rb +++ b/lib/scss_lint/linter/zero_unit.rb @@ -35,7 +35,7 @@ def visit_script_funcall(node) (? ex + rescue OptionParser::InvalidOption => e raise SCSSLint::Exceptions::InvalidCLIOption, - ex.message, - ex.backtrace + e.message, + e.backtrace end private diff --git a/lib/scss_lint/rake_task.rb b/lib/scss_lint/rake_task.rb index 782cd2c5..61b7ba5f 100644 --- a/lib/scss_lint/rake_task.rb +++ b/lib/scss_lint/rake_task.rb @@ -73,7 +73,7 @@ def define end end - def run_cli(task_args) # rubocop:disable AbcSize + def run_cli(task_args) cli_args = ['--config', config] if config logger = quiet ? SCSSLint::Logger.silent : SCSSLint::Logger.new(STDOUT) diff --git a/lib/scss_lint/runner.rb b/lib/scss_lint/runner.rb index 0a9cd5c2..c2fd9b06 100644 --- a/lib/scss_lint/runner.rb +++ b/lib/scss_lint/runner.rb @@ -33,18 +33,18 @@ def find_lints(file) # rubocop:disable AbcSize @linters.each do |linter| begin run_linter(linter, engine, file[:path]) - rescue StandardError => error + rescue StandardError => e raise SCSSLint::Exceptions::LinterError, "#{linter.class} raised unexpected error linting file #{file[:path]}: " \ - "'#{error.message}'", - error.backtrace + "'#{e.message}'", + e.backtrace end end - rescue Sass::SyntaxError => ex - @lints << Lint.new(Linter::Syntax.new, ex.sass_filename, Location.new(ex.sass_line), - "Syntax Error: #{ex}", :error) - rescue FileEncodingError => ex - @lints << Lint.new(Linter::Encoding.new, file[:path], Location.new, ex.to_s, :error) + rescue Sass::SyntaxError => e + @lints << Lint.new(Linter::Syntax.new, e.sass_filename, Location.new(e.sass_line), + "Syntax Error: #{e}", :error) + rescue FileEncodingError => e + @lints << Lint.new(Linter::Encoding.new, file[:path], Location.new, e.to_s, :error) end # For stubbing in tests. diff --git a/lib/scss_lint/utils.rb b/lib/scss_lint/utils.rb index 60878d8a..53d5510a 100644 --- a/lib/scss_lint/utils.rb +++ b/lib/scss_lint/utils.rb @@ -1,7 +1,7 @@ module SCSSLint # Collection of helpers used across a variety of linters. module Utils - COLOR_REGEX = /^#[a-f0-9]{3,6}$/i + COLOR_REGEX = /^#[a-f0-9]{3,6}$/i.freeze # Returns whether the given string is a color literal (keyword or hex code). # diff --git a/scss_lint.gemspec b/scss_lint.gemspec index edf8c2a3..f7a55520 100644 --- a/scss_lint.gemspec +++ b/scss_lint.gemspec @@ -1,4 +1,4 @@ -$LOAD_PATH << File.expand_path('../lib', __FILE__) +$LOAD_PATH << File.expand_path('lib', __dir__) require 'scss_lint/constants' require 'scss_lint/version' diff --git a/spec/scss_lint/plugins/linter_dir_spec.rb b/spec/scss_lint/plugins/linter_dir_spec.rb index 8a7836d8..02b6b3e4 100644 --- a/spec/scss_lint/plugins/linter_dir_spec.rb +++ b/spec/scss_lint/plugins/linter_dir_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe SCSSLint::Plugins::LinterDir do - let(:plugin_directory) { File.expand_path('../../fixtures/plugins', __FILE__) } + let(:plugin_directory) { File.expand_path('../fixtures/plugins', __dir__) } let(:subject) { described_class.new(plugin_directory) } describe '#load' do