import_js 0.1.5 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/import-js +1 -4
- data/lib/import_js/emacs_editor.rb +7 -1
- data/lib/import_js/importer.rb +13 -25
- 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: 86109b8414f1f1331814080ff3de584129f4dd59
|
4
|
+
data.tar.gz: 671e9ed2c481e73f359c70552e4bec13b42cd4f8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6bd23adbe12eb5db1232d685eee4021955b2c0190a977b844b8cc0b19ffb4d15de1e8dd8d9296e5627b9b1de96962fe8b07ff6a5204a3ca8ddf2e5db2db34917
|
7
|
+
data.tar.gz: 5e6e62dba0ce0f132c2b8bd0e7ab5de8202239195424a3726156235eac0041936fc55cd3ef7781da5b18e0e2052011542d0ba7bf1e0010ca5061a3c5c06bb7ad
|
data/bin/import-js
CHANGED
@@ -7,7 +7,6 @@ require 'json'
|
|
7
7
|
opts = Slop.parse do |o|
|
8
8
|
o.string '-w', '--word', 'a word/variable to import'
|
9
9
|
o.bool '--goto', 'instead of importing, just print the path to a module'
|
10
|
-
o.bool '--fix', 'imports all undefined variables, and removes unused imports'
|
11
10
|
o.array '--selections', 'a list of resolved selections, e.g. Foo:0,Bar:1'
|
12
11
|
o.string '--filename',
|
13
12
|
'a path to the file which contents are being passed in as stdin'
|
@@ -37,10 +36,8 @@ if opts.goto?
|
|
37
36
|
importer.goto
|
38
37
|
elsif opts[:word]
|
39
38
|
importer.import
|
40
|
-
elsif opts.fix?
|
41
|
-
importer.fix_imports
|
42
39
|
else
|
43
|
-
importer.
|
40
|
+
importer.fix_imports
|
44
41
|
end
|
45
42
|
|
46
43
|
if opts.goto?
|
@@ -22,6 +22,12 @@ class ImportJS::EmacsEditor
|
|
22
22
|
puts 'import:success'
|
23
23
|
when 'goto'
|
24
24
|
ImportJS::Importer.new(self).goto
|
25
|
+
when 'fix'
|
26
|
+
ImportJS::Importer.new(self).fix_imports
|
27
|
+
write_file
|
28
|
+
puts 'import:success'
|
29
|
+
else
|
30
|
+
puts "unknown command: #{command}"
|
25
31
|
end
|
26
32
|
rescue Exception => e
|
27
33
|
puts e.inspect
|
@@ -61,7 +67,7 @@ class ImportJS::EmacsEditor
|
|
61
67
|
#
|
62
68
|
# @return [String]
|
63
69
|
def current_file_content
|
64
|
-
@file.join(
|
70
|
+
@file.join("\n")
|
65
71
|
end
|
66
72
|
|
67
73
|
# Reads a line from the file.
|
data/lib/import_js/importer.rb
CHANGED
@@ -47,36 +47,17 @@ module ImportJS
|
|
47
47
|
@editor.open_file(js_module.file_path) if js_module
|
48
48
|
end
|
49
49
|
|
50
|
+
# Removes unused imports and adds imports for undefined variables
|
50
51
|
def fix_imports
|
51
|
-
remove_unused_imports
|
52
|
-
import_all
|
53
|
-
end
|
54
|
-
|
55
|
-
# Finds all variables that haven't yet been imported.
|
56
|
-
def import_all
|
57
52
|
@config.refresh
|
58
|
-
|
53
|
+
eslint_result = run_eslint_command
|
54
|
+
undefined_variables = eslint_result.map do |line|
|
59
55
|
/(["'])([^"']+)\1 is not defined/.match(line) do |match_data|
|
60
56
|
match_data[2]
|
61
57
|
end
|
62
58
|
end.compact.uniq
|
63
59
|
|
64
|
-
|
65
|
-
|
66
|
-
old_imports = find_current_imports
|
67
|
-
undefined_variables.each do |variable|
|
68
|
-
if js_module = find_one_js_module(variable)
|
69
|
-
inject_js_module(variable, js_module, old_imports[:imports])
|
70
|
-
end
|
71
|
-
end
|
72
|
-
replace_imports(old_imports[:newline_count],
|
73
|
-
old_imports[:imports],
|
74
|
-
old_imports[:imports_start_at])
|
75
|
-
end
|
76
|
-
|
77
|
-
def remove_unused_imports
|
78
|
-
@config.refresh
|
79
|
-
unused_variables = run_eslint_command.map do |line|
|
60
|
+
unused_variables = eslint_result.map do |line|
|
80
61
|
/"([^"]+)" is defined but never used/.match(line) do |match_data|
|
81
62
|
match_data[1]
|
82
63
|
end
|
@@ -89,6 +70,13 @@ module ImportJS
|
|
89
70
|
end
|
90
71
|
import_statement.variables.empty?
|
91
72
|
end
|
73
|
+
|
74
|
+
undefined_variables.each do |variable|
|
75
|
+
if js_module = find_one_js_module(variable)
|
76
|
+
inject_js_module(variable, js_module, new_imports)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
92
80
|
replace_imports(old_imports[:newline_count],
|
93
81
|
new_imports,
|
94
82
|
old_imports[:imports_start_at])
|
@@ -105,14 +93,14 @@ module ImportJS
|
|
105
93
|
command = %w[
|
106
94
|
eslint
|
107
95
|
--stdin
|
108
|
-
--format
|
96
|
+
--format unix
|
109
97
|
--rule 'no-undef: 2'
|
110
98
|
--rule 'no-unused-vars: [2, { "vars": "all", "args": "none" }]'
|
111
99
|
].join(' ')
|
112
100
|
out, err = Open3.capture3(command,
|
113
101
|
stdin_data: @editor.current_file_content)
|
114
102
|
|
115
|
-
if out =~ /
|
103
|
+
if out =~ /Parsing error: / ||
|
116
104
|
out =~ /Unrecoverable syntax error/
|
117
105
|
fail ImportJS::ParseError.new, out
|
118
106
|
end
|
data/lib/import_js/version.rb
CHANGED