Skip to content

Commit 7705098

Browse files
committed
scan comments
1 parent 19ddc81 commit 7705098

File tree

4 files changed

+60
-28
lines changed

4 files changed

+60
-28
lines changed

src/JsFunctionsScanner.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
class JsFunctionsScanner implements FunctionsScannerInterface
1010
{
1111
protected $validFunctions;
12-
protected $parser = ['latest', []];
12+
protected $parser;
1313

1414
public function __construct(array $validFunctions = null)
1515
{
1616
$this->validFunctions = $validFunctions;
17+
$this->parser('latest');
1718
}
1819

19-
public function parser(string $version, array $options): self
20+
public function parser(string $version, array $options = ['comments' => true]): self
2021
{
2122
$this->parser = [$version, $options];
2223

src/JsNodeVisitor.php

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace Gettext\Scanner;
55

66
use Peast\Syntax\Node\CallExpression;
7+
use Peast\Syntax\Node\Comment;
78
use Peast\Syntax\Node\Node;
89

910
class JsNodeVisitor
@@ -51,20 +52,30 @@ protected function createFunction(CallExpression $node): ?ParsedFunction
5152
$position->getEnd()->getLine()
5253
);
5354

55+
static::addComments($function, $node->getCallee());
56+
5457
foreach ($node->getArguments() as $argument) {
5558
switch ($argument->getType()) {
5659
case 'Literal':
5760
$function->addArgument($argument->getValue());
61+
static::addComments($function, $argument);
62+
break;
63+
case 'TemplateLiteral':
64+
if ($argument->getExpressions()) {
65+
$function->addArgument();
66+
break;
67+
}
68+
69+
$quasis = $argument->getQuasis();
70+
$quasis = array_shift($quasis);
71+
$function->addArgument($quasis->getValue());
72+
static::addComments($function, $argument);
5873
break;
5974
default:
6075
$function->addArgument();
6176
}
6277
}
6378

64-
foreach ($node->getLeadingComments() as $comment) {
65-
$function->addComment($comment->getText());
66-
}
67-
6879
return $function;
6980
}
7081

@@ -81,4 +92,24 @@ protected static function getFunctionName(CallExpression $node): ?string
8192
return null;
8293
}
8394
}
95+
96+
protected static function addComments(ParsedFunction $function, Node $node): void
97+
{
98+
foreach ($node->getLeadingComments() as $comment) {
99+
$function->addComment(static::getComment($comment));
100+
}
101+
}
102+
103+
protected static function getComment(Comment $comment): string
104+
{
105+
$text = $comment->getText();
106+
107+
$lines = array_map(function ($line) {
108+
$line = ltrim($line, "#*/ \t");
109+
$line = rtrim($line, "#*/ \t");
110+
return trim($line);
111+
}, explode("\n", $text));
112+
113+
return trim(implode("\n", $lines));
114+
}
84115
}

tests/JsFunctionsScannerTest.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ public function testJsFunctionsExtractor()
2424
$this->assertSame(4, $function->getLine());
2525
$this->assertSame(4, $function->getLastLine());
2626
$this->assertSame($file, $function->getFilename());
27-
// $this->assertCount(1, $function->getComments());
27+
$this->assertCount(1, $function->getComments());
2828

29-
// $comments = $function->getComments();
30-
// $this->assertSame('This comment is related with the first function', array_shift($comments));
29+
$comments = $function->getComments();
30+
$this->assertSame('This comment is related with the first function', array_shift($comments));
3131

3232
//fn2
3333
$function = array_shift($functions);
@@ -96,11 +96,11 @@ public function testJsFunctionsExtractor()
9696
$this->assertSame(11, $function->getLine());
9797
$this->assertSame(11, $function->getLastLine());
9898
$this->assertSame($file, $function->getFilename());
99-
// $this->assertCount(2, $function->getComments());
99+
$this->assertCount(2, $function->getComments());
100100

101-
// $comments = $function->getComments();
102-
// $this->assertSame('fn_8();', array_shift($comments));
103-
// $this->assertSame('ALLOW: This is a comment to fn9', array_shift($comments));
101+
$comments = $function->getComments();
102+
$this->assertSame('fn_8();', array_shift($comments));
103+
$this->assertSame('ALLOW: This is a comment to fn9', array_shift($comments));
104104

105105
//fn10
106106
$function = array_shift($functions);
@@ -110,24 +110,24 @@ public function testJsFunctionsExtractor()
110110
$this->assertSame(13, $function->getLine());
111111
$this->assertSame(13, $function->getLastLine());
112112
$this->assertSame($file, $function->getFilename());
113-
// $this->assertCount(1, $function->getComments());
113+
$this->assertCount(1, $function->getComments());
114114

115-
// $comments = $function->getComments();
116-
// $this->assertSame('Comment to fn10', array_shift($comments));
115+
$comments = $function->getComments();
116+
$this->assertSame('Comment to fn10', array_shift($comments));
117117

118118
//fn11
119119
$function = array_shift($functions);
120120
$this->assertSame('fn11', $function->getName());
121-
$this->assertSame(2, $function->countArguments());
122-
$this->assertSame([null, 'arg10'], $function->getArguments());
121+
$this->assertSame(3, $function->countArguments());
122+
$this->assertSame(['arg9', 'arg10', null], $function->getArguments());
123123
$this->assertSame(16, $function->getLine());
124124
$this->assertSame(16, $function->getLastLine());
125125
$this->assertSame($file, $function->getFilename());
126-
// $this->assertCount(2, $function->getComments());
126+
$this->assertCount(2, $function->getComments());
127127

128-
// $comments = $function->getComments();
129-
// $this->assertSame('Related comment 1', array_shift($comments));
130-
// $this->assertSame('ALLOW: Related comment 2', array_shift($comments));
128+
$comments = $function->getComments();
129+
$this->assertSame('Related comment 1', array_shift($comments));
130+
$this->assertSame('ALLOW: Related comment 2', array_shift($comments));
131131

132132
//fn12
133133
$function = array_shift($functions);
@@ -137,12 +137,12 @@ public function testJsFunctionsExtractor()
137137
$this->assertSame(22, $function->getLine());
138138
$this->assertSame(28, $function->getLastLine());
139139
$this->assertSame($file, $function->getFilename());
140-
// $this->assertCount(3, $function->getComments());
140+
$this->assertCount(3, $function->getComments());
141141

142-
// $comments = $function->getComments();
143-
// $this->assertSame("Related comment\nnumber one", array_shift($comments));
144-
// $this->assertSame('Related comment 2', array_shift($comments));
145-
// $this->assertSame('ALLOW: Related comment 3', array_shift($comments));
142+
$comments = $function->getComments();
143+
$this->assertSame("Related comment\nnumber one", array_shift($comments));
144+
$this->assertSame('Related comment 2', array_shift($comments));
145+
$this->assertSame('ALLOW: Related comment 3', array_shift($comments));
146146

147147
//fn13
148148
$function = array_shift($functions);

tests/assets/functions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn9(ARG_8);
1313
/* Comment to fn10 */ fn10({});
1414
(function () {
1515
//Related comment 1
16-
fn11(/* ALLOW: Related comment 2 */ `arg9`, 'arg10' /* No related comment 3 */);
16+
fn11(/* ALLOW: Related comment 2 */ `arg9`, 'arg10' /* No related comment 3 */, `ignored dynamic ${value}`);
1717

1818
/*
1919
Related comment

0 commit comments

Comments
 (0)