Class: ImportJS::ImportStatement

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

Overview

Class that represents an import statement, e.g. ‘const foo = require(’foo’);‘ `var foo = myCustomRequire(’foo’);‘ `import foo from ’foo’;‘

Constant Summary collapse

REGEX_CONST_LET_VAR =
/
  \A
  (?<declaration_keyword>const|let|var)\s+ # <declaration_keyword>
  (?<assignment>.+?)   # <assignment> variable assignment
  \s*=\s*
  (?<import_function>\w+?)\( # <import_function> variable assignment
    (?<quote>'|")      # <quote> opening quote
    (?<path>[^\2\n]+)  # <path> module path
    \k<quote>          # closing quote
  \);?
  \s*
  \Z
/xm
REGEX_IMPORT =
/
  \A
  (?<declaration_keyword>import)\s+ # <declaration_keyword>
  (?<assignment>.*?) # <assignment> variable assignment
  \s+from\s+
  (?<quote>'|")      # <quote> opening quote
  (?<path>[^\2\n]+)  # <path> module path
  \k<quote>          # closing quote
  ;?\s*
  \Z
/xm
REGEX_NAMED =
/
  \A
  (?:                # non-capturing group
    (?<default>.*?)  # <default> default import
    ,\s*
  )?
  \{
    \s*
    (?<named>.*)      # <named> named imports
    \s*
  \}
  \Z
/xm

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(assignment: nil, declaration_keyword: nil, default_import: nil, import_function: nil, named_imports: nil, original_import_string: nil, path: nil) ⇒ ImportStatement

Returns a new instance of ImportStatement.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/import_js/import_statement.rb', line 92

def initialize(
  assignment: nil,
  declaration_keyword: nil,
  default_import: nil,
  import_function: nil,
  named_imports: nil,
  original_import_string: nil,
  path: nil
)
  @assignment = assignment
  @declaration_keyword = declaration_keyword
  @default_import = default_import
  @import_function = import_function
  @named_imports = named_imports
  @original_import_string = original_import_string
  @path = path
end

Instance Attribute Details

#assignmentObject

Returns the value of attribute assignment.



47
48
49
# File 'lib/import_js/import_statement.rb', line 47

def assignment
  @assignment
end

#declaration_keywordObject

Returns the value of attribute declaration_keyword.



48
49
50
# File 'lib/import_js/import_statement.rb', line 48

def declaration_keyword
  @declaration_keyword
end

#default_importObject

Returns the value of attribute default_import.



49
50
51
# File 'lib/import_js/import_statement.rb', line 49

def default_import
  @default_import
end

#import_functionObject

Returns the value of attribute import_function.



51
52
53
# File 'lib/import_js/import_statement.rb', line 51

def import_function
  @import_function
end

#named_importsObject

Returns the value of attribute named_imports.



50
51
52
# File 'lib/import_js/import_statement.rb', line 50

def named_imports
  @named_imports
end

#original_import_stringObject

a cache of the parsed import string



52
53
54
# File 'lib/import_js/import_statement.rb', line 52

def original_import_string
  @original_import_string
end

#pathObject

Returns the value of attribute path.



53
54
55
# File 'lib/import_js/import_statement.rb', line 53

def path
  @path
end

Class Method Details

.parse(string) ⇒ ImportJS::ImportStatement?

Returns a parsed statement, or nil if the string can’t be parsed.

Parameters:

  • string (String)

    a possible import statement, e.g. ‘const foo = require(’foo’);‘ `var foo = myCustomRequire(’foo’);‘ `import foo from ’foo’;‘

Returns:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/import_js/import_statement.rb', line 61

def self.parse(string)
  match = REGEX_CONST_LET_VAR.match(string) ||
          REGEX_IMPORT.match(string)
  return unless match

  import_function = if match.names.include?('import_function')
                      match[:import_function]
                    else
                      'require'
                    end

  dest_match = match[:assignment].match(REGEX_NAMED)
  if dest_match
    default_import = dest_match[:default]
    named_imports = dest_match[:named].split(/,\s*/).map(&:strip)
  else
    default_import = match[:assignment]
    return unless default_import =~ /\A\S+\Z/
  end

  new(
    assignment: match[:assignment],
    declaration_keyword: match[:declaration_keyword],
    default_import: default_import,
    import_function: import_function,
    named_imports: named_imports,
    original_import_string: match.string,
    path: match[:path]
  )
end

Instance Method Details

#delete_variable!(variable_name) ⇒ Object

Deletes a variable from an already existing default import or set of named imports.

Parameters:

  • variable_name (String)


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/import_js/import_statement.rb', line 113

def delete_variable!(variable_name)
  touched = false

  if default_import == variable_name
    @default_import = nil
    touched = true
  end

  if named_imports?
    deleted = @named_imports.delete(variable_name)
    touched = true if deleted
  end

  clear_import_string_cache if touched
end

#empty?Boolean

Returns true if there is no default import and there are no named imports.

Returns:

  • (Boolean)

    true if there is no default import and there are no named imports



136
137
138
# File 'lib/import_js/import_statement.rb', line 136

def empty?
  default_import.nil? && !named_imports?
end

#merge(import_statement) ⇒ Object

Merge another ImportStatement into this one.

Parameters:



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/import_js/import_statement.rb', line 178

def merge(import_statement)
  if import_statement.default_import &&
     @default_import != import_statement.default_import
    @default_import = import_statement.default_import
    clear_import_string_cache
  end

  if import_statement.named_imports?
    @named_imports ||= []
    original_named_imports = @named_imports.clone
    @named_imports.concat(import_statement.named_imports)
    @named_imports.sort!.uniq!
    clear_import_string_cache if original_named_imports != @named_imports
  end

  if @declaration_keyword != import_statement.declaration_keyword
    @declaration_keyword = import_statement.declaration_keyword
    clear_import_string_cache
  end
end

#named_imports?Boolean

Returns true if there are named imports.

Returns:

  • (Boolean)

    true if there are named imports



130
131
132
# File 'lib/import_js/import_statement.rb', line 130

def named_imports?
  !named_imports.nil? && !named_imports.empty?
end

#parsed_and_untouched?Boolean

Returns true if this instance was created through parsing an existing import and it hasn’t been altered since it was created.

Returns:

  • (Boolean)

    true if this instance was created through parsing an existing import and it hasn’t been altered since it was created.



142
143
144
# File 'lib/import_js/import_statement.rb', line 142

def parsed_and_untouched?
  !original_import_string.nil?
end

#to_import_strings(max_line_length, tab) ⇒ Array<String>

Returns generated import statement strings.

Parameters:

  • max_line_length (Number)

    where to cap lines at

  • tab (String)

    e.g. ‘ ’ (two spaces)

Returns:

  • (Array<String>)

    generated import statement strings



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/import_js/import_statement.rb', line 160

def to_import_strings(max_line_length, tab)
  return [original_import_string] if original_import_string

  if declaration_keyword == 'import'
    # ES2015 Modules (ESM) syntax can support default imports and
    # named imports on the same line.
    return [named_import_string(max_line_length, tab)] if named_imports?
    [default_import_string(max_line_length, tab)]
  else # const/var
    strings = []
    strings << default_import_string(max_line_length, tab) if default_import
    strings << named_import_string(max_line_length, tab) if named_imports?
    strings
  end
end

#to_normalizedArray

Returns an array that can be used in ‘sort` and `uniq`.

Returns:

  • (Array)

    an array that can be used in ‘sort` and `uniq`



147
148
149
# File 'lib/import_js/import_statement.rb', line 147

def to_normalized
  [default_import || '', named_imports || '']
end

#variablesArray<String>

Returns Array of all variables that this ImportStatement imports.

Returns:

  • (Array<String>)

    Array of all variables that this ImportStatement imports.



153
154
155
# File 'lib/import_js/import_statement.rb', line 153

def variables
  [@default_import, *@named_imports].compact
end