import_js 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8482eba183609eed41a5a8a9564c4d81f8047875
4
- data.tar.gz: 4c4b4d506a28dcbeab5ee3f29b21be24afa67750
3
+ metadata.gz: b5855fcda8dcbe929e0aecbd16b9b940cb6fa80a
4
+ data.tar.gz: 458cbb3e52b3358ec99b5c8891f9ae54b06769b9
5
5
  SHA512:
6
- metadata.gz: 73dd6ae7d9b26f4c03aa2cb37160bcb3eb48f1f7d27aa2642a830a7e83a5081290ea62bbda3865ea9ac99ab2561efcde9125182e53b699dfadd1592a827f4746
7
- data.tar.gz: a991d45c0496e466fb36a13abfdfb006157e444a835b872b44373a5bda1a5a9fbecaecda4ebb754a78054358a3f7361677f293a1f3cf51f13c45fc0aee0cf752
6
+ metadata.gz: e4a57078bbd9ebf4a07fe2f09c6e666fcd118f223c8ac35d54ca8538648a5d920f5ed7883170d905bf40a96969a74aae46aa4b4fc4d424f1451fcae6a0b82f10
7
+ data.tar.gz: 2722de373880d2530db9eb769543248dc341067fba8cf7791ddac1fc5380cd9fde64b548fc0fbcc8e0d5ea0f51938758da8a4ec62d8c2d7c8a2b4847e0a8c2a7
@@ -90,7 +90,7 @@ module ImportJS
90
90
  # @param variable_name [String]
91
91
  def delete_variable(variable_name)
92
92
  @default_variable = nil if default_variable == variable_name
93
- @destructured_variables.delete(variable_name) unless destructured_variables.nil?
93
+ @destructured_variables.delete(variable_name) if destructured?
94
94
 
95
95
  clear_import_string_cache
96
96
  end
@@ -124,42 +124,32 @@ module ImportJS
124
124
  if declaration_keyword == 'import'
125
125
  # ES2015 Modules (ESM) syntax can support default values and
126
126
  # destructuring on the same line.
127
- declaration = declaration_keyword
128
127
  if destructured?
129
- declaration += " #{default_variable}," if default_variable
130
- declaration += " #{destructured_string}"
128
+ [destructured_import_string(declaration_keyword, max_line_length, tab)]
131
129
  else
132
- declaration += " #{default_variable}"
130
+ [default_import_string(declaration_keyword, max_line_length, tab)]
133
131
  end
134
- declaration += ' from'
135
-
136
- [wrap_import(declaration, "'#{path}';", max_line_length, tab)]
137
132
  else # const/let/var
138
- value = "require('#{path}');"
139
-
140
- if destructured? && !default_variable.nil?
141
- # We have both a default variable and a destructuring to do, so we
142
- # need to generate 2 lines for CommonJS style syntax.
143
- default_declaration = "#{declaration_keyword} #{default_variable} ="
144
- destructured_declaration = "#{declaration_keyword} #{destructured_string} ="
145
-
146
- return [
147
- wrap_import(default_declaration, value, max_line_length, tab),
148
- wrap_import(destructured_declaration, value, max_line_length, tab)
149
- ]
133
+ strings = []
134
+
135
+ if default_variable
136
+ strings <<
137
+ default_import_string(declaration_keyword, max_line_length, tab)
138
+ end
139
+
140
+ if destructured?
141
+ strings <<
142
+ destructured_import_string(declaration_keyword, max_line_length, tab)
150
143
  end
151
144
 
152
- declaration_assignment =
153
- destructured? ? destructured_string : default_variable
154
- declaration = "#{declaration_keyword} #{declaration_assignment} ="
155
- [wrap_import(declaration, value, max_line_length, tab)]
145
+ strings
156
146
  end
157
147
  end
158
148
 
159
149
  # Merge another ImportStatement into this one.
160
150
  # @param import_statement [ImportJS::ImportStatement]
161
151
  def merge(import_statement)
162
- unless import_statement.default_variable.nil?
152
+ if import_statement.default_variable
163
153
  @default_variable = import_statement.default_variable
164
154
  clear_import_string_cache
165
155
  end
@@ -174,23 +164,48 @@ module ImportJS
174
164
 
175
165
  private
176
166
 
177
- # @return [String]
178
- def destructured_string
179
- "{ #{destructured_variables.join(', ')} }"
167
+ # @param line [String]
168
+ # @param max_line_length [Number] where to cap lines at
169
+ # @return [Boolean]
170
+ def line_too_long?(line, max_line_length)
171
+ max_line_length && line.length > max_line_length
180
172
  end
181
173
 
182
- # @param declaration [String]
183
- # @param value [String]
174
+ # @param declaration_keyword [String] e.g. 'import'
175
+ # @return [Array]
176
+ def equals_and_value(declaration_keyword)
177
+ return ['from', "'#{path}';"] if declaration_keyword == 'import'
178
+ ['=', "require('#{path}');"]
179
+ end
180
+
181
+ # @param declaration_keyword [String]
184
182
  # @param max_line_length [Number] where to cap lines at
185
183
  # @param tab [String] e.g. ' ' (two spaces)
186
184
  # @return [String] import statement, wrapped at max line length if necessary
187
- def wrap_import(declaration, value, max_line_length, tab)
188
- if max_line_length &&
189
- "#{declaration} #{value}".length > max_line_length
190
- "#{declaration}\n#{tab}#{value}"
191
- else
192
- "#{declaration} #{value}"
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}"
188
+ return line unless line_too_long?(line, max_line_length)
189
+
190
+ "#{declaration_keyword} #{default_variable} #{equals}\n#{tab}#{value}"
191
+ end
192
+
193
+ # @param declaration_keyword [String]
194
+ # @param max_line_length [Number] where to cap lines at
195
+ # @param tab [String] e.g. ' ' (two spaces)
196
+ # @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}, "
193
201
  end
202
+
203
+ destructured = "{ #{destructured_variables.join(', ')} }"
204
+ line = "#{declaration_keyword} #{prefix}#{destructured} #{equals} #{value}"
205
+ return line unless line_too_long?(line, max_line_length)
206
+
207
+ destructured = "{\n#{tab}#{destructured_variables.join(",\n#{tab}")},\n}"
208
+ "#{declaration_keyword} #{prefix}#{destructured} #{equals} #{value}"
194
209
  end
195
210
 
196
211
  def clear_import_string_cache
@@ -1,4 +1,4 @@
1
1
  # Defines the gem version.
2
2
  module ImportJS
3
- VERSION = '0.2.4'
3
+ VERSION = '0.2.5'
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: import_js
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henric Trotzig