rubocop-jbuilder 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rubocop/cop/jbuilder/partial_exists.rb +28 -8
- data/lib/rubocop/jbuilder/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 65d8956a31b6c1e7a41da5b6f141d6e87ad3c57aaaf177001a309309966cb1f1
|
4
|
+
data.tar.gz: 3aecca35e0f09f13d09336918a3d4337f9f5968434cf2cba837489ef6a7a0b10
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 93dc1a5baab5301e85af4f4261d22dbda7c100a8f01160fbbc750666aa3a297d0db6916436e70aea7bcee874479010fc6c2ad752e6150be474dd096bc4d3fb70
|
7
|
+
data.tar.gz: 420c7475ea90388c43174aa979bd71c61e6aceb1a37668d47cdb372b4f6d87078cab6bd6924f7fcfb537ef9fcf107a101045c5b3bfa213258ea3323ca6b680db
|
@@ -1,12 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
require 'pathname'
|
4
|
+
|
5
|
+
TEST_FILENAME = File.join(Dir.pwd, "app/views/testing/nested/show.json.jbuilder")
|
4
6
|
|
5
7
|
module RuboCop
|
6
8
|
module Cop
|
7
9
|
module Jbuilder
|
8
10
|
class PartialExists < Base
|
9
|
-
MSG = "Partial not found. Looked for: %<
|
11
|
+
MSG = "Partial not found. Looked for: %<paths>s"
|
10
12
|
|
11
13
|
def_node_matcher :has_partial?, <<~PATTERN
|
12
14
|
{
|
@@ -17,21 +19,39 @@ module RuboCop
|
|
17
19
|
|
18
20
|
def on_send(node)
|
19
21
|
has_partial?(node) do |actual|
|
20
|
-
|
21
|
-
|
22
|
-
|
22
|
+
all_paths = get_view_paths_from(actual)
|
23
|
+
|
24
|
+
return if all_paths.any? { |full_path| File.exist?(full_path) }
|
25
|
+
|
26
|
+
base_path = rails_root_dirname(node)
|
27
|
+
relative_paths = all_paths.map { |full_path| relative_path(full_path, base_path) }
|
28
|
+
add_offense(actual, message: format(MSG, paths: relative_paths.join(", ")))
|
23
29
|
end
|
24
30
|
end
|
25
31
|
|
26
32
|
private
|
27
33
|
|
28
|
-
def
|
34
|
+
def get_view_paths_from(node)
|
29
35
|
partial = node.source[1..-2]
|
30
36
|
segments = partial.split("/")
|
31
37
|
segments.last.sub!(/.*/, '_\&.json.jbuilder')
|
32
38
|
|
33
|
-
|
34
|
-
File.join(
|
39
|
+
specific_path = segments.count != 1
|
40
|
+
return [File.join(rails_views_dirname(node), *segments)] if specific_path
|
41
|
+
|
42
|
+
paths = []
|
43
|
+
current_dirname = sut_dirname(node)
|
44
|
+
while current_dirname != rails_views_dirname(node)
|
45
|
+
paths << File.join(current_dirname, *segments)
|
46
|
+
current_dirname = File.expand_path("..", current_dirname)
|
47
|
+
end
|
48
|
+
paths
|
49
|
+
end
|
50
|
+
|
51
|
+
def relative_path(full_path, base_path)
|
52
|
+
full = Pathname.new(full_path)
|
53
|
+
base = Pathname.new(base_path)
|
54
|
+
full.relative_path_from(base).to_s
|
35
55
|
end
|
36
56
|
|
37
57
|
def sut_dirname(node)
|