Skip to content

Commit

Permalink
Upgrade Rubocop to 0.27.1 for Travis builds
Browse files Browse the repository at this point in the history
Change-Id: Ie6d5a39667069b49c11100ce6e75f78a64eb522a
Reviewed-on: http://gerrit.causes.com/44444
Tested-by: jenkins <[email protected]>
Reviewed-by: Shane da Silva <[email protected]>
  • Loading branch information
sds committed Nov 13, 2014
1 parent ce34a0c commit 0d9ebf6
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 54 deletions.
6 changes: 6 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ LineLength:
MethodLength:
Max: 20

Metrics/AbcSize:
Max: 20

PerceivedComplexity:
Max: 8

Expand Down Expand Up @@ -84,6 +87,9 @@ SignalException:
SingleLineBlockParams:
Enabled: false

Style/MultilineOperationIndentation:
Enabled: false

TrailingComma:
Enabled: false

Expand Down
4 changes: 2 additions & 2 deletions lib/scss_lint/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def parse_arguments
end

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

Expand Down Expand Up @@ -110,7 +110,7 @@ def options_parser # rubocop:disable MethodLength
end
end

def run # rubocop:disable MethodLength
def run # rubocop:disable AbcSize, MethodLength
runner = Runner.new(@config)
runner.run(files_to_lint)
report_lints(runner.lints)
Expand Down
26 changes: 21 additions & 5 deletions lib/scss_lint/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ class Engine

attr_reader :contents, :filename, :lines, :tree

# Creates a parsed representation of an SCSS document from the given string
# or file.
#
# @param scss_or_filename [String]
def initialize(scss_or_filename)
if File.exist?(scss_or_filename)
@filename = scss_or_filename
@engine = Sass::Engine.for_file(scss_or_filename, ENGINE_OPTIONS)
@contents = File.open(scss_or_filename, 'r').read
build_from_file(scss_or_filename)
else
@engine = Sass::Engine.new(scss_or_filename, ENGINE_OPTIONS)
@contents = scss_or_filename
build_from_string(scss_or_filename)
end

# Need to force encoding to avoid Windows-related bugs.
Expand All @@ -34,5 +35,20 @@ def initialize(scss_or_filename)
raise
end
end

private

# @param path [String]
def build_from_file(path)
@filename = path
@engine = Sass::Engine.for_file(path, ENGINE_OPTIONS)
@contents = File.open(path, 'r').read
end

# @param scss [String]
def build_from_string(scss)
@engine = Sass::Engine.new(scss, ENGINE_OPTIONS)
@contents = scss
end
end
end
3 changes: 3 additions & 0 deletions lib/scss_lint/exceptions.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module SCSSLint::Exceptions
# Raised when an invalid flag is given via the command line.
class InvalidCLIOption < StandardError; end

# Raised when an unexpected error occurs in a linter
class LinterError < StandardError; end
end
4 changes: 2 additions & 2 deletions lib/scss_lint/linter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def add_lint(node_or_line_or_location, message)
#
# @param range [Sass::Source::Range]
# @return [SCSSLint::Location]
def location_from_range(range)
def location_from_range(range) # rubocop:disable Metrics/AbcSize
length = if range.start_pos.line == range.end_pos.line
range.end_pos.offset - range.start_pos.offset
else
Expand All @@ -69,7 +69,7 @@ def character_at(source_position, offset = 0)
#
# @param source_range [Sass::Source::Range]
# @return [String] the original source code
def source_from_range(source_range)
def source_from_range(source_range) # rubocop:disable Metrics/AbcSize
current_line = source_range.start_pos.line - 1
last_line = source_range.end_pos.line - 1
start_pos = source_range.start_pos.offset - 1
Expand Down
20 changes: 13 additions & 7 deletions lib/scss_lint/linter/single_line_per_property.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@ def visit_rule(node) # rubocop:disable CyclomaticComplexity
'on separate line from selector')
end

# Compare each property against the next property to see if they are on
# the same line
properties[0..-2].zip(properties[1..-1]).each do |first, second|
next unless first.line == second.line

add_lint(second, "Property '#{second.name.join}' should be placed on own line")
end
check_adjacent_properties(properties)
end

private
Expand All @@ -49,5 +43,17 @@ def single_line_rule_set?(rule)
def first_property_not_on_own_line?(rule, properties)
properties.any? && properties.first.line == rule.line
end

# Compare each property against the next property to see if they are on
# the same line.
#
# @param properties [Array<Sass::Tree::PropNode>]
def check_adjacent_properties(properties)
properties[0..-2].zip(properties[1..-1]).each do |first, second|
next unless first.line == second.line

add_lint(second, "Property '#{second.name.join}' should be placed on own line")
end
end
end
end
30 changes: 19 additions & 11 deletions lib/scss_lint/linter/single_line_per_selector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,27 @@ class Linter::SingleLinePerSelector < Linter
def visit_comma_sequence(node)
return unless node.members.count > 1

if node.members[0].members[1] == "\n"
# Comma is on its own line
add_lint(node, MESSAGE)
end
check_comma_on_own_line(node)

node.members[1..-1].each_with_index do |sequence, index|
if sequence.members[0] != "\n"
# Next sequence doesn't reside on its own line
add_lint(node.line + index, MESSAGE)
elsif sequence.members[1] == "\n"
# Comma is on its own line
add_lint(node.line + index, MESSAGE)
end
check_sequence_commas(node, sequence, index)
end
end

private

def check_comma_on_own_line(node)
return unless node.members[0].members[1] == "\n"
add_lint(node, MESSAGE)
end

def check_sequence_commas(node, sequence, index)
if sequence.members[0] != "\n"
# Next sequence doesn't reside on its own line
add_lint(node.line + index, MESSAGE)
elsif sequence.members[1] == "\n"
# Comma is on its own line
add_lint(node.line + index, MESSAGE)
end
end
end
Expand Down
22 changes: 15 additions & 7 deletions lib/scss_lint/reporter/default_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@ def report_lints
return unless lints.any?

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

linter_name = "#{lint.linter.name}: ".color(:green) if lint.linter
message = "#{linter_name}#{lint.description}"
private

"#{lint.filename.color(:cyan)}:" <<
"#{lint.location.line}".color(:magenta) <<
" #{type} #{message}"
end.join("\n") + "\n"
def location(lint)
"#{lint.filename.color(:cyan)}:#{lint.location.line.to_s.color(:magenta)}"
end

def type(lint)
lint.error? ? '[E]'.color(:red) : '[W]'.color(:yellow)
end

def message(lint)
linter_name = "#{lint.linter.name}: ".color(:green) if lint.linter
"#{linter_name}#{lint.description}"
end
end
end
23 changes: 15 additions & 8 deletions lib/scss_lint/reporter/json_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,24 @@ def report_lints
output = {}
lints.group_by(&:filename).each do |filename, file_lints|
output[filename] = file_lints.map do |lint|
issue = {}
issue['linter'] = lint.linter.name if lint.linter
issue['line'] = lint.location.line
issue['column'] = lint.location.column
issue['length'] = lint.location.length
issue['severity'] = lint.severity
issue['reason'] = lint.description
issue
issue_hash(lint)
end
end
JSON.pretty_generate(output)
end

private

def issue_hash(lint)
{
'line' => lint.location.line,
'column' => lint.location.column,
'length' => lint.location.length,
'severity' => lint.severity,
'reason' => lint.description,
}.tap do |hash|
hash['linter'] = lint.linter.name if lint.linter
end
end
end
end
18 changes: 12 additions & 6 deletions lib/scss_lint/reporter/xml_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ def report_lints
output << "<file name=#{filename.encode(xml: :attr)}>"

file_lints.each do |lint|
output << "<issue linter=\"#{lint.linter.name if lint.linter}\" " \
"line=\"#{lint.location.line}\" " \
"column=\"#{lint.location.column}\" " \
"length=\"#{lint.location.length}\" " \
"severity=\"#{lint.severity}\" " \
"reason=#{lint.description.encode(xml: :attr)} />"
output << issue_tag(lint)
end

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

output
end

private

def issue_tag(lint)
"<issue linter=\"#{lint.linter.name if lint.linter}\" " \
"line=\"#{lint.location.line}\" " \
"column=\"#{lint.location.column}\" " \
"length=\"#{lint.location.length}\" " \
"severity=\"#{lint.severity}\" " \
"reason=#{lint.description.encode(xml: :attr)} />"
end
end
end
9 changes: 4 additions & 5 deletions lib/scss_lint/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,8 @@ def find_lints(file)
config ||= @config

@linters.each do |linter|
next unless config.linter_enabled?(linter)
next if config.excluded_file_for_linter?(file, linter)

begin
run_linter(linter, engine, config)
run_linter(linter, engine, config, file)
rescue => error
raise SCSSLint::Exceptions::LinterError,
"#{linter.class} raised unexpected error linting file #{file}: " \
Expand All @@ -55,7 +52,9 @@ def find_lints(file)
end

# For stubbing in tests.
def run_linter(linter, engine, config)
def run_linter(linter, engine, config, file)
return unless config.linter_enabled?(linter)
return if config.excluded_file_for_linter?(file, linter)
linter.run(engine, config.linter_options(linter))
end
end
Expand Down
2 changes: 1 addition & 1 deletion scss-lint.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ Gem::Specification.new do |s|

s.add_development_dependency 'nokogiri', '~> 1.6.0'
s.add_development_dependency 'rspec', '~> 3.0'
s.add_development_dependency 'rubocop', '0.26.0'
s.add_development_dependency 'rubocop', '0.27.1'
end

0 comments on commit 0d9ebf6

Please sign in to comment.