Class: JsDependency::Extractor::ExtractImportPath

Inherits:
Object
  • Object
show all
Defined in:
lib/js_dependency/extractor/extract_import_path.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ ExtractImportPath

Returns a new instance of ExtractImportPath.

Parameters:

  • str (String)


7
8
9
# File 'lib/js_dependency/extractor/extract_import_path.rb', line 7

def initialize(str)
  @str = str
end

Class Method Details

.call(str) ⇒ Object

rubocop:enable Metrics/MethodLength, Metrics/AbcSize



75
76
77
# File 'lib/js_dependency/extractor/extract_import_path.rb', line 75

def self.call(str)
  new(str).call
end

Instance Method Details

#callArray<String>

rubocop:disable Metrics/MethodLength, Metrics/AbcSize

Returns:

  • (Array<String>)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/js_dependency/extractor/extract_import_path.rb', line 13

def call
  str = @str
  # import defaultExport from 'module-name';
  paths = str.gsub(/import\s+\S+\s+from\s+"([^"]+)"/).with_object([]) { |_, list| list << Regexp.last_match(1) }
  paths += str.gsub(/import\s+\S+\s+from\s+'([^']+)'/).with_object([]) { |_, list| list << Regexp.last_match(1) }

  # import * as name from \"module-name\";
  paths += str.gsub(/import\s+\S+\s+as\s+\S+\s+from\s+"([^"]+)"/).with_object([]) do |_, list|
    list << Regexp.last_match(1)
  end
  paths += str.gsub(/import\s+\S+\s+as\s+\S+\s+from\s+'([^']+)'/).with_object([]) do |_, list|
    list << Regexp.last_match(1)
  end

  # import { export1 } from \"module-name\";
  # import { export1 as alias1 } from "module-name";
  # import { export1 , export2 } from "module-name";
  # import { foo , bar } from "module-name/path/to/specific/un-exported/file";
  # import { export1 , export2 as alias2 , [...] } from "module-name";
  paths += str.gsub(/import\s+\{\s+.+\s+\}\s+from\s+"([^"]+)"/).with_object([]) do |_, list|
    list << Regexp.last_match(1)
  end
  paths += str.gsub(/import\s+\{\s+.+\s+\}\s+from\s+'([^']+)'/).with_object([]) do |_, list|
    list << Regexp.last_match(1)
  end

  # import defaultExport, { export1 [ , [...] ] } from "module-name";
  paths += str.gsub(/import\s+\S+,\s+\{\s+.+\s+\}\s+from\s+"([^"]+)"/).with_object([]) do |_, list|
    list << Regexp.last_match(1)
  end
  paths += str.gsub(/import\s+\S+,\s+\{\s+.+\s+\}\s+from\s+'([^']+)'/).with_object([]) do |_, list|
    list << Regexp.last_match(1)
  end

  # import defaultExport, * as name from "module-name";
  paths += str.gsub(/import\s+\S+,\s+.+\s+as\s+\S+\s+from\s+"([^"]+)"/).with_object([]) do |_, list|
    list << Regexp.last_match(1)
  end
  paths += str.gsub(/import\s+\S+,\s+.+\s+as\s+\S+\s+from\s+'([^']+)'/).with_object([]) do |_, list|
    list << Regexp.last_match(1)
  end

  # import "module-name";
  paths += str.gsub(/import\s+"([^"]+)"/).with_object([]) do |_, list|
    list << Regexp.last_match(1)
  end
  paths += str.gsub(/import\s+'([^']+)'/).with_object([]) do |_, list|
    list << Regexp.last_match(1)
  end

  # var promise = import("module-name");
  paths += str.gsub(/import\("([^"]+)"\)/).with_object([]) do |_, list|
    list << Regexp.last_match(1)
  end
  paths += str.gsub(/import\('([^']+)'\)/).with_object([]) do |_, list|
    list << Regexp.last_match(1)
  end
  paths.uniq.sort
end