gl_rubocop 0.2.2 → 0.2.4

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: a173779cd13f263e06b1335bc3d616fff54ba25d1d36df283bc5e1f1bf0f40ad
4
- data.tar.gz: 7404b7c467ea8781b216155568f6f6d7e777428475866cbbf2f581e51918f6d3
3
+ metadata.gz: 20051ad7be570098d65231ae0699f2a8e0388ac1d46e635c2b967b04d4490278
4
+ data.tar.gz: 92d0f9a01f967880e8db09a2715adad74144071489e3a39e0b05fd260dd54f33
5
5
  SHA512:
6
- metadata.gz: 711216513be498b83798ff8fb78f7e3b02ffe52496bb51ecd719ad2e9cb6f4ea0966aae586d28157b4abb7c51f410eccd56c024cd38225612b61e5b807f610e1
7
- data.tar.gz: 24f7d44b9036db679ec633999cb686d48519e2ec7e220b3ce559b79e8ea95e1f3cd597ccee85ed87e04f1fd4319c794485e06d9a590dcac51f44cb1e746a6f68
6
+ metadata.gz: 0ad7a44349b6182c777f3277b84458624ed4010c6e8d5effc08461779262dc021081a83b2dbe1c0a325f86292b621ac2f73181c06ee24cda60b69a5ecd79f0eb
7
+ data.tar.gz: 17b072554bdbcb5df215abe5dd0c01c7d64324a0db62fdd555c8e931b16fbaa259d97a1a1028581fc3340b27e6e4bfb93963581c7414f5300d322a577d826ccd
data/default.yml CHANGED
@@ -3,10 +3,13 @@ require:
3
3
  - rubocop-rails
4
4
  - rubocop-rspec
5
5
  - rubocop-magic_numbers
6
+ - rubocop-haml
6
7
  - ./lib/gl_rubocop/gl_cops/interactor_inherits_from_interactor_base.rb
7
8
  - ./lib/gl_rubocop/gl_cops/callback_method_names.rb
8
9
  - ./lib/gl_rubocop/gl_cops/prevent_erb_files.rb
9
10
  - ./lib/gl_rubocop/gl_cops/sidekiq_inherits_from_sidekiq_job.rb
11
+ - ./lib/gl_rubocop/gl_cops/unique_identifier.rb
12
+
10
13
 
11
14
  AllCops:
12
15
  SuggestExtensions: false
@@ -39,6 +42,11 @@ GLCops/SidekiqInheritsFromSidekiqJob:
39
42
  - 'app/**/*_worker.rb'
40
43
  - 'app/**/*_job.rb'
41
44
 
45
+ GLCops/UniqueIdentifier:
46
+ Enabled: true
47
+ Include:
48
+ - 'app/components/**/*.haml'
49
+
42
50
  Layout/LineLength:
43
51
  Max: 100
44
52
  AllowedPatterns:
data/gl_rubocop.gemspec CHANGED
@@ -26,6 +26,7 @@ Gem::Specification.new do |spec|
26
26
  spec.require_paths = ['lib']
27
27
 
28
28
  spec.add_dependency 'rubocop', '~> 1.62.0'
29
+ spec.add_dependency 'rubocop-haml', '~> 0.2.4'
29
30
  spec.add_dependency 'rubocop-magic_numbers'
30
31
  spec.add_dependency 'rubocop-performance'
31
32
  spec.add_dependency 'rubocop-rails'
@@ -0,0 +1,62 @@
1
+ module GLRubocop
2
+ module GLCops
3
+ class UniqueIdentifier < RuboCop::Cop::Base
4
+ # This cop ensures that view components include a data-test-id attribute.
5
+ #
6
+ # Good:
7
+ # {data-test-id: "unique-id"}
8
+ # {data-test-id: @unique_id }
9
+ # {'data-test-id': "unique-id"}
10
+ # {"data-test-id": "unique-id"}
11
+ #
12
+ # Bad:
13
+ # {data: {testId: "unique-id"}}
14
+ # {data: {test: {id: "unique-id"}}
15
+
16
+ MSG = 'View components must include a data-test-id attribute'.freeze
17
+ EMPTY_MSG = 'data-test-id attribute must not be empty'.freeze
18
+ UNIQUE_IDENTIFIER = 'data-test-id'.freeze
19
+
20
+ def on_send(node)
21
+ return unless file_exists? && valid_method_name?(node)
22
+
23
+ return add_offense(node) unless raw_content.include?(UNIQUE_IDENTIFIER)
24
+
25
+ add_offense(node, message: EMPTY_MSG) if test_id_value.strip.empty?
26
+ end
27
+
28
+ private
29
+
30
+ def file_exists?
31
+ File.exist?(processed_source.file_path)
32
+ end
33
+
34
+ def identifiable_line
35
+ line_number = raw_content.lines.find_index { |line| line.include?(UNIQUE_IDENTIFIER) }
36
+ raw_content.lines[line_number]
37
+ end
38
+
39
+ def raw_content
40
+ @raw_content ||= File.read(processed_source.file_path)
41
+ end
42
+
43
+ def regex_for_indentifier_and_value
44
+ key = Regexp.quote(UNIQUE_IDENTIFIER)
45
+ /(?:#{key}|["']#{key}["']):\s*(["']([^"']*)["']|@\w+)/
46
+ end
47
+
48
+ def test_id_value
49
+ match = identifiable_line.match(regex_for_indentifier_and_value)
50
+
51
+ return '' unless match
52
+
53
+ value = match[1]
54
+ value.start_with?('"', "'") ? value[1..-2] : value
55
+ end
56
+
57
+ def valid_method_name?(node)
58
+ node.method_name == :render || node.method_name == :template
59
+ end
60
+ end
61
+ end
62
+ end
@@ -1,3 +1,3 @@
1
1
  module GLRubocop
2
- VERSION = '0.2.2'.freeze
2
+ VERSION = '0.2.4'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gl_rubocop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Give Lively
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-01-08 00:00:00.000000000 Z
11
+ date: 2025-01-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 1.62.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubocop-haml
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.2.4
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.2.4
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rubocop-magic_numbers
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -110,6 +124,7 @@ files:
110
124
  - lib/gl_rubocop/gl_cops/interactor_inherits_from_interactor_base.rb
111
125
  - lib/gl_rubocop/gl_cops/prevent_erb_files.rb
112
126
  - lib/gl_rubocop/gl_cops/sidekiq_inherits_from_sidekiq_job.rb
127
+ - lib/gl_rubocop/gl_cops/unique_identifier.rb
113
128
  - lib/gl_rubocop/version.rb
114
129
  homepage: https://github.com/givelively/gl_rubocop
115
130
  licenses: