Skip to content

Commit cd0f1f9

Browse files
committed
Improve how multiline comments are handled
1 parent 99af4e8 commit cd0f1f9

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

node_translators/doc.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,28 @@
22
'use strict';
33

44
module.exports = function (node, indent) {
5+
var self = this, union, body;
6+
57
if (node.alreadyParsed) {
68
return '';
79
}
8-
if (!node.isDoc && node.lines.length === 1 && node.lines[0].indexOf('\n') > -1) {
9-
node.isDoc = true;
10-
node.lines = ('\n' + node.lines[0] + '\n').split('\n');
11-
}
1210

1311
if (node.isDoc) {
14-
var body = node.lines.join(this.nl + indent + ' * ');
12+
body = node.lines.join(this.nl + indent + ' * ');
1513
if (body.substring(body.length - 3) === ' * ') {
1614
body = body.substring(0, body.length - 3);
1715
}
1816
return this.nl + indent + '/** ' + body + ' */';
1917
}
20-
return this.nl + indent + '// ' + node.lines.join(this.nl + indent + '// ');
18+
19+
union = self.nl + indent + self.ws + self.ws;
20+
return node.lines.reduce(function (acc, line) {
21+
22+
if (line.indexOf('\n') > -1) {
23+
return acc.concat('/*' + line.split("\n").join(union) + '*/');
24+
}
25+
26+
return acc.concat('// ' + line);
27+
}, []).join(self.nl + indent);
28+
2129
};

test/spec/acid1-parsed.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
use bar as nsBar;
2020

21-
2221
// generic constant
2322
const FOOBAR = "foo & bar";
2423

@@ -42,7 +41,6 @@ abstract class fooBar implements namespace\fooBaz
4241
*/
4342
final public function doSomething()
4443
{
45-
4644
// do not wanna do
4745
foreach ($this->dwarf as $name => $greeting) {
4846
echo 'Hey ho {$name}, {$greeting} !';
@@ -86,7 +84,6 @@ private function shuut()
8684
}
8785
}
8886

89-
9087
// this is SPARTA !
9188
function sparta() : ?int
9289
{
@@ -144,7 +141,6 @@ public function goatIt()
144141
$x %= ($i * 2) / ($i - 1);
145142
$what = $this->$x[++$i] ? "yes!" : "noo!";
146143
}
147-
148144
// @todo $this->a_$foo
149145
return $$foo ?? false;
150146
}
@@ -157,7 +153,6 @@ public function goatIt()
157153
echo `ls -larth`;
158154
endif;
159155

160-
161156
// list version
162157
list($a, list($b, $c)) = [1, [2, 3]];
163158
print (<<<BAZ
@@ -169,7 +164,6 @@ public function goatIt()
169164
eval(<<<FOO
170165
return 'This is madness!';
171166
FOO);
172-
173167
// nested blocks
174168

175169
{

test/tests.spec.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ describe('Array', function () {
1515
});
1616
describe('Comments', function () {
1717
it('must correcty convert multiline comments that start with only one *', function () {
18-
expect(parseUnparse('<?php /*\naa\naa\naa\ncc\n*/')).toBe(['<?php', '', '/** ', ' * aa', ' * aa', ' * aa', ' * cc', ' */', ''].join('\n'));
18+
expect(parseUnparse('<?php /*\naa\naa\naa\ncc\n*/')).toBe(['<?php', '/*aa', ' aa', ' aa', ' cc*/', ''].join('\n'));
19+
});
20+
it('must correcty convert multiline comments that dont have an space', function () {
21+
expect(parseUnparse('<?php { /******\nbbb\nccc\n*****/\n//echo $ee;\n}')).toBe(['<?php', '', '{', ' /******', ' bbb', ' ccc', ' *****/', ' // echo $ee;', '}', '', ''].join('\n'));
1922
});
2023
});
2124
describe('Comments', function () {
@@ -25,7 +28,7 @@ describe('Comments', function () {
2528
});
2629
describe('Blocks', function () {
2730
it('must correcty convert nested blocks', function () {
28-
expect(parseUnparse('<?php {{\n// Code generation for this\n}}')).toBe(['<?php', '', '{', ' ', ' {', ' ', ' // Code generation for this', ' }', '', '}', '', ''].join('\n'));
31+
expect(parseUnparse('<?php {{\n// Code generation for this\n}}')).toBe(['<?php', '', '{', ' ', ' {', ' // Code generation for this', ' }', '', '}', '', ''].join('\n'));
2932
});
3033
});
3134
describe('Blocks', function () {

0 commit comments

Comments
 (0)