import_js 0.1.5 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c2c89c88f08b5caa370d8b929e5448f918b2f2d4
4
- data.tar.gz: 5783b944690642d76215f53e7e85cfe36715d018
3
+ metadata.gz: 86109b8414f1f1331814080ff3de584129f4dd59
4
+ data.tar.gz: 671e9ed2c481e73f359c70552e4bec13b42cd4f8
5
5
  SHA512:
6
- metadata.gz: ba11ff91ca83a95ac4bbc0808e435fefc6eebc4dafebfc848d94415c1c7d6401a85f7ff2af32ce3037d94044e8d3274fa03519534761e377ee26a7c161d0a546
7
- data.tar.gz: bfd88afddeb5e09b671796067a2d52c2c92fac6f70aee60272e61e20d9e15e5e13891ce3708b0006cda0d8867e6a249702cd5fe5bab46966b59544698d0713e5
6
+ metadata.gz: 6bd23adbe12eb5db1232d685eee4021955b2c0190a977b844b8cc0b19ffb4d15de1e8dd8d9296e5627b9b1de96962fe8b07ff6a5204a3ca8ddf2e5db2db34917
7
+ data.tar.gz: 5e6e62dba0ce0f132c2b8bd0e7ab5de8202239195424a3726156235eac0041936fc55cd3ef7781da5b18e0e2052011542d0ba7bf1e0010ca5061a3c5c06bb7ad
data/bin/import-js CHANGED
@@ -7,7 +7,6 @@ require 'json'
7
7
  opts = Slop.parse do |o|
8
8
  o.string '-w', '--word', 'a word/variable to import'
9
9
  o.bool '--goto', 'instead of importing, just print the path to a module'
10
- o.bool '--fix', 'imports all undefined variables, and removes unused imports'
11
10
  o.array '--selections', 'a list of resolved selections, e.g. Foo:0,Bar:1'
12
11
  o.string '--filename',
13
12
  'a path to the file which contents are being passed in as stdin'
@@ -37,10 +36,8 @@ if opts.goto?
37
36
  importer.goto
38
37
  elsif opts[:word]
39
38
  importer.import
40
- elsif opts.fix?
41
- importer.fix_imports
42
39
  else
43
- importer.import_all
40
+ importer.fix_imports
44
41
  end
45
42
 
46
43
  if opts.goto?
@@ -22,6 +22,12 @@ class ImportJS::EmacsEditor
22
22
  puts 'import:success'
23
23
  when 'goto'
24
24
  ImportJS::Importer.new(self).goto
25
+ when 'fix'
26
+ ImportJS::Importer.new(self).fix_imports
27
+ write_file
28
+ puts 'import:success'
29
+ else
30
+ puts "unknown command: #{command}"
25
31
  end
26
32
  rescue Exception => e
27
33
  puts e.inspect
@@ -61,7 +67,7 @@ class ImportJS::EmacsEditor
61
67
  #
62
68
  # @return [String]
63
69
  def current_file_content
64
- @file.join('\n')
70
+ @file.join("\n")
65
71
  end
66
72
 
67
73
  # Reads a line from the file.
@@ -47,36 +47,17 @@ module ImportJS
47
47
  @editor.open_file(js_module.file_path) if js_module
48
48
  end
49
49
 
50
+ # Removes unused imports and adds imports for undefined variables
50
51
  def fix_imports
51
- remove_unused_imports
52
- import_all
53
- end
54
-
55
- # Finds all variables that haven't yet been imported.
56
- def import_all
57
52
  @config.refresh
58
- undefined_variables = run_eslint_command.map do |line|
53
+ eslint_result = run_eslint_command
54
+ undefined_variables = eslint_result.map do |line|
59
55
  /(["'])([^"']+)\1 is not defined/.match(line) do |match_data|
60
56
  match_data[2]
61
57
  end
62
58
  end.compact.uniq
63
59
 
64
- return message('No variables to import') if undefined_variables.empty?
65
-
66
- old_imports = find_current_imports
67
- undefined_variables.each do |variable|
68
- if js_module = find_one_js_module(variable)
69
- inject_js_module(variable, js_module, old_imports[:imports])
70
- end
71
- end
72
- replace_imports(old_imports[:newline_count],
73
- old_imports[:imports],
74
- old_imports[:imports_start_at])
75
- end
76
-
77
- def remove_unused_imports
78
- @config.refresh
79
- unused_variables = run_eslint_command.map do |line|
60
+ unused_variables = eslint_result.map do |line|
80
61
  /"([^"]+)" is defined but never used/.match(line) do |match_data|
81
62
  match_data[1]
82
63
  end
@@ -89,6 +70,13 @@ module ImportJS
89
70
  end
90
71
  import_statement.variables.empty?
91
72
  end
73
+
74
+ undefined_variables.each do |variable|
75
+ if js_module = find_one_js_module(variable)
76
+ inject_js_module(variable, js_module, new_imports)
77
+ end
78
+ end
79
+
92
80
  replace_imports(old_imports[:newline_count],
93
81
  new_imports,
94
82
  old_imports[:imports_start_at])
@@ -105,14 +93,14 @@ module ImportJS
105
93
  command = %w[
106
94
  eslint
107
95
  --stdin
108
- --format compact
96
+ --format unix
109
97
  --rule 'no-undef: 2'
110
98
  --rule 'no-unused-vars: [2, { "vars": "all", "args": "none" }]'
111
99
  ].join(' ')
112
100
  out, err = Open3.capture3(command,
113
101
  stdin_data: @editor.current_file_content)
114
102
 
115
- if out =~ /Error - Parsing error: / ||
103
+ if out =~ /Parsing error: / ||
116
104
  out =~ /Unrecoverable syntax error/
117
105
  fail ImportJS::ParseError.new, out
118
106
  end
@@ -1,4 +1,4 @@
1
1
  # Defines the gem version.
2
2
  module ImportJS
3
- VERSION = '0.1.5'
3
+ VERSION = '0.2.0'
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.1.5
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henric Trotzig