Commit
As reported by @lencioni in #9: If I have a section of required modules at the top that looks like this: const MyModule = require('path/to/my_module'); const MyModuleThatHasALongName = require('path/to/my_module_that_has_a_long_name'); const SomeModule = require('path/to/some_module'); import-js gets confused when adding a new module to the list: const AnotherModule = require('path/to/another_module'); const MyModule = require('path/to/my_module'); const MyModuleThatHasALongName = require('path/to/my_module_that_has_a_long_name'); const SomeModule = require('path/to/some_module'); where I would expect: const AnotherModule = require('path/to/another_module'); const MyModule = require('path/to/my_module'); const MyModuleThatHasALongName = require('path/to/my_module_that_has_a_long_name'); const SomeModule = require('path/to/some_module'); It would be nice if import-js worked better with this format. I've added basic support for line breaks in this commit. I say basic because I expect there to be edge cases that I haven't thought of. We use simple regexs to parse the js file content, and like all simple regex parsers, they are bound to fail. However, I don't want to bring in a js parsing framework just yet. So in this commit I extend the regex approach with some logic to deal with possible line-breaks. One idea also listed in #9 was to add support for import-js to automatically line-break long lines. I haven't made that change in this commit (new imports are still one-line), but I might do that later if I find time.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,42 +103,55 @@ def window | |
# @return [number] the number of lines changed | ||
def write_imports(variable_name, path_to_file) | ||
current_imports = find_current_imports | ||
before_length = current_imports.length | ||
current_imports.length.times do | ||
|
||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
trotzig
Collaborator
|
||
unless buffer[current_imports[:newline_count] + 1].strip.empty? | ||
# Add a newline after imports | ||
buffer.append(current_imports[:newline_count], '') | ||
end | ||
|
||
# delete current imports | ||
current_imports[:newline_count].times do | ||
buffer.delete(1) | ||
end | ||
|
||
imports = current_imports[:imports] | ||
before_length = imports.length | ||
|
||
declaration_keyword = @config['declaration_keyword'] | ||
current_imports << "#{declaration_keyword} #{variable_name} = require('#{path_to_file}');" | ||
imports << "#{declaration_keyword} #{variable_name} = require('#{path_to_file}');" | ||
|
||
current_imports.sort!.uniq! do |import| | ||
imports.sort!.uniq! do |import| | ||
# Determine uniqueness by discarding the declaration keyword (`const`, | ||
# `let`, or `var`). | ||
import.sub(/\A(const|let|var)\s+/, '') | ||
# `let`, or `var`) and normalizing multiple whitespace chars to single | ||
# spaces. | ||
import.sub(/\A(const|let|var)\s+/, '').sub(/\s\s+/s, ' ') | ||
end | ||
|
||
current_imports.reverse.each do |import_line| | ||
# add imports back in | ||
imports.reverse.each do |import_line| | ||
buffer.append(0, import_line) | ||
end | ||
|
||
after_length = current_imports.length | ||
unless buffer[current_imports.length + 1].strip.empty? | ||
# Add a newline after imports | ||
buffer.append(current_imports.length, '') | ||
after_length += 1 | ||
end | ||
after_length - before_length | ||
imports.length - before_length # truthy if the import was new | ||
end | ||
|
||
# @return [Array] | ||
def find_current_imports | ||
lines = [] | ||
imports_blob = '' | ||
buffer.count.times do |n| | ||
line = buffer[n + 1] | ||
break unless line.match(/^(const|let|var)\s+.+=\s+require\(.*\).*;\s*$/) | ||
lines << line | ||
break if line.strip.empty? | ||
imports_blob << "\n#{line}" | ||
end | ||
lines | ||
|
||
imports = imports_blob.scan(/(?:const|let|var)\s+.+=\s+require\(.*\).*;/) | ||
newline_count = imports.length + imports.reduce(0) do |sum, import| | ||
sum + import.scan(/\n/).length | ||
end | ||
{ | ||
imports: imports, | ||
newline_count: newline_count | ||
} | ||
end | ||
|
||
# @param variable_name [String] | ||
|
2 comments
on commit 1cc2157
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a little on the tired side so I'm having a hard time following along, but it seems 👍 to me. Thanks for working on this!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it's a little hacky. My approach here is to make sure that specs are clear and correct and then not worrying too much about the implementation. There's always room for making it nicer later on.
It might be nice to add a comment around here explaining your strategy, to make the code a little more readable.