Skip to content

Commit

Permalink
Update rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
weppos committed Jul 24, 2022
1 parent aa8ab9a commit 5e3379b
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 75 deletions.
28 changes: 0 additions & 28 deletions .rubocop_opinionated.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ Layout/LineLength:
- 'test/**/*_test.rb'
Max: 100

Lint/ConstantDefinitionInBlock:
Exclude:
- 'Rakefile'
- 'spec/**/*'
- 'test/**/*'

# [codesmell]
Metrics/AbcSize:
Enabled: false
Expand Down Expand Up @@ -101,12 +95,6 @@ Layout/EmptyLinesAroundModuleBody:
Layout/EmptyLineBetweenDefs:
Enabled: false

# I personally don't care about the format style.
# In most cases I like to use %, but not at the point I want to enforce it
# as a convention in the entire code.
Style/FormatString:
Enabled: false

# Annotated tokens (like %<foo>s) are a good thing, but in most cases we don't need them.
# %s is a simpler and straightforward version that works in almost all cases. So don't complain.
Style/FormatStringToken:
Expand All @@ -116,11 +104,6 @@ Style/FormatStringToken:
Style/NegatedIf:
Enabled: false

# For years, %w() has been the de-facto standard. A lot of libraries are using ().
# Switching to [] would be a nightmare.
Style/PercentLiteralDelimiters:
Enabled: false

# There are cases were the inline rescue is ok. We can either downgrade the severity,
# or rely on the developer judgement on a case-by-case basis.
Style/RescueModifier:
Expand All @@ -129,17 +112,6 @@ Style/RescueModifier:
Style/SymbolArray:
EnforcedStyle: brackets

# Sorry, but using trailing spaces helps readability.
#
# %w( foo bar )
#
# looks better to me than
#
# %w( foo bar )
#
Layout/SpaceInsidePercentLiteralDelimiters:
Enabled: false

# Hate It or Love It, I prefer double quotes as this is more consistent
# with several other programming languages and the output of puts and inspect.
Style/StringLiterals:
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ gem "memory_profiler", require: false
gem "minitest"
gem "minitest-reporters"
gem "mocha"
gem "rubocop", "~>0.90", require: false
gem "rubocop", require: false
gem "yard"
6 changes: 3 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ task default: [:test, :rubocop]
require "rake/testtask"

Rake::TestTask.new do |t|
t.libs = %w( lib test )
t.libs = %w[lib test]
t.pattern = "test/**/*_test.rb"
t.verbose = !ENV["VERBOSE"].nil?
t.warning = !ENV["WARNING"].nil?
Expand Down Expand Up @@ -42,10 +42,10 @@ desc "Downloads the Public Suffix List file from the repository and stores it lo
task :"update-list" do
require "net/http"

DEFINITION_URL = "https://raw.githubusercontent.com/publicsuffix/list/master/public_suffix_list.dat"
definition_url = "https://raw.githubusercontent.com/publicsuffix/list/master/public_suffix_list.dat"

File.open("data/list.txt", "w+") do |f|
response = Net::HTTP.get_response(URI.parse(DEFINITION_URL))
response = Net::HTTP.get_response(URI.parse(definition_url))
response.body
f.write(response.body)
end
Expand Down
6 changes: 3 additions & 3 deletions lib/public_suffix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def self.parse(name, list: List.default, default_rule: list.default_rule, ignore
what = normalize(name)
raise what if what.is_a?(DomainInvalid)

rule = list.find(what, default: default_rule, ignore_private: ignore_private)
rule = list.find(what, default: default_rule, ignore_private:)

# rubocop:disable Style/IfUnlessModifier
if rule.nil?
Expand Down Expand Up @@ -124,7 +124,7 @@ def self.valid?(name, list: List.default, default_rule: list.default_rule, ignor
what = normalize(name)
return false if what.is_a?(DomainInvalid)

rule = list.find(what, default: default_rule, ignore_private: ignore_private)
rule = list.find(what, default: default_rule, ignore_private:)

!rule.nil? && !rule.decompose(what).last.nil?
end
Expand Down Expand Up @@ -169,7 +169,7 @@ def self.normalize(name)

return DomainInvalid.new("Name is blank") if name.empty?
return DomainInvalid.new("Name starts with a dot") if name.start_with?(DOT)
return DomainInvalid.new("%s is not expected to contain a scheme" % name) if name.include?("://")
return DomainInvalid.new(format("%s is not expected to contain a scheme", name)) if name.include?("://")

name
end
Expand Down
8 changes: 4 additions & 4 deletions lib/public_suffix/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def self.parse(input, private_domains: true)
section = 2

# skip comments
when line.start_with?(comment_token)
when line.start_with?(comment_token) # rubocop:disable Lint/DuplicateBranch
next

else
Expand Down Expand Up @@ -125,12 +125,12 @@ def ==(other)
alias eql? ==

# Iterates each rule in the list.
def each(&block)
def each(&)
Enumerator.new do |y|
@rules.each do |key, node|
y << entry_to_rule(node, key)
end
end.each(&block)
end.each(&)
end


Expand Down Expand Up @@ -236,7 +236,7 @@ def default_rule
private

def entry_to_rule(entry, value)
entry.type.new(value: value, length: entry.length, private: entry.private)
entry.type.new(value:, length: entry.length, private: entry.private)
end

def rule_to_entry(rule)
Expand Down
16 changes: 8 additions & 8 deletions lib/public_suffix/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class Base
# @param content [String] the content of the rule
# @param private [Boolean]
def self.build(content, private: false)
new(value: content, private: private)
new(value: content, private:)
end

# Initializes a new rule.
Expand All @@ -125,7 +125,7 @@ def self.build(content, private: false)
# @param private [Boolean]
def initialize(value:, length: nil, private: false)
@value = value.to_s
@length = length || @value.count(DOT) + 1
@length = length || (@value.count(DOT) + 1)
@private = private
end

Expand Down Expand Up @@ -161,7 +161,7 @@ def ==(other)
# @param name [String] the domain name to check
# @return [Boolean]
def match?(name)
# Note: it works because of the assumption there are no
# NOTE: it works because of the assumption there are no
# rules like foo.*.com. If the assumption is incorrect,
# we need to properly walk the input and skip parts according
# to wildcard component.
Expand Down Expand Up @@ -221,7 +221,7 @@ class Wildcard < Base
# @param content [String] the content of the rule
# @param private [Boolean]
def self.build(content, private: false)
new(value: content.to_s[2..-1], private: private)
new(value: content.to_s[2..], private:)
end

# Initializes a new rule.
Expand All @@ -230,7 +230,7 @@ def self.build(content, private: false)
# @param length [Integer]
# @param private [Boolean]
def initialize(value:, length: nil, private: false)
super(value: value, length: length, private: private)
super(value:, length:, private:)
length or @length += 1 # * counts as 1
end

Expand Down Expand Up @@ -269,7 +269,7 @@ class Exception < Base
# @param content [#to_s] the content of the rule
# @param private [Boolean]
def self.build(content, private: false)
new(value: content.to_s[1..-1], private: private)
new(value: content.to_s[1..], private:)
end

# Gets the original rule definition.
Expand Down Expand Up @@ -299,7 +299,7 @@ def decompose(domain)
#
# @return [Array<String>]
def parts
@value.split(DOT)[1..-1]
@value.split(DOT)[1..]
end

end
Expand Down Expand Up @@ -331,7 +331,7 @@ def self.factory(content, private: false)
Exception
else
Normal
end.build(content, private: private)
end.build(content, private:)
end

# The default rule to use if no rule match.
Expand Down
14 changes: 7 additions & 7 deletions test/acceptance_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,17 @@ def test_invalid
def test_rejected
REJECTED_CASES.each do |name, expected|
assert_equal expected, PublicSuffix.valid?(name),
"Expected %s to be %s" % [name.inspect, expected.inspect]
format("Expected %s to be %s", name.inspect, expected.inspect)
assert !valid_domain?(name),
"#{name} expected to be invalid"
end
end


CASE_CASES = [
["Www.google.com", %w( www google com )],
["www.Google.com", %w( www google com )],
["www.google.Com", %w( www google com )],
["Www.google.com", %w[www google com]],
["www.Google.com", %w[www google com]],
["www.google.Com", %w[www google com]],
].freeze

def test_ignore_case
Expand All @@ -101,14 +101,14 @@ def test_ignore_private
# test domain and parse
INCLUDE_PRIVATE_CASES.each do |given, ignore_private, expected|
if expected.nil?
assert_nil PublicSuffix.domain(given, ignore_private: ignore_private)
assert_nil PublicSuffix.domain(given, ignore_private:)
else
assert_equal expected, PublicSuffix.domain(given, ignore_private: ignore_private)
assert_equal expected, PublicSuffix.domain(given, ignore_private:)
end
end
# test valid?
INCLUDE_PRIVATE_CASES.each do |given, ignore_private, expected|
assert_equal !expected.nil?, PublicSuffix.valid?(given, ignore_private: ignore_private)
assert_equal !expected.nil?, PublicSuffix.valid?(given, ignore_private:)
end
end
# rubocop:enable Style/CombinableLoops
Expand Down
2 changes: 1 addition & 1 deletion test/psl_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def test_valid
end

message = "The following #{failures.size} tests fail:\n"
failures.each { |i, o, d| message += "Expected %s to be %s, got %s\n" % [i.inspect, o.inspect, d.inspect] }
failures.each { |i, o, d| message += format("Expected %s to be %s, got %s\n", i.inspect, o.inspect, d.inspect) }
assert_equal 0, failures.size, message
end

Expand Down
6 changes: 3 additions & 3 deletions test/unit/domain_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ def setup

# Tokenizes given input into labels.
def test_self_name_to_labels
assert_equal %w( someone spaces live com ),
assert_equal %w[someone spaces live com],
PublicSuffix::Domain.name_to_labels("someone.spaces.live.com")
assert_equal %w( leontina23samiko wiki zoho com ),
assert_equal %w[leontina23samiko wiki zoho com],
PublicSuffix::Domain.name_to_labels("leontina23samiko.wiki.zoho.com")
end

# Converts input into String.
def test_self_name_to_labels_converts_input_to_string
assert_equal %w( someone spaces live com ),
assert_equal %w[someone spaces live com],
PublicSuffix::Domain.name_to_labels(:"someone.spaces.live.com")
end

Expand Down
2 changes: 1 addition & 1 deletion test/unit/list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def test_self_parse
assert_instance_of PublicSuffix::List, list
assert_equal 4, list.size

rules = %w( com *.uk !british-library.uk blogspot.com ).map { |name| PublicSuffix::Rule.factory(name) }
rules = %w[com *.uk !british-library.uk blogspot.com].map { |name| PublicSuffix::Rule.factory(name) }
assert_equal rules, list.each.to_a

# private domains
Expand Down
2 changes: 1 addition & 1 deletion test/unit/public_suffix_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def test_self_parse_with_custom_list
list = PublicSuffix::List.new
list << PublicSuffix::Rule.factory("test")

domain = PublicSuffix.parse("www.example.test", list: list)
domain = PublicSuffix.parse("www.example.test", list:)
assert_instance_of PublicSuffix::Domain, domain
assert_equal "test", domain.tld
assert_equal "example", domain.sld
Expand Down
30 changes: 15 additions & 15 deletions test/unit/rule_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def test_factory_should_return_rule_wildcard
def test_default_returns_default_wildcard
default = PublicSuffix::Rule.default
assert_equal PublicSuffix::Rule::Wildcard.build("*"), default
assert_equal %w( example tldnotlisted ), default.decompose("example.tldnotlisted")
assert_equal %w( www.example tldnotlisted ), default.decompose("www.example.tldnotlisted")
assert_equal %w[example tldnotlisted], default.decompose("example.tldnotlisted")
assert_equal %w[www.example tldnotlisted], default.decompose("www.example.tldnotlisted")
end

end
Expand Down Expand Up @@ -140,15 +140,15 @@ def test_length
end

def test_parts
assert_equal %w(com), @klass.build("com").parts
assert_equal %w(co com), @klass.build("co.com").parts
assert_equal %w(mx co com), @klass.build("mx.co.com").parts
assert_equal %w[com], @klass.build("com").parts
assert_equal %w[co com], @klass.build("co.com").parts
assert_equal %w[mx co com], @klass.build("mx.co.com").parts
end

def test_decompose
assert_equal [nil, nil], @klass.build("com").decompose("com")
assert_equal %w( example com ), @klass.build("com").decompose("example.com")
assert_equal %w( foo.example com ), @klass.build("com").decompose("foo.example.com")
assert_equal %w[example com], @klass.build("com").decompose("example.com")
assert_equal %w[foo.example com], @klass.build("com").decompose("foo.example.com")
end

end
Expand All @@ -175,14 +175,14 @@ def test_length
end

def test_parts
assert_equal %w( uk ), @klass.build("!british-library.uk").parts
assert_equal %w( tokyo jp ), @klass.build("!metro.tokyo.jp").parts
assert_equal %w[uk], @klass.build("!british-library.uk").parts
assert_equal %w[tokyo jp], @klass.build("!metro.tokyo.jp").parts
end

def test_decompose
assert_equal [nil, nil], @klass.build("!british-library.uk").decompose("uk")
assert_equal %w( british-library uk ), @klass.build("!british-library.uk").decompose("british-library.uk")
assert_equal %w( foo.british-library uk ), @klass.build("!british-library.uk").decompose("foo.british-library.uk")
assert_equal %w[british-library uk], @klass.build("!british-library.uk").decompose("british-library.uk")
assert_equal %w[foo.british-library uk], @klass.build("!british-library.uk").decompose("foo.british-library.uk")
end

end
Expand All @@ -209,14 +209,14 @@ def test_length
end

def test_parts
assert_equal %w( uk ), @klass.build("*.uk").parts
assert_equal %w( co uk ), @klass.build("*.co.uk").parts
assert_equal %w[uk], @klass.build("*.uk").parts
assert_equal %w[co uk], @klass.build("*.co.uk").parts
end

def test_decompose
assert_equal [nil, nil], @klass.build("*.do").decompose("nic.do")
assert_equal %w( google co.uk ), @klass.build("*.uk").decompose("google.co.uk")
assert_equal %w( foo.google co.uk ), @klass.build("*.uk").decompose("foo.google.co.uk")
assert_equal %w[google co.uk], @klass.build("*.uk").decompose("google.co.uk")
assert_equal %w[foo.google co.uk], @klass.build("*.uk").decompose("foo.google.co.uk")
end

end

0 comments on commit 5e3379b

Please sign in to comment.