Merge pull request #3132 from caitp/issue-3132

Format block-comments better
This commit is contained in:
Jeremy Ashkenas
2013-10-20 09:08:40 -07:00
3 changed files with 193 additions and 3 deletions

View File

@@ -825,8 +825,9 @@
Comment.prototype.makeReturn = THIS;
Comment.prototype.compileNode = function(o, level) {
var code;
code = "/*" + (multident(this.comment, this.tab)) + (__indexOf.call(this.comment, '\n') >= 0 ? "\n" + this.tab : '') + "*/";
var code, comment;
comment = this.comment.replace(/^(\s*)#/gm, "$1 *");
code = "/*" + (multident(comment, this.tab)) + (__indexOf.call(comment, '\n') >= 0 ? "\n" + this.tab : '') + " */";
if ((level || o.level) === LEVEL_TOP) {
code = o.indent + code;
}

View File

@@ -556,7 +556,8 @@ exports.Comment = class Comment extends Base
makeReturn: THIS
compileNode: (o, level) ->
code = "/*#{multident @comment, @tab}#{if '\n' in @comment then "\n#{@tab}" else ''}*/"
comment = @comment.replace /^(\s*)#/gm, "$1 *"
code = "/*#{multident comment, @tab}#{if '\n' in comment then "\n#{@tab}" else ''} */"
code = o.indent + code if (level or o.level) is LEVEL_TOP
[@makeCode("\n"), @makeCode(code)]

View File

@@ -211,3 +211,191 @@ test "#2916: block comment before implicit call with implicit object", ->
### ###
fn
a: yes
test "#3132: Format single-line block comment nicely", ->
input = """
### Single-line block comment without additional space here => ###"""
result = """
/* Single-line block comment without additional space here => */
"""
eq CoffeeScript.compile(input, bare: on), result
test "#3132: Format multi-line block comment nicely", ->
input = """
###
# Multi-line
# block
# comment
###"""
result = """
/*
* Multi-line
* block
* comment
*/
"""
eq CoffeeScript.compile(input, bare: on), result
test "#3132: Format simple block comment nicely", ->
input = """
###
No
Preceding hash
###"""
result = """
/*
No
Preceding hash
*/
"""
eq CoffeeScript.compile(input, bare: on), result
test "#3132: Format indented block-comment nicely", ->
input = """
fn = () ->
###
# Indented
Multiline
###
1"""
result = """
var fn;
fn = function() {
/*
* Indented
Multiline
*/
return 1;
};
"""
eq CoffeeScript.compile(input, bare: on), result
# Although adequately working, block comment-placement is not yet perfect.
# (Considering a case where multiple variables have been declared …)
test "#3132: Format jsdoc-style block-comment nicely", ->
input = """
###*
# Multiline for jsdoc-"@doctags"
#
# @type {Function}
###
fn = () -> 1
"""
result = """
/**
* Multiline for jsdoc-"@doctags"
*
* @type {Function}
*/
var fn;
fn = function() {
return 1;
};
"""
eq CoffeeScript.compile(input, bare: on), result
# Although adequately working, block comment-placement is not yet perfect.
# (Considering a case where multiple variables have been declared …)
test "#3132: Format hand-made (raw) jsdoc-style block-comment nicely", ->
input = """
###*
* Multiline for jsdoc-"@doctags"
*
* @type {Function}
###
fn = () -> 1
"""
result = """
/**
* Multiline for jsdoc-"@doctags"
*
* @type {Function}
*/
var fn;
fn = function() {
return 1;
};
"""
eq CoffeeScript.compile(input, bare: on), result
# Although adequately working, block comment-placement is not yet perfect.
# (Considering a case where multiple variables have been declared …)
test "#3132: Place block-comments nicely", ->
input = """
###*
# A dummy class definition
#
# @class
###
class DummyClass
###*
# @constructor
###
constructor: ->
###*
# Singleton reference
#
# @type {DummyClass}
###
@instance = new DummyClass()
"""
result = """
/**
* A dummy class definition
*
* @class
*/
var DummyClass;
DummyClass = (function() {
/**
* @constructor
*/
function DummyClass() {}
/**
* Singleton reference
*
* @type {DummyClass}
*/
DummyClass.instance = new DummyClass();
return DummyClass;
})();
"""
eq CoffeeScript.compile(input, bare: on), result