Class: ImportJS::Importer

Inherits:
Object
  • Object
show all
Defined in:
lib/import_js/importer.rb

Constant Summary collapse

REGEX_USE_STRICT =
/(['"])use strict\1;?/
REGEX_SINGLE_LINE_COMMENT =
%r{\A\s*//}
REGEX_MULTI_LINE_COMMENT_START =
%r{\A\s*/\*}
REGEX_MULTI_LINE_COMMENT_END =
%r{\*/}
REGEX_WHITESPACE_ONLY =
/\A\s*\Z/
REGEX_ESLINT_RESULT =
/
  (?<quote>["'])              # <quote> opening quote
  (?<variable_name>[^\1]+)    # <variable_name>
  \k<quote>
  \s
  (?<type>                    # <type>
   is\sdefined\sbut\snever\sused         # is defined but never used
   |
   is\snot\sdefined                      # is not defined
   |
   must\sbe\sin\sscope\swhen\susing\sJSX # must be in scope when using JSX
  )
/x

Instance Method Summary collapse

Constructor Details

#initialize(editor = ImportJS::VIMEditor.new) ⇒ Importer

Returns a new instance of Importer.



12
13
14
# File 'lib/import_js/importer.rb', line 12

def initialize(editor = ImportJS::VIMEditor.new)
  @editor = editor
end

Instance Method Details

#fix_importsObject

Removes unused imports and adds imports for undefined variables



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/import_js/importer.rb', line 70

def fix_imports
  @config = ImportJS::Configuration.new(@editor.path_to_current_file)
  eslint_result = run_eslint_command

  unused_variables = []
  undefined_variables = []

  eslint_result.each do |line|
    match = REGEX_ESLINT_RESULT.match(line)
    next unless match
    if match[:type] == 'is defined but never used'
      unused_variables << match[:variable_name]
    else
      undefined_variables << match[:variable_name]
    end
  end

  unused_variables.uniq!
  undefined_variables.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|
    js_module = find_one_js_module(variable)
    inject_js_module(variable, js_module, new_imports) if js_module
  end

  replace_imports(old_imports[:newline_count],
                  new_imports,
                  old_imports[:imports_start_at])
end

#gotoObject



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/import_js/importer.rb', line 41

def goto
  @config = ImportJS::Configuration.new(@editor.path_to_current_file)
  js_modules = []
  variable_name = @editor.current_word
  time do
    js_modules = find_js_modules(variable_name)
  end
  return if js_modules.empty?
  js_module = resolve_one_js_module(js_modules, variable_name)
  if js_module
    @editor.open_file(js_module.open_file_path(@editor.path_to_current_file))
  end
end

#importObject

Finds variable under the cursor to import. By default, this is bound to ‘<Leader>j`.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/import_js/importer.rb', line 18

def import
  @config = ImportJS::Configuration.new(@editor.path_to_current_file)
  variable_name = @editor.current_word
  if variable_name.empty?
    message(<<-EOS.split.join(' '))
      No variable to import. Place your cursor on a variable, then try
      again.
    EOS
    return
  end

  js_module = find_one_js_module(variable_name)
  return unless js_module

  maintain_cursor_position do
    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])
  end
end