Skip to content

Commit

Permalink
Simplify comments extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy committed May 2, 2018
1 parent 8f71278 commit 9cc71ef
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions src/JsFunctionsScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,40 +42,31 @@ public function disableCommentsExtraction() {
*/
public function saveGettextFunctions( Translations $translations, array $options ) {
$ast = Peast::latest( $this->code, [
'sourceType' => Peast::SOURCE_TYPE_SCRIPT,
'sourceType' => Peast::SOURCE_TYPE_MODULE,
'comments' => false !== $this->extractComments,
] )->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 ) {
$traverser->addFunction( function ( $node ) use ( &$translations, $options, &$all_comments ) {
$functions = $options['functions'];
$file = $options['file'];

/* @var Node $node */

if ( 'VariableDeclaration' === $node->getType() ) {
/* @var VariableDeclaration $node */
foreach ( $node->getDeclarations() as $declarator ) {
$declarator->getInit()->setLeadingComments(
$declarator->getInit()->getLeadingComments() + $node->getLeadingComments()
);

if ( 'CallExpression' === $declarator->getInit()->getType() && 'Identifier' === $declarator->getInit()->getCallee()->getType() ) {
$declarator->getInit()->getCallee()->setLeadingComments(
$declarator->getInit()->getCallee()->getLeadingComments() + $node->getLeadingComments()
);
}
}
foreach( $node->getLeadingComments() as $comment ) {
$all_comments[] = $comment;
}

if ( 'CallExpression' !== $node->getType() ) {
/* @var CallExpression $node */
if ( 'CallExpression' !== $node->getType() || 'Identifier' !== $node->getCallee()->getType() ) {
return;
}

Expand All @@ -88,22 +79,22 @@ public function saveGettextFunctions( Translations $translations, array $options
/* @var Identifier $callee */
$callee = $node->getCallee();

while ( 'MemberExpression' === $callee->getType() ) {
$callee = $callee->getObject();
}

if ( ! isset( $functions[ $callee->getName() ] ) ) {
return;
}

foreach( $callee->getLeadingComments() as $comment ) {
$all_comments[] = $comment;
}

$domain = $context = $original = $plural = null;
$args = [];

$comments = $node->getLeadingComments() + $callee->getLeadingComments();

/* @var Node $argument */
foreach ( $node->getArguments() as $argument ) {
$comments = array_merge( $comments, $argument->getLeadingComments() );
foreach( $argument->getLeadingComments() as $comment ) {
$all_comments[] = $comment;
}

if ( 'Identifier' === $argument->getType() ) {
$args[] = ''; // The value doesn't matter as it's unused.
Expand Down Expand Up @@ -156,14 +147,26 @@ public function saveGettextFunctions( Translations $translations, array $options
$translation->addReference( $file, $node->getLocation()->getStart()->getLine() );

/* @var \Peast\Syntax\Node\Comment $comment */
foreach ( $comments as $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 = [];
}
}
} );

Expand Down

0 comments on commit 9cc71ef

Please sign in to comment.