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’);”

Constant Summary collapse

REGEX_CONST_LET_VAR =
%r{
  \A
  (?:const|let|var)\s+ # declaration keyword
  (?<assignment>.+?)   # <assignment> variable assignment
  \s*=\s*
  require\(
    (?<quote>'|")      # <quote> opening quote
    (?<path>[^\2]+)    # <path> module path
    \k<quote>          # closing quote
  \);?
  \s*
}xm
REGEX_IMPORT =
%r{
  \A
  import\s+
  (?<assignment>.*?) # <assignment> variable assignment
  \s+from\s+
  (?<quote>'|")      # <quote> opening quote
  (?<path>[^\2]+)    # <path> module path
  \k<quote>          # closing quote
  ;?\s*
}xm
REGEX_DESTRUCTURE =
%r{
  (?:                    # non-capturing group
    (?<default>.*?)      # <default> variable
    ,\s*
  )?
  \{
    \s*
    (?<destructured>.*)  # <destructured> variables
    \s*
  \}
}x

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#assignmentObject

Returns the value of attribute assignment.



41
42
43
# File 'lib/import_js/import_statement.rb', line 41

def assignment
  @assignment
end

#default_variableObject

Returns the value of attribute default_variable.



43
44
45
# File 'lib/import_js/import_statement.rb', line 43

def default_variable
  @default_variable
end

#destructured_variablesObject

Returns the value of attribute destructured_variables.



44
45
46
# File 'lib/import_js/import_statement.rb', line 44

def destructured_variables
  @destructured_variables
end

#original_import_stringObject

a cache of the parsed import string



42
43
44
# File 'lib/import_js/import_statement.rb', line 42

def original_import_string
  @original_import_string
end

#pathObject

Returns the value of attribute path.



45
46
47
# File 'lib/import_js/import_statement.rb', line 45

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’);‘

Returns:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/import_js/import_statement.rb', line 51

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

  statement = new
  statement.original_import_string = match.string
  statement.path = match[:path]
  statement.assignment = match[:assignment]
  if dest_match = statement.assignment.match(REGEX_DESTRUCTURE)
    statement.default_variable = dest_match[:default]
    statement.destructured_variables =
      dest_match[:destructured].split(/,\s*/).map(&:strip)
  else
    statement.default_variable = statement.assignment
  end
  statement
end

Instance Method Details

#delete_variable(variable_name) ⇒ Object

Deletes a variable from an already existing default variable or set of

destructured variables.

Parameters:

  • variable_name (String)


91
92
93
94
95
96
# File 'lib/import_js/import_statement.rb', line 91

def delete_variable(variable_name)
  @default_variable = nil if default_variable == variable_name
  @destructured_variables.delete(variable_name) if destructured?

  clear_import_string_cache
end

#destructured?Boolean

Returns true if there are destructured variables.

Returns:

  • (Boolean)

    true if there are destructured variables



99
100
101
# File 'lib/import_js/import_statement.rb', line 99

def destructured?
  !destructured_variables.nil? && !destructured_variables.empty?
end

#empty?Boolean

Returns true if there is no default variable and there are no destructured variables.

Returns:

  • (Boolean)

    true if there is no default variable and there are no destructured variables



105
106
107
# File 'lib/import_js/import_statement.rb', line 105

def empty?
  default_variable.nil? && !destructured?
end

#inject_destructured_variable(variable_name) ⇒ Object

Injects a new variable into an already existing set of destructured

variables.

Parameters:

  • variable_name (String)


80
81
82
83
84
85
86
# File 'lib/import_js/import_statement.rb', line 80

def inject_destructured_variable(variable_name)
  @destructured_variables ||= []
  destructured_variables << variable_name
  destructured_variables.sort!.uniq!

  clear_import_string_cache
end

#merge(import_statement) ⇒ Object

Merge another ImportStatement into this one.

Parameters:



151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/import_js/import_statement.rb', line 151

def merge(import_statement)
  if import_statement.default_variable
    @default_variable = import_statement.default_variable
    clear_import_string_cache
  end

  if import_statement.destructured?
    @destructured_variables ||= []
    @destructured_variables.concat(import_statement.destructured_variables)
    @destructured_variables.sort!.uniq!
    clear_import_string_cache
  end
end

#set_default_variable(value) ⇒ Object

Sets the default_variable and clears the original import string cache.

Parameters:

  • value (String)


72
73
74
75
# File 'lib/import_js/import_statement.rb', line 72

def set_default_variable(value)
  @default_variable = value
  clear_import_string_cache
end

#to_import_strings(declaration_keyword, max_line_length, tab) ⇒ Array

Returns generated import statement strings.

Parameters:

  • declaration_keyword (String)

    const, let, var, or import

  • max_line_length (Number)

    where to cap lines at

  • tab (String)

    e.g. ‘ ’ (two spaces)

Returns:

  • (Array)

    generated import statement strings



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/import_js/import_statement.rb', line 121

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

  if declaration_keyword == 'import'
    # ES2015 Modules (ESM) syntax can support default values and
    # destructuring on the same line.
    if destructured?
      [destructured_import_string(declaration_keyword, max_line_length, tab)]
    else
      [default_import_string(declaration_keyword, max_line_length, tab)]
    end
  else # const/let/var
    strings = []

    if default_variable
      strings <<
        default_import_string(declaration_keyword, max_line_length, tab)
    end

    if destructured?
      strings <<
        destructured_import_string(declaration_keyword, max_line_length, tab)
    end

    strings
  end
end

#to_normalizedArray

Returns an array that can be used in ‘uniq!` to dedupe equal statements, e.g. `const foo = require(’foo’);‘ `import foo from ’foo’;‘.

Returns:

  • (Array)

    an array that can be used in ‘uniq!` to dedupe equal statements, e.g. `const foo = require(’foo’);‘ `import foo from ’foo’;‘



113
114
115
# File 'lib/import_js/import_statement.rb', line 113

def to_normalized
  [default_variable, destructured_variables, path]
end