react-manifest-rails 0.2.13 → 0.2.14
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/lib/react_manifest/scanner.rb +57 -34
- data/lib/react_manifest/version.rb +1 -1
- data/lib/react_manifest.rb +5 -0
- 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: 9c85309337765f3f24fae46e4cf3895e11e4a3a59b5cbfb96fa90f296f3ba48e
|
|
4
|
+
data.tar.gz: b0f320a6ebf2a4210a4fac89d04f5609bd3d261a458f2db26fb92dd814b45465
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 85edbe32bf32f739097e60779a34874d1c792db5ea40ed41e671e8b0552718d4549dfddb9b120d74bcce42a15bbc598ed0384c7e609cb6f0d4044fb80900098c
|
|
7
|
+
data.tar.gz: d2728828f1fd930cd702aa35fb4ab5ab88b4219dff1fe996fa5cdb8491888b15bb25b0fd1d44c84aa9c0707cd80b87d3d3c2554b15f0723fd979bf484db4b3f1
|
|
@@ -41,6 +41,9 @@ module ReactManifest
|
|
|
41
41
|
# Patterns to detect usage in controller files
|
|
42
42
|
JSX_ELEMENT_PATTERN = %r{<([A-Z][A-Za-z0-9_]*)[\s/>]}
|
|
43
43
|
REACT_CREATE_PATTERN = /React\.createElement\(\s*([A-Z][A-Za-z0-9_]*)[\s,)]/
|
|
44
|
+
JSX_PROP_COMPONENT_PATTERN = /[A-Za-z_][A-Za-z0-9_]*\s*=\s*\{\s*([A-Z][A-Za-z0-9_]*)\s*\}/
|
|
45
|
+
OBJECT_COMPONENT_PATTERN = /:\s*([A-Z][A-Za-z0-9_]*)\b/
|
|
46
|
+
ARRAY_COMPONENT_LIST_PATTERN = /\[\s*([A-Z][A-Za-z0-9_]*(?:\s*,\s*[A-Z][A-Za-z0-9_]*)*\s*,?)\s*\]/
|
|
44
47
|
HOOK_CALL_PATTERN = /\b(use[A-Z][A-Za-z0-9_]*)\s*\(/
|
|
45
48
|
# Lib calls matched against known lib symbols to reduce false positives
|
|
46
49
|
LIB_CALL_PATTERN = /\b([a-z][A-Za-z0-9_]{2,})\s*\(/
|
|
@@ -93,41 +96,10 @@ module ReactManifest
|
|
|
93
96
|
|
|
94
97
|
files.each do |file_path|
|
|
95
98
|
validate_naming(file_path, ctrl[:name], warnings)
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
rescue Errno::ENOENT, Errno::EACCES => e
|
|
99
|
-
warnings << "Skipping #{file_path}: #{e.message}"
|
|
100
|
-
next
|
|
101
|
-
rescue Encoding::InvalidByteSequenceError
|
|
102
|
-
warnings << "Skipping #{file_path}: not valid UTF-8"
|
|
103
|
-
next
|
|
104
|
-
end
|
|
105
|
-
|
|
106
|
-
# JSX element usage: <PrimaryButton (JSX tag syntax)
|
|
107
|
-
content.scan(JSX_ELEMENT_PATTERN) do |match|
|
|
108
|
-
sym = match[0]
|
|
109
|
-
used << symbol_index[sym] if symbol_index.key?(sym)
|
|
110
|
-
end
|
|
111
|
-
|
|
112
|
-
# React.createElement(PrimaryButton, ...) (non-JSX style)
|
|
113
|
-
content.scan(REACT_CREATE_PATTERN) do |match|
|
|
114
|
-
sym = match[0]
|
|
115
|
-
used << symbol_index[sym] if symbol_index.key?(sym)
|
|
116
|
-
end
|
|
117
|
-
|
|
118
|
-
# Hook calls: useFetch(
|
|
119
|
-
content.scan(HOOK_CALL_PATTERN) do |match|
|
|
120
|
-
sym = match[0]
|
|
121
|
-
used << symbol_index[sym] if symbol_index.key?(sym)
|
|
122
|
-
end
|
|
99
|
+
content = read_controller_file(file_path, warnings)
|
|
100
|
+
next unless content
|
|
123
101
|
|
|
124
|
-
|
|
125
|
-
content.scan(LIB_CALL_PATTERN) do |match|
|
|
126
|
-
sym = match[0]
|
|
127
|
-
next if JS_BUILTINS.include?(sym)
|
|
128
|
-
|
|
129
|
-
used << symbol_index[sym] if symbol_index.key?(sym)
|
|
130
|
-
end
|
|
102
|
+
used.merge(extract_used_shared_paths(content, symbol_index))
|
|
131
103
|
end
|
|
132
104
|
|
|
133
105
|
controller_usages[ctrl[:name]] = used.to_a.sort
|
|
@@ -204,5 +176,56 @@ module ReactManifest
|
|
|
204
176
|
end
|
|
205
177
|
end
|
|
206
178
|
end
|
|
179
|
+
|
|
180
|
+
def read_controller_file(file_path, warnings)
|
|
181
|
+
File.read(file_path, encoding: "utf-8")
|
|
182
|
+
rescue Errno::ENOENT, Errno::EACCES => e
|
|
183
|
+
warnings << "Skipping #{file_path}: #{e.message}"
|
|
184
|
+
nil
|
|
185
|
+
rescue Encoding::InvalidByteSequenceError
|
|
186
|
+
warnings << "Skipping #{file_path}: not valid UTF-8"
|
|
187
|
+
nil
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
def extract_used_shared_paths(content, symbol_index)
|
|
191
|
+
used = Set.new
|
|
192
|
+
|
|
193
|
+
scan_component_usage(content, JSX_ELEMENT_PATTERN, symbol_index, used)
|
|
194
|
+
scan_component_usage(content, REACT_CREATE_PATTERN, symbol_index, used)
|
|
195
|
+
scan_component_usage(content, JSX_PROP_COMPONENT_PATTERN, symbol_index, used)
|
|
196
|
+
scan_component_usage(content, OBJECT_COMPONENT_PATTERN, symbol_index, used)
|
|
197
|
+
scan_array_component_usage(content, symbol_index, used)
|
|
198
|
+
scan_component_usage(content, HOOK_CALL_PATTERN, symbol_index, used)
|
|
199
|
+
|
|
200
|
+
content.scan(LIB_CALL_PATTERN) do |match|
|
|
201
|
+
sym = match[0]
|
|
202
|
+
next if JS_BUILTINS.include?(sym)
|
|
203
|
+
next unless symbol_index.key?(sym)
|
|
204
|
+
|
|
205
|
+
used << symbol_index[sym]
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
used
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
def scan_component_usage(content, pattern, symbol_index, used)
|
|
212
|
+
content.scan(pattern) do |match|
|
|
213
|
+
sym = match[0]
|
|
214
|
+
next unless symbol_index.key?(sym)
|
|
215
|
+
|
|
216
|
+
used << symbol_index[sym]
|
|
217
|
+
end
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
def scan_array_component_usage(content, symbol_index, used)
|
|
221
|
+
content.scan(ARRAY_COMPONENT_LIST_PATTERN) do |match|
|
|
222
|
+
list = match[0]
|
|
223
|
+
list.split(/\s*,\s*/).each do |sym|
|
|
224
|
+
next unless symbol_index.key?(sym)
|
|
225
|
+
|
|
226
|
+
used << symbol_index[sym]
|
|
227
|
+
end
|
|
228
|
+
end
|
|
229
|
+
end
|
|
207
230
|
end
|
|
208
231
|
end
|
data/lib/react_manifest.rb
CHANGED
|
@@ -169,6 +169,11 @@ module ReactManifest
|
|
|
169
169
|
|
|
170
170
|
content.scan(ReactManifest::Scanner::JSX_ELEMENT_PATTERN) { |m| symbols << m[0] }
|
|
171
171
|
content.scan(ReactManifest::Scanner::REACT_CREATE_PATTERN) { |m| symbols << m[0] }
|
|
172
|
+
content.scan(ReactManifest::Scanner::JSX_PROP_COMPONENT_PATTERN) { |m| symbols << m[0] }
|
|
173
|
+
content.scan(ReactManifest::Scanner::OBJECT_COMPONENT_PATTERN) { |m| symbols << m[0] }
|
|
174
|
+
content.scan(ReactManifest::Scanner::ARRAY_COMPONENT_LIST_PATTERN) do |m|
|
|
175
|
+
m[0].split(/\s*,\s*/).each { |sym| symbols << sym }
|
|
176
|
+
end
|
|
172
177
|
|
|
173
178
|
symbols.uniq
|
|
174
179
|
rescue Errno::ENOENT, Errno::EACCES, Encoding::InvalidByteSequenceError
|