Skip to content

Commit 08a6f60

Browse files
committed
Fix jashkenas#4290 definitively, with improved output for arrays with interspersed block comments
1 parent 0c6364b commit 08a6f60

File tree

3 files changed

+116
-7
lines changed

3 files changed

+116
-7
lines changed

lib/coffeescript/nodes.js

Lines changed: 27 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/nodes.coffee

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,13 +1458,23 @@ exports.Arr = class Arr extends Base
14581458

14591459
compiledObjs = (obj.compileToFragments o, LEVEL_LIST for obj in @objects)
14601460
for fragments, index in compiledObjs
1461-
if index
1461+
for fragment in fragments when fragment.type is 'HereComment'
1462+
fragment.code = fragment.code.trim()
1463+
if index isnt 0
14621464
answer.push @makeCode ", "
14631465
answer.push fragments...
1464-
if fragmentsToText(answer).indexOf('\n') >= 0
1465-
answer.unshift @makeCode "[\n#{o.indent}"
1466+
if '\n' in fragmentsToText(answer)
1467+
for fragment, fragmentIndex in answer
1468+
if fragment.type is 'HereComment'
1469+
fragment.code = "#{multident(fragment.code, o.indent)}\n#{o.indent}"
1470+
fragment.code = "\n#{fragment.code}" unless fragmentIndex is 0
1471+
else if fragment.code is ', '
1472+
fragment.code = ','
1473+
answer.unshift @makeCode "[\n#{unless answer[0].type is 'HereComment' then o.indent else ''}"
14661474
answer.push @makeCode "\n#{@tab}]"
14671475
else
1476+
for fragment in answer when fragment.type is 'HereComment'
1477+
fragment.code = "#{fragment.code} "
14681478
answer.unshift @makeCode '['
14691479
answer.push @makeCode ']'
14701480
answer

test/comments.coffee

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,82 @@ test "#4290: Block comments in array literals", ->
403403
]
404404
arrayEq arr, [3, 42]
405405

406+
test "Block comments in array literals are properly indented 1", ->
407+
eqJS '''
408+
arr = [
409+
### ! ###
410+
3
411+
42
412+
]''', '''
413+
var arr;
414+
415+
arr = [/* ! */ 3, 42];'''
416+
417+
test "Block comments in array literals are properly indented 2", ->
418+
eqJS '''
419+
arr = [
420+
### ###
421+
3
422+
###
423+
What is the meaning of life, the universe, and everything?
424+
###
425+
42
426+
]''', '''
427+
var arr;
428+
429+
arr = [
430+
/* */
431+
3,
432+
/*
433+
What is the meaning of life, the universe, and everything?
434+
*/
435+
42
436+
];'''
437+
438+
test "Block comments in array literals are properly indented 3", ->
439+
eqJS '''
440+
arr = [
441+
###
442+
How many stooges are there?
443+
###
444+
3
445+
### Who’s on first? ###
446+
'Who'
447+
]''', '''
448+
var arr;
449+
450+
arr = [
451+
/*
452+
How many stooges are there?
453+
*/
454+
3,
455+
/* Who’s on first? */
456+
'Who'
457+
];'''
458+
459+
test "Block comments in array literals are properly indented 4", ->
460+
eqJS '''
461+
arr = [
462+
1
463+
###
464+
How many stooges are there?
465+
###
466+
3
467+
### Who’s on first? ###
468+
'Who'
469+
]''', '''
470+
var arr;
471+
472+
arr = [
473+
1,
474+
/*
475+
How many stooges are there?
476+
*/
477+
3,
478+
/* Who’s on first? */
479+
'Who'
480+
];'''
481+
406482
test "Line comments are properly indented", ->
407483
eqJS '''
408484
# Unindented comment

0 commit comments

Comments
 (0)