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.
- checksums.yaml +4 -4
- data/bin/import-js +2 -2
- data/lib/import_js/command_line_editor.rb +4 -9
- data/lib/import_js/configuration.rb +65 -39
- data/lib/import_js/emacs_editor.rb +137 -137
- data/lib/import_js/import_statement.rb +81 -76
- data/lib/import_js/importer.rb +227 -102
- data/lib/import_js/js_module.rb +97 -54
- data/lib/import_js/version.rb +3 -1
- data/lib/import_js/vim_editor.rb +2 -2
- data/lib/import_js.rb +8 -0
- metadata +1 -1
@@ -1,51 +1,57 @@
|
|
1
1
|
module ImportJS
|
2
2
|
# Class that represents an import statement, e.g.
|
3
|
-
#
|
3
|
+
# `const foo = require('foo');`
|
4
|
+
# `let foo = myCustomRequire('foo');`
|
5
|
+
# `import foo from 'foo';`
|
4
6
|
class ImportStatement
|
5
|
-
REGEX_CONST_LET_VAR =
|
7
|
+
REGEX_CONST_LET_VAR = /
|
6
8
|
\A
|
7
|
-
(
|
9
|
+
(?<declaration_keyword>const|let|var)\s+ # <declaration_keyword>
|
8
10
|
(?<assignment>.+?) # <assignment> variable assignment
|
9
11
|
\s*=\s*
|
10
|
-
|
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
|
-
|
18
|
+
/xm
|
17
19
|
|
18
|
-
REGEX_IMPORT =
|
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
|
-
|
29
|
+
/xm
|
28
30
|
|
29
|
-
|
30
|
-
(?:
|
31
|
-
(?<default>.*?)
|
31
|
+
REGEX_NAMED = /
|
32
|
+
(?: # non-capturing group
|
33
|
+
(?<default>.*?) # <default> default import
|
32
34
|
,\s*
|
33
35
|
)?
|
34
36
|
\{
|
35
37
|
\s*
|
36
|
-
(?<
|
38
|
+
(?<named>.*) # <named> named imports
|
37
39
|
\s*
|
38
40
|
\}
|
39
|
-
|
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
|
61
|
-
statement.
|
62
|
-
|
63
|
-
|
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.
|
76
|
+
statement.default_import = statement.assignment
|
66
77
|
end
|
67
78
|
statement
|
68
79
|
end
|
69
80
|
|
70
|
-
# Sets the
|
81
|
+
# Sets the default_import and clears the original import string cache.
|
71
82
|
# @param value [String]
|
72
|
-
def
|
73
|
-
@
|
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
|
78
|
-
# variables.
|
88
|
+
# Injects a new variable into an already existing set of named imports.
|
79
89
|
# @param variable_name [String]
|
80
|
-
def
|
81
|
-
@
|
82
|
-
|
83
|
-
|
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
|
89
|
-
#
|
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
|
-
@
|
93
|
-
@
|
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
|
99
|
-
def
|
100
|
-
!
|
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
|
104
|
-
#
|
113
|
+
# @return [Boolean] true if there is no default import and there are no
|
114
|
+
# named imports
|
105
115
|
def empty?
|
106
|
-
|
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
|
-
[
|
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(
|
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
|
126
|
-
#
|
127
|
-
if
|
128
|
-
[
|
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(
|
139
|
+
[default_import_string(max_line_length, tab)]
|
131
140
|
end
|
132
141
|
else # const/let/var
|
133
142
|
strings = []
|
134
143
|
|
135
|
-
if
|
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
|
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.
|
153
|
-
@
|
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.
|
158
|
-
@
|
159
|
-
@
|
160
|
-
@
|
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
|
182
|
+
def equals_and_value
|
177
183
|
return ['from', "'#{path}';"] if declaration_keyword == 'import'
|
178
|
-
['=', "
|
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(
|
186
|
-
equals, value = equals_and_value
|
187
|
-
line = "#{declaration_keyword} #{
|
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} #{
|
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
|
198
|
-
equals, value = equals_and_value
|
199
|
-
if declaration_keyword == 'import' &&
|
200
|
-
prefix = "#{
|
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
|
-
|
204
|
-
line = "#{declaration_keyword} #{prefix}#{
|
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
|
-
|
208
|
-
"#{declaration_keyword} #{prefix}#{
|
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
|