rubocop-erb 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5296190cd666230a75345238e04672607de63d81fd9743916dfaa0bbcb93d3cf
4
- data.tar.gz: ddf6afb2fff89a0b138c185e98b95ff8ada7d51bfe1e73a43b021261253739f2
3
+ metadata.gz: 99ed46b9d8eba4e69cea7d354412e033b02648eace4dfd38d9bcd59f83e0943d
4
+ data.tar.gz: bb8f0f0aeae48270f3c2ea4a7a90da7b2ebc5f51c85c7df2e9f2dda039fd6f1e
5
5
  SHA512:
6
- metadata.gz: 2b1a9f87e73b4003762d77c8d9a4d3925f8551ea466149d2f8900a4af6df86ffee1d5f05bf1de4d88225a6a01b1cb8a94c8ec1f87a9b66b1da758e6cd324bb91
7
- data.tar.gz: f7de81a3f61e54fcdf3a6aaf4c7ccd14634710d758c149acd98efd9f2d7a33b860f7e4a6025dcff0111478f480f320eeec9248dd53c9dc1045aeaa58e43291c1
6
+ metadata.gz: 8aa2e1476e145e3174417b20baef489ada2b253fcfcf5bdb0e2ad80463e192588a7180c9a643b1eccf9d85a3b8a6828cc23b5372b4e5928856855a7cec744575
7
+ data.tar.gz: 8e5c7ecc7da0bbea9d862d5aac33ff89768de9b2d85365a560e4b799363d07636cd4d9cfb654ceb0794cf79c556cea3c685204ed3b118bc46c581e7debb53899
data/.rubocop.yml CHANGED
@@ -2,6 +2,7 @@ require:
2
2
  - rubocop-erb
3
3
  - rubocop-rake
4
4
  - rubocop-rspec
5
+ - sevencop
5
6
 
6
7
  inherit_mode:
7
8
  merge:
@@ -19,10 +20,6 @@ Gemspec/RequireMFA:
19
20
  Metrics:
20
21
  Enabled: false
21
22
 
22
- Naming/FileName:
23
- Exclude:
24
- - lib/rubocop-erb.rb
25
-
26
23
  RSpec/ExampleLength:
27
24
  Enabled: false
28
25
 
@@ -32,5 +29,23 @@ RSpec/MultipleExpectations:
32
29
  RSpec/NamedSubject:
33
30
  Enabled: false
34
31
 
32
+ Sevencop/AutoloadOrdered:
33
+ Enabled: true
34
+
35
+ Sevencop/HashElementOrdered:
36
+ Enabled: true
37
+
38
+ Sevencop/MethodDefinitionArgumentsMultiline:
39
+ Enabled: true
40
+
41
+ Sevencop/MethodDefinitionKeywordArgumentOrdered:
42
+ Enabled: true
43
+
44
+ Sevencop/MethodDefinitionOrdered:
45
+ Enabled: true
46
+
35
47
  Style/Documentation:
36
48
  Enabled: false
49
+
50
+ Style/MultilineBlockChain:
51
+ Enabled: false
data/Gemfile CHANGED
@@ -9,3 +9,4 @@ gem 'rake'
9
9
  gem 'rspec'
10
10
  gem 'rubocop-rake'
11
11
  gem 'rubocop-rspec'
12
+ gem 'sevencop'
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rubocop-erb (0.2.0)
4
+ rubocop-erb (0.2.2)
5
5
  better_html
6
6
  rubocop (~> 1.45)
7
7
 
@@ -87,6 +87,9 @@ GEM
87
87
  rubocop (~> 1.33)
88
88
  rubocop-capybara (~> 2.17)
89
89
  ruby-progressbar (1.11.0)
90
+ sevencop (0.33.0)
91
+ activesupport
92
+ rubocop
90
93
  smart_properties (1.17.0)
91
94
  tzinfo (2.0.6)
92
95
  concurrent-ruby (~> 1.0)
@@ -101,6 +104,7 @@ DEPENDENCIES
101
104
  rubocop-erb!
102
105
  rubocop-rake
103
106
  rubocop-rspec
107
+ sevencop
104
108
 
105
109
  BUNDLED WITH
106
110
  2.3.19
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # rubocop-erb
2
2
 
3
- [RuboCop](https://github.com/rubocop/rubocop) plugin for ERB template language.
3
+ [![test](https://github.com/r7kamura/rubocop-erb/actions/workflows/test.yml/badge.svg)](https://github.com/r7kamura/rubocop-erb/actions/workflows/test.yml)
4
+
5
+ [RuboCop](https://github.com/rubocop/rubocop) plugin for ERB template.
4
6
 
5
7
  ## Installation
6
8
 
@@ -18,7 +20,7 @@ gem install rubocop-erb
18
20
 
19
21
  ## Usage
20
22
 
21
- Require `"rubocop-erb"` in your RuboCop config.
23
+ Require `rubocop-erb` in your RuboCop config.
22
24
 
23
25
  ```yaml
24
26
  # .rubocop.yml
@@ -51,3 +53,9 @@ spec/fixtures/dummy.erb:7:11: E: Lint/Syntax: unexpected token $end
51
53
 
52
54
  1 file inspected, 4 offenses detected, 3 offenses autocorrectable
53
55
  ```
56
+
57
+ ## Related projects
58
+
59
+ - https://github.com/r7kamura/rubocop-haml
60
+ - https://github.com/r7kamura/rubocop-slim
61
+ - https://github.com/r7kamura/rubocop-markdown
@@ -2,19 +2,19 @@
2
2
 
3
3
  module RuboCop
4
4
  module Erb
5
- # Remove unnecessary part (e.g. `if`, `unless`, `do`, ...) from Ruby-ish code.
6
- class RubyClipper
5
+ # Remove unnecessary keyword part (e.g. `if`, `unless`, `do`, ...) from Ruby-ish code.
6
+ class KeywordRemover
7
7
  class << self
8
- # @param [String] code
9
- # @return [Hash]
10
- def call(code)
11
- new(code).call
8
+ # @param [RuboCop::Erb::RubyClip] ruby_clip
9
+ # @return [RuboCop::Erb::RubyClip]
10
+ def call(ruby_clip)
11
+ new(ruby_clip).call
12
12
  end
13
13
  end
14
14
 
15
- # @param [String] code
16
- def initialize(code)
17
- @code = code
15
+ # @param [RuboCop::Erb::RubyClip] ruby_clip
16
+ def initialize(ruby_clip)
17
+ @ruby_clip = ruby_clip
18
18
  end
19
19
 
20
20
  # @return [RuboCop::Erb::RubyClip]
@@ -22,12 +22,7 @@ module RuboCop
22
22
  [
23
23
  PrecedingKeywordRemover,
24
24
  TrailingDoRemover
25
- ].reduce(
26
- RubyClip.new(
27
- code: @code,
28
- offset: 0
29
- )
30
- ) do |previous, callable|
25
+ ].reduce(@ruby_clip) do |previous, callable|
31
26
  result = callable.call(previous.code)
32
27
  RubyClip.new(
33
28
  code: result.code,
@@ -25,23 +25,19 @@ module RuboCop
25
25
  def call
26
26
  return unless supported_file_path_pattern?
27
27
 
28
- nodes.map do |node|
29
- snippet = node.children.first
30
- clip = RubyClipper.new(snippet).call
31
- next if clip[:code].match?(/\A\s*\z/)
32
-
28
+ ruby_clips.map do |ruby_clip|
33
29
  processed_source = ::RuboCop::ProcessedSource.new(
34
- clip[:code],
30
+ ruby_clip.code,
35
31
  @processed_source.ruby_version,
36
32
  file_path
37
33
  )
38
34
  processed_source.config = @processed_source.config
39
35
  processed_source.registry = @processed_source.registry
40
36
  {
41
- offset: node.location.begin_pos + clip[:offset],
37
+ offset: ruby_clip.offset,
42
38
  processed_source: processed_source
43
39
  }
44
- end.compact
40
+ end
45
41
  end
46
42
 
47
43
  private
@@ -53,6 +49,11 @@ module RuboCop
53
49
  end
54
50
  end
55
51
 
52
+ # @return [String, nil]
53
+ def file_path
54
+ @processed_source.path
55
+ end
56
+
56
57
  # @return [Enumerator<BetterHtml::AST::Node>]
57
58
  def nodes
58
59
  erbs.flat_map do |erb|
@@ -71,9 +72,20 @@ module RuboCop
71
72
  ).ast
72
73
  end
73
74
 
74
- # @return [String, nil]
75
- def file_path
76
- @processed_source.path
75
+ # @return [Array<RuboCop::Erb::RubyClip>]
76
+ def ruby_clips
77
+ nodes.map do |node|
78
+ RubyClip.new(
79
+ code: node.children.first,
80
+ offset: node.location.begin_pos
81
+ )
82
+ end.flat_map do |ruby_clip|
83
+ WhenDecomposer.call(ruby_clip)
84
+ end.map do |ruby_clip|
85
+ KeywordRemover.call(ruby_clip)
86
+ end.reject do |ruby_clip|
87
+ ruby_clip.code.match?(/\A\s*\z/)
88
+ end
77
89
  end
78
90
 
79
91
  # @return [Boolean]
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Rubocop
3
+ module RuboCop
4
4
  module Erb
5
- VERSION = '0.2.0'
5
+ VERSION = '0.2.2'
6
6
  end
7
7
  end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'parser/current'
4
+ require 'rubocop/ast/builder'
5
+
6
+ module RuboCop
7
+ module Erb
8
+ class WhenDecomposer
9
+ REGEXP = /
10
+ \A
11
+ \s*
12
+ when[ \t]
13
+ /x.freeze
14
+
15
+ class << self
16
+ # @param [RuboCop::Erb::RubyClip] ruby_clip
17
+ # @return [Array<RuboCop::Erb::RubyClip>]
18
+ def call(ruby_clip)
19
+ new(ruby_clip).call
20
+ end
21
+ end
22
+
23
+ # @param [RuboCop::Erb::RubyClip] ruby_clip
24
+ def initialize(ruby_clip)
25
+ @ruby_clip = ruby_clip
26
+ end
27
+
28
+ # @return [Array<RuboCop::Erb::RubyClip>]
29
+ def call
30
+ match_data = @ruby_clip.code.match(REGEXP)
31
+ if match_data
32
+ offset = match_data[0].length
33
+ parse("[#{@ruby_clip.code[offset..]}]").children.map do |child|
34
+ RubyClip.new(
35
+ code: child.location.expression.source,
36
+ offset: @ruby_clip.offset + offset + child.location.expression.begin_pos - 1
37
+ )
38
+ end
39
+ else
40
+ [@ruby_clip]
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ # @param [String] source
47
+ # @return [RuboCop::AST::Node]
48
+ def parse(source)
49
+ ::Parser::CurrentRuby.new(
50
+ ::RuboCop::AST::Builder.new
51
+ ).parse(
52
+ ::Parser::Source::Buffer.new(
53
+ '(string)',
54
+ source: source
55
+ )
56
+ )
57
+ end
58
+ end
59
+ end
60
+ end
data/lib/rubocop/erb.rb CHANGED
@@ -3,9 +3,10 @@
3
3
  module RuboCop
4
4
  module Erb
5
5
  autoload :ConfigLoader, 'rubocop/erb/config_loader'
6
+ autoload :KeywordRemover, 'rubocop/erb/keyword_remover'
6
7
  autoload :RubyClip, 'rubocop/erb/ruby_clip'
7
- autoload :RubyClipper, 'rubocop/erb/ruby_clipper'
8
8
  autoload :RubyExtractor, 'rubocop/erb/ruby_extractor'
9
+ autoload :WhenDecomposer, 'rubocop/erb/when_decomposer'
9
10
  end
10
11
  end
11
12
 
data/rubocop-erb.gemspec CHANGED
@@ -4,7 +4,7 @@ require_relative 'lib/rubocop/erb/version'
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'rubocop-erb'
7
- spec.version = Rubocop::Erb::VERSION
7
+ spec.version = RuboCop::Erb::VERSION
8
8
  spec.authors = ['Ryo Nakamura']
9
9
  spec.email = ['r7kamura@gmail.com']
10
10
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-erb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-02-08 00:00:00.000000000 Z
11
+ date: 2023-02-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: better_html
@@ -38,7 +38,7 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.45'
41
- description:
41
+ description:
42
42
  email:
43
43
  - r7kamura@gmail.com
44
44
  executables: []
@@ -54,14 +54,14 @@ files:
54
54
  - README.md
55
55
  - Rakefile
56
56
  - config/default.yml
57
- - lib/rubocop-erb.rb
58
57
  - lib/rubocop/erb.rb
59
58
  - lib/rubocop/erb/config_loader.rb
59
+ - lib/rubocop/erb/keyword_remover.rb
60
60
  - lib/rubocop/erb/rubocop_extension.rb
61
61
  - lib/rubocop/erb/ruby_clip.rb
62
- - lib/rubocop/erb/ruby_clipper.rb
63
62
  - lib/rubocop/erb/ruby_extractor.rb
64
63
  - lib/rubocop/erb/version.rb
64
+ - lib/rubocop/erb/when_decomposer.rb
65
65
  - rubocop-erb.gemspec
66
66
  homepage: https://github.com/r7kamura/rubocop-erb
67
67
  licenses:
@@ -70,7 +70,7 @@ metadata:
70
70
  homepage_uri: https://github.com/r7kamura/rubocop-erb
71
71
  source_code_uri: https://github.com/r7kamura/rubocop-erb
72
72
  changelog_uri: https://github.com/r7kamura/rubocop-erb/releases
73
- post_install_message:
73
+ post_install_message:
74
74
  rdoc_options: []
75
75
  require_paths:
76
76
  - lib
@@ -85,8 +85,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
85
  - !ruby/object:Gem::Version
86
86
  version: '0'
87
87
  requirements: []
88
- rubygems_version: 3.3.7
89
- signing_key:
88
+ rubygems_version: 3.3.26
89
+ signing_key:
90
90
  specification_version: 4
91
91
  summary: RuboCop plugin for ERB template.
92
92
  test_files: []
data/lib/rubocop-erb.rb DELETED
@@ -1,3 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rubocop/erb'