rubocop-erb 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5296190cd666230a75345238e04672607de63d81fd9743916dfaa0bbcb93d3cf
4
- data.tar.gz: ddf6afb2fff89a0b138c185e98b95ff8ada7d51bfe1e73a43b021261253739f2
3
+ metadata.gz: 9a314d2115fff57bc77a2fa817590a4678e6d87a3f003956a928e57b9cd42b5b
4
+ data.tar.gz: 51e9a745d150b3c3c99a4294f0cf1f4f14be871ecf44b415087c411f7c54e33d
5
5
  SHA512:
6
- metadata.gz: 2b1a9f87e73b4003762d77c8d9a4d3925f8551ea466149d2f8900a4af6df86ffee1d5f05bf1de4d88225a6a01b1cb8a94c8ec1f87a9b66b1da758e6cd324bb91
7
- data.tar.gz: f7de81a3f61e54fcdf3a6aaf4c7ccd14634710d758c149acd98efd9f2d7a33b860f7e4a6025dcff0111478f480f320eeec9248dd53c9dc1045aeaa58e43291c1
6
+ metadata.gz: d39cc8a0b4a91a7e29b2a68434dd19f02e41f31ebb31ad5c5027593e588e8677e452de6e8b3f40dca654dad03b9eb5f34db3e09af87508d94be8d54a569f0c0e
7
+ data.tar.gz: 45789da936d92b4a212f5160af6ea49f770a07b8ec1a398c60364c2b0e27465a4868ea44d177e775c81010642b51322b6ce9242053cbf250dada6abe489583ff
data/.rubocop.yml CHANGED
@@ -34,3 +34,6 @@ RSpec/NamedSubject:
34
34
 
35
35
  Style/Documentation:
36
36
  Enabled: false
37
+
38
+ Style/MultilineBlockChain:
39
+ Enabled: false
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.1)
5
5
  better_html
6
6
  rubocop (~> 1.45)
7
7
 
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
@@ -5,16 +5,16 @@ module RuboCop
5
5
  # Remove unnecessary part (e.g. `if`, `unless`, `do`, ...) from Ruby-ish code.
6
6
  class RubyClipper
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
@@ -71,6 +67,22 @@ module RuboCop
71
67
  ).ast
72
68
  end
73
69
 
70
+ # @return [Array<RuboCop::Erb::RubyClip>]
71
+ def ruby_clips
72
+ nodes.map do |node|
73
+ RubyClip.new(
74
+ code: node.children.first,
75
+ offset: node.location.begin_pos
76
+ )
77
+ end.flat_map do |ruby_clip|
78
+ WhenDecomposer.call(ruby_clip)
79
+ end.map do |ruby_clip|
80
+ RubyClipper.call(ruby_clip)
81
+ end.reject do |ruby_clip|
82
+ ruby_clip.code.match?(/\A\s*\z/)
83
+ end
84
+ end
85
+
74
86
  # @return [String, nil]
75
87
  def file_path
76
88
  @processed_source.path
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Rubocop
4
4
  module Erb
5
- VERSION = '0.2.0'
5
+ VERSION = '0.2.1'
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
@@ -6,6 +6,7 @@ module RuboCop
6
6
  autoload :RubyClip, 'rubocop/erb/ruby_clip'
7
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
 
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.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-02-08 00:00:00.000000000 Z
11
+ date: 2023-02-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: better_html
@@ -62,6 +62,7 @@ files:
62
62
  - lib/rubocop/erb/ruby_clipper.rb
63
63
  - lib/rubocop/erb/ruby_extractor.rb
64
64
  - lib/rubocop/erb/version.rb
65
+ - lib/rubocop/erb/when_decomposer.rb
65
66
  - rubocop-erb.gemspec
66
67
  homepage: https://github.com/r7kamura/rubocop-erb
67
68
  licenses: