Class: ImportJS::Importer
- Inherits:
-
Object
- Object
- ImportJS::Importer
- Defined in:
- lib/import_js/importer.rb
Instance Method Summary collapse
-
#fix_imports ⇒ Object
Removes unused imports and adds imports for undefined variables.
- #goto ⇒ Object
-
#import ⇒ Object
Finds variable under the cursor to import.
-
#initialize(editor = ImportJS::VIMEditor.new) ⇒ Importer
constructor
A new instance of Importer.
Constructor Details
Instance Method Details
#fix_imports ⇒ Object
Removes unused imports and adds imports for undefined variables
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/import_js/importer.rb', line 51 def fix_imports @config.refresh 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 unused_variables = eslint_result.map do |line| /"([^"]+)" is defined but never used/.match(line) do |match_data| match_data[1] end end.compact.uniq old_imports = find_current_imports new_imports = old_imports[:imports].reject do |import_statement| unused_variables.each do |unused_variable| import_statement.delete_variable(unused_variable) end import_statement.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]) end |
#goto ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/import_js/importer.rb', line 39 def goto @config.refresh @timing = { start: Time.now } variable_name = @editor.current_word js_modules = find_js_modules(variable_name) @timing[:end] = Time.now return if js_modules.empty? js_module = resolve_one_js_module(js_modules, variable_name) @editor.open_file(js_module.file_path) if js_module end |
#import ⇒ Object
Finds variable under the cursor to import. By default, this is bound to ‘<Leader>j`.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/import_js/importer.rb', line 13 def import @config.refresh variable_name = @editor.current_word if variable_name.empty? (<<-EOS.split.join(' ')) No variable to import. Place your cursor on a variable, then try again. EOS return end current_row, current_col = @editor.cursor old_buffer_lines = @editor.count_lines js_module = find_one_js_module(variable_name) return unless js_module old_imports = find_current_imports inject_js_module(variable_name, js_module, old_imports[:imports]) replace_imports(old_imports[:newline_count], old_imports[:imports], old_imports[:imports_start_at]) lines_changed = @editor.count_lines - old_buffer_lines return unless lines_changed @editor.cursor = [current_row + lines_changed, current_col] end |