rubocop-slim 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7cf87a8612b574266dfd299f12015fdeb0bab05209d10a7442986d246afaefac
4
+ data.tar.gz: b0e326fe05f6ae9ac27cd10dbd68413baae05ddb75d6858ffc6cf9f6f1120d5d
5
+ SHA512:
6
+ metadata.gz: cf91db8b95de79e09cc0dd2871cb4765ffe8add777acafd28506e05087962ce16a93669eda08b642d3019904bd9a6a6803b0bf09cda5bb99004b9f3843da5b4c
7
+ data.tar.gz: 5cafcb80661c32d6589ce183b3276697475e473b53e7424c823c1751e9f6bea69add7fbe9ccd8e33b1cf4252e0ae4392c19eef8cccc4f6e0c68583fa0f0a02f9
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,33 @@
1
+ require:
2
+ - rubocop-rake
3
+ - rubocop-rspec
4
+ - rubocop-slim
5
+
6
+ inherit_mode:
7
+ merge:
8
+ - Exclude
9
+
10
+ AllCops:
11
+ Exclude:
12
+ - spec/**/*.slim
13
+ NewCops: enable
14
+ TargetRubyVersion: 2.6
15
+
16
+ Metrics:
17
+ Enabled: false
18
+
19
+ Naming/FileName:
20
+ Exclude:
21
+ - lib/rubocop-slim.rb
22
+
23
+ RSpec/ExampleLength:
24
+ Enabled: false
25
+
26
+ RSpec/MultipleExpectations:
27
+ Enabled: false
28
+
29
+ RSpec/NamedSubject:
30
+ Enabled: false
31
+
32
+ Style/Documentation:
33
+ Enabled: false
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at r7kamura@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in rubocop-slim.gemspec
6
+ gemspec
7
+
8
+ gem 'rake'
9
+ gem 'rspec'
10
+ gem 'rubocop'
11
+ gem 'rubocop-rake'
12
+ gem 'rubocop-rspec'
data/Gemfile.lock ADDED
@@ -0,0 +1,75 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rubocop-slim (0.2.0)
5
+ rubocop (~> 1.45)
6
+ slimi (~> 0.7)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.4.2)
12
+ diff-lcs (1.5.0)
13
+ json (2.6.3)
14
+ parallel (1.22.1)
15
+ parser (3.2.0.0)
16
+ ast (~> 2.4.1)
17
+ rainbow (3.1.1)
18
+ rake (13.0.6)
19
+ regexp_parser (2.6.2)
20
+ rexml (3.2.5)
21
+ rspec (3.12.0)
22
+ rspec-core (~> 3.12.0)
23
+ rspec-expectations (~> 3.12.0)
24
+ rspec-mocks (~> 3.12.0)
25
+ rspec-core (3.12.1)
26
+ rspec-support (~> 3.12.0)
27
+ rspec-expectations (3.12.2)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.12.0)
30
+ rspec-mocks (3.12.3)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.12.0)
33
+ rspec-support (3.12.0)
34
+ rubocop (1.45.0)
35
+ json (~> 2.3)
36
+ parallel (~> 1.10)
37
+ parser (>= 3.2.0.0)
38
+ rainbow (>= 2.2.2, < 4.0)
39
+ regexp_parser (>= 1.8, < 3.0)
40
+ rexml (>= 3.2.5, < 4.0)
41
+ rubocop-ast (>= 1.24.1, < 2.0)
42
+ ruby-progressbar (~> 1.7)
43
+ unicode-display_width (>= 2.4.0, < 3.0)
44
+ rubocop-ast (1.24.1)
45
+ parser (>= 3.1.1.0)
46
+ rubocop-capybara (2.17.0)
47
+ rubocop (~> 1.41)
48
+ rubocop-rake (0.6.0)
49
+ rubocop (~> 1.0)
50
+ rubocop-rspec (2.18.1)
51
+ rubocop (~> 1.33)
52
+ rubocop-capybara (~> 2.17)
53
+ ruby-progressbar (1.11.0)
54
+ slimi (0.7.2)
55
+ temple
56
+ thor
57
+ tilt
58
+ temple (0.10.0)
59
+ thor (1.2.1)
60
+ tilt (2.0.11)
61
+ unicode-display_width (2.4.2)
62
+
63
+ PLATFORMS
64
+ x86_64-linux
65
+
66
+ DEPENDENCIES
67
+ rake
68
+ rspec
69
+ rubocop
70
+ rubocop-rake
71
+ rubocop-rspec
72
+ rubocop-slim!
73
+
74
+ BUNDLED WITH
75
+ 2.3.19
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Ryo Nakamura
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # rubocop-slim
2
+
3
+ [RuboCop](https://github.com/rubocop/rubocop) plugin for [Slim](https://github.com/slim-template/slim) template language.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ ```
10
+ bundle add rubocop-slim
11
+ ```
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ ```
16
+ gem install rubocop-slim
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ Require `"rubocop-slim"` in your RuboCop config.
22
+
23
+ ```yaml
24
+ # .rubocop.yml
25
+ require:
26
+ - rubocop-slim
27
+ ```
28
+
29
+ Now you can use RuboCop also for Slim templates.
30
+
31
+ ```
32
+ $ bundle exec rubocop spec/fixtures/dummy.slim
33
+ Inspecting 1 file
34
+ C
35
+
36
+ Offenses:
37
+
38
+ spec/fixtures/dummy.slim:1:3: C: [Correctable] Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
39
+ - "a"
40
+ ^^^
41
+ spec/fixtures/dummy.slim:3:5: C: [Correctable] Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
42
+ | #{"c"}
43
+ ^^^
44
+ spec/fixtures/dummy.slim:5:8: C: [Correctable] Style/ZeroLengthPredicate: Use !empty? instead of size > 0.
45
+ - a if array.size > 0
46
+ ^^^^^^^^^^^^^^
47
+ spec/fixtures/dummy.slim:6:3: C: [Correctable] Style/NegatedIf: Favor unless over if for negative conditions.
48
+ - a if !b
49
+ ^^^^^^^
50
+ spec/fixtures/dummy.slim:8:6: C: [Correctable] Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
51
+ - if "a"
52
+ ^^^
53
+
54
+ 1 file inspected, 5 offenses detected, 5 offenses autocorrectable
55
+ ```
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,204 @@
1
+ inherit_mode:
2
+ merge:
3
+ - Exclude
4
+ - Include
5
+
6
+ AllCops:
7
+ Include:
8
+ - '**/*.slim'
9
+
10
+ Layout/ArgumentAlignment:
11
+ Exclude:
12
+ - '**/*.slim'
13
+
14
+ Layout/ArrayAlignment:
15
+ Exclude:
16
+ - '**/*.slim'
17
+
18
+ Layout/BlockAlignment:
19
+ Exclude:
20
+ - '**/*.slim'
21
+
22
+ Layout/ClosingParenthesisIndentation:
23
+ Exclude:
24
+ - '**/*.slim'
25
+
26
+ Layout/EmptyLineAfterGuardClause:
27
+ Exclude:
28
+ - '**/*.slim'
29
+
30
+ Layout/EndAlignment:
31
+ Exclude:
32
+ - '**/*.slim'
33
+
34
+ Layout/FirstArgumentIndentation:
35
+ Exclude:
36
+ - '**/*.slim'
37
+
38
+ Layout/FirstArrayElementIndentation:
39
+ Exclude:
40
+ - '**/*.slim'
41
+
42
+ Layout/FirstArrayElementLineBreak:
43
+ Exclude:
44
+ - '**/*.slim'
45
+
46
+ Layout/FirstHashElementIndentation:
47
+ Exclude:
48
+ - '**/*.slim'
49
+
50
+ Layout/FirstHashElementLineBreak:
51
+ Exclude:
52
+ - '**/*.slim'
53
+
54
+ Layout/FirstMethodArgumentLineBreak:
55
+ Exclude:
56
+ - '**/*.slim'
57
+
58
+ Layout/FirstParameterIndentation:
59
+ Exclude:
60
+ - '**/*.slim'
61
+
62
+ Layout/HashAlignment:
63
+ Exclude:
64
+ - '**/*.slim'
65
+
66
+ Layout/IndentationConsistency:
67
+ Exclude:
68
+ - '**/*.slim'
69
+
70
+ Layout/IndentationWidth:
71
+ Exclude:
72
+ - '**/*.slim'
73
+
74
+ Layout/LineEndStringConcatenationIndentation:
75
+ Exclude:
76
+ - '**/*.slim'
77
+
78
+ Layout/LineLength:
79
+ Exclude:
80
+ - '**/*.slim'
81
+
82
+ Layout/MultilineArrayBraceLayout:
83
+ Exclude:
84
+ - '**/*.slim'
85
+
86
+ Layout/MultilineAssignmentLayout:
87
+ Exclude:
88
+ - '**/*.slim'
89
+
90
+ Layout/MultilineHashBraceLayout:
91
+ Exclude:
92
+ - '**/*.slim'
93
+
94
+ Layout/MultilineMethodCallBraceLayout:
95
+ Exclude:
96
+ - '**/*.slim'
97
+
98
+ Layout/MultilineMethodCallIndentation:
99
+ Exclude:
100
+ - '**/*.slim'
101
+
102
+ Layout/MultilineMethodDefinitionBraceLayout:
103
+ Exclude:
104
+ - '**/*.slim'
105
+
106
+ Layout/MultilineOperationIndentation:
107
+ Exclude:
108
+ - '**/*.slim'
109
+
110
+ Layout/ParameterAlignment:
111
+ Exclude:
112
+ - '**/*.slim'
113
+
114
+ Layout/TrailingEmptyLines:
115
+ Exclude:
116
+ - '**/*.slim'
117
+
118
+ Layout/TrailingWhitespace:
119
+ Exclude:
120
+ - '**/*.slim'
121
+
122
+ Lint/EmptyFile:
123
+ Exclude:
124
+ - '**/*.slim'
125
+
126
+ Lint/SyntaxError:
127
+ Exclude:
128
+ - '**/*.slim'
129
+
130
+ Lint/UselessAssignment:
131
+ Exclude:
132
+ - '**/*.slim'
133
+
134
+ Lint/Void:
135
+ Exclude:
136
+ - '**/*.slim'
137
+
138
+ Metrics/BlockLength:
139
+ Exclude:
140
+ - '**/*.slim'
141
+
142
+ Metrics/BlockNesting:
143
+ Exclude:
144
+ - '**/*.slim'
145
+
146
+ Naming/FileName:
147
+ Exclude:
148
+ - '**/*.slim'
149
+
150
+ Style/FrozenStringLiteralComment:
151
+ Exclude:
152
+ - '**/*.slim'
153
+
154
+ Style/IdenticalConditionalBranches:
155
+ Exclude:
156
+ - '**/*.slim'
157
+
158
+ Style/IfUnlessModifier:
159
+ Exclude:
160
+ - '**/*.slim'
161
+
162
+ Style/MultilineTernaryOperator:
163
+ Exclude:
164
+ - '**/*.slim'
165
+
166
+ Style/NestedTernaryOperator:
167
+ Exclude:
168
+ - '**/*.slim'
169
+
170
+ Style/Next:
171
+ Exclude:
172
+ - '**/*.slim'
173
+
174
+ Style/ParallelAssignment:
175
+ Exclude:
176
+ - '**/*.slim'
177
+
178
+ Style/RescueModifier:
179
+ Exclude:
180
+ - '**/*.slim'
181
+
182
+ Style/Semicolon:
183
+ Exclude:
184
+ - '**/*.slim'
185
+
186
+ Style/TrailingCommaInArguments:
187
+ Exclude:
188
+ - '**/*.slim'
189
+
190
+ Style/TrailingCommaInArrayLiteral:
191
+ Exclude:
192
+ - '**/*.slim'
193
+
194
+ Style/TrailingCommaInHashLiteral:
195
+ Exclude:
196
+ - '**/*.slim'
197
+
198
+ Style/WhileUntilDo:
199
+ Exclude:
200
+ - '**/*.slim'
201
+
202
+ Style/WhileUntilModifier:
203
+ Exclude:
204
+ - '**/*.slim'
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubocop'
4
+
5
+ module RuboCop
6
+ module Slim
7
+ # Merge default RuboCop config with plugin config.
8
+ class ConfigLoader
9
+ PLUGIN_CONFIG_PATH = ::File.expand_path(
10
+ '../../../config/default.yml',
11
+ __dir__
12
+ )
13
+
14
+ class << self
15
+ # @return [RuboCop::Config]
16
+ def call
17
+ new.call
18
+ end
19
+ end
20
+
21
+ # @return [RuboCop::Config]
22
+ def call
23
+ RuboCop::ConfigLoader.merge_with_default(
24
+ plugin_config,
25
+ PLUGIN_CONFIG_PATH
26
+ )
27
+ end
28
+
29
+ private
30
+
31
+ # @return [RuboCop::Config]
32
+ def plugin_config
33
+ config = ::RuboCop::Config.new(
34
+ plugin_config_hash,
35
+ PLUGIN_CONFIG_PATH
36
+ )
37
+ config.make_excludes_absolute
38
+ config
39
+ end
40
+
41
+ # @return [Hash]
42
+ def plugin_config_hash
43
+ ::RuboCop::ConfigLoader.send(
44
+ :load_yaml_configuration,
45
+ PLUGIN_CONFIG_PATH
46
+ )
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubocop'
4
+
5
+ RuboCop::Runner.ruby_extractors.unshift(RuboCop::Slim::RubyExtractor)
6
+
7
+ RuboCop::ConfigLoader.instance_variable_set(
8
+ :@default_configuration,
9
+ RuboCop::Slim::ConfigLoader.call
10
+ )
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Slim
5
+ RubyClip = ::Struct.new(
6
+ :code,
7
+ :offset,
8
+ keyword_init: true
9
+ )
10
+ end
11
+ end
@@ -0,0 +1,125 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Slim
5
+ # Remove unnecessary part (e.g. `if`, `unless`, `do`, ...) from Ruby-ish code.
6
+ class RubyClipper
7
+ class << self
8
+ # @param [String] code
9
+ # @return [Hash]
10
+ def call(code)
11
+ new(code).call
12
+ end
13
+ end
14
+
15
+ # @param [String] code
16
+ def initialize(code)
17
+ @code = code
18
+ end
19
+
20
+ # @return [RuboCop::Slim::RubyClip]
21
+ def call
22
+ [
23
+ PrecedingKeywordRemover,
24
+ TrailingDoRemover
25
+ ].reduce(
26
+ RubyClip.new(
27
+ code: @code,
28
+ offset: 0
29
+ )
30
+ ) do |previous, callable|
31
+ result = callable.call(previous.code)
32
+ RubyClip.new(
33
+ code: result.code,
34
+ offset: previous.offset + result.offset
35
+ )
36
+ end
37
+ end
38
+
39
+ # Remove preceding keyword.
40
+ class PrecedingKeywordRemover
41
+ REGEXP = /
42
+ \A
43
+ (?:
44
+ begin
45
+ | case
46
+ | else
47
+ | elsif
48
+ | ensure
49
+ | if
50
+ | rescue
51
+ | unless
52
+ | until
53
+ | when
54
+ | while
55
+ | for[ \t]+\w+[ \t]+in
56
+ )
57
+ [ \t]
58
+ /x.freeze
59
+
60
+ class << self
61
+ # @param [String] code
62
+ # @return [RubyClip]
63
+ def call(code)
64
+ new(code).call
65
+ end
66
+ end
67
+
68
+ # @param [String] code
69
+ def initialize(code)
70
+ @code = code
71
+ end
72
+
73
+ # @return [Hash]
74
+ def call
75
+ data = @code.match(REGEXP)
76
+ if data
77
+ offset = data[0].length
78
+ RubyClip.new(
79
+ code: @code[offset..],
80
+ offset: offset
81
+ )
82
+ else
83
+ RubyClip.new(
84
+ code: @code,
85
+ offset: 0
86
+ )
87
+ end
88
+ end
89
+ end
90
+
91
+ # Remove trailing `do`.
92
+ class TrailingDoRemover
93
+ REGEXP = /
94
+ [ \t]
95
+ do
96
+ [ \t]*
97
+ (\|[^|]*\|)?
98
+ [ \t]*
99
+ \Z
100
+ /x.freeze
101
+
102
+ class << self
103
+ # @param [String] code
104
+ # @return [RubyClip]
105
+ def call(code)
106
+ new(code).call
107
+ end
108
+ end
109
+
110
+ # @param [String] code
111
+ def initialize(code)
112
+ @code = code
113
+ end
114
+
115
+ # @return [Hash]
116
+ def call
117
+ RubyClip.new(
118
+ code: @code.sub(REGEXP, ''),
119
+ offset: 0
120
+ )
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubocop'
4
+ require 'slimi'
5
+
6
+ module RuboCop
7
+ module Slim
8
+ # Extract Ruby codes from Slim template.
9
+ class RubyExtractor
10
+ class << self
11
+ # @param [RuboCop::ProcessedSource] processed_source
12
+ # @return [Array<RuboCop::ProcessedSource>, nil]
13
+ def call(processed_source)
14
+ new(processed_source).call
15
+ end
16
+ end
17
+
18
+ # @param [RuboCop::ProcessedSource] processed_source
19
+ def initialize(processed_source)
20
+ @processed_source = processed_source
21
+ end
22
+
23
+ # @return [Array<RuboCop::ProcessedSource>, nil]
24
+ def call
25
+ return unless supported_file_path_pattern?
26
+
27
+ ruby_ranges.map do |(begin_, end_)|
28
+ clip = RubyClipper.new(template_source[begin_...end_]).call
29
+ processed_source = ::RuboCop::ProcessedSource.new(
30
+ clip[:code],
31
+ @processed_source.ruby_version,
32
+ file_path
33
+ )
34
+ processed_source.config = @processed_source.config
35
+ processed_source.registry = @processed_source.registry
36
+ {
37
+ offset: begin_ + clip[:offset],
38
+ processed_source: processed_source
39
+ }
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ # @return [Array] Slim AST, represented in S-expression.
46
+ def ast
47
+ ::Slimi::Filters::Interpolation.new.call(
48
+ ::Slimi::Parser.new(file: file_path).call(template_source)
49
+ )
50
+ end
51
+
52
+ # @return [String, nil]
53
+ def file_path
54
+ @processed_source.path
55
+ end
56
+
57
+ # @return [Array<Array<Integer>>]
58
+ def ruby_ranges
59
+ result = []
60
+ traverse(ast) do |begin_, end_|
61
+ result << [begin_, end_]
62
+ end
63
+ result
64
+ end
65
+
66
+ # @return [Boolean]
67
+ def supported_file_path_pattern?
68
+ file_path&.end_with?('.slim')
69
+ end
70
+
71
+ def traverse(node, &block)
72
+ return unless node.instance_of?(::Array)
73
+
74
+ block.call(node[2], node[3]) if node[0] == :slimi && node[1] == :position
75
+ node.each do |element|
76
+ traverse(element, &block)
77
+ end
78
+ end
79
+
80
+ # @return [String]
81
+ def template_source
82
+ @processed_source.raw_source
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rubocop
4
+ module Slim
5
+ VERSION = '0.2.0'
6
+ end
7
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Slim
5
+ autoload :ConfigLoader, 'rubocop/slim/config_loader'
6
+ autoload :RubyClip, 'rubocop/slim/ruby_clip'
7
+ autoload :RubyClipper, 'rubocop/slim/ruby_clipper'
8
+ autoload :RubyExtractor, 'rubocop/slim/ruby_extractor'
9
+ end
10
+ end
11
+
12
+ require_relative 'slim/rubocop_extension'
13
+ require_relative 'slim/version'
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubocop/slim'
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/rubocop/slim/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'rubocop-slim'
7
+ spec.version = Rubocop::Slim::VERSION
8
+ spec.authors = ['Ryo Nakamura']
9
+ spec.email = ['r7kamura@gmail.com']
10
+
11
+ spec.summary = 'RuboCop plugin for Slim template.'
12
+ spec.homepage = 'https://github.com/r7kamura/rubocop-slim'
13
+ spec.license = 'MIT'
14
+ spec.required_ruby_version = '>= 2.6.0'
15
+
16
+ spec.metadata['homepage_uri'] = spec.homepage
17
+ spec.metadata['source_code_uri'] = spec.homepage
18
+ spec.metadata['changelog_uri'] = "#{spec.homepage}/releases"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(__dir__) do
23
+ `git ls-files -z`.split("\x0").reject do |f|
24
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
25
+ end
26
+ end
27
+ spec.bindir = 'exe'
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ['lib']
30
+
31
+ spec.add_dependency 'rubocop', '~> 1.45'
32
+ spec.add_dependency 'slimi', '~> 0.7'
33
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubocop-slim
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryo Nakamura
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-02-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rubocop
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.45'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.45'
27
+ - !ruby/object:Gem::Dependency
28
+ name: slimi
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.7'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.7'
41
+ description:
42
+ email:
43
+ - r7kamura@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".rspec"
49
+ - ".rubocop.yml"
50
+ - CODE_OF_CONDUCT.md
51
+ - Gemfile
52
+ - Gemfile.lock
53
+ - LICENSE.txt
54
+ - README.md
55
+ - Rakefile
56
+ - config/default.yml
57
+ - lib/rubocop-slim.rb
58
+ - lib/rubocop/slim.rb
59
+ - lib/rubocop/slim/config_loader.rb
60
+ - lib/rubocop/slim/rubocop_extension.rb
61
+ - lib/rubocop/slim/ruby_clip.rb
62
+ - lib/rubocop/slim/ruby_clipper.rb
63
+ - lib/rubocop/slim/ruby_extractor.rb
64
+ - lib/rubocop/slim/version.rb
65
+ - rubocop-slim.gemspec
66
+ homepage: https://github.com/r7kamura/rubocop-slim
67
+ licenses:
68
+ - MIT
69
+ metadata:
70
+ homepage_uri: https://github.com/r7kamura/rubocop-slim
71
+ source_code_uri: https://github.com/r7kamura/rubocop-slim
72
+ changelog_uri: https://github.com/r7kamura/rubocop-slim/releases
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: 2.6.0
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubygems_version: 3.3.7
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: RuboCop plugin for Slim template.
92
+ test_files: []