import_js 0.5.0 → 0.5.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/lib/import_js/import_statement.rb +1 -1
- data/lib/import_js/importer.rb +39 -13
- data/lib/import_js/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cd6adff72ccc7afa4dc2e926b56398d666942449
|
4
|
+
data.tar.gz: 1190a25ee2e7e0af64a7de229ab279bebded2951
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f6d1a84089692701188e7cd7c18131e980f7f81ed8c9c785c1552fdcd625c006a8cd3ab4b89407e71370a47b5e1eba6f61d2076c43bac362da0262e3f791084d
|
7
|
+
data.tar.gz: 576275490822799e44ad5427c4512169a7f279876c56dac305739fa4bde0ce7dce0de0aee5c7984a8e2aa88ebe311c4825e7c09137ce5f6b8441c8b582fca48e
|
data/lib/import_js/importer.rb
CHANGED
@@ -55,6 +55,10 @@ module ImportJS
|
|
55
55
|
end
|
56
56
|
|
57
57
|
REGEX_ESLINT_RESULT = /
|
58
|
+
:
|
59
|
+
(?<line>\d+) # <line> line number
|
60
|
+
:\d+:
|
61
|
+
\s
|
58
62
|
(?<quote>["']) # <quote> opening quote
|
59
63
|
(?<variable_name>[^\1]+) # <variable_name>
|
60
64
|
\k<quote>
|
@@ -73,22 +77,33 @@ module ImportJS
|
|
73
77
|
reload_config
|
74
78
|
eslint_result = run_eslint_command
|
75
79
|
|
76
|
-
|
80
|
+
return if eslint_result.empty?
|
81
|
+
|
82
|
+
unused_variables = {}
|
77
83
|
undefined_variables = Set.new
|
78
84
|
|
79
85
|
eslint_result.each do |line|
|
80
86
|
match = REGEX_ESLINT_RESULT.match(line)
|
81
87
|
next unless match
|
82
88
|
if match[:type] == 'is defined but never used'
|
83
|
-
unused_variables
|
89
|
+
unused_variables[match[:variable_name]] ||= Set.new
|
90
|
+
unused_variables[match[:variable_name]].add match[:line].to_i
|
84
91
|
else
|
85
92
|
undefined_variables.add match[:variable_name]
|
86
93
|
end
|
87
94
|
end
|
88
95
|
|
96
|
+
return if unused_variables.empty? && undefined_variables.empty?
|
97
|
+
|
89
98
|
old_imports = find_current_imports
|
99
|
+
|
100
|
+
# Filter out unused variables that do not appear within the imports block.
|
101
|
+
unused_variables.select! do |_, line_numbers|
|
102
|
+
any_numbers_within_range?(line_numbers, old_imports[:range])
|
103
|
+
end
|
104
|
+
|
90
105
|
new_imports = old_imports[:imports].clone
|
91
|
-
new_imports.delete_variables!(unused_variables.
|
106
|
+
new_imports.delete_variables!(unused_variables.keys)
|
92
107
|
|
93
108
|
undefined_variables.each do |variable|
|
94
109
|
js_module = find_one_js_module(variable)
|
@@ -130,6 +145,16 @@ module ImportJS
|
|
130
145
|
@editor.message("ImportJS: #{str}")
|
131
146
|
end
|
132
147
|
|
148
|
+
# @param numbers [Set]
|
149
|
+
# @param range [Range]
|
150
|
+
# @return [Boolean]
|
151
|
+
def any_numbers_within_range?(numbers, range)
|
152
|
+
numbers.each do |number|
|
153
|
+
return true if range.include?(number)
|
154
|
+
end
|
155
|
+
false
|
156
|
+
end
|
157
|
+
|
133
158
|
ESLINT_STDOUT_ERROR_REGEXES = [
|
134
159
|
/Parsing error: /,
|
135
160
|
/Unrecoverable syntax error/,
|
@@ -200,15 +225,15 @@ module ImportJS
|
|
200
225
|
|
201
226
|
# Ensure that there is a blank line after the block of all imports
|
202
227
|
if old_imports_range.size + import_strings.length > 0 &&
|
203
|
-
!@editor.read_line(old_imports_range.last
|
204
|
-
@editor.append_line(old_imports_range.last, '')
|
228
|
+
!@editor.read_line(old_imports_range.last).strip.empty?
|
229
|
+
@editor.append_line(old_imports_range.last - 1, '')
|
205
230
|
end
|
206
231
|
|
207
232
|
# Find old import strings so we can compare with the new import strings
|
208
233
|
# and see if anything has changed.
|
209
234
|
old_import_strings = []
|
210
|
-
old_imports_range.each do |
|
211
|
-
old_import_strings << @editor.read_line(
|
235
|
+
old_imports_range.each do |line_number|
|
236
|
+
old_import_strings << @editor.read_line(line_number)
|
212
237
|
end
|
213
238
|
|
214
239
|
# If nothing has changed, bail to prevent unnecessarily dirtying the
|
@@ -217,17 +242,17 @@ module ImportJS
|
|
217
242
|
|
218
243
|
# Delete old imports, then add the modified list back in.
|
219
244
|
old_imports_range.each do
|
220
|
-
@editor.delete_line(
|
245
|
+
@editor.delete_line(old_imports_range.first)
|
221
246
|
end
|
222
247
|
import_strings.reverse_each do |import_string|
|
223
248
|
# We need to add each line individually because the Vim buffer will
|
224
249
|
# convert newline characters to `~@`.
|
225
250
|
if import_string.include? "\n"
|
226
251
|
import_string.split("\n").reverse_each do |line|
|
227
|
-
@editor.append_line(old_imports_range.first, line)
|
252
|
+
@editor.append_line(old_imports_range.first - 1, line)
|
228
253
|
end
|
229
254
|
else
|
230
|
-
@editor.append_line(old_imports_range.first, import_string)
|
255
|
+
@editor.append_line(old_imports_range.first - 1, import_string)
|
231
256
|
end
|
232
257
|
end
|
233
258
|
end
|
@@ -248,7 +273,7 @@ module ImportJS
|
|
248
273
|
|
249
274
|
# @return [Hash]
|
250
275
|
def find_current_imports
|
251
|
-
|
276
|
+
imports_start_at_line_number = 1
|
252
277
|
newline_count = 0
|
253
278
|
|
254
279
|
scanner = StringScanner.new(@editor.current_file_content)
|
@@ -261,7 +286,7 @@ module ImportJS
|
|
261
286
|
if skipped =~ /\A(\s*\n)+\Z/m
|
262
287
|
scanner = StringScanner.new(@editor.current_file_content)
|
263
288
|
else
|
264
|
-
|
289
|
+
imports_start_at_line_number += skipped.count("\n")
|
265
290
|
end
|
266
291
|
|
267
292
|
imports = ImportStatements.new(@config)
|
@@ -273,9 +298,10 @@ module ImportJS
|
|
273
298
|
newline_count += potential_import.scan(/\n/).length
|
274
299
|
end
|
275
300
|
|
301
|
+
imports_end_at_line_number = imports_start_at_line_number + newline_count
|
276
302
|
{
|
277
303
|
imports: imports,
|
278
|
-
range:
|
304
|
+
range: imports_start_at_line_number...imports_end_at_line_number,
|
279
305
|
}
|
280
306
|
end
|
281
307
|
|
data/lib/import_js/version.rb
CHANGED