Skip to content

Commit

Permalink
Merge pull request #95 from trotzig/remove-import_all
Browse files Browse the repository at this point in the history
Remove import all
  • Loading branch information
trotzig committed Jan 4, 2016
2 parents 8970b98 + c763e98 commit 2cf40b3
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 95 deletions.
4 changes: 0 additions & 4 deletions Default.sublime-commands
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
[
{
"caption": "ImportJS: import all dependencies",
"command": "import_js"
},
{
"caption": "ImportJS: fix all imports",
"command": "import_js",
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ a `require` statement. But keep reading for some more neat features.
## Fix imports

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

## Experimental: Go to module

Expand Down
10 changes: 5 additions & 5 deletions SUBLIME.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
5. Open the root of your project as a folder (Project -> Add Folder to Project…)
6. Import a file!
* Whenever you have undefined variables, open the Command Palette
(CTRL/CMD+SHIFT+P) and select "ImportJS: import all dependencies",
"ImportJS: fix all imports" or "ImportJS: import word under cursor".
(CTRL/CMD+SHIFT+P) and select "ImportJS: fix all imports",
or "ImportJS: import word under cursor".
* It will be helpful to bind `import_js` to easy-to-use bindings,
such as:

```
{ "keys": ["super+alt+i"], "command": "import_js", "args": {"fix": true} },
{ "keys": ["super+alt+j"], "command": "import_js", "args": {"word": true} },
{ "keys": ["super+alt+g"], "command": "import_js", "args": {"word": true, "goto": true} },
{ "keys": ["super+alt+i"], "command": "import_js" },
{ "keys": ["super+alt+j"], "command": "import_js", "args": { "word": true } },
{ "keys": ["super+alt+g"], "command": "import_js", "args": { "word": true, "goto": true } },
```
2 changes: 0 additions & 2 deletions autoload/importjs.vim
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
function importjs#ImportJSImport()
ruby $import_js.import
endfunction
function importjs#ImportJSImportAll()
ruby $import_js.import_all
endfunction
function importjs#ImportJSGoTo()
ruby $import_js.goto
Expand Down
5 changes: 1 addition & 4 deletions bin/import-js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require 'json'
opts = Slop.parse do |o|
o.string '-w', '--word', 'a word/variable to import'
o.bool '--goto', 'instead of importing, just print the path to a module'
o.bool '--fix', 'imports all undefined variables, and removes unused imports'
o.array '--selections', 'a list of resolved selections, e.g. Foo:0,Bar:1'
o.string '--filename',
'a path to the file which contents are being passed in as stdin'
Expand Down Expand Up @@ -37,10 +36,8 @@ if opts.goto?
importer.goto
elsif opts[:word]
importer.import
elsif opts.fix?
importer.fix_imports
else
importer.import_all
importer.fix_imports
end

if opts.goto?
Expand Down
18 changes: 0 additions & 18 deletions import-js.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,5 @@
import sublime, sublime_plugin, subprocess, os, json

def plugin_loaded():
settings = sublime.load_settings('ImportJS.sublime-settings')
executable = os.path.expanduser(settings.get('executable'))
try:
version = subprocess.check_output(
[executable, '--version']) .decode('UTF-8').strip()
except FileNotFoundError:
print(no_executable_error(executable))

version_rb = os.path.join(os.path.dirname(__file__), 'lib/import_js/version.rb')
with open(version_rb) as f:
contents = f.read()

if("VERSION = '" + version + "'" not in contents):
print('The installed version of import-js does not match ' +
'the current version: ' + version)


def no_executable_error(executable):
return (
"Couldn't find executable "
Expand Down
34 changes: 11 additions & 23 deletions lib/import_js/importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,36 +47,17 @@ def goto
@editor.open_file(js_module.file_path) if js_module
end

# Removes unused imports and adds imports for undefined variables
def fix_imports
remove_unused_imports
import_all
end

# Finds all variables that haven't yet been imported.
def import_all
@config.refresh
undefined_variables = run_eslint_command.map do |line|
eslint_result = run_eslint_command
undefined_variables = eslint_result.map do |line|
/(["'])([^"']+)\1 is not defined/.match(line) do |match_data|
match_data[2]
end
end.compact.uniq

return message('No variables to import') if undefined_variables.empty?

old_imports = find_current_imports
undefined_variables.each do |variable|
if js_module = find_one_js_module(variable)
inject_js_module(variable, js_module, old_imports[:imports])
end
end
replace_imports(old_imports[:newline_count],
old_imports[:imports],
old_imports[:imports_start_at])
end

def remove_unused_imports
@config.refresh
unused_variables = run_eslint_command.map do |line|
unused_variables = eslint_result.map do |line|
/"([^"]+)" is defined but never used/.match(line) do |match_data|
match_data[1]
end
Expand All @@ -89,6 +70,13 @@ def remove_unused_imports
end
import_statement.variables.empty?
end

undefined_variables.each do |variable|
if js_module = find_one_js_module(variable)
inject_js_module(variable, js_module, new_imports)
end
end

replace_imports(old_imports[:newline_count],
new_imports,
old_imports[:imports_start_at])
Expand Down
2 changes: 1 addition & 1 deletion lib/import_js/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Defines the gem version.
module ImportJS
VERSION = '0.1.5'
VERSION = '0.2.0'
end
1 change: 0 additions & 1 deletion plugin/import-js.vim
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ endif
let g:import_js_loaded = 1

command ImportJSImport call importjs#ImportJSImport()
command ImportJSImportAll call importjs#ImportJSImportAll()
command ImportJSGoTo call importjs#ImportJSGoTo()
command ImportJSRemoveUnusedImports call importjs#ImportJSRemoveUnusedImports()
command ImportJSFixImports call importjs#ImportJSFixImports()
Expand Down
59 changes: 25 additions & 34 deletions spec/import_js/importer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1191,7 +1191,7 @@ def self.current_selection=(index)
end
end

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

subject do
ImportJS::Importer.new.import_all
ImportJS::Importer.new.fix_imports
VIM::Buffer.current_buffer.to_s
end

context 'when no undefined variables exist' do
it 'leaves the buffer unchanged' do
expect(subject).to eq(text)
end

it 'displays a message' do
subject
expect(VIM.last_command_message).to eq(
'ImportJS: No variables to import'
)
end
end

context 'when eslint can not parse' do
Expand Down Expand Up @@ -1312,38 +1305,14 @@ def self.current_selection=(index)
EOS
end
end
end

describe '#remove_unused_imports' do
let(:eslint_result) { '' }
before do
allow(Open3).to receive(:capture3).and_call_original
allow(Open3).to receive(:capture3).with(/eslint/, anything)
.and_return([eslint_result, nil])
end

subject do
ImportJS::Importer.new.remove_unused_imports
VIM::Buffer.current_buffer.to_s
end

context 'when no unused variables exist' do
it 'leaves the buffer unchanged' do
expect(subject).to eq(text)
end
end

context 'when eslint can not parse' do
let(:eslint_result) do
'stdin:1:1: Parsing error: Unexpected token ILLEGAL [Error]'
end

it 'throws an error' do
expect { subject }.to raise_error(ImportJS::ParseError)
end
end

context 'when one unused variable exists' do
context 'when one unused import exists' do
let(:text) { <<-EOS.strip }
var bar = require('foo/bar');
var foo = require('bar/foo');
Expand Down Expand Up @@ -1386,6 +1355,28 @@ def self.current_selection=(index)
end
end

context 'when an unused import and an undefined variable exists' do
let(:existing_files) { ['bar/foo.jsx'] }
let(:text) { <<-EOS.strip }
var bar = require('foo/bar');
foo
EOS

let(:eslint_result) do
"stdin:3:11: \"bar\" is defined but never used [Error/no-unused-vars]\n" \
"stdin:3:11: \"foo\" is not defined. [Error/no-undef]"
end

it 'removes the unused import and adds the missing one' do
expect(subject).to eq(<<-EOS.strip)
var foo = require('bar/foo');
foo
EOS
end
end

context 'when a destructured import has an unused variable' do
let(:text) { <<-EOS.strip }
var { bar, foo } = require('baz');
Expand Down

0 comments on commit 2cf40b3

Please sign in to comment.