Skip to content

Commit 2cf40b3

Browse files
committed
Merge pull request #95 from trotzig/remove-import_all
Remove import all
2 parents 8970b98 + c763e98 commit 2cf40b3

File tree

10 files changed

+48
-95
lines changed

10 files changed

+48
-95
lines changed

Default.sublime-commands

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
[
2-
{
3-
"caption": "ImportJS: import all dependencies",
4-
"command": "import_js"
5-
},
62
{
73
"caption": "ImportJS: fix all imports",
84
"command": "import_js",

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,11 @@ a `require` statement. But keep reading for some more neat features.
7171
## Fix imports
7272

7373
If you have [eslint](http://eslint.org/) installed, import-js can be used to
74-
automatically fix all imports. By hiting `<leader>i` (Vim), all your undefined
75-
variables will be resolved, and all your unused imports will be removed. By
76-
default, import-js expects a global `eslint` command to be available.
74+
automatically fix all imports. By hiting `<leader>i` (Vim), `(M-x)
75+
import-js-fix` (Emacs), or choose `ImportJS: fix all imports` (Sublime), all
76+
your undefined variables will be resolved, and all your unused imports will be
77+
removed. By default, import-js expects a global `eslint` command to be
78+
available.
7779

7880
## Experimental: Go to module
7981

SUBLIME.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
5. Open the root of your project as a folder (Project -> Add Folder to Project…)
1212
6. Import a file!
1313
* Whenever you have undefined variables, open the Command Palette
14-
(CTRL/CMD+SHIFT+P) and select "ImportJS: import all dependencies",
15-
"ImportJS: fix all imports" or "ImportJS: import word under cursor".
14+
(CTRL/CMD+SHIFT+P) and select "ImportJS: fix all imports",
15+
or "ImportJS: import word under cursor".
1616
* It will be helpful to bind `import_js` to easy-to-use bindings,
1717
such as:
1818

1919
```
20-
{ "keys": ["super+alt+i"], "command": "import_js", "args": {"fix": true} },
21-
{ "keys": ["super+alt+j"], "command": "import_js", "args": {"word": true} },
22-
{ "keys": ["super+alt+g"], "command": "import_js", "args": {"word": true, "goto": true} },
20+
{ "keys": ["super+alt+i"], "command": "import_js" },
21+
{ "keys": ["super+alt+j"], "command": "import_js", "args": { "word": true } },
22+
{ "keys": ["super+alt+g"], "command": "import_js", "args": { "word": true, "goto": true } },
2323
```

autoload/importjs.vim

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
function importjs#ImportJSImport()
22
ruby $import_js.import
33
endfunction
4-
function importjs#ImportJSImportAll()
5-
ruby $import_js.import_all
64
endfunction
75
function importjs#ImportJSGoTo()
86
ruby $import_js.goto

bin/import-js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ require 'json'
77
opts = Slop.parse do |o|
88
o.string '-w', '--word', 'a word/variable to import'
99
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'
1110
o.array '--selections', 'a list of resolved selections, e.g. Foo:0,Bar:1'
1211
o.string '--filename',
1312
'a path to the file which contents are being passed in as stdin'
@@ -37,10 +36,8 @@ if opts.goto?
3736
importer.goto
3837
elsif opts[:word]
3938
importer.import
40-
elsif opts.fix?
41-
importer.fix_imports
4239
else
43-
importer.import_all
40+
importer.fix_imports
4441
end
4542

4643
if opts.goto?

import-js.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,5 @@
11
import sublime, sublime_plugin, subprocess, os, json
22

3-
def plugin_loaded():
4-
settings = sublime.load_settings('ImportJS.sublime-settings')
5-
executable = os.path.expanduser(settings.get('executable'))
6-
try:
7-
version = subprocess.check_output(
8-
[executable, '--version']) .decode('UTF-8').strip()
9-
except FileNotFoundError:
10-
print(no_executable_error(executable))
11-
12-
version_rb = os.path.join(os.path.dirname(__file__), 'lib/import_js/version.rb')
13-
with open(version_rb) as f:
14-
contents = f.read()
15-
16-
if("VERSION = '" + version + "'" not in contents):
17-
print('The installed version of import-js does not match ' +
18-
'the current version: ' + version)
19-
20-
213
def no_executable_error(executable):
224
return (
235
"Couldn't find executable "

lib/import_js/importer.rb

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,36 +47,17 @@ def goto
4747
@editor.open_file(js_module.file_path) if js_module
4848
end
4949

50+
# Removes unused imports and adds imports for undefined variables
5051
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
5752
@config.refresh
58-
undefined_variables = run_eslint_command.map do |line|
53+
eslint_result = run_eslint_command
54+
undefined_variables = eslint_result.map do |line|
5955
/(["'])([^"']+)\1 is not defined/.match(line) do |match_data|
6056
match_data[2]
6157
end
6258
end.compact.uniq
6359

64-
return message('No variables to import') if undefined_variables.empty?
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|
8061
/"([^"]+)" is defined but never used/.match(line) do |match_data|
8162
match_data[1]
8263
end
@@ -89,6 +70,13 @@ def remove_unused_imports
8970
end
9071
import_statement.variables.empty?
9172
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+
9280
replace_imports(old_imports[:newline_count],
9381
new_imports,
9482
old_imports[:imports_start_at])

lib/import_js/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Defines the gem version.
22
module ImportJS
3-
VERSION = '0.1.5'
3+
VERSION = '0.2.0'
44
end

plugin/import-js.vim

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ endif
44
let g:import_js_loaded = 1
55

66
command ImportJSImport call importjs#ImportJSImport()
7-
command ImportJSImportAll call importjs#ImportJSImportAll()
87
command ImportJSGoTo call importjs#ImportJSGoTo()
98
command ImportJSRemoveUnusedImports call importjs#ImportJSRemoveUnusedImports()
109
command ImportJSFixImports call importjs#ImportJSFixImports()

spec/import_js/importer_spec.rb

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,7 +1191,7 @@ def self.current_selection=(index)
11911191
end
11921192
end
11931193

1194-
describe '#import_all' do
1194+
describe '#fix_imports' do
11951195
let(:eslint_result) { '' }
11961196
before do
11971197
allow(Open3).to receive(:capture3).and_call_original
@@ -1200,21 +1200,14 @@ def self.current_selection=(index)
12001200
end
12011201

12021202
subject do
1203-
ImportJS::Importer.new.import_all
1203+
ImportJS::Importer.new.fix_imports
12041204
VIM::Buffer.current_buffer.to_s
12051205
end
12061206

12071207
context 'when no undefined variables exist' do
12081208
it 'leaves the buffer unchanged' do
12091209
expect(subject).to eq(text)
12101210
end
1211-
1212-
it 'displays a message' do
1213-
subject
1214-
expect(VIM.last_command_message).to eq(
1215-
'ImportJS: No variables to import'
1216-
)
1217-
end
12181211
end
12191212

12201213
context 'when eslint can not parse' do
@@ -1312,38 +1305,14 @@ def self.current_selection=(index)
13121305
EOS
13131306
end
13141307
end
1315-
end
1316-
1317-
describe '#remove_unused_imports' do
1318-
let(:eslint_result) { '' }
1319-
before do
1320-
allow(Open3).to receive(:capture3).and_call_original
1321-
allow(Open3).to receive(:capture3).with(/eslint/, anything)
1322-
.and_return([eslint_result, nil])
1323-
end
1324-
1325-
subject do
1326-
ImportJS::Importer.new.remove_unused_imports
1327-
VIM::Buffer.current_buffer.to_s
1328-
end
13291308

13301309
context 'when no unused variables exist' do
13311310
it 'leaves the buffer unchanged' do
13321311
expect(subject).to eq(text)
13331312
end
13341313
end
13351314

1336-
context 'when eslint can not parse' do
1337-
let(:eslint_result) do
1338-
'stdin:1:1: Parsing error: Unexpected token ILLEGAL [Error]'
1339-
end
1340-
1341-
it 'throws an error' do
1342-
expect { subject }.to raise_error(ImportJS::ParseError)
1343-
end
1344-
end
1345-
1346-
context 'when one unused variable exists' do
1315+
context 'when one unused import exists' do
13471316
let(:text) { <<-EOS.strip }
13481317
var bar = require('foo/bar');
13491318
var foo = require('bar/foo');
@@ -1386,6 +1355,28 @@ def self.current_selection=(index)
13861355
end
13871356
end
13881357

1358+
context 'when an unused import and an undefined variable exists' do
1359+
let(:existing_files) { ['bar/foo.jsx'] }
1360+
let(:text) { <<-EOS.strip }
1361+
var bar = require('foo/bar');
1362+
1363+
foo
1364+
EOS
1365+
1366+
let(:eslint_result) do
1367+
"stdin:3:11: \"bar\" is defined but never used [Error/no-unused-vars]\n" \
1368+
"stdin:3:11: \"foo\" is not defined. [Error/no-undef]"
1369+
end
1370+
1371+
it 'removes the unused import and adds the missing one' do
1372+
expect(subject).to eq(<<-EOS.strip)
1373+
var foo = require('bar/foo');
1374+
1375+
foo
1376+
EOS
1377+
end
1378+
end
1379+
13891380
context 'when a destructured import has an unused variable' do
13901381
let(:text) { <<-EOS.strip }
13911382
var { bar, foo } = require('baz');

0 commit comments

Comments
 (0)