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 = VIMEditor.new) ⇒ Importer

Returns a new instance of Importer.



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

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

Instance Method Details

#fix_importsObject

Removes unused imports and adds imports for undefined variables



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
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/import_js/importer.rb', line 82

def fix_imports
  reload_config
  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
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/import_js/importer.rb', line 41

def goto
  reload_config
  js_modules = []
  variable_name = @editor.current_word
  time do
    js_modules = find_js_modules(variable_name)
  end

  if js_modules.empty?
    # No JS modules are found for the variable, so there is nothing to go to
    # and we return early.
    return message("No modules were found for `#{variable_name}`")
  end

  js_module = resolve_goto_module(js_modules, variable_name)

  unless js_module
    # The current word is not mappable to one of the JS modules that we
    # found. This can happen if the user does not select one from the list.
    # We have nothing to go to, so we return early.
    return message("Could not resolve a module for `#{variable_name}`")
  end

  @editor.open_file(js_module.open_file_path(@editor.path_to_current_file))
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
  reload_config
  variable_name = @editor.current_word
  if variable_name.empty?
    message("      No variable to import. Place your cursor on a variable, then try\n      again.\n    EOS\n    return\n  end\n\n  js_module = find_one_js_module(variable_name)\n  return unless js_module\n\n  maintain_cursor_position do\n    old_imports = find_current_imports\n    inject_js_module(variable_name, js_module, old_imports[:imports])\n    replace_imports(old_imports[:newline_count],\n                    old_imports[:imports],\n                    old_imports[:imports_start_at])\n  end\nend\n".split.join(' '))