import_js 0.5.1 → 0.6.0

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: cd6adff72ccc7afa4dc2e926b56398d666942449
4
- data.tar.gz: 1190a25ee2e7e0af64a7de229ab279bebded2951
3
+ metadata.gz: b839cb49fbb3b2162281bfc7dd5d8302aaec49d8
4
+ data.tar.gz: 28c0680aca73df347201d7e1e2e8271ef7d618f1
5
5
  SHA512:
6
- metadata.gz: f6d1a84089692701188e7cd7c18131e980f7f81ed8c9c785c1552fdcd625c006a8cd3ab4b89407e71370a47b5e1eba6f61d2076c43bac362da0262e3f791084d
7
- data.tar.gz: 576275490822799e44ad5427c4512169a7f279876c56dac305739fa4bde0ce7dce0de0aee5c7984a8e2aa88ebe311c4825e7c09137ce5f6b8441c8b582fca48e
6
+ metadata.gz: 674ac79c69f71c4131f21f1ce82e0e4dae5650321d90b5823d21439514f93d83f007643c8becae2b188857f89e2c31bece2d4a6db8bc1f0fb643f98ce42e63a4
7
+ data.tar.gz: 7a4ad17523e23d16574152f241734b37cdf62747ae82d85b35db9580d3a5f3ad4bc7ecd64ecd82acf4b3ff94f67c862864b5ad9ab765e9edd22e990f2fdd1615
@@ -9,8 +9,10 @@ module ImportJS
9
9
  'aliases' => {},
10
10
  'declaration_keyword' => 'import',
11
11
  'named_exports' => {},
12
+ 'environments' => [],
12
13
  'eslint_executable' => 'eslint',
13
14
  'excludes' => [],
15
+ 'group_imports' => true,
14
16
  'ignore_package_prefixes' => [],
15
17
  'import_dev_dependencies' => false,
16
18
  'import_function' => 'require',
@@ -87,6 +89,55 @@ module ImportJS
87
89
  end.compact.flatten
88
90
  end
89
91
 
92
+ ENVIRONMENT_CORE_MODULES = {
93
+ # As listed in https://github.com/nodejs/node/tree/master/lib
94
+ 'node' => %w[
95
+ assert
96
+ buffer
97
+ child_process
98
+ cluster
99
+ console
100
+ constants
101
+ crypto
102
+ dgram
103
+ dns
104
+ domain
105
+ events
106
+ fs
107
+ http
108
+ https
109
+ module
110
+ net
111
+ os
112
+ path
113
+ process
114
+ punycode
115
+ querystring
116
+ readline
117
+ repl
118
+ stream
119
+ string_decoder
120
+ sys
121
+ timers
122
+ tls
123
+ tty
124
+ url
125
+ util
126
+ v8
127
+ vm
128
+ zlib
129
+ ],
130
+ }.freeze
131
+
132
+ # @return [Array<String>]
133
+ def environment_core_modules
134
+ result = []
135
+ get('environments').map do |environment|
136
+ result.concat(ENVIRONMENT_CORE_MODULES[environment])
137
+ end
138
+ result
139
+ end
140
+
90
141
  private
91
142
 
92
143
  # @param file [File]
@@ -75,7 +75,7 @@ module ImportJS
75
75
  named_imports = dest_match[:named].split(/,\s*/).map(&:strip)
76
76
  else
77
77
  default_import = match[:assignment]
78
- return unless default_import =~ /\A[^\s]+\Z/
78
+ return unless default_import =~ /\A\S+\Z/
79
79
  end
80
80
 
81
81
  new(
@@ -11,12 +11,14 @@ module ImportJS
11
11
  # Order is significant here
12
12
  STYLES = [STYLE_IMPORT, STYLE_CONST, STYLE_VAR, STYLE_CUSTOM].freeze
13
13
 
14
+ PATH_TYPE_CORE_MODULE = :core_module
14
15
  PATH_TYPE_PACKAGE = :package
15
16
  PATH_TYPE_NON_RELATIVE = :non_relative
16
17
  PATH_TYPE_RELATIVE = :relative
17
18
 
18
19
  # Order is significant here
19
20
  PATH_TYPES = [
21
+ PATH_TYPE_CORE_MODULE,
20
22
  PATH_TYPE_PACKAGE,
21
23
  PATH_TYPE_NON_RELATIVE,
22
24
  PATH_TYPE_RELATIVE,
@@ -121,11 +123,16 @@ module ImportJS
121
123
  !import_statement.parsed_and_untouched?
122
124
  end.flatten.uniq(&:to_normalized).sort_by(&:to_normalized)
123
125
 
126
+ return [partitioned] unless @config.get('group_imports')
127
+
124
128
  package_dependencies = @config.package_dependencies
129
+ core_modules = @config.environment_core_modules
125
130
  partitioned.each do |import_statement|
126
131
  # Figure out what group to put this import statement in
127
132
  group_index = import_statement_group_index(
128
- import_statement, package_dependencies)
133
+ import_statement,
134
+ package_dependencies,
135
+ core_modules)
129
136
 
130
137
  # Add the import statement to the group
131
138
  groups[group_index] ||= []
@@ -138,11 +145,14 @@ module ImportJS
138
145
 
139
146
  # @param import_statement [ImportJS::ImportStatement]
140
147
  # @param package_dependencies [Array<String>]
148
+ # @param core_modules [Array<String>]
141
149
  # @return [Number]
142
- def import_statement_group_index(import_statement, package_dependencies)
150
+ def import_statement_group_index(import_statement,
151
+ package_dependencies,
152
+ core_modules)
143
153
  style = import_statement_style(import_statement)
144
154
  path_type = import_statement_path_type(
145
- import_statement, package_dependencies)
155
+ import_statement, package_dependencies, core_modules)
146
156
 
147
157
  GROUPINGS["#{style} #{path_type}"]
148
158
  end
@@ -164,10 +174,19 @@ module ImportJS
164
174
  # Determine import path type
165
175
  # @param import_statement [ImportJS::ImportStatement]
166
176
  # @param package_dependencies [Array<String>]
177
+ # @param core_modules [Array<String>]
167
178
  # @return [String] 'package, 'non-relative', 'relative'
168
- def import_statement_path_type(import_statement, package_dependencies)
169
- path = import_statement.path
170
- return PATH_TYPE_RELATIVE if path.start_with?('.')
179
+ def import_statement_path_type(import_statement,
180
+ package_dependencies,
181
+ core_modules)
182
+
183
+ return PATH_TYPE_RELATIVE if import_statement.path.start_with?('.')
184
+ return PATH_TYPE_CORE_MODULE if core_modules.include?(import_statement.path)
185
+ #
186
+ # If there is a slash in the path, remove that and everything after it.
187
+ # This is so that imports for modules inside package dependencies end up
188
+ # in the right group (PATH_TYPE_PACKAGE).
189
+ path = import_statement.path.sub(%r{\A(.*?)/.*\Z}, '\1')
171
190
  return PATH_TYPE_PACKAGE if package_dependencies.include?(path)
172
191
  PATH_TYPE_NON_RELATIVE
173
192
  end
@@ -244,6 +244,16 @@ module ImportJS
244
244
  old_imports_range.each do
245
245
  @editor.delete_line(old_imports_range.first)
246
246
  end
247
+
248
+ if import_strings.empty? &&
249
+ @editor.read_line(old_imports_range.first).empty?
250
+ # We have no newlines to write back to the file. Clearing out potential
251
+ # whitespace where the imports used to be leaves the file in a better
252
+ # state.
253
+ @editor.delete_line(old_imports_range.first)
254
+ return
255
+ end
256
+
247
257
  import_strings.reverse_each do |import_string|
248
258
  # We need to add each line individually because the Vim buffer will
249
259
  # convert newline characters to `~@`.
@@ -374,6 +384,12 @@ module ImportJS
374
384
  matched_modules << js_module if js_module
375
385
  end
376
386
 
387
+ @config.environment_core_modules.each do |dep|
388
+ next unless dep.casecmp(variable_name) == 0
389
+
390
+ matched_modules << JSModule.new(import_path: dep)
391
+ end
392
+
377
393
  # If you have overlapping lookup paths, you might end up seeing the same
378
394
  # module to import twice. In order to dedupe these, we remove the module
379
395
  # with the longest path
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Defines the gem version.
4
4
  module ImportJS
5
- VERSION = '0.5.1'.freeze
5
+ VERSION = '0.6.0'.freeze
6
6
  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.5.1
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henric Trotzig