@@ -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 }
13481317var bar = require('foo/bar');
13491318var 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 }
13911382var { bar, foo } = require('baz');
0 commit comments