From a81e08b7fc7dbffec2a5d6891294fe2bcd9b2233 Mon Sep 17 00:00:00 2001 From: Tejas Bubane Date: Thu, 10 Sep 2020 21:00:52 +0530 Subject: [PATCH] [Fix #8683] Make naming cops work with non-ascii characters Closes https://github.com/rubocop-hq/rubocop/issues/8683 --- CHANGELOG.md | 1 + lib/rubocop/cop/mixin/configurable_naming.rb | 4 ++-- lib/rubocop/cop/naming/file_name.rb | 2 +- spec/rubocop/cop/naming/file_name_spec.rb | 8 ++++++++ spec/rubocop/cop/naming/method_name_spec.rb | 6 ++++++ spec/rubocop/cop/naming/variable_name_spec.rb | 4 ++++ 6 files changed, 22 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 81f6bdfd083..b71dd260f9a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ * [#8682](https://github.com/rubocop-hq/rubocop/pull/8682): Fix a positive for `Style/HashTransformKeys` and `Style/HashTransformValues` when the `each_with_object` hash is used in the transformed key or value. ([@eugeneius][]) * [#8688](https://github.com/rubocop-hq/rubocop/issues/8688): Mark `Style/GlobalStdStream` as unsafe autocorrection. ([@marcandre][]) * [#8642](https://github.com/rubocop-hq/rubocop/issues/8642): Fix a false negative for `Style/SpaceInsideHashLiteralBraces` when a correct empty hash precedes the incorrect hash. ([@dvandersluis][]) +* [#8683](https://github.com/rubocop-hq/rubocop/issues/8683): Make naming cops work with non-ascii characters. ([@tejasbubane][]) ### Changes diff --git a/lib/rubocop/cop/mixin/configurable_naming.rb b/lib/rubocop/cop/mixin/configurable_naming.rb index 49734795aa8..e9b6d358b54 100644 --- a/lib/rubocop/cop/mixin/configurable_naming.rb +++ b/lib/rubocop/cop/mixin/configurable_naming.rb @@ -8,8 +8,8 @@ module ConfigurableNaming include ConfigurableFormatting FORMATS = { - snake_case: /^@{0,2}[\da-z_]+[!?=]?$/, - camelCase: /^@{0,2}(?:_|_?[a-z][\da-zA-Z]*)[!?=]?$/ + snake_case: /^@{0,2}[\d[[:lower:]]_]+[!?=]?$/, + camelCase: /^@{0,2}(?:_|_?[[[:lower:]]][\d[[:lower:]][[:upper:]]]*)[!?=]?$/ }.freeze end end diff --git a/lib/rubocop/cop/naming/file_name.rb b/lib/rubocop/cop/naming/file_name.rb index 9bc07078b20..7fde76f6645 100644 --- a/lib/rubocop/cop/naming/file_name.rb +++ b/lib/rubocop/cop/naming/file_name.rb @@ -33,7 +33,7 @@ class FileName < Base 'called `%s`.' MSG_REGEX = '`%s` should match `%s`.' - SNAKE_CASE = /^[\da-z_.?!]+$/.freeze + SNAKE_CASE = /^[\d[[:lower:]]_.?!]+$/.freeze def on_new_investigation file_path = processed_source.file_path diff --git a/spec/rubocop/cop/naming/file_name_spec.rb b/spec/rubocop/cop/naming/file_name_spec.rb index 991eba59c2f..2a6b5fa9fbe 100644 --- a/spec/rubocop/cop/naming/file_name_spec.rb +++ b/spec/rubocop/cop/naming/file_name_spec.rb @@ -468,4 +468,12 @@ class HTTPServer expect(offenses.empty?).to be(true) end end + + context 'with non-ascii characters in filename' do + let(:filename) { '/some/dir/ünbound_sérvér.rb' } + + it 'reports an offense' do + expect(offenses.empty?).to be(true) + end + end end diff --git a/spec/rubocop/cop/naming/method_name_spec.rb b/spec/rubocop/cop/naming/method_name_spec.rb index f2659c8b885..7cfcced2284 100644 --- a/spec/rubocop/cop/naming/method_name_spec.rb +++ b/spec/rubocop/cop/naming/method_name_spec.rb @@ -344,4 +344,10 @@ def self.foo_bar include_examples 'never accepted', 'camelCase' include_examples 'multiple attr methods', 'camelCase' end + + it 'works for non-ascii characters' do + expect_no_offenses(<<~RUBY) + def última_vista; end + RUBY + end end diff --git a/spec/rubocop/cop/naming/variable_name_spec.rb b/spec/rubocop/cop/naming/variable_name_spec.rb index 5513771c36d..238ed297556 100644 --- a/spec/rubocop/cop/naming/variable_name_spec.rb +++ b/spec/rubocop/cop/naming/variable_name_spec.rb @@ -213,6 +213,10 @@ def foo(&block_arg); end RUBY end + it 'works with non-ascii characters' do + expect_no_offenses('léo = 1') + end + include_examples 'always accepted' end end