platformos-check 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/CONTRIBUTING.md +2 -2
- data/README.md +1 -1
- data/config/default.yml +0 -1
- data/data/platformos_liquid/documentation/filters.json +1 -1
- data/data/platformos_liquid/documentation/latest.json +1 -1
- data/docs/checks/undefined_object.md +0 -5
- data/lib/platformos_check/analyzer.rb +1 -1
- data/lib/platformos_check/checks/undefined_object.rb +35 -39
- data/lib/platformos_check/checks/unused_assign.rb +4 -0
- data/lib/platformos_check/checks.rb +6 -0
- data/lib/platformos_check/liquid_visitor.rb +8 -1
- data/lib/platformos_check/version.rb +1 -1
- metadata +2 -2
@@ -1,2 +1,2 @@
|
|
1
1
|
|
2
|
-
{"revision":"
|
2
|
+
{"revision":"949281126d705b543c2fa4e5f328c13ed411351a"}
|
@@ -45,13 +45,8 @@ The default configuration for this check is the following:
|
|
45
45
|
```yaml
|
46
46
|
UndefinedObject:
|
47
47
|
enabled: true
|
48
|
-
exclude_partials: true
|
49
48
|
```
|
50
49
|
|
51
|
-
### `exclude_partials`
|
52
|
-
|
53
|
-
The `exclude_partials` (Default: `true`) option determines whether to check for undefined objects in snippets file (as objects _may_ be defined as arguments)
|
54
|
-
|
55
50
|
## When Not To Use It
|
56
51
|
|
57
52
|
It is discouraged to disable this rule.
|
@@ -105,7 +105,7 @@ module PlatformosCheck
|
|
105
105
|
end
|
106
106
|
|
107
107
|
# Call checks that run on a single files, only on specified file
|
108
|
-
liquid_visitor = LiquidVisitor.new(@liquid_checks.single_file, @disabled_checks)
|
108
|
+
liquid_visitor = LiquidVisitor.new(@liquid_checks.single_file, @disabled_checks, only_single_file:)
|
109
109
|
html_visitor = HtmlVisitor.new(@html_checks.single_file)
|
110
110
|
files.each_with_index do |app_file, i|
|
111
111
|
yield(app_file.relative_path.to_s, offset + i, total) if block_given?
|
@@ -16,7 +16,7 @@ module PlatformosCheck
|
|
16
16
|
@app_file = app_file
|
17
17
|
end
|
18
18
|
|
19
|
-
attr_reader :all_assigns, :all_captures, :all_forloops, :app_file
|
19
|
+
attr_reader :all_assigns, :all_captures, :all_forloops, :app_file, :all_renders
|
20
20
|
|
21
21
|
def add_render(name:, node:)
|
22
22
|
@all_renders[name] = node
|
@@ -58,39 +58,32 @@ module PlatformosCheck
|
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
61
|
-
def
|
61
|
+
def self.single_file(**_args)
|
62
|
+
true
|
63
|
+
end
|
64
|
+
|
65
|
+
def initialize(config_type: :default)
|
62
66
|
@config_type = config_type
|
63
|
-
@exclude_partials = exclude_partials
|
64
67
|
@files = {}
|
65
68
|
end
|
66
69
|
|
67
70
|
def on_document(node)
|
68
|
-
return if ignore?(node)
|
69
|
-
|
70
71
|
@files[node.app_file.name] = TemplateInfo.new(app_file: node.app_file)
|
71
72
|
end
|
72
73
|
|
73
74
|
def on_assign(node)
|
74
|
-
return if ignore?(node)
|
75
|
-
|
76
75
|
@files[node.app_file.name].all_assigns[node.value.to] = node
|
77
76
|
end
|
78
77
|
|
79
78
|
def on_capture(node)
|
80
|
-
return if ignore?(node)
|
81
|
-
|
82
79
|
@files[node.app_file.name].all_captures[node.value.instance_variable_get(:@to)] = node
|
83
80
|
end
|
84
81
|
|
85
82
|
def on_parse_json(node)
|
86
|
-
return if ignore?(node)
|
87
|
-
|
88
83
|
@files[node.app_file.name].all_captures[node.value.to] = node
|
89
84
|
end
|
90
85
|
|
91
86
|
def on_for(node)
|
92
|
-
return if ignore?(node)
|
93
|
-
|
94
87
|
@files[node.app_file.name].all_forloops[node.value.variable_name] = node
|
95
88
|
end
|
96
89
|
|
@@ -99,7 +92,6 @@ module PlatformosCheck
|
|
99
92
|
end
|
100
93
|
|
101
94
|
def on_render(node)
|
102
|
-
return if ignore?(node)
|
103
95
|
return unless node.value.template_name_expr.is_a?(String)
|
104
96
|
|
105
97
|
partial_name = node.value.template_name_expr
|
@@ -110,45 +102,52 @@ module PlatformosCheck
|
|
110
102
|
end
|
111
103
|
|
112
104
|
def on_function(node)
|
113
|
-
|
105
|
+
@files[node.app_file.name].all_assigns[node.value.to] = node
|
106
|
+
|
107
|
+
return unless node.value.from.is_a?(String)
|
114
108
|
|
115
|
-
name = node.value.from.is_a?(String) ? node.value.from : node.value.from.name
|
116
109
|
@files[node.app_file.name].add_render(
|
117
|
-
name
|
110
|
+
name: node.value.from,
|
118
111
|
node:
|
119
112
|
)
|
120
|
-
|
121
|
-
@files[node.app_file.name].all_assigns[node.value.to] = node
|
122
113
|
end
|
123
114
|
|
124
115
|
def on_graphql(node)
|
125
|
-
return if ignore?(node)
|
126
|
-
|
127
116
|
@files[node.app_file.name].all_assigns[node.value.to] = node
|
128
117
|
end
|
129
118
|
|
130
119
|
def on_background(node)
|
131
|
-
return if ignore?(node)
|
132
120
|
return unless node.value.partial_syntax
|
121
|
+
|
122
|
+
@files[node.app_file.name].all_assigns[node.value.to] = node
|
123
|
+
|
133
124
|
return unless node.value.partial_name.is_a?(String)
|
134
125
|
|
135
126
|
@files[node.app_file.name].add_render(
|
136
127
|
name: node.value.partial_name,
|
137
128
|
node:
|
138
129
|
)
|
139
|
-
|
140
|
-
@files[node.app_file.name].all_assigns[node.value.to] = node
|
141
130
|
end
|
142
131
|
|
143
132
|
def on_variable_lookup(node)
|
144
|
-
return if ignore?(node)
|
145
|
-
|
146
133
|
@files[node.app_file.name].add_variable_lookup(
|
147
134
|
name: node.value.name,
|
148
135
|
node:
|
149
136
|
)
|
150
137
|
end
|
151
138
|
|
139
|
+
def single_file_end_dependencies(app_file)
|
140
|
+
@files[app_file.name].all_renders.keys.map do |partial_name|
|
141
|
+
next if @files[partial_name]
|
142
|
+
|
143
|
+
partial_file = @platformos_app.partials.detect { |p| p.name == partial_name } # NOTE: undefined partial
|
144
|
+
|
145
|
+
next unless partial_file
|
146
|
+
|
147
|
+
partial_file
|
148
|
+
end.compact
|
149
|
+
end
|
150
|
+
|
152
151
|
def on_end
|
153
152
|
all_global_objects = PlatformosCheck::PlatformosLiquid::Object.labels
|
154
153
|
all_global_objects.freeze
|
@@ -170,31 +169,28 @@ module PlatformosCheck
|
|
170
169
|
|
171
170
|
attr_reader :config_type
|
172
171
|
|
173
|
-
def ignore?(node)
|
174
|
-
@exclude_partials && node.app_file.partial?
|
175
|
-
end
|
176
|
-
|
177
172
|
def each_template
|
178
173
|
@files.each do |(name, info)|
|
179
|
-
next if info.app_file.partial?
|
180
|
-
|
181
174
|
yield [name, info]
|
182
175
|
end
|
183
176
|
end
|
184
177
|
|
185
|
-
def check_object(info, all_global_objects, render_node = nil, visited_partials = Set.new)
|
186
|
-
|
178
|
+
def check_object(info, all_global_objects, render_node = nil, visited_partials = Set.new, level = 0)
|
179
|
+
return if level > 1
|
180
|
+
|
181
|
+
check_undefined(info, all_global_objects, render_node) unless info.app_file.partial? && render_node.nil? # ||
|
187
182
|
|
188
183
|
info.each_partial do |(partial_name, node)|
|
184
|
+
next if visited_partials.include?(partial_name)
|
185
|
+
|
189
186
|
partial_info = @files[partial_name]
|
187
|
+
|
190
188
|
next unless partial_info # NOTE: undefined partial
|
191
189
|
|
192
190
|
partial_variables = node.value.attributes.keys +
|
193
191
|
[node.value.instance_variable_get(:@alias_name)]
|
194
|
-
|
195
|
-
|
196
|
-
check_object(partial_info, all_global_objects + partial_variables, node, visited_partials)
|
197
|
-
end
|
192
|
+
visited_partials << partial_name
|
193
|
+
check_object(partial_info, all_global_objects + partial_variables, node, visited_partials, level + 1)
|
198
194
|
end
|
199
195
|
end
|
200
196
|
|
@@ -213,7 +209,7 @@ module PlatformosCheck
|
|
213
209
|
|
214
210
|
if render_node
|
215
211
|
add_offense("Missing argument `#{name}`", node: render_node)
|
216
|
-
|
212
|
+
elsif !info.app_file.partial?
|
217
213
|
add_offense("Undefined object `#{name}`", node:, line_number:)
|
218
214
|
end
|
219
215
|
end
|
@@ -25,6 +25,12 @@ module PlatformosCheck
|
|
25
25
|
@single_file ||= self.class.new(select(&:single_file?))
|
26
26
|
end
|
27
27
|
|
28
|
+
def single_file_end_dependencies(app_file)
|
29
|
+
map do |check|
|
30
|
+
check.respond_to?(:single_file_end_dependencies) ? check.single_file_end_dependencies(app_file) : []
|
31
|
+
end.flatten.compact.uniq
|
32
|
+
end
|
33
|
+
|
28
34
|
private
|
29
35
|
|
30
36
|
def call_check_method(check, method, *args)
|
@@ -4,13 +4,20 @@ module PlatformosCheck
|
|
4
4
|
class LiquidVisitor
|
5
5
|
attr_reader :checks
|
6
6
|
|
7
|
-
def initialize(checks, disabled_checks)
|
7
|
+
def initialize(checks, disabled_checks, only_single_file: false)
|
8
8
|
@checks = checks
|
9
9
|
@disabled_checks = disabled_checks
|
10
|
+
@only_single_file = only_single_file
|
10
11
|
end
|
11
12
|
|
12
13
|
def visit_liquid_file(liquid_file)
|
13
14
|
visit(LiquidNode.new(liquid_file.root, nil, liquid_file))
|
15
|
+
|
16
|
+
if @only_single_file
|
17
|
+
checks.single_file_end_dependencies(liquid_file).each do |file|
|
18
|
+
visit(LiquidNode.new(file.root, nil, file))
|
19
|
+
end
|
20
|
+
end
|
14
21
|
rescue Liquid::Error => e
|
15
22
|
e.template_name = liquid_file.relative_path
|
16
23
|
call_checks(:on_error, e)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: platformos-check
|
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
|
- Piotr Bliszczyk
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: exe
|
12
12
|
cert_chain: []
|
13
|
-
date: 2023-09-
|
13
|
+
date: 2023-09-14 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: graphql
|