import_js 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,51 +1,57 @@
1
1
  module ImportJS
2
2
  # Class that represents an import statement, e.g.
3
- # "const foo = require('foo');"
3
+ # `const foo = require('foo');`
4
+ # `let foo = myCustomRequire('foo');`
5
+ # `import foo from 'foo';`
4
6
  class ImportStatement
5
- REGEX_CONST_LET_VAR = %r{
7
+ REGEX_CONST_LET_VAR = /
6
8
  \A
7
- (?:const|let|var)\s+ # declaration keyword
9
+ (?<declaration_keyword>const|let|var)\s+ # <declaration_keyword>
8
10
  (?<assignment>.+?) # <assignment> variable assignment
9
11
  \s*=\s*
10
- require\(
12
+ (?<import_function>[^\(]+?)\( # <import_function> variable assignment
11
13
  (?<quote>'|") # <quote> opening quote
12
14
  (?<path>[^\2]+) # <path> module path
13
15
  \k<quote> # closing quote
14
16
  \);?
15
17
  \s*
16
- }xm
18
+ /xm
17
19
 
18
- REGEX_IMPORT = %r{
20
+ REGEX_IMPORT = /
19
21
  \A
20
- import\s+
22
+ (?<declaration_keyword>import)\s+ # <declaration_keyword>
21
23
  (?<assignment>.*?) # <assignment> variable assignment
22
24
  \s+from\s+
23
25
  (?<quote>'|") # <quote> opening quote
24
26
  (?<path>[^\2]+) # <path> module path
25
27
  \k<quote> # closing quote
26
28
  ;?\s*
27
- }xm
29
+ /xm
28
30
 
29
- REGEX_DESTRUCTURE = %r{
30
- (?: # non-capturing group
31
- (?<default>.*?) # <default> variable
31
+ REGEX_NAMED = /
32
+ (?: # non-capturing group
33
+ (?<default>.*?) # <default> default import
32
34
  ,\s*
33
35
  )?
34
36
  \{
35
37
  \s*
36
- (?<destructured>.*) # <destructured> variables
38
+ (?<named>.*) # <named> named imports
37
39
  \s*
38
40
  \}
39
- }xm
41
+ /xm
40
42
 
41
43
  attr_accessor :assignment
44
+ attr_accessor :declaration_keyword
45
+ attr_accessor :default_import
46
+ attr_accessor :named_imports
47
+ attr_accessor :import_function
42
48
  attr_accessor :original_import_string # a cache of the parsed import string
43
- attr_accessor :default_variable
44
- attr_accessor :destructured_variables
45
49
  attr_accessor :path
46
50
 
47
51
  # @param string [String] a possible import statement, e.g.
48
52
  # `const foo = require('foo');`
53
+ # `let foo = myCustomRequire('foo');`
54
+ # `import foo from 'foo';`
49
55
  # @return [ImportJS::ImportStatement?] a parsed statement, or nil if the
50
56
  # string can't be parsed
51
57
  def self.parse(string)
@@ -55,55 +61,59 @@ module ImportJS
55
61
 
56
62
  statement = new
57
63
  statement.original_import_string = match.string
64
+ statement.declaration_keyword = match[:declaration_keyword]
58
65
  statement.path = match[:path]
59
66
  statement.assignment = match[:assignment]
60
- if dest_match = statement.assignment.match(REGEX_DESTRUCTURE)
61
- statement.default_variable = dest_match[:default]
62
- statement.destructured_variables =
63
- dest_match[:destructured].split(/,\s*/).map(&:strip)
67
+ if match.names.include? 'import_function'
68
+ statement.import_function = match[:import_function]
69
+ end
70
+ dest_match = statement.assignment.match(REGEX_NAMED)
71
+ if dest_match
72
+ statement.default_import = dest_match[:default]
73
+ statement.named_imports =
74
+ dest_match[:named].split(/,\s*/).map(&:strip)
64
75
  else
65
- statement.default_variable = statement.assignment
76
+ statement.default_import = statement.assignment
66
77
  end
67
78
  statement
68
79
  end
69
80
 
70
- # Sets the default_variable and clears the original import string cache.
81
+ # Sets the default_import and clears the original import string cache.
71
82
  # @param value [String]
72
- def set_default_variable(value)
73
- @default_variable = value
83
+ def set_default_import(value)
84
+ @default_import = value
74
85
  clear_import_string_cache
75
86
  end
76
87
 
77
- # Injects a new variable into an already existing set of destructured
78
- # variables.
88
+ # Injects a new variable into an already existing set of named imports.
79
89
  # @param variable_name [String]
80
- def inject_destructured_variable(variable_name)
81
- @destructured_variables ||= []
82
- destructured_variables << variable_name
83
- destructured_variables.sort!.uniq!
90
+ def inject_named_import(variable_name)
91
+ @named_imports ||= []
92
+ named_imports << variable_name
93
+ named_imports.sort!.uniq!
84
94
 
85
95
  clear_import_string_cache
86
96
  end
87
97
 
88
- # Deletes a variable from an already existing default variable or set of
89
- # destructured variables.
98
+ # Deletes a variable from an already existing default import or set of
99
+ # named imports.
90
100
  # @param variable_name [String]
91
101
  def delete_variable(variable_name)
92
- @default_variable = nil if default_variable == variable_name
93
- @destructured_variables.delete(variable_name) if destructured?
102
+ @default_import = nil if default_import == variable_name
103
+ @named_imports.delete(variable_name) if named_imports?
94
104
 
95
105
  clear_import_string_cache
96
106
  end
97
107
 
98
- # @return [Boolean] true if there are destructured variables
99
- def destructured?
100
- !destructured_variables.nil? && !destructured_variables.empty?
108
+ # @return [Boolean] true if there are named imports
109
+ def named_imports?
110
+ !named_imports.nil? && !named_imports.empty?
101
111
  end
102
112
 
103
- # @return [Boolean] true if there is no default variable and there are no
104
- # destructured variables
113
+ # @return [Boolean] true if there is no default import and there are no
114
+ # named imports
105
115
  def empty?
106
- default_variable.nil? && !destructured?
116
+ default_import.nil? && !named_imports?
107
117
  end
108
118
 
109
119
  # @return [Array] an array that can be used in `uniq!` to dedupe equal
@@ -111,35 +121,32 @@ module ImportJS
111
121
  # `const foo = require('foo');`
112
122
  # `import foo from 'foo';`
113
123
  def to_normalized
114
- [default_variable, destructured_variables, path]
124
+ [default_import, named_imports, path]
115
125
  end
116
126
 
117
- # @param declaration_keyword [String] const, let, var, or import
118
127
  # @param max_line_length [Number] where to cap lines at
119
128
  # @param tab [String] e.g. ' ' (two spaces)
120
129
  # @return [Array] generated import statement strings
121
- def to_import_strings(declaration_keyword, max_line_length, tab)
130
+ def to_import_strings(max_line_length, tab)
122
131
  return [original_import_string] if original_import_string
123
132
 
124
133
  if declaration_keyword == 'import'
125
- # ES2015 Modules (ESM) syntax can support default values and
126
- # destructuring on the same line.
127
- if destructured?
128
- [destructured_import_string(declaration_keyword, max_line_length, tab)]
134
+ # ES2015 Modules (ESM) syntax can support default imports and
135
+ # named imports on the same line.
136
+ if named_imports?
137
+ [named_import_string(max_line_length, tab)]
129
138
  else
130
- [default_import_string(declaration_keyword, max_line_length, tab)]
139
+ [default_import_string(max_line_length, tab)]
131
140
  end
132
141
  else # const/let/var
133
142
  strings = []
134
143
 
135
- if default_variable
136
- strings <<
137
- default_import_string(declaration_keyword, max_line_length, tab)
144
+ if default_import
145
+ strings << default_import_string(max_line_length, tab)
138
146
  end
139
147
 
140
- if destructured?
141
- strings <<
142
- destructured_import_string(declaration_keyword, max_line_length, tab)
148
+ if named_imports?
149
+ strings << named_import_string(max_line_length, tab)
143
150
  end
144
151
 
145
152
  strings
@@ -149,15 +156,15 @@ module ImportJS
149
156
  # Merge another ImportStatement into this one.
150
157
  # @param import_statement [ImportJS::ImportStatement]
151
158
  def merge(import_statement)
152
- if import_statement.default_variable
153
- @default_variable = import_statement.default_variable
159
+ if import_statement.default_import
160
+ @default_import = import_statement.default_import
154
161
  clear_import_string_cache
155
162
  end
156
163
 
157
- if import_statement.destructured?
158
- @destructured_variables ||= []
159
- @destructured_variables.concat(import_statement.destructured_variables)
160
- @destructured_variables.sort!.uniq!
164
+ if import_statement.named_imports?
165
+ @named_imports ||= []
166
+ @named_imports.concat(import_statement.named_imports)
167
+ @named_imports.sort!.uniq!
161
168
  clear_import_string_cache
162
169
  end
163
170
  end
@@ -171,41 +178,39 @@ module ImportJS
171
178
  max_line_length && line.length > max_line_length
172
179
  end
173
180
 
174
- # @param declaration_keyword [String] e.g. 'import'
175
181
  # @return [Array]
176
- def equals_and_value(declaration_keyword)
182
+ def equals_and_value
177
183
  return ['from', "'#{path}';"] if declaration_keyword == 'import'
178
- ['=', "require('#{path}');"]
184
+ ['=', "#{import_function}('#{path}');"]
179
185
  end
180
186
 
181
- # @param declaration_keyword [String]
182
187
  # @param max_line_length [Number] where to cap lines at
183
188
  # @param tab [String] e.g. ' ' (two spaces)
184
189
  # @return [String] import statement, wrapped at max line length if necessary
185
- def default_import_string(declaration_keyword, max_line_length, tab)
186
- equals, value = equals_and_value(declaration_keyword)
187
- line = "#{declaration_keyword} #{default_variable} #{equals} #{value}"
190
+ def default_import_string(max_line_length, tab)
191
+ equals, value = equals_and_value
192
+ line = "#{declaration_keyword} #{default_import} #{equals} #{value}"
188
193
  return line unless line_too_long?(line, max_line_length)
189
194
 
190
- "#{declaration_keyword} #{default_variable} #{equals}\n#{tab}#{value}"
195
+ "#{declaration_keyword} #{default_import} #{equals}\n#{tab}#{value}"
191
196
  end
192
197
 
193
- # @param declaration_keyword [String]
194
198
  # @param max_line_length [Number] where to cap lines at
195
199
  # @param tab [String] e.g. ' ' (two spaces)
196
200
  # @return [String] import statement, wrapped at max line length if necessary
197
- def destructured_import_string(declaration_keyword, max_line_length, tab)
198
- equals, value = equals_and_value(declaration_keyword)
199
- if declaration_keyword == 'import' && default_variable
200
- prefix = "#{default_variable}, "
201
+ def named_import_string(max_line_length, tab)
202
+ equals, value = equals_and_value
203
+ if declaration_keyword == 'import' && default_import
204
+ prefix = "#{default_import}, "
201
205
  end
202
206
 
203
- destructured = "{ #{destructured_variables.join(', ')} }"
204
- line = "#{declaration_keyword} #{prefix}#{destructured} #{equals} #{value}"
207
+ named = "{ #{named_imports.join(', ')} }"
208
+ line = "#{declaration_keyword} #{prefix}#{named} #{equals} " \
209
+ "#{value}"
205
210
  return line unless line_too_long?(line, max_line_length)
206
211
 
207
- destructured = "{\n#{tab}#{destructured_variables.join(",\n#{tab}")},\n}"
208
- "#{declaration_keyword} #{prefix}#{destructured} #{equals} #{value}"
212
+ named = "{\n#{tab}#{named_imports.join(",\n#{tab}")},\n}"
213
+ "#{declaration_keyword} #{prefix}#{named} #{equals} #{value}"
209
214
  end
210
215
 
211
216
  def clear_import_string_cache