Skip to content

Commit 9e64404

Browse files
committed
Fix javascript splitting for empty lists
1 parent 9bb338c commit 9e64404

File tree

2 files changed

+86
-45
lines changed

2 files changed

+86
-45
lines changed

autoload/sj/js.vim

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function! sj#js#SplitObjectLiteral()
1212

1313
let pairs = sj#ParseJsonObjectBody(from + 1, to - 1)
1414
let body = join(pairs, ",\n")
15-
if sj#settings#Read('trailing_comma')
15+
if sj#settings#Read('trailing_comma') && len(body) > 0
1616
let body .= ','
1717
endif
1818
let body = "{\n".body."\n}"
@@ -60,7 +60,7 @@ function! sj#js#JoinObjectLiteral()
6060
let body = join(lines, ' ')
6161
let body = substitute(body, ',$', '', '')
6262

63-
if sj#settings#Read('curly_brace_padding')
63+
if sj#settings#Read('curly_brace_padding') && len(body) > 0
6464
let body = '{ '.body.' }'
6565
else
6666
let body = '{'.body.'}'
@@ -168,10 +168,8 @@ function! s:SplitList(delimiter, cursor_position)
168168

169169
let items = sj#ParseJsonObjectBody(from + 1, to - 1)
170170
if empty(items)
171-
return 0
172-
endif
173-
174-
if sj#settings#Read('trailing_comma')
171+
let body = start."\n".end
172+
elseif sj#settings#Read('trailing_comma')
175173
let body = start."\n".join(items, ",\n").",\n".end
176174
else
177175
let body = start."\n".join(items, ",\n")."\n".end

spec/plugin/js_spec.rb

Lines changed: 82 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -192,59 +192,102 @@
192192
end
193193
end
194194

195-
specify "object literals" do
196-
set_file_contents "{ one: two, 'three': four }"
195+
describe "object literals" do
196+
specify "basic" do
197+
set_file_contents "{ one: two, 'three': four }"
197198

198-
vim.search '{'
199-
split
199+
vim.search '{'
200+
split
200201

201-
# Fix issue with inconsistent indenting
202-
vim.normal 'gg<G'
203-
vim.write
202+
# Fix issue with inconsistent indenting
203+
vim.normal 'gg<G'
204+
vim.write
204205

205-
assert_file_contents <<~EOF
206-
{
207-
one: two,
208-
'three': four
209-
}
210-
EOF
206+
assert_file_contents <<~EOF
207+
{
208+
one: two,
209+
'three': four
210+
}
211+
EOF
211212

212-
join
213+
join
213214

214-
assert_file_contents "{ one: two, 'three': four }"
215-
end
215+
assert_file_contents "{ one: two, 'three': four }"
216+
end
216217

217-
specify "lists" do
218-
set_file_contents "[ 'one', 'two', 'three', 'four' ]"
218+
specify "empty" do
219+
set_file_contents "foo = {}"
219220

220-
vim.search '['
221-
split
221+
vim.search '{'
222+
split
222223

223-
assert_file_contents <<~EOF
224-
[
225-
'one',
226-
'two',
227-
'three',
228-
'four'
229-
]
230-
EOF
224+
# Fix issue with inconsistent indenting
225+
vim.normal 'gg<G'
226+
vim.write
231227

232-
join
228+
assert_file_contents <<~EOF
229+
foo = {
230+
231+
}
232+
EOF
233233

234-
assert_file_contents "['one', 'two', 'three', 'four']"
234+
join
235+
236+
assert_file_contents "foo = {}"
237+
end
235238
end
236239

237-
specify "lists (with trailing comma)" do
238-
set_file_contents <<~EOF
239-
[
240-
'one',
241-
'two',
242-
]
243-
EOF
240+
describe "lists" do
241+
specify "basic" do
242+
set_file_contents "[ 'one', 'two', 'three', 'four' ]"
244243

245-
join
244+
vim.search '['
245+
split
246+
247+
assert_file_contents <<~EOF
248+
[
249+
'one',
250+
'two',
251+
'three',
252+
'four'
253+
]
254+
EOF
246255

247-
assert_file_contents "['one', 'two']"
256+
join
257+
258+
assert_file_contents "['one', 'two', 'three', 'four']"
259+
end
260+
261+
specify "with trailing comma" do
262+
set_file_contents <<~EOF
263+
[
264+
'one',
265+
'two',
266+
]
267+
EOF
268+
269+
join
270+
271+
assert_file_contents "['one', 'two']"
272+
end
273+
274+
specify "empty" do
275+
set_file_contents "foo = []"
276+
vim.search('[')
277+
278+
split
279+
280+
assert_file_contents <<~EOF
281+
foo = [
282+
]
283+
EOF
284+
285+
join
286+
287+
assert_file_contents <<~EOF
288+
foo = []
289+
EOF
290+
end
248291
end
249292

250293
specify "functions" do

0 commit comments

Comments
 (0)