import_js 0.3.1 → 0.4.0

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.
@@ -3,12 +3,11 @@ require 'pathname'
3
3
  module ImportJS
4
4
  # Class that represents a js module found in the file system
5
5
  class JSModule
6
- attr_reader :import_path
7
- attr_reader :lookup_path
8
- attr_reader :file_path
9
- attr_reader :main_file
10
- attr_reader :skip
11
- attr_accessor :is_destructured
6
+ attr_accessor :import_path
7
+ attr_accessor :lookup_path
8
+ attr_accessor :file_path
9
+ attr_accessor :main_file
10
+ attr_accessor :has_named_exports
12
11
 
13
12
  # @param lookup_path [String] the lookup path in which this module was found
14
13
  # @param relative_file_path [String] a full path to the file, relative to
@@ -17,64 +16,88 @@ module ImportJS
17
16
  # e.g. ['.js', '.jsx']
18
17
  # @param make_relative_to [String|nil] a path to a different file which the
19
18
  # resulting import path should be relative to.
20
- def initialize(lookup_path: nil,
21
- relative_file_path: nil,
22
- strip_file_extensions: nil,
23
- make_relative_to: nil)
24
- @lookup_path = lookup_path
25
- @file_path = relative_file_path
26
-
27
- if @lookup_path && @lookup_path.start_with?('.')
28
- @lookup_path.sub!(/^\.\/?/, '')
29
- @file_path.sub!(/^\.\/?/, '')
30
- end
31
- if relative_file_path.end_with? '/package.json'
32
- @main_file = JSON.parse(File.read(relative_file_path))['main']
33
- match = relative_file_path.match(/(.*)\/package\.json/)
34
- @import_path = match[1]
35
- @skip = !@main_file
36
- elsif relative_file_path.match(%r{/index\.js[^/]*$})
37
- match = relative_file_path.match(/(.*)\/(index\.js.*)/)
38
- @main_file = match[2]
39
- @import_path = match[1]
40
- else
41
- @import_path = relative_file_path
42
- strip_file_extensions.each do |ext|
43
- if @import_path.end_with?(ext)
44
- @import_path = @import_path[0...-ext.length]
45
- break
46
- end
47
- end
48
- end
19
+ def self.construct(lookup_path: nil,
20
+ relative_file_path: nil,
21
+ strip_file_extensions: nil,
22
+ make_relative_to: nil,
23
+ strip_from_path: nil)
24
+ js_module = new
25
+ js_module.lookup_path = normalize_path(lookup_path)
26
+ js_module.file_path = normalize_path(relative_file_path)
49
27
 
50
- if @lookup_path
51
- @import_path.sub!(/^#{Regexp.escape(@lookup_path)}\//, '')
52
- if make_relative_to
53
- make_import_path_relative_to(make_relative_to)
54
- end
28
+ import_path, main_file = resolve_import_path_and_main(
29
+ js_module.file_path, strip_file_extensions)
30
+
31
+ return unless import_path
32
+
33
+ import_path = import_path.sub(
34
+ %r{^#{Regexp.escape(js_module.lookup_path)}/}, '')
35
+
36
+ js_module.import_path = import_path
37
+ js_module.main_file = main_file
38
+ js_module.make_relative_to(make_relative_to) if make_relative_to
39
+ js_module.strip_from_path(strip_from_path) unless make_relative_to
40
+ js_module
41
+ end
42
+
43
+ # @param path [String]
44
+ # @return [String]
45
+ def self.normalize_path(path)
46
+ return unless path
47
+ path.sub(%r{^\./?}, '')
48
+ end
49
+
50
+ # @param file_path [String]
51
+ # @param strip_file_extensions [Array]
52
+ # @return [String, String]
53
+ def self.resolve_import_path_and_main(file_path, strip_file_extensions)
54
+ if file_path.end_with? '/package.json'
55
+ main_file = JSON.parse(File.read(file_path))['main']
56
+ return [nil, nil] unless main_file
57
+ match = file_path.match(%r{(.*)/package\.json})
58
+ return match[1], main_file
55
59
  end
60
+
61
+ match = file_path.match(%r{(.*)/(index\.js[^/]*)$})
62
+ return match[1], match[2] if match
63
+
64
+ extensions = strip_file_extensions.map { |str| Regexp.escape(str) }
65
+ import_path = file_path.sub(/(?:#{extensions.join('|')})$/, '')
66
+ [import_path, nil]
67
+ end
68
+
69
+ # @param import_path [String]
70
+ def initialize(import_path: nil)
71
+ self.import_path = import_path
56
72
  end
57
73
 
58
74
  # @param make_relative_to [String]
59
- def make_import_path_relative_to(make_relative_to)
75
+ def make_relative_to(make_relative_to)
76
+ return unless lookup_path
60
77
  # First, strip out any absolute path up until the current directory
61
- make_relative_to = make_relative_to.sub(Dir.pwd + "\/", '')
78
+ make_relative_to = make_relative_to.sub("#{Dir.pwd}/", '')
62
79
 
63
80
  # Ignore if the file to relate to is part of a different lookup_path
64
- return unless make_relative_to.start_with? @lookup_path
81
+ return unless make_relative_to.start_with? lookup_path
65
82
 
66
83
  # Strip out the lookup_path
67
- make_relative_to.sub!(/^#{Regexp.escape(@lookup_path)}\//, '')
84
+ make_relative_to = make_relative_to.sub(
85
+ %r{^#{Regexp.escape(lookup_path)}/}, '')
68
86
 
69
- path = Pathname.new(@import_path).relative_path_from(
87
+ path = Pathname.new(import_path).relative_path_from(
70
88
  Pathname.new(File.dirname(make_relative_to))
71
89
  ).to_s
72
90
 
73
- unless path.start_with?('.')
74
- # `Pathname.relative_path_from` will not add "./" automatically
75
- path = './' + path
76
- end
77
- @import_path = path
91
+ # `Pathname.relative_path_from` will not add "./" automatically
92
+ path = './' + path unless path.start_with?('.')
93
+
94
+ self.import_path = path
95
+ end
96
+
97
+ # @param prefix [String]
98
+ def strip_from_path(prefix)
99
+ return unless prefix
100
+ self.import_path = import_path.sub(/^#{Regexp.escape(prefix)}/, '')
78
101
  end
79
102
 
80
103
  # @return [String] a readable description of the module
@@ -84,16 +107,36 @@ module ImportJS
84
107
  parts.join('')
85
108
  end
86
109
 
110
+ # @param path_to_current_file [String]
111
+ # @return [String]
112
+ def open_file_path(path_to_current_file)
113
+ if file_path && file_path.end_with?('/package.json')
114
+ return file_path.sub(/package\.json$/, main_file)
115
+ end
116
+ return file_path if file_path
117
+
118
+ if import_path.start_with?('.')
119
+ return File.expand_path(import_path, File.dirname(path_to_current_file))
120
+ end
121
+
122
+ import_path
123
+ end
124
+
87
125
  # @param variable_name [String]
126
+ # @param config [ImportJS::Configuration]
88
127
  # @return [ImportJS::ImportStatement]
89
- def to_import_statement(variable_name)
128
+ def to_import_statement(variable_name, config)
90
129
  ImportJS::ImportStatement.new.tap do |statement|
91
- if is_destructured
92
- statement.inject_destructured_variable(variable_name)
130
+ if has_named_exports
131
+ statement.inject_named_import(variable_name)
93
132
  else
94
- statement.default_variable = variable_name
133
+ statement.default_import = variable_name
95
134
  end
96
135
  statement.path = import_path
136
+ statement.declaration_keyword = config.get('declaration_keyword',
137
+ from_file: file_path)
138
+ statement.import_function = config.get('import_function',
139
+ from_file: file_path)
97
140
  end
98
141
  end
99
142
  end
@@ -1,4 +1,6 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Defines the gem version.
2
4
  module ImportJS
3
- VERSION = '0.3.1'
5
+ VERSION = '0.4.0'.freeze
4
6
  end
@@ -145,13 +145,13 @@ module ImportJS
145
145
  # @param name [String]
146
146
  # @return [Number?]
147
147
  def get_number(name)
148
- exists?(name) ? VIM.evaluate("#{name}").to_i : nil
148
+ exists?(name) ? VIM.evaluate(name).to_i : nil
149
149
  end
150
150
 
151
151
  # @param name [String]
152
152
  # @return [Boolean?]
153
153
  def get_bool(name)
154
- exists?(name) ? VIM.evaluate("#{name}").to_i != 0 : nil
154
+ exists?(name) ? VIM.evaluate(name).to_i != 0 : nil
155
155
  end
156
156
 
157
157
  # @return [Boolean?]
data/lib/import_js.rb CHANGED
@@ -6,6 +6,14 @@ module ImportJS
6
6
  class ParseError < StandardError
7
7
  # Error thrown when a JS file can't be parsed
8
8
  end
9
+
10
+ class FindError < StandardError
11
+ # Error thrown when the find command fails
12
+ end
13
+
14
+ class ClientTooOldError < StandardError
15
+ # Error thrown when the client is too old to handle the config
16
+ end
9
17
  end
10
18
 
11
19
  require_relative 'import_js/command_line_editor'
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.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henric Trotzig