rubocop-slim 0.2.0 → 0.2.2

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: 7cf87a8612b574266dfd299f12015fdeb0bab05209d10a7442986d246afaefac
4
- data.tar.gz: b0e326fe05f6ae9ac27cd10dbd68413baae05ddb75d6858ffc6cf9f6f1120d5d
3
+ metadata.gz: 615a7a02efeebe4eb7aa61eff708e91e350af4b7ea7ccbabf9e92280fcdd930d
4
+ data.tar.gz: 811f1ffbbdaeef6b6a53707b77b0d4ea8d34f568df965f73eccc5c09cadae571
5
5
  SHA512:
6
- metadata.gz: cf91db8b95de79e09cc0dd2871cb4765ffe8add777acafd28506e05087962ce16a93669eda08b642d3019904bd9a6a6803b0bf09cda5bb99004b9f3843da5b4c
7
- data.tar.gz: 5cafcb80661c32d6589ce183b3276697475e473b53e7424c823c1751e9f6bea69add7fbe9ccd8e33b1cf4252e0ae4392c19eef8cccc4f6e0c68583fa0f0a02f9
6
+ metadata.gz: ec5e1b391202975d5524d19ff384188387222829f7aa2dbc72d3e4d327e7a5e49522e1cab6d41c631f6eec5984c67340529b1ca9103d8cd7e28f892bb98e2974
7
+ data.tar.gz: 38e99fb1eac0f504f113ac3b9f00253da9a248aff2a2f708802c900c17cf26cb5337922658cf8b50a0c9832711babc310637513b4d1e83c88d6389202acfcc98
data/.rubocop.yml CHANGED
@@ -2,6 +2,7 @@ require:
2
2
  - rubocop-rake
3
3
  - rubocop-rspec
4
4
  - rubocop-slim
5
+ - sevencop
5
6
 
6
7
  inherit_mode:
7
8
  merge:
@@ -11,7 +12,10 @@ AllCops:
11
12
  Exclude:
12
13
  - spec/**/*.slim
13
14
  NewCops: enable
14
- TargetRubyVersion: 2.6
15
+ TargetRubyVersion: 2.7
16
+
17
+ Gemspec/RequireMFA:
18
+ Enabled: false
15
19
 
16
20
  Metrics:
17
21
  Enabled: false
@@ -29,5 +33,23 @@ RSpec/MultipleExpectations:
29
33
  RSpec/NamedSubject:
30
34
  Enabled: false
31
35
 
36
+ Sevencop/AutoloadOrdered:
37
+ Enabled: true
38
+
39
+ Sevencop/HashElementOrdered:
40
+ Enabled: true
41
+
42
+ Sevencop/MethodDefinitionArgumentsMultiline:
43
+ Enabled: true
44
+
45
+ Sevencop/MethodDefinitionKeywordArgumentOrdered:
46
+ Enabled: true
47
+
48
+ Sevencop/MethodDefinitionOrdered:
49
+ Enabled: true
50
+
32
51
  Style/Documentation:
33
52
  Enabled: false
53
+
54
+ Style/MultilineBlockChain:
55
+ Enabled: false
data/Gemfile CHANGED
@@ -7,6 +7,6 @@ gemspec
7
7
 
8
8
  gem 'rake'
9
9
  gem 'rspec'
10
- gem 'rubocop'
11
10
  gem 'rubocop-rake'
12
11
  gem 'rubocop-rspec'
12
+ gem 'sevencop'
data/Gemfile.lock CHANGED
@@ -1,16 +1,25 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rubocop-slim (0.2.0)
4
+ rubocop-slim (0.2.2)
5
5
  rubocop (~> 1.45)
6
6
  slimi (~> 0.7)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
+ activesupport (7.0.4.2)
12
+ concurrent-ruby (~> 1.0, >= 1.0.2)
13
+ i18n (>= 1.6, < 2)
14
+ minitest (>= 5.1)
15
+ tzinfo (~> 2.0)
11
16
  ast (2.4.2)
17
+ concurrent-ruby (1.2.0)
12
18
  diff-lcs (1.5.0)
19
+ i18n (1.12.0)
20
+ concurrent-ruby (~> 1.0)
13
21
  json (2.6.3)
22
+ minitest (5.17.0)
14
23
  parallel (1.22.1)
15
24
  parser (3.2.0.0)
16
25
  ast (~> 2.4.1)
@@ -51,6 +60,9 @@ GEM
51
60
  rubocop (~> 1.33)
52
61
  rubocop-capybara (~> 2.17)
53
62
  ruby-progressbar (1.11.0)
63
+ sevencop (0.33.0)
64
+ activesupport
65
+ rubocop
54
66
  slimi (0.7.2)
55
67
  temple
56
68
  thor
@@ -58,6 +70,8 @@ GEM
58
70
  temple (0.10.0)
59
71
  thor (1.2.1)
60
72
  tilt (2.0.11)
73
+ tzinfo (2.0.6)
74
+ concurrent-ruby (~> 1.0)
61
75
  unicode-display_width (2.4.2)
62
76
 
63
77
  PLATFORMS
@@ -66,10 +80,10 @@ PLATFORMS
66
80
  DEPENDENCIES
67
81
  rake
68
82
  rspec
69
- rubocop
70
83
  rubocop-rake
71
84
  rubocop-rspec
72
85
  rubocop-slim!
86
+ sevencop
73
87
 
74
88
  BUNDLED WITH
75
89
  2.3.19
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # rubocop-slim
2
2
 
3
- [RuboCop](https://github.com/rubocop/rubocop) plugin for [Slim](https://github.com/slim-template/slim) template language.
3
+ [![test](https://github.com/r7kamura/rubocop-slim/actions/workflows/test.yml/badge.svg)](https://github.com/r7kamura/rubocop-slim/actions/workflows/test.yml)
4
+
5
+ [RuboCop](https://github.com/rubocop/rubocop) plugin for [Slim](https://github.com/slim-template/slim) template.
4
6
 
5
7
  ## Installation
6
8
 
@@ -18,7 +20,7 @@ gem install rubocop-slim
18
20
 
19
21
  ## Usage
20
22
 
21
- Require `"rubocop-slim"` in your RuboCop config.
23
+ Require `rubocop-slim` in your RuboCop config.
22
24
 
23
25
  ```yaml
24
26
  # .rubocop.yml
@@ -53,3 +55,9 @@ spec/fixtures/dummy.slim:8:6: C: [Correctable] Style/StringLiterals: Prefer sing
53
55
 
54
56
  1 file inspected, 5 offenses detected, 5 offenses autocorrectable
55
57
  ```
58
+
59
+ ## Related projects
60
+
61
+ - https://github.com/r7kamura/rubocop-erb
62
+ - https://github.com/r7kamura/rubocop-haml
63
+ - https://github.com/r7kamura/rubocop-markdown
@@ -3,18 +3,18 @@
3
3
  module RuboCop
4
4
  module Slim
5
5
  # Remove unnecessary part (e.g. `if`, `unless`, `do`, ...) from Ruby-ish code.
6
- class RubyClipper
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::Slim::RubyClip] ruby_clip
9
+ # @return [RuboCop::Slim::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 [String] ruby_clip
16
+ def initialize(ruby_clip)
17
+ @ruby_clip = ruby_clip
18
18
  end
19
19
 
20
20
  # @return [RuboCop::Slim::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,
@@ -54,7 +49,7 @@ module RuboCop
54
49
  | while
55
50
  | for[ \t]+\w+[ \t]+in
56
51
  )
57
- [ \t]
52
+ \b[ \t]*
58
53
  /x.freeze
59
54
 
60
55
  class << self
@@ -24,17 +24,16 @@ module RuboCop
24
24
  def call
25
25
  return unless supported_file_path_pattern?
26
26
 
27
- ruby_ranges.map do |(begin_, end_)|
28
- clip = RubyClipper.new(template_source[begin_...end_]).call
27
+ ruby_clips.map do |ruby_clip|
29
28
  processed_source = ::RuboCop::ProcessedSource.new(
30
- clip[:code],
29
+ ruby_clip.code,
31
30
  @processed_source.ruby_version,
32
31
  file_path
33
32
  )
34
33
  processed_source.config = @processed_source.config
35
34
  processed_source.registry = @processed_source.registry
36
35
  {
37
- offset: begin_ + clip[:offset],
36
+ offset: ruby_clip.offset,
38
37
  processed_source: processed_source
39
38
  }
40
39
  end
@@ -54,6 +53,20 @@ module RuboCop
54
53
  @processed_source.path
55
54
  end
56
55
 
56
+ # @return [Array<RuboCop::Slim::RubyClip]
57
+ def ruby_clips
58
+ ruby_ranges.map do |(begin_, end_)|
59
+ RubyClip.new(
60
+ code: template_source[begin_...end_],
61
+ offset: begin_
62
+ )
63
+ end.flat_map do |ruby_clip|
64
+ WhenDecomposer.call(ruby_clip)
65
+ end.map do |ruby_clip|
66
+ KeywordRemover.call(ruby_clip)
67
+ end
68
+ end
69
+
57
70
  # @return [Array<Array<Integer>>]
58
71
  def ruby_ranges
59
72
  result = []
@@ -68,7 +81,15 @@ module RuboCop
68
81
  file_path&.end_with?('.slim')
69
82
  end
70
83
 
71
- def traverse(node, &block)
84
+ # @return [String]
85
+ def template_source
86
+ @processed_source.raw_source
87
+ end
88
+
89
+ def traverse(
90
+ node,
91
+ &block
92
+ )
72
93
  return unless node.instance_of?(::Array)
73
94
 
74
95
  block.call(node[2], node[3]) if node[0] == :slimi && node[1] == :position
@@ -76,11 +97,6 @@ module RuboCop
76
97
  traverse(element, &block)
77
98
  end
78
99
  end
79
-
80
- # @return [String]
81
- def template_source
82
- @processed_source.raw_source
83
- end
84
100
  end
85
101
  end
86
102
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Rubocop
3
+ module RuboCop
4
4
  module Slim
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 Slim
8
+ class WhenDecomposer
9
+ REGEXP = /
10
+ \A
11
+ \s*
12
+ when[ \t]
13
+ /x.freeze
14
+
15
+ class << self
16
+ # @param [RuboCop::Slim::RubyClip] ruby_clip
17
+ # @return [Array<RuboCop::Slim::RubyClip>]
18
+ def call(ruby_clip)
19
+ new(ruby_clip).call
20
+ end
21
+ end
22
+
23
+ # @param [RuboCop::Slim::RubyClip] ruby_clip
24
+ def initialize(ruby_clip)
25
+ @ruby_clip = ruby_clip
26
+ end
27
+
28
+ # @return [Array<RuboCop::Slim::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/slim.rb CHANGED
@@ -3,9 +3,10 @@
3
3
  module RuboCop
4
4
  module Slim
5
5
  autoload :ConfigLoader, 'rubocop/slim/config_loader'
6
+ autoload :KeywordRemover, 'rubocop/slim/keyword_remover'
6
7
  autoload :RubyClip, 'rubocop/slim/ruby_clip'
7
- autoload :RubyClipper, 'rubocop/slim/ruby_clipper'
8
8
  autoload :RubyExtractor, 'rubocop/slim/ruby_extractor'
9
+ autoload :WhenDecomposer, 'rubocop/slim/when_decomposer'
9
10
  end
10
11
  end
11
12
 
data/rubocop-slim.gemspec CHANGED
@@ -4,14 +4,14 @@ require_relative 'lib/rubocop/slim/version'
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'rubocop-slim'
7
- spec.version = Rubocop::Slim::VERSION
7
+ spec.version = RuboCop::Slim::VERSION
8
8
  spec.authors = ['Ryo Nakamura']
9
9
  spec.email = ['r7kamura@gmail.com']
10
10
 
11
11
  spec.summary = 'RuboCop plugin for Slim template.'
12
12
  spec.homepage = 'https://github.com/r7kamura/rubocop-slim'
13
13
  spec.license = 'MIT'
14
- spec.required_ruby_version = '>= 2.6.0'
14
+ spec.required_ruby_version = '>= 2.7'
15
15
 
16
16
  spec.metadata['homepage_uri'] = spec.homepage
17
17
  spec.metadata['source_code_uri'] = spec.homepage
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-slim
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-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -38,7 +38,7 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0.7'
41
- description:
41
+ description:
42
42
  email:
43
43
  - r7kamura@gmail.com
44
44
  executables: []
@@ -57,11 +57,12 @@ files:
57
57
  - lib/rubocop-slim.rb
58
58
  - lib/rubocop/slim.rb
59
59
  - lib/rubocop/slim/config_loader.rb
60
+ - lib/rubocop/slim/keyword_remover.rb
60
61
  - lib/rubocop/slim/rubocop_extension.rb
61
62
  - lib/rubocop/slim/ruby_clip.rb
62
- - lib/rubocop/slim/ruby_clipper.rb
63
63
  - lib/rubocop/slim/ruby_extractor.rb
64
64
  - lib/rubocop/slim/version.rb
65
+ - lib/rubocop/slim/when_decomposer.rb
65
66
  - rubocop-slim.gemspec
66
67
  homepage: https://github.com/r7kamura/rubocop-slim
67
68
  licenses:
@@ -70,7 +71,7 @@ metadata:
70
71
  homepage_uri: https://github.com/r7kamura/rubocop-slim
71
72
  source_code_uri: https://github.com/r7kamura/rubocop-slim
72
73
  changelog_uri: https://github.com/r7kamura/rubocop-slim/releases
73
- post_install_message:
74
+ post_install_message:
74
75
  rdoc_options: []
75
76
  require_paths:
76
77
  - lib
@@ -78,15 +79,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
78
79
  requirements:
79
80
  - - ">="
80
81
  - !ruby/object:Gem::Version
81
- version: 2.6.0
82
+ version: '2.7'
82
83
  required_rubygems_version: !ruby/object:Gem::Requirement
83
84
  requirements:
84
85
  - - ">="
85
86
  - !ruby/object:Gem::Version
86
87
  version: '0'
87
88
  requirements: []
88
- rubygems_version: 3.3.7
89
- signing_key:
89
+ rubygems_version: 3.3.26
90
+ signing_key:
90
91
  specification_version: 4
91
92
  summary: RuboCop plugin for Slim template.
92
93
  test_files: []