Skip to content

Commit

Permalink
Merge pull request #129 from trotzig/plural-matching
Browse files Browse the repository at this point in the history
Allow pluralized file paths to match variable
  • Loading branch information
trotzig committed Jan 17, 2016
2 parents 09291c1 + fb0cd1e commit 3e8b861
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 4 deletions.
24 changes: 20 additions & 4 deletions lib/import_js/importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,26 @@ def resolve_one_js_module(js_modules, variable_name)
def formatted_to_regex(string)
# Based on
# http://stackoverflow.com/questions/1509915/converting-camel-case-to-underscore-case-in-ruby
string.
gsub(/([a-z\d])([A-Z])/, '\1.?\2'). # separates camelCase words with '.?'
tr('-_', '.'). # replaces underscores or dashes with '.'
downcase # converts all upper to lower case

# The pattern to match in between words. The "es" and "s" match is there
# to catch pluralized folder names. There is a risk that this is overly
# aggressive and will lead to trouble down the line. In that case, we can
# consider adding a configuration option to control mapping a singular
# variable name to a plural folder name (suggested by @lencioni in #127).
# E.g.
#
# {
# "^mock": "./mocks/"
# }
split_pattern = '(es|s)?.?'

# Split up the string, allow pluralizing and a single (any) character
# in between. This will make e.g. 'fooBar' match 'foos/bar', 'foo_bar',
# and 'foobar'.
string
.gsub(/([a-z\d])([A-Z])/, '\1' + split_pattern + '\2') # camelCase
.tr('-_', split_pattern)
.downcase
end

# @return [String]
Expand Down
132 changes: 132 additions & 0 deletions spec/import_js/importer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,138 @@ def self.current_selection=(index)
end
end

context 'when the variable name matches last folder+filename' do
let(:existing_files) { ['sko/bar/foo.jsx'] }
let(:word) { 'barFoo' }
let(:text) { 'barFoo' }

it 'resolves the import' do
expect(subject).to eq(<<-EOS.strip)
import barFoo from 'sko/bar/foo';
barFoo
EOS
end

context 'when the last folder ends with an "s"' do
let(:existing_files) { ['sko/bars/foo.jsx'] }

it 'resolves the import' do
expect(subject).to eq(<<-EOS.strip)
import barFoo from 'sko/bars/foo';
barFoo
EOS
end

context 'when the variable also has "s" at the end' do
let(:word) { 'barsFoo' }
let(:text) { 'barsFoo' }

it 'resolves the import' do
expect(subject).to eq(<<-EOS.strip)
import barsFoo from 'sko/bars/foo';
barsFoo
EOS
end
end
end

context 'when the last folder ends with "es"' do
let(:existing_files) { ['sko/statuses/foo.jsx'] }
let(:word) { 'statusFoo' }
let(:text) { 'statusFoo' }

it 'resolves the import' do
expect(subject).to eq(<<-EOS.strip)
import statusFoo from 'sko/statuses/foo';
statusFoo
EOS
end

context 'when the variable also has "es" at the end' do
let(:word) { 'statusesFoo' }
let(:text) { 'statusesFoo' }

it 'resolves the import' do
expect(subject).to eq(<<-EOS.strip)
import statusesFoo from 'sko/statuses/foo';
statusesFoo
EOS
end
end
end
end

context 'when the variable name matches a few folders + filename' do
let(:existing_files) { ['sko/bar/foo/ta.jsx'] }
let(:word) { 'BarFooTa' }
let(:text) { 'BarFooTa' }

it 'resolves the import' do
expect(subject).to eq(<<-EOS.strip)
import BarFooTa from 'sko/bar/foo/ta';
BarFooTa
EOS
end

context 'when the folders end with "s"' do
let(:existing_files) { ['sko/bars/foos/ta.jsx'] }

it 'resolves the import' do
expect(subject).to eq(<<-EOS.strip)
import BarFooTa from 'sko/bars/foos/ta';
BarFooTa
EOS
end

context 'when the variable also has "s"' do
let(:word) { 'BarsFoosTa' }
let(:text) { 'BarsFoosTa' }

it 'resolves the import' do
expect(subject).to eq(<<-EOS.strip)
import BarsFoosTa from 'sko/bars/foos/ta';
BarsFoosTa
EOS
end
end
end

context 'when the folders end with "es"' do
let(:existing_files) { ['sko/statuses/buses/ta.jsx'] }
let(:word) { 'statusBusTa' }
let(:text) { 'statusBusTa' }

it 'resolves the import' do
expect(subject).to eq(<<-EOS.strip)
import statusBusTa from 'sko/statuses/buses/ta';
statusBusTa
EOS
end

context 'when the variable also has "es"' do
let(:word) { 'StatusesBusesTa' }
let(:text) { 'StatusesBusesTa' }

it 'resolves the import' do
expect(subject).to eq(<<-EOS.strip)
import StatusesBusesTa from 'sko/statuses/buses/ta';
StatusesBusesTa
EOS
end
end
end
end

context "when there are other imports under 'use strict'" do
let(:text) { <<-EOS.strip }
'use strict';
Expand Down

0 comments on commit 3e8b861

Please sign in to comment.