Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve s:resolve to also look in parent directories #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion autoload/node/lib.vim
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,25 @@ function! s:resolve(path)
if !empty(path_with_suffix) | return path_with_suffix | endif
endif

if isdirectory(a:path) | return s:resolveFromDirectory(a:path) | endif
if isdirectory(a:path)
return s:resolveFromDirectory(a:path)
else
" If we're looking for a module in node_modules and it can't
" be found, Node will also try searching for it in parent
" directories.
let modulename = fnamemodify(a:path, ":t")
let parentname = fnamemodify(a:path, ":h:t")
if parentname == "node_modules"
let root = fnamemodify(a:path, ":h:h:h")
while 1
let dir = l:root . "/node_modules/" . l:modulename
if isdirectory(l:dir) | return s:resolveFromDirectory(l:dir) | endif
let parent = fnamemodify(root, ":h")
if l:parent == l:root | break | endif
let root = l:parent
endwhile
endif
endif
endfunction

function! s:resolveFromDirectory(path)
Expand Down
8 changes: 8 additions & 0 deletions test/autoload/lib_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@ def find(name)
find(".").must_equal target
end

it "must return node_modules/foo/index.js given foo in packages/bar/index.js" do
target = touch File.join(@dir, "node_modules", "foo", "index.js")
touch File.join(@dir, "node_modules", "foo", "package.json"), JSON.dump(:main => "")
touch File.join(@dir, "packages", "bar", "package.json")
$vim.edit File.join(@dir, "packages", "bar", "index.js")
find("foo").must_equal target
end

it "must return node_modules/foo/index.js given foo" do
index = File.join(@dir, "node_modules", "foo", "index.js")
touch index
Expand Down