Skip to content

Commit 0d9ebf6

Browse files
committed
Upgrade Rubocop to 0.27.1 for Travis builds
Change-Id: Ie6d5a39667069b49c11100ce6e75f78a64eb522a Reviewed-on: http://gerrit.causes.com/44444 Tested-by: jenkins <[email protected]> Reviewed-by: Shane da Silva <[email protected]>
1 parent ce34a0c commit 0d9ebf6

12 files changed

+113
-54
lines changed

.rubocop.yml

+6
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ LineLength:
5555
MethodLength:
5656
Max: 20
5757

58+
Metrics/AbcSize:
59+
Max: 20
60+
5861
PerceivedComplexity:
5962
Max: 8
6063

@@ -84,6 +87,9 @@ SignalException:
8487
SingleLineBlockParams:
8588
Enabled: false
8689

90+
Style/MultilineOperationIndentation:
91+
Enabled: false
92+
8793
TrailingComma:
8894
Enabled: false
8995

lib/scss_lint/cli.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def parse_arguments
5454
end
5555

5656
# @return [OptionParser]
57-
def options_parser # rubocop:disable MethodLength
57+
def options_parser # rubocop:disable AbcSize, MethodLength
5858
@options_parser ||= OptionParser.new do |opts|
5959
opts.banner = "Usage: #{opts.program_name} [options] [scss-files]"
6060

@@ -110,7 +110,7 @@ def options_parser # rubocop:disable MethodLength
110110
end
111111
end
112112

113-
def run # rubocop:disable MethodLength
113+
def run # rubocop:disable AbcSize, MethodLength
114114
runner = Runner.new(@config)
115115
runner.run(files_to_lint)
116116
report_lints(runner.lints)

lib/scss_lint/engine.rb

+21-5
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ class Engine
1010

1111
attr_reader :contents, :filename, :lines, :tree
1212

13+
# Creates a parsed representation of an SCSS document from the given string
14+
# or file.
15+
#
16+
# @param scss_or_filename [String]
1317
def initialize(scss_or_filename)
1418
if File.exist?(scss_or_filename)
15-
@filename = scss_or_filename
16-
@engine = Sass::Engine.for_file(scss_or_filename, ENGINE_OPTIONS)
17-
@contents = File.open(scss_or_filename, 'r').read
19+
build_from_file(scss_or_filename)
1820
else
19-
@engine = Sass::Engine.new(scss_or_filename, ENGINE_OPTIONS)
20-
@contents = scss_or_filename
21+
build_from_string(scss_or_filename)
2122
end
2223

2324
# Need to force encoding to avoid Windows-related bugs.
@@ -34,5 +35,20 @@ def initialize(scss_or_filename)
3435
raise
3536
end
3637
end
38+
39+
private
40+
41+
# @param path [String]
42+
def build_from_file(path)
43+
@filename = path
44+
@engine = Sass::Engine.for_file(path, ENGINE_OPTIONS)
45+
@contents = File.open(path, 'r').read
46+
end
47+
48+
# @param scss [String]
49+
def build_from_string(scss)
50+
@engine = Sass::Engine.new(scss, ENGINE_OPTIONS)
51+
@contents = scss
52+
end
3753
end
3854
end

lib/scss_lint/exceptions.rb

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
module SCSSLint::Exceptions
2+
# Raised when an invalid flag is given via the command line.
3+
class InvalidCLIOption < StandardError; end
4+
25
# Raised when an unexpected error occurs in a linter
36
class LinterError < StandardError; end
47
end

lib/scss_lint/linter.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def add_lint(node_or_line_or_location, message)
4444
#
4545
# @param range [Sass::Source::Range]
4646
# @return [SCSSLint::Location]
47-
def location_from_range(range)
47+
def location_from_range(range) # rubocop:disable Metrics/AbcSize
4848
length = if range.start_pos.line == range.end_pos.line
4949
range.end_pos.offset - range.start_pos.offset
5050
else
@@ -69,7 +69,7 @@ def character_at(source_position, offset = 0)
6969
#
7070
# @param source_range [Sass::Source::Range]
7171
# @return [String] the original source code
72-
def source_from_range(source_range)
72+
def source_from_range(source_range) # rubocop:disable Metrics/AbcSize
7373
current_line = source_range.start_pos.line - 1
7474
last_line = source_range.end_pos.line - 1
7575
start_pos = source_range.start_pos.offset - 1

lib/scss_lint/linter/single_line_per_property.rb

+13-7
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,7 @@ def visit_rule(node) # rubocop:disable CyclomaticComplexity
1818
'on separate line from selector')
1919
end
2020

21-
# Compare each property against the next property to see if they are on
22-
# the same line
23-
properties[0..-2].zip(properties[1..-1]).each do |first, second|
24-
next unless first.line == second.line
25-
26-
add_lint(second, "Property '#{second.name.join}' should be placed on own line")
27-
end
21+
check_adjacent_properties(properties)
2822
end
2923

3024
private
@@ -49,5 +43,17 @@ def single_line_rule_set?(rule)
4943
def first_property_not_on_own_line?(rule, properties)
5044
properties.any? && properties.first.line == rule.line
5145
end
46+
47+
# Compare each property against the next property to see if they are on
48+
# the same line.
49+
#
50+
# @param properties [Array<Sass::Tree::PropNode>]
51+
def check_adjacent_properties(properties)
52+
properties[0..-2].zip(properties[1..-1]).each do |first, second|
53+
next unless first.line == second.line
54+
55+
add_lint(second, "Property '#{second.name.join}' should be placed on own line")
56+
end
57+
end
5258
end
5359
end

lib/scss_lint/linter/single_line_per_selector.rb

+19-11
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,27 @@ class Linter::SingleLinePerSelector < Linter
88
def visit_comma_sequence(node)
99
return unless node.members.count > 1
1010

11-
if node.members[0].members[1] == "\n"
12-
# Comma is on its own line
13-
add_lint(node, MESSAGE)
14-
end
11+
check_comma_on_own_line(node)
1512

1613
node.members[1..-1].each_with_index do |sequence, index|
17-
if sequence.members[0] != "\n"
18-
# Next sequence doesn't reside on its own line
19-
add_lint(node.line + index, MESSAGE)
20-
elsif sequence.members[1] == "\n"
21-
# Comma is on its own line
22-
add_lint(node.line + index, MESSAGE)
23-
end
14+
check_sequence_commas(node, sequence, index)
15+
end
16+
end
17+
18+
private
19+
20+
def check_comma_on_own_line(node)
21+
return unless node.members[0].members[1] == "\n"
22+
add_lint(node, MESSAGE)
23+
end
24+
25+
def check_sequence_commas(node, sequence, index)
26+
if sequence.members[0] != "\n"
27+
# Next sequence doesn't reside on its own line
28+
add_lint(node.line + index, MESSAGE)
29+
elsif sequence.members[1] == "\n"
30+
# Comma is on its own line
31+
add_lint(node.line + index, MESSAGE)
2432
end
2533
end
2634
end

lib/scss_lint/reporter/default_reporter.rb

+15-7
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,23 @@ def report_lints
55
return unless lints.any?
66

77
lints.map do |lint|
8-
type = lint.error? ? '[E]'.color(:red) : '[W]'.color(:yellow)
8+
"#{location(lint)} #{type(lint)} #{message(lint)}"
9+
end.join("\n") + "\n"
10+
end
911

10-
linter_name = "#{lint.linter.name}: ".color(:green) if lint.linter
11-
message = "#{linter_name}#{lint.description}"
12+
private
1213

13-
"#{lint.filename.color(:cyan)}:" <<
14-
"#{lint.location.line}".color(:magenta) <<
15-
" #{type} #{message}"
16-
end.join("\n") + "\n"
14+
def location(lint)
15+
"#{lint.filename.color(:cyan)}:#{lint.location.line.to_s.color(:magenta)}"
16+
end
17+
18+
def type(lint)
19+
lint.error? ? '[E]'.color(:red) : '[W]'.color(:yellow)
20+
end
21+
22+
def message(lint)
23+
linter_name = "#{lint.linter.name}: ".color(:green) if lint.linter
24+
"#{linter_name}#{lint.description}"
1725
end
1826
end
1927
end

lib/scss_lint/reporter/json_reporter.rb

+15-8
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,24 @@ def report_lints
77
output = {}
88
lints.group_by(&:filename).each do |filename, file_lints|
99
output[filename] = file_lints.map do |lint|
10-
issue = {}
11-
issue['linter'] = lint.linter.name if lint.linter
12-
issue['line'] = lint.location.line
13-
issue['column'] = lint.location.column
14-
issue['length'] = lint.location.length
15-
issue['severity'] = lint.severity
16-
issue['reason'] = lint.description
17-
issue
10+
issue_hash(lint)
1811
end
1912
end
2013
JSON.pretty_generate(output)
2114
end
15+
16+
private
17+
18+
def issue_hash(lint)
19+
{
20+
'line' => lint.location.line,
21+
'column' => lint.location.column,
22+
'length' => lint.location.length,
23+
'severity' => lint.severity,
24+
'reason' => lint.description,
25+
}.tap do |hash|
26+
hash['linter'] = lint.linter.name if lint.linter
27+
end
28+
end
2229
end
2330
end

lib/scss_lint/reporter/xml_reporter.rb

+12-6
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@ def report_lints
99
output << "<file name=#{filename.encode(xml: :attr)}>"
1010

1111
file_lints.each do |lint|
12-
output << "<issue linter=\"#{lint.linter.name if lint.linter}\" " \
13-
"line=\"#{lint.location.line}\" " \
14-
"column=\"#{lint.location.column}\" " \
15-
"length=\"#{lint.location.length}\" " \
16-
"severity=\"#{lint.severity}\" " \
17-
"reason=#{lint.description.encode(xml: :attr)} />"
12+
output << issue_tag(lint)
1813
end
1914

2015
output << '</file>'
@@ -23,5 +18,16 @@ def report_lints
2318

2419
output
2520
end
21+
22+
private
23+
24+
def issue_tag(lint)
25+
"<issue linter=\"#{lint.linter.name if lint.linter}\" " \
26+
"line=\"#{lint.location.line}\" " \
27+
"column=\"#{lint.location.column}\" " \
28+
"length=\"#{lint.location.length}\" " \
29+
"severity=\"#{lint.severity}\" " \
30+
"reason=#{lint.description.encode(xml: :attr)} />"
31+
end
2632
end
2733
end

lib/scss_lint/runner.rb

+4-5
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,8 @@ def find_lints(file)
3535
config ||= @config
3636

3737
@linters.each do |linter|
38-
next unless config.linter_enabled?(linter)
39-
next if config.excluded_file_for_linter?(file, linter)
40-
4138
begin
42-
run_linter(linter, engine, config)
39+
run_linter(linter, engine, config, file)
4340
rescue => error
4441
raise SCSSLint::Exceptions::LinterError,
4542
"#{linter.class} raised unexpected error linting file #{file}: " \
@@ -55,7 +52,9 @@ def find_lints(file)
5552
end
5653

5754
# For stubbing in tests.
58-
def run_linter(linter, engine, config)
55+
def run_linter(linter, engine, config, file)
56+
return unless config.linter_enabled?(linter)
57+
return if config.excluded_file_for_linter?(file, linter)
5958
linter.run(engine, config.linter_options(linter))
6059
end
6160
end

scss-lint.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ Gem::Specification.new do |s|
2929

3030
s.add_development_dependency 'nokogiri', '~> 1.6.0'
3131
s.add_development_dependency 'rspec', '~> 3.0'
32-
s.add_development_dependency 'rubocop', '0.26.0'
32+
s.add_development_dependency 'rubocop', '0.27.1'
3333
end

0 commit comments

Comments
 (0)