diff --git a/.rubocop_opinionated.yml b/.rubocop_opinionated.yml index 761e2c7d..a9bc89bc 100644 --- a/.rubocop_opinionated.yml +++ b/.rubocop_opinionated.yml @@ -15,12 +15,6 @@ Layout/LineLength: - 'test/**/*_test.rb' Max: 100 -Lint/ConstantDefinitionInBlock: - Exclude: - - 'Rakefile' - - 'spec/**/*' - - 'test/**/*' - # [codesmell] Metrics/AbcSize: Enabled: false @@ -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 %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: @@ -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: @@ -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: diff --git a/Gemfile b/Gemfile index 8c679696..b41c6f2e 100644 --- a/Gemfile +++ b/Gemfile @@ -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" diff --git a/Rakefile b/Rakefile index 26a6c994..59b3b3b7 100644 --- a/Rakefile +++ b/Rakefile @@ -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? @@ -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 diff --git a/lib/public_suffix.rb b/lib/public_suffix.rb index c0f3fab6..e93ca0a6 100644 --- a/lib/public_suffix.rb +++ b/lib/public_suffix.rb @@ -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? @@ -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 @@ -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 diff --git a/lib/public_suffix/list.rb b/lib/public_suffix/list.rb index e11f071d..e294afcc 100644 --- a/lib/public_suffix/list.rb +++ b/lib/public_suffix/list.rb @@ -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 @@ -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 @@ -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) diff --git a/lib/public_suffix/rule.rb b/lib/public_suffix/rule.rb index d41a4807..1c3636b2 100644 --- a/lib/public_suffix/rule.rb +++ b/lib/public_suffix/rule.rb @@ -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. @@ -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 @@ -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. @@ -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. @@ -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 @@ -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. @@ -299,7 +299,7 @@ def decompose(domain) # # @return [Array] def parts - @value.split(DOT)[1..-1] + @value.split(DOT)[1..] end end @@ -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. diff --git a/test/acceptance_test.rb b/test/acceptance_test.rb index 371bfe13..1e299b73 100644 --- a/test/acceptance_test.rb +++ b/test/acceptance_test.rb @@ -64,7 +64,7 @@ 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 @@ -72,9 +72,9 @@ def test_rejected 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 @@ -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 diff --git a/test/psl_test.rb b/test/psl_test.rb index fae398f7..919029b4 100644 --- a/test/psl_test.rb +++ b/test/psl_test.rb @@ -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 diff --git a/test/unit/domain_test.rb b/test/unit/domain_test.rb index 968462d9..f3ae4bed 100644 --- a/test/unit/domain_test.rb +++ b/test/unit/domain_test.rb @@ -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 diff --git a/test/unit/list_test.rb b/test/unit/list_test.rb index 98529352..3b0160e5 100644 --- a/test/unit/list_test.rb +++ b/test/unit/list_test.rb @@ -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 diff --git a/test/unit/public_suffix_test.rb b/test/unit/public_suffix_test.rb index ba51e138..a1e9b8e0 100644 --- a/test/unit/public_suffix_test.rb +++ b/test/unit/public_suffix_test.rb @@ -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 diff --git a/test/unit/rule_test.rb b/test/unit/rule_test.rb index d1cc6094..81d4522e 100644 --- a/test/unit/rule_test.rb +++ b/test/unit/rule_test.rb @@ -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 @@ -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 @@ -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 @@ -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