Class: ImportJS::JSModule

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

Overview

Class that represents a js module found in the file system

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lookup_path, relative_file_path, strip_file_extensions) ⇒ JSModule

Returns a new instance of JSModule.

Parameters:

  • lookup_path (String)

    the lookup path in which this module was found

  • relative_file_path (String)

    a full path to the file, relative to the project root.

  • strip_file_extensions (Array)

    a list of file extensions to strip, e.g. [‘.js’, ‘.jsx’]



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

def initialize(lookup_path, relative_file_path, strip_file_extensions)
  @lookup_path = lookup_path
  @file_path = relative_file_path
  if relative_file_path.end_with? '/package.json'
    @main_file = JSON.parse(File.read(relative_file_path))['main']
    match = relative_file_path.match(/(.*)\/package\.json/)
    @import_path = match[1]
    @skip = !@main_file
  elsif relative_file_path.match(/\/index\.js.*$/)
    match = relative_file_path.match(/(.*)\/(index\.js.*)/)
    @main_file = match[2]
    @import_path = match[1]
  else
    @import_path = relative_file_path
    strip_file_extensions.each do |ext|
      if @import_path.end_with?(ext)
        @import_path = @import_path[0...-ext.length]
        break
      end
    end
  end

  if lookup_path
    @import_path = @import_path.sub("#{@lookup_path}\/", '') # remove path prefix
  end
end

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



6
7
8
# File 'lib/import_js/js_module.rb', line 6

def file_path
  @file_path
end

#import_pathObject (readonly)

Returns the value of attribute import_path.



4
5
6
# File 'lib/import_js/js_module.rb', line 4

def import_path
  @import_path
end

#is_destructuredObject

Returns the value of attribute is_destructured.



9
10
11
# File 'lib/import_js/js_module.rb', line 9

def is_destructured
  @is_destructured
end

#lookup_pathObject (readonly)

Returns the value of attribute lookup_path.



5
6
7
# File 'lib/import_js/js_module.rb', line 5

def lookup_path
  @lookup_path
end

#main_fileObject (readonly)

Returns the value of attribute main_file.



7
8
9
# File 'lib/import_js/js_module.rb', line 7

def main_file
  @main_file
end

#skipObject (readonly)

Returns the value of attribute skip.



8
9
10
# File 'lib/import_js/js_module.rb', line 8

def skip
  @skip
end

Instance Method Details

#display_nameString

Returns a readable description of the module.

Returns:

  • (String)

    a readable description of the module



44
45
46
47
48
# File 'lib/import_js/js_module.rb', line 44

def display_name
  parts = [import_path]
  parts << " (main: #{@main_file})" if @main_file
  parts.join('')
end

#to_import_statement(variable_name) ⇒ ImportJS::ImportStatement

Parameters:

  • variable_name (String)

Returns:



52
53
54
55
56
57
58
59
60
61
# File 'lib/import_js/js_module.rb', line 52

def to_import_statement(variable_name)
  ImportJS::ImportStatement.new.tap do |statement|
    if is_destructured
      statement.inject_destructured_variable(variable_name)
    else
      statement.default_variable = variable_name
    end
    statement.path = import_path
  end
end