Class: ImportJS::ImportStatement
- Inherits:
-
Object
- Object
- ImportJS::ImportStatement
- Defined in:
- lib/import_js/import_statement.rb
Overview
Class that represents an import statement, e.g. ‘const foo = require(’foo’);‘ `let foo = myCustomRequire(’foo’);‘ `import foo from ’foo’;‘
Constant Summary collapse
- REGEX_CONST_LET_VAR =
/ \A (?<declaration_keyword>const|let|var)\s+ # <declaration_keyword> (?<assignment>.+?) # <assignment> variable assignment \s*=\s* (?<import_function>[^\(]+?)\( # <import_function> variable assignment (?<quote>'|") # <quote> opening quote (?<path>[^\2]+) # <path> module path \k<quote> # closing quote \);? \s* /xm- REGEX_IMPORT =
/ \A (?<declaration_keyword>import)\s+ # <declaration_keyword> (?<assignment>.*?) # <assignment> variable assignment \s+from\s+ (?<quote>'|") # <quote> opening quote (?<path>[^\2]+) # <path> module path \k<quote> # closing quote ;?\s* /xm- REGEX_NAMED =
/ (?: # non-capturing group (?<default>.*?) # <default> default import ,\s* )? \{ \s* (?<named>.*) # <named> named imports \s* \} /xm
Instance Attribute Summary collapse
-
#assignment ⇒ Object
Returns the value of attribute assignment.
-
#declaration_keyword ⇒ Object
Returns the value of attribute declaration_keyword.
-
#default_import ⇒ Object
Returns the value of attribute default_import.
-
#import_function ⇒ Object
Returns the value of attribute import_function.
-
#named_imports ⇒ Object
Returns the value of attribute named_imports.
-
#original_import_string ⇒ Object
a cache of the parsed import string.
-
#path ⇒ Object
Returns the value of attribute path.
Class Method Summary collapse
-
.parse(string) ⇒ ImportJS::ImportStatement?
A parsed statement, or nil if the string can’t be parsed.
Instance Method Summary collapse
-
#delete_variable(variable_name) ⇒ Object
Deletes a variable from an already existing default import or set of named imports.
-
#empty? ⇒ Boolean
True if there is no default import and there are no named imports.
-
#inject_named_import(variable_name) ⇒ Object
Injects a new variable into an already existing set of named imports.
-
#merge(import_statement) ⇒ Object
Merge another ImportStatement into this one.
-
#named_imports? ⇒ Boolean
True if there are named imports.
-
#set_default_import(value) ⇒ Object
Sets the default_import and clears the original import string cache.
-
#to_import_strings(max_line_length, tab) ⇒ Array
Generated import statement strings.
-
#to_normalized ⇒ Array
An array that can be used in ‘uniq!` to dedupe equal statements, e.g.
Instance Attribute Details
#assignment ⇒ Object
Returns the value of attribute assignment.
43 44 45 |
# File 'lib/import_js/import_statement.rb', line 43 def assignment @assignment end |
#declaration_keyword ⇒ Object
Returns the value of attribute declaration_keyword.
44 45 46 |
# File 'lib/import_js/import_statement.rb', line 44 def declaration_keyword @declaration_keyword end |
#default_import ⇒ Object
Returns the value of attribute default_import.
45 46 47 |
# File 'lib/import_js/import_statement.rb', line 45 def default_import @default_import end |
#import_function ⇒ Object
Returns the value of attribute import_function.
47 48 49 |
# File 'lib/import_js/import_statement.rb', line 47 def import_function @import_function end |
#named_imports ⇒ Object
Returns the value of attribute named_imports.
46 47 48 |
# File 'lib/import_js/import_statement.rb', line 46 def named_imports @named_imports end |
#original_import_string ⇒ Object
a cache of the parsed import string
48 49 50 |
# File 'lib/import_js/import_statement.rb', line 48 def original_import_string @original_import_string end |
#path ⇒ Object
Returns the value of attribute path.
49 50 51 |
# File 'lib/import_js/import_statement.rb', line 49 def path @path end |
Class Method Details
.parse(string) ⇒ ImportJS::ImportStatement?
Returns a parsed statement, or nil if the string can’t be parsed.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/import_js/import_statement.rb', line 57 def self.parse(string) match = REGEX_CONST_LET_VAR.match(string) || REGEX_IMPORT.match(string) return unless match statement = new statement.original_import_string = match.string statement.declaration_keyword = match[:declaration_keyword] statement.path = match[:path] statement.assignment = match[:assignment] if match.names.include? 'import_function' statement.import_function = match[:import_function] end dest_match = statement.assignment.match(REGEX_NAMED) if dest_match statement.default_import = dest_match[:default] statement.named_imports = dest_match[:named].split(/,\s*/).map(&:strip) else statement.default_import = statement.assignment end statement end |
Instance Method Details
#delete_variable(variable_name) ⇒ Object
Deletes a variable from an already existing default import or set of
named imports.
101 102 103 104 105 106 |
# File 'lib/import_js/import_statement.rb', line 101 def delete_variable(variable_name) @default_import = nil if default_import == variable_name @named_imports.delete(variable_name) if named_imports? clear_import_string_cache end |
#empty? ⇒ Boolean
Returns true if there is no default import and there are no named imports.
115 116 117 |
# File 'lib/import_js/import_statement.rb', line 115 def empty? default_import.nil? && !named_imports? end |
#inject_named_import(variable_name) ⇒ Object
Injects a new variable into an already existing set of named imports.
90 91 92 93 94 95 96 |
# File 'lib/import_js/import_statement.rb', line 90 def inject_named_import(variable_name) @named_imports ||= [] named_imports << variable_name named_imports.sort!.uniq! clear_import_string_cache end |
#merge(import_statement) ⇒ Object
Merge another ImportStatement into this one.
158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/import_js/import_statement.rb', line 158 def merge(import_statement) if import_statement.default_import @default_import = import_statement.default_import clear_import_string_cache end if import_statement.named_imports? @named_imports ||= [] @named_imports.concat(import_statement.named_imports) @named_imports.sort!.uniq! clear_import_string_cache end end |
#named_imports? ⇒ Boolean
Returns true if there are named imports.
109 110 111 |
# File 'lib/import_js/import_statement.rb', line 109 def named_imports? !named_imports.nil? && !named_imports.empty? end |
#set_default_import(value) ⇒ Object
Sets the default_import and clears the original import string cache.
83 84 85 86 |
# File 'lib/import_js/import_statement.rb', line 83 def set_default_import(value) @default_import = value clear_import_string_cache end |
#to_import_strings(max_line_length, tab) ⇒ Array
Returns generated import statement strings.
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/import_js/import_statement.rb', line 130 def to_import_strings(max_line_length, tab) return [original_import_string] if original_import_string if declaration_keyword == 'import' # ES2015 Modules (ESM) syntax can support default imports and # named imports on the same line. if named_imports? [named_import_string(max_line_length, tab)] else [default_import_string(max_line_length, tab)] end else # const/let/var strings = [] if default_import strings << default_import_string(max_line_length, tab) end if named_imports? strings << named_import_string(max_line_length, tab) end strings end end |
#to_normalized ⇒ Array
Returns an array that can be used in ‘uniq!` to dedupe equal statements, e.g. `const foo = require(’foo’);‘ `import foo from ’foo’;‘.
123 124 125 |
# File 'lib/import_js/import_statement.rb', line 123 def to_normalized [default_import, named_imports, path] end |