Skip to content

Commit

Permalink
Fix parsing for imports using non-alphanumeric assignments
Browse files Browse the repository at this point in the history
I noticed a bug in the brigade codebase where import parsing had stopped
at this line:

const $ = require('jquery');

Which is a perfectly valid import. I had been a little bit too
aggressive in 73bce8a when I fixed a different parsing bug.
  • Loading branch information
trotzig committed Mar 3, 2016
1 parent 88147b2 commit bd696a0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/import_js/import_statement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def self.parse(string)
named_imports = dest_match[:named].split(/,\s*/).map(&:strip)
else
default_import = match[:assignment]
return unless default_import =~ /\A\w+\Z/
return unless default_import =~ /\A[^\s]+\Z/

This comment has been minimized.

Copy link
@lencioni

lencioni Mar 3, 2016

Collaborator

You can use \S instead of [^\s]

This comment has been minimized.

Copy link
@trotzig

trotzig Mar 3, 2016

Author Collaborator

Ah, didn't know. Will fix.

end

new(
Expand Down
9 changes: 9 additions & 0 deletions spec/import_js/import_statement_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
expect(subject.path).to eq('foo')
end

context 'and it has a non-alphanumeric variable name' do
let(:string) { "import $ from 'jquery';" }

it 'returns a valid ImportStatement instance' do
expect(subject.assignment).to eq('$')
expect(subject.path).to eq('jquery')
end
end

context 'and it has line breaks' do
let(:string) { "import foo\n from 'foo';" }

Expand Down

0 comments on commit bd696a0

Please sign in to comment.