Skip to content

Commit e86e84c

Browse files
committed
Add suport for react-native
1 parent 13b3121 commit e86e84c

File tree

3 files changed

+112
-1
lines changed

3 files changed

+112
-1
lines changed

autoload/node.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
" Vim by default sets the filetype to JavaScript for the following suffices.
22
" And, yes, it has .jsx there.
3-
let node#suffixesadd = [".js", ".json", ".es", ".jsx"]
3+
let node#suffixesadd = [".js", ".json", ".es", ".jsx", ".ios.js", ".android.js", ".ios.jsx", ".android.jsx"]
44

55
function! node#initialize(root)
66
let b:node_root = a:root

autoload/node/lib.vim

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,16 @@ function! node#lib#version()
3333
endfunction
3434

3535
function! s:absolutize(name, from)
36+
let PACKAGENAME = s:nameFromPackage(b:node_root . "/package.json")
3637
if a:name =~# s:ABSPATH
3738
return a:name
3839
elseif a:name =~# s:RELPATH
3940
let dir = isdirectory(a:from) ? a:from : fnamemodify(a:from, ":h")
4041
return dir . "/" . a:name
42+
elseif a:name =~# "^" . PACKAGENAME && !empty(PACKAGENAME)
43+
let l:slashPos = match(a:name, "/")
44+
let finalPath = a:name[l:slashPos :]
45+
return b:node_root . finalPath
4146
else
4247
return b:node_root . "/node_modules/" . a:name
4348
endif
@@ -86,13 +91,41 @@ function! s:mainFromPackage(path)
8691
endfor
8792
endfunction
8893

94+
function! s:nameFromPackage(path)
95+
if !filereadable(a:path)
96+
return
97+
endif
98+
for line in readfile(a:path)
99+
if line !~# '"name"\s*:' | continue | endif
100+
return matchstr(line, '"name"\s*:\s*"\zs[^"]\+\ze"')
101+
endfor
102+
return ""
103+
endfunction
104+
89105
function! s:resolveSuffix(path)
90106
for suffix in s:uniq([""] + g:node#suffixesadd + split(&l:suffixesadd, ","))
91107
let path = a:path . suffix
92108
if filereadable(path) | return path | endif
93109
endfor
94110
endfunction
95111

112+
function! s:resolveReactNativeGlobal(path)
113+
" React-Native allow to use absolute path using the name of a package.json
114+
" https://medium.com/@davidjwoody/how-to-use-absolute-paths-in-react-native-6b06ae3f65d1
115+
" Here is implemented only with the main one
116+
if filereadable(b:node_root . "/package.json")
117+
let name = s:nameFromPackage(b:node_root . "/package.json")
118+
119+
let slashPos = match(a:path, "/")
120+
let prefix = a:path[0: slashPos]
121+
let finalPath = a:path[slashPos:]
122+
123+
if (prefix == name)
124+
return s:resolve(b:node_root . finalPath)
125+
endif
126+
endif
127+
endfunction
128+
96129
let s:GLOB_WILDIGNORE = 1
97130

98131
function! node#lib#glob(name)

test/autoload_test.rb

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,84 @@
253253
$vim.feedkeys "$hhgf"
254254
$vim.echo(%(bufname("%"))).must_equal target
255255
end
256+
257+
it "must edit ./other.android.js relative to file" do
258+
touch File.join(@dir, "foo", "index.js"), %(require("./other"))
259+
touch File.join(@dir, "foo", "other.android.js")
260+
261+
$vim.edit File.join(@dir, "foo", "index.js")
262+
$vim.feedkeys "f.gf"
263+
264+
bufname = File.realpath($vim.echo(%(bufname("%"))))
265+
bufname.must_equal File.join(@dir, "foo", "other.android.js")
266+
end
267+
268+
it "must edit ./other.ios.js relative to file" do
269+
touch File.join(@dir, "foo", "index.js"), %(require("./other"))
270+
touch File.join(@dir, "foo", "other.ios.js")
271+
272+
$vim.edit File.join(@dir, "foo", "index.js")
273+
$vim.feedkeys "f.gf"
274+
275+
bufname = File.realpath($vim.echo(%(bufname("%"))))
276+
bufname.must_equal File.join(@dir, "foo", "other.ios.js")
277+
end
278+
279+
it "must edit ./other.ios.js relative to file if both android and ios exist" do
280+
touch File.join(@dir, "foo", "index.js"), %(require("./other"))
281+
touch File.join(@dir, "foo", "other.ios.js")
282+
touch File.join(@dir, "foo", "other.android.js")
283+
284+
$vim.edit File.join(@dir, "foo", "index.js")
285+
$vim.feedkeys "f.gf"
286+
287+
bufname = File.realpath($vim.echo(%(bufname("%"))))
288+
bufname.must_equal File.join(@dir, "foo", "other.ios.js")
289+
end
290+
291+
it "must edit ./node_modules/foo/index.android.js given foo" do
292+
touch File.join(@dir, "index.js"), %(require("foo"))
293+
target = touch File.join(@dir, "node_modules", "foo", "index.android.js")
294+
295+
$vim.edit File.join(@dir, "index.js")
296+
$vim.feedkeys "$hhgf"
297+
$vim.echo(%(bufname("%"))).must_equal target
298+
end
299+
300+
it "must edit ./bar.js given packagejsonname/bar" do
301+
touch File.join(@dir, "foo", "index.js"), %(require("packagejsonname/bar"))
302+
touch File.join(@dir, "package.json"), %({ "name": "packagejsonname" })
303+
touch File.join(@dir, "bar.js")
304+
305+
$vim.edit File.join(@dir, "foo", "index.js")
306+
$vim.feedkeys "$hhgf"
307+
308+
bufname = File.realpath($vim.echo(%(bufname("%"))))
309+
bufname.must_equal File.join(@dir, "bar.js")
310+
end
311+
312+
it "must edit ./node_modules/foo/bar.js given foo/bar" do
313+
touch File.join(@dir, "index.js"), %(require("foo/bar"))
314+
touch File.join(@dir, "package.json"), %({ "name": "packagejsonname" })
315+
target = touch File.join(@dir, "node_modules", "foo", "bar.js")
316+
317+
$vim.edit File.join(@dir, "index.js")
318+
$vim.feedkeys "$hhgf"
319+
$vim.echo(%(bufname("%"))).must_equal target
320+
end
321+
322+
it "must edit ./bar.js given packagejsonname/bar event if module exists" do
323+
touch File.join(@dir, "foo", "index.js"), %(require("packagejsonname/bar"))
324+
touch File.join(@dir, "package.json"), %({ "name": "packagejsonname" })
325+
target = touch File.join(@dir, "node_modules", "packagejsonname", "bar.js")
326+
touch File.join(@dir, "bar.js")
327+
328+
$vim.edit File.join(@dir, "foo", "index.js")
329+
$vim.feedkeys "$hhgf"
330+
331+
bufname = File.realpath($vim.echo(%(bufname("%"))))
332+
bufname.must_equal File.join(@dir, "bar.js")
333+
end
256334
end
257335

258336
describe "Goto file with split" do

0 commit comments

Comments
 (0)