import_js 0.2.4 → 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/import_js/import_statement.rb +51 -36
- data/lib/import_js/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b5855fcda8dcbe929e0aecbd16b9b940cb6fa80a
|
4
|
+
data.tar.gz: 458cbb3e52b3358ec99b5c8891f9ae54b06769b9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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)
|
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
|
-
|
130
|
-
declaration += " #{destructured_string}"
|
128
|
+
[destructured_import_string(declaration_keyword, max_line_length, tab)]
|
131
129
|
else
|
132
|
-
|
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
|
-
|
139
|
-
|
140
|
-
if
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
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
|
-
|
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
|
-
|
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
|
-
# @
|
178
|
-
|
179
|
-
|
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
|
183
|
-
# @
|
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
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
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
|
data/lib/import_js/version.rb
CHANGED