Skip to content

Commit c9e0185

Browse files
committed
Rubocop
1 parent d29ee0b commit c9e0185

19 files changed

+102
-99
lines changed

.rubocop.yml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# The behavior of RuboCop can be controlled via the .rubocop.yml
2+
# configuration file. It makes it possible to enable/disable
3+
# certain cops (checks) and to alter their behavior if they accept
4+
# any parameters. The file can be placed either in your home
5+
# directory or in some project directory.
6+
#
7+
# RuboCop will start looking for the configuration file in the directory
8+
# where the inspected file is and continue its way up to the root directory.
9+
#
10+
# See https://docs.rubocop.org/rubocop/configuration
11+
12+
13+
Style:
14+
Enabled: false
15+
Metrics:
16+
Enabled: false
17+
18+
AllCops:
19+
NewCops: enable
20+
Exclude:
21+
- "lib/extract_i18n/adapters/slim_adapter_wip.rb"
22+
23+
Naming/PredicateName:
24+
Enabled: false
25+
Layout/DotPosition:
26+
EnforcedStyle: trailing
27+
Layout/EmptyLineAfterGuardClause:
28+
Enabled: false
29+
Lint/MissingCopEnableDirective:
30+
Enabled: false
31+
Lint/MixedRegexpCaptureTypes:
32+
Enabled: false
33+
Layout/LineLength:
34+
Exclude:
35+
- "spec/**/*.rb"

Gemfile

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
# frozen_string_literal: true
2+
13
source "https://rubygems.org"
24

3-
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
5+
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
46

57
# Specify your gem's dependencies in extract_i18n.gemspec
68
gemspec
79

810
gem 'pry'
11+
gem 'rake'
912
gem 'rspec'
13+
gem 'rubocop'
1014
gem 'solargraph'

extract_i18n.gemspec

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
88
spec.authors = ["Stefan Wienert"]
99
spec.email = ["[email protected]"]
1010

11-
spec.summary = %q{Extact i18n from RUBY Files using Ruby parser}
12-
spec.description = %q{Extact i18n from RUBY Files using Ruby parser}
11+
spec.summary = %q{Extact i18n from Ruby files using Ruby parser and slim files using regex}
12+
spec.description = %q{Extact i18n from Ruby files using Ruby parser and slim files using regex interactively}
1313
spec.homepage = "https://github.com/pludoni/extract_i18n"
1414
spec.license = "MIT"
1515

@@ -27,8 +27,8 @@ Gem::Specification.new do |spec|
2727

2828
spec.add_runtime_dependency 'parser', '>= 2.6'
2929
spec.add_runtime_dependency 'slim'
30-
spec.add_runtime_dependency 'zeitwerk'
3130
spec.add_runtime_dependency 'tty-prompt'
31+
spec.add_runtime_dependency 'zeitwerk'
3232
spec.add_dependency "diff-lcs"
3333
spec.add_dependency "diffy"
3434
end

lib/extract_i18n/adapters/adapter.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module ExtractI18n::Adapters
24
class Adapter
35
def self.for(file_path)

lib/extract_i18n/adapters/ruby_adapter.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'parser/current'
24
require 'tty-prompt'
35
require 'pry'
@@ -58,7 +60,9 @@ def on_dstr(node)
5860

5961
i18n_key = ExtractI18n.key(node.children.select { |i| i.type == :str }.map { |i| i.children[0] }.join(' '))
6062

61-
ask_and_continue(i18n_key: i18n_key, i18n_string: out_string, interpolate_arguments: interpolate_arguments, node: node)
63+
ask_and_continue(
64+
i18n_key: i18n_key, i18n_string: out_string, interpolate_arguments: interpolate_arguments, node: node,
65+
)
6266
end
6367

6468
def on_str(node)

lib/extract_i18n/adapters/slim_adapter.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module ExtractI18n::Adapters
24
class SlimAdapter < Adapter
35
def self.supports_relative_keys?

lib/extract_i18n/adapters/slim_adapter_wip.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'slim'
24

35
module ExtractI18n::Adapters
@@ -107,7 +109,6 @@ def indent
107109
# binding.pry
108110
# result = super
109111

110-
111112
# change = ExtractI18n::SourceChange.new(
112113
# i18n_key: "#{@file_key}.#{i18n_key}",
113114
# i18n_string: i18n_string,

lib/extract_i18n/adapters/vue_adapter.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module ExtractI18n::Adapters
24
class VueAdapter < SlimAdapter
35
def process_line(old_line)

lib/extract_i18n/cli.rb

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
# rubocop:disable Layout/LineLength
4+
15
require 'optparse'
26
require 'extract_i18n'
37
require 'extract_i18n/file_processor'

lib/extract_i18n/file_processor.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def run
3434

3535
private
3636

37-
def read_and_transform(&block)
37+
def read_and_transform(&_block)
3838
key = if @options[:namespace]
3939
"#{@options[:namespace]}.#{@file_key}"
4040
else

lib/extract_i18n/slimkeyfy/slim_transformer.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module ExtractI18n
24
class Slimkeyfy::SlimTransformer
35
TRANSLATED = /t\s*\(?\s*(".*?"|'.*?')\s*\)?/.freeze
@@ -49,7 +51,7 @@ def initialize(word, file_key)
4951

5052
private
5153

52-
def parse_html(&block)
54+
def parse_html(&_block)
5355
return @word.line if @word.line.match(TRANSLATED)
5456

5557
tagged_with_equals = Slimkeyfy::Whitespacer.convert_slim(@word.head)

lib/extract_i18n/slimkeyfy/vue_transformer.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
class ExtractI18n::Slimkeyfy::VueTransformer < ExtractI18n::Slimkeyfy::SlimTransformer
24
BEFORE = /(?<before>.*[\( ^])/.freeze
35
HTML_ARGUMENTS = {
@@ -15,7 +17,7 @@ def regex_list
1517
HTML_ARGUMENTS.map { |_, regex| /#{BEFORE}#{regex}#{TRANSLATION}#{AFTER}/ }
1618
end
1719

18-
def parse_html(&block)
20+
def parse_html(&_block)
1921
return @word.line if @word.line.match(TRANSLATED)
2022
return @word.line if @word.tail.join(" ")[/^{{.*}}$/]
2123

lib/extract_i18n/slimkeyfy/whitespacer.rb

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1+
# frozen_string_literal: true
2+
13
module ExtractI18n
24
class Slimkeyfy::Whitespacer
35
HTML = /([a-z\.]+[0-9\-]*)+/.freeze
46
HTML_TAGS = /^(?<html_tag>'|\||#{HTML})/.freeze
57

6-
def self.convert_slim(s)
7-
m = s.match(HTML_TAGS)
8+
def self.convert_slim(string)
9+
m = string.match(HTML_TAGS)
810

9-
return s if m.nil? or m[:html_tag].nil?
10-
return s if has_equals_tag?(s, m[:html_tag])
11+
return string if m.nil? or m[:html_tag].nil?
12+
return string if has_equals_tag?(string, m[:html_tag])
1113
tag = m[:html_tag]
1214

1315
case tag
1416
when /\|/
15-
s.gsub("|", "=")
17+
string.gsub("|", "=")
1618
when /\'/
17-
s.gsub("'", "=>")
19+
string.gsub("'", "=>")
1820
when HTML
19-
s.gsub(s, "#{s} =")
20-
else s end
21+
string.gsub(string, "#{string} =")
22+
else string end
2123
end
2224

2325
def self.convert_nbsp(body, tag)
@@ -37,8 +39,8 @@ def self.trailing_nsbp(body, tag)
3739
return ">" if body.end_with?("&nbsp;")
3840
end
3941

40-
def self.has_equals_tag?(s, html_tag)
41-
s.gsub(html_tag, "").strip.start_with?("=")
42+
def self.has_equals_tag?(string, html_tag)
43+
string.gsub(html_tag, "").strip.start_with?("=")
4244
end
4345
end
4446
end

lib/extract_i18n/slimkeyfy/word.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module ExtractI18n::Slimkeyfy
24
class Word
35
attr_reader :line, :tokens, :indentation
@@ -20,9 +22,9 @@ def as_list
2022
has_html_form = !!@line[/^ *[\.#]?[a-z\.#-]+\(/]
2123
delimiter_items =
2224
@line.
23-
sub(/^(\s*\|)(\w)/, "\\1 \\2"). # add a whitespace to support "|string"
24-
split(has_html_form ? /(?<=[\(])| +/ : ' '). # split by whitespace or ( but keep (
25-
drop_while { |i| i == "" } # .. but that leaves leading ""
25+
sub(/^(\s*\|)(\w)/, "\\1 \\2"). # add a whitespace to support "|string"
26+
split(has_html_form ? /(?<=[\(])| +/ : ' '). # split by whitespace or ( but keep (
27+
drop_while { |i| i == "" } # .. but that leaves leading ""
2628
items = []
2729
# div: div
2830
delimiter_items.reverse_each do |item|

lib/extract_i18n/source_change.rb

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# rubocop:disable Metrics/ParameterLists
1+
# frozen_string_literal: true
2+
23
require 'pastel'
34

45
module ExtractI18n
@@ -21,7 +22,15 @@ class SourceChange
2122
# how to format the replacement translation, use 2 placeholder %s for the string and for the optional arguments
2223
# @param interpolation_type [Symbol]
2324
# :ruby or :vue
24-
def initialize(i18n_key:, i18n_string:, interpolate_arguments:, source_line:, remove:, t_template: %{I18n.t("%s"%s)}, interpolation_type: :ruby)
25+
def initialize(
26+
i18n_key:,
27+
i18n_string:,
28+
interpolate_arguments:,
29+
source_line:,
30+
remove:,
31+
t_template: %{I18n.t("%s"%s)},
32+
interpolation_type: :ruby
33+
)
2534
@i18n_string = i18n_string
2635
@key = i18n_key
2736
@interpolate_arguments = interpolate_arguments
@@ -34,12 +43,12 @@ def initialize(i18n_key:, i18n_string:, interpolate_arguments:, source_line:, re
3443
def format
3544
s = ""
3645
s += PASTEL.cyan("replace: ") + PASTEL.blue(@source_line).
37-
gsub(@remove, PASTEL.red(@remove))
46+
gsub(@remove, PASTEL.red(@remove))
3847
unless @source_line.include?("\n")
3948
s += "\n"
4049
end
4150
s += PASTEL.cyan("with: ") + PASTEL.blue(@source_line).
42-
gsub(@remove, PASTEL.green(i18n_t))
51+
gsub(@remove, PASTEL.green(i18n_t))
4352
unless @source_line.include?("\n")
4453
s += "\n"
4554
end

spec/adapters/slim_adapter_spec.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"div.foobar\n p Hello World" => [
77
"div.foobar\n p = t('models.foo.hello_world')\n",
88
{ "models.foo.hello_world" => "Hello World" }
9-
])
9+
]
10+
)
1011
end
1112

1213
specify 'pipe' do

spec/adapters/vue_adapter_spec.rb

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# rubocop:disable Lint/InterpolationCheck
2-
31
RSpec.describe ExtractI18n::Adapters::VueAdapter do
42
specify "block content" do
53
view = template("div.foo\n | Content\n")

spec/file_processor_spec.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
# rubocop:disable Lint/InterpolationCheck
24
RSpec.describe ExtractI18n::FileProcessor do
35
around(:each) do |ex|

spec/spec_helper.rb

+1-70
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,3 @@
1-
# This file was generated by the `rspec --init` command. Conventionally, all
2-
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3-
# The generated `.rspec` file contains `--require spec_helper` which will cause
4-
# this file to always be loaded, without a need to explicitly require it in any
5-
# files.
6-
#
7-
# Given that it is always loaded, you are encouraged to keep this file as
8-
# light-weight as possible. Requiring heavyweight dependencies from this file
9-
# will add to the boot time of your test suite on EVERY test run, even for an
10-
# individual file that may not need all of that loaded. Instead, consider making
11-
# a separate helper file that requires the additional dependencies and performs
12-
# the additional setup, and require it from the spec files that actually need
13-
# it.
14-
#
15-
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
161
require 'extract_i18n'
172
require 'pry'
183

@@ -66,59 +51,5 @@ def cmp!(hash)
6651
config.shared_context_metadata_behavior = :apply_to_host_groups
6752

6853
config.example_status_persistence_file_path = "spec/.examples.txt"
69-
# The settings below are suggested to provide a good initial experience
70-
# with RSpec, but feel free to customize to your heart's content.
71-
=begin
72-
# This allows you to limit a spec run to individual examples or groups
73-
# you care about by tagging them with `:focus` metadata. When nothing
74-
# is tagged with `:focus`, all examples get run. RSpec also provides
75-
# aliases for `it`, `describe`, and `context` that include `:focus`
76-
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
77-
config.filter_run_when_matching :focus
78-
79-
# Allows RSpec to persist some state between runs in order to support
80-
# the `--only-failures` and `--next-failure` CLI options. We recommend
81-
# you configure your source control system to ignore this file.
82-
config.example_status_persistence_file_path = "spec/examples.txt"
83-
84-
# Limits the available syntax to the non-monkey patched syntax that is
85-
# recommended. For more details, see:
86-
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
87-
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
88-
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
89-
config.disable_monkey_patching!
90-
91-
# This setting enables warnings. It's recommended, but in some cases may
92-
# be too noisy due to issues in dependencies.
93-
config.warnings = true
94-
95-
# Many RSpec users commonly either run the entire suite or an individual
96-
# file, and it's useful to allow more verbose output when running an
97-
# individual spec file.
98-
if config.files_to_run.one?
99-
# Use the documentation formatter for detailed output,
100-
# unless a formatter has already been configured
101-
# (e.g. via a command-line flag).
102-
config.default_formatter = "doc"
103-
end
104-
105-
# Print the 10 slowest examples and example groups at the
106-
# end of the spec run, to help surface which specs are running
107-
# particularly slow.
108-
config.profile_examples = 10
109-
110-
# Run specs in random order to surface order dependencies. If you find an
111-
# order dependency and want to debug it, you can fix the order by providing
112-
# the seed, which is printed after each run.
113-
# --seed 1234
114-
config.order = :random
115-
116-
# Seed global randomization in this process using the `--seed` CLI option.
117-
# Setting this allows you to use `--seed` to deterministically reproduce
118-
# test failures related to randomization by passing the same `--seed` value
119-
# as the one that triggered the failure.
120-
Kernel.srand config.seed
121-
=end
122-
123-
config.include AdapterHelpers, file_path: /spec\/adapters/
54+
config.include AdapterHelpers, file_path: %r{spec/adapters}
12455
end

0 commit comments

Comments
 (0)