/
JsFunctionsScanner.php
195 lines (161 loc) · 5.79 KB
/
JsFunctionsScanner.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
namespace WP_CLI\I18n;
use Gettext\Translations;
use Gettext\Utils\JsFunctionsScanner as GettextJsFunctionsScanner;
use Gettext\Utils\ParsedComment;
use Peast\Peast;
use Peast\Syntax\Node;
use Peast\Traverser;
final class JsFunctionsScanner extends GettextJsFunctionsScanner {
/**
* If not false, comments will be extracted.
*
* @var string|false|array
*/
private $extractComments = false;
/**
* Enable extracting comments that start with a tag (if $tag is empty all the comments will be extracted).
*
* @param mixed $tag
*/
public function enableCommentsExtraction( $tag = '' ) {
$this->extractComments = $tag;
}
/**
* Disable comments extraction.
*/
public function disableCommentsExtraction() {
$this->extractComments = false;
}
/**
* {@inheritdoc}
*/
public function saveGettextFunctions( Translations $translations, array $options ) {
$ast = Peast::latest( $this->code, [
'sourceType' => Peast::SOURCE_TYPE_MODULE,
'comments' => false !== $this->extractComments,
'jsx' => true,
] )->parse();
$traverser = new Traverser();
$all_comments = [];
/**
* Traverse through JS code to find and extract gettext functions.
*
* Make sure translator comments in front of variable declarations
* and inside nested call expressions are available when parsing the function call.
*/
$traverser->addFunction( function ( $node ) use ( &$translations, $options, &$all_comments ) {
$functions = $options['functions'];
$file = $options['file'];
$callee = false;
/** @var Node\Node $node */
foreach( $node->getLeadingComments() as $comment ) {
$all_comments[] = $comment;
}
/** @var Node\CallExpression $node */
if ( 'CallExpression' !== $node->getType() ) {
return;
}
$callee = $this->resolveExpressionCallee( $node );
if ( ! $callee || ! isset( $functions[ $callee->getName() ] ) ) {
return;
}
/** @var Node\CallExpression $node */
foreach ( $node->getArguments() as $argument ) {
// Support nested function calls.
$argument->setLeadingComments( $argument->getLeadingComments() + $node->getLeadingComments() );
}
foreach( $callee->getLeadingComments() as $comment ) {
$all_comments[] = $comment;
}
$context = $plural = null;
$args = [];
/** @var Node\Node $argument */
foreach ( $node->getArguments() as $argument ) {
foreach( $argument->getLeadingComments() as $comment ) {
$all_comments[] = $comment;
}
if ( 'Identifier' === $argument->getType() ) {
$args[] = ''; // The value doesn't matter as it's unused.
}
if ( 'Literal' === $argument->getType() ) {
/** @var Node\Literal $argument */
$args[] = $argument->getValue();
}
}
switch ( $functions[ $callee->getName() ] ) {
case 'text_domain':
case 'gettext':
list( $original, $domain ) = array_pad( $args, 2, null );
break;
case 'text_context_domain':
list( $original, $context, $domain ) = array_pad( $args, 3, null );
break;
case 'single_plural_number_domain':
list( $original, $plural, $number, $domain ) = array_pad( $args, 4, null );
break;
case 'single_plural_number_context_domain':
list( $original, $plural, $number, $context, $domain ) = array_pad( $args, 5, null );
break;
}
if ( (string) $original !== '' && ( $domain === $translations->getDomain() || null === $translations->getDomain() ) ) {
$translation = $translations->insert( $context, $original, $plural );
$translation->addReference( $file, $node->getLocation()->getStart()->getLine() );
/** @var Node\Comment $comment */
foreach ( $all_comments as $comment ) {
if ( $node->getLocation()->getStart()->getLine() - $comment->getLocation()->getEnd()->getLine() > 1 ) {
continue;
}
if ( $node->getLocation()->getStart()->getColumn() < $comment->getLocation()->getStart()->getColumn() ) {
continue;
}
$parsed_comment = ParsedComment::create( $comment->getRawText(), $comment->getLocation()->getStart()->getLine() );
$prefixes = array_filter( (array) $this->extractComments );
if ( $parsed_comment->checkPrefixes( $prefixes ) ) {
$translation->addExtractedComment( $parsed_comment->getComment() );
}
}
if ( isset( $parsed_comment ) ) {
$all_comments = [];
}
}
} );
$traverser->traverse( $ast );
}
/**
* Resolve the callee of a call expression using known formats.
*
* @param Node\CallExpression $node The call expression whose callee to resolve.
*
* @return Node\Identifier|bool The identifier if resolved. False if not.
*/
private function resolveExpressionCallee( Node\CallExpression $node ) {
// If the callee is a simple identifier it can simply be returned.
// For example: __( "translation" ).
if ( 'Identifier' === $node->getCallee()->getType() ) {
return $node->getCallee();
}
// If the callee is a member expression resolve it to the property.
// For example: wp.i18n.__( "translation" ) or u.__( "translation" ).
if (
'MemberExpression' === $node->getCallee()->getType() &&
'Identifier' === $node->getCallee()->getProperty()->getType()
) {
return $node->getCallee()->getProperty();
}
// If the callee is a call expression as created by WebPack resolve it.
// For example: Object(u.__)( "translation" ).
if (
'CallExpression' === $node->getCallee()->getType() &&
'Identifier' === $node->getCallee()->getCallee()->getType() &&
'Object' === $node->getCallee()->getCallee()->getName() &&
! empty( $node->getCallee()->getArguments() ) &&
'MemberExpression' === $node->getCallee()->getArguments()[0]->getType() &&
'Identifier' === $node->getCallee()->getArguments()[0]->getProperty()->getType()
) {
return $node->getCallee()->getArguments()[0]->getProperty();
}
// Unknown format.
return false;
}
}