Class: ImportJS::ImportStatements

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/import_js/import_statements.rb

Overview

Class that sorts ImportStatements as they are pushed in

Constant Summary collapse

STYLE_IMPORT =
:import
STYLE_CONST =
:const
STYLE_VAR =
:var
STYLE_CUSTOM =
:custom
STYLES =

Order is significant here

[STYLE_IMPORT, STYLE_CONST, STYLE_VAR, STYLE_CUSTOM].freeze
PATH_TYPE_CORE_MODULE =
:core_module
PATH_TYPE_PACKAGE =
:package
PATH_TYPE_NON_RELATIVE =
:non_relative
PATH_TYPE_RELATIVE =
:relative
PATH_TYPES =

Order is significant here

[
  PATH_TYPE_CORE_MODULE,
  PATH_TYPE_PACKAGE,
  PATH_TYPE_NON_RELATIVE,
  PATH_TYPE_RELATIVE,
].freeze
GROUPINGS_ARRAY =
STYLES.map do |style|
  PATH_TYPES.map do |location|
    "#{style} #{location}"
  end
end.flatten.freeze
GROUPINGS =
Hash[
  GROUPINGS_ARRAY.each_with_index.map { |group, index| [group, index] }
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(config, imports = {}) ⇒ ImportStatements

Returns a new instance of ImportStatements.

Parameters:



39
40
41
42
# File 'lib/import_js/import_statements.rb', line 39

def initialize(config, imports = {})
  @config = config
  @imports = imports
end

Instance Method Details

#cloneObject



52
53
54
# File 'lib/import_js/import_statements.rb', line 52

def clone
  ImportStatements.new(@config, @imports.clone)
end

#delete_variables!(variable_names) ⇒ ImportJS::ImportStatements

Parameters:

  • variable_names (Array<String>)

Returns:



74
75
76
77
78
79
80
81
82
83
# File 'lib/import_js/import_statements.rb', line 74

def delete_variables!(variable_names)
  @imports.reject! do |_, import_statement|
    variable_names.each do |variable_name|
      import_statement.delete_variable!(variable_name)
    end
    import_statement.empty?
  end

  self # for chaining
end

#eachObject



44
45
46
47
48
49
50
# File 'lib/import_js/import_statements.rb', line 44

def each
  return enum_for(:each) unless block_given?

  @imports.each do |_, import_statement|
    yield import_statement
  end
end

#push(import_statement) ⇒ ImportJS::ImportStatements Also known as: <<

Parameters:

Returns:



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/import_js/import_statements.rb', line 58

def push(import_statement)
  if @imports[import_statement.path]
    # Import already exists, so this line is likely one of a named imports
    # pair. Combine it into the same ImportStatement.
    @imports[import_statement.path].merge(import_statement)
  else
    # This is a new import, so we just add it to the hash.
    @imports[import_statement.path] = import_statement
  end

  self # for chaining
end

#to_aArray<String>

Convert the import statements into an array of strings, with an empty string between each group.

Returns:

  • (Array<String>)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/import_js/import_statements.rb', line 88

def to_a
  max_line_length = @config.get('max_line_length')
  tab = @config.get('tab')

  strings = []
  to_groups.each do |group|
    group.each do |import_statement|
      strings.concat(
        import_statement.to_import_strings(max_line_length, tab))
    end
    strings << '' # Add a blank line between groups.
  end

  # We don't want to include a trailing newline at the end of all the
  # groups here.
  strings.pop if strings.last == ''

  strings
end