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 +4 -4
- data/.rubocop.yml +3 -0
- data/Gemfile.lock +1 -1
- data/README.md +10 -2
- data/lib/rubocop/erb/ruby_clipper.rb +8 -13
- data/lib/rubocop/erb/ruby_extractor.rb +20 -8
- data/lib/rubocop/erb/version.rb +1 -1
- data/lib/rubocop/erb/when_decomposer.rb +60 -0
- data/lib/rubocop/erb.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9a314d2115fff57bc77a2fa817590a4678e6d87a3f003956a928e57b9cd42b5b
|
4
|
+
data.tar.gz: 51e9a745d150b3c3c99a4294f0cf1f4f14be871ecf44b415087c411f7c54e33d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d39cc8a0b4a91a7e29b2a68434dd19f02e41f31ebb31ad5c5027593e588e8677e452de6e8b3f40dca654dad03b9eb5f34db3e09af87508d94be8d54a569f0c0e
|
7
|
+
data.tar.gz: 45789da936d92b4a212f5160af6ea49f770a07b8ec1a398c60364c2b0e27465a4868ea44d177e775c81010642b51322b6ce9242053cbf250dada6abe489583ff
|
data/.rubocop.yml
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# rubocop-erb
|
2
2
|
|
3
|
-
[
|
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 `
|
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 [
|
9
|
-
# @return [
|
10
|
-
def call(
|
11
|
-
new(
|
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 [
|
16
|
-
def initialize(
|
17
|
-
@
|
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
|
-
|
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
|
-
|
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:
|
37
|
+
offset: ruby_clip.offset,
|
42
38
|
processed_source: processed_source
|
43
39
|
}
|
44
|
-
end
|
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
|
data/lib/rubocop/erb/version.rb
CHANGED
@@ -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
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.
|
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-
|
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:
|