From 212edb5a7d7a5b05f1d0bb7d300c78dcec2d4bdc Mon Sep 17 00:00:00 2001 From: cwis Date: Thu, 29 Nov 2018 11:49:27 +0100 Subject: [PATCH] Improve s:resolve to also look in parent directories --- autoload/node/lib.vim | 20 +++++++++++++++++++- test/autoload/lib_test.rb | 8 ++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/autoload/node/lib.vim b/autoload/node/lib.vim index 1c7a8c1..838c2e9 100644 --- a/autoload/node/lib.vim +++ b/autoload/node/lib.vim @@ -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) diff --git a/test/autoload/lib_test.rb b/test/autoload/lib_test.rb index e39280c..180de40 100644 --- a/test/autoload/lib_test.rb +++ b/test/autoload/lib_test.rb @@ -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