Skip to content

Commit 84cef50

Browse files
author
飞羏
committed
fix deep calling & find sibling module
1 parent c563942 commit 84cef50

2 files changed

Lines changed: 32 additions & 10 deletions

File tree

ftdetect/javascript.vim

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,21 @@ let node#suffixesadd = [".js", ".json", ".es", ".jsx"]
88
setlocal path-=/usr/include
99
let &l:suffixesadd .= "," . join(g:node#suffixesadd, ",")
1010

11-
let &l:include = '\<require(\(["'']\)\zs[^\1]\+\ze\1)\>'
12-
let &l:includeexpr = "javascript#find(v:fname)"
11+
au BufEnter *.js call javascript#findinit()
1312

14-
let b:path = expand('<sfile>:p:h')
15-
let b:findJsFile = substitute(b:path, '[/\\]\?[^/\\]*$', '', '').'/javascript/find.js'
13+
fun! javascript#findinit()
14+
let &l:include = '\<require(\(["'']\)\zs[^\1]\+\ze\1)\>'
15+
let &l:includeexpr = "javascript#find(v:fname)"
16+
endfun
17+
18+
let node_gf_js_path = substitute(expand('<sfile>:p:h'), '[/\\]\?[^/\\]*$', '', '').'/javascript/find.js'
1619

1720
fun! javascript#find(name)
18-
let cmd = 'node'.s:WrapString(b:findJsFile).s:WrapString(getcwd()).s:WrapString(expand('%:p')).s:WrapString(a:name)
19-
return substitute(system(cmd), '^[ \n]\+\|[ \n]\+$', '', 'g')
21+
let cmd = 'node'.s:WrapString(g:node_gf_js_path).s:WrapString(getcwd()).s:WrapString(expand('%:p')).s:WrapString(a:name)
22+
let path = substitute(system(cmd), '^[ \n]\+\|[ \n]\+$', '', 'g')
23+
if path != ''
24+
return path
25+
endif
2026
endfun
2127

2228
fun! s:WrapString(str)

javascript/find.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11

22
var path = require('path');
3+
var fs = require('fs');
34
var relative = require('require-relative');
45

5-
var project = process.argv[2] || '';
6-
var file = process.argv[3] || '';
7-
var ref = process.argv[4] || '';
6+
var project = process.argv[2] || ''; // project root path
7+
var file = process.argv[3] || ''; // project file name eg: '/Users/project/index.js'
8+
var ref = process.argv[4] || ''; // ref name eg: 'react'
9+
10+
// console.log(__dirname + '/a.txt', [project, file, ref].join('\n'));
811

9-
// require('/module/file')
1012
// project absolute path
13+
// eg: require('/module/file')
1114
if (ref.match(/^\//)) {
1215
ref = path.join(project, ref);
1316
}
@@ -19,4 +22,17 @@ try {
1922
} catch(ex) {
2023
}
2124

25+
// sometimes module's ref is sibling
26+
// eg:
27+
// node_modules
28+
// a/index.js
29+
// b
30+
// in a/index.js require('b')
31+
if (!ret && ref.match(/^[^\.\/]/) && file.match(/node_modules/)) {
32+
try {
33+
ret = relative.resolve(ref, file.replace(/\/node_modules\/.*?$/, ''));
34+
} catch(ex) {
35+
}
36+
}
37+
2238
console.log(ret || '');

0 commit comments

Comments
 (0)