From 9cc71efdd5b70dff91fcc49a1c59152f28fb7ed5 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Thu, 3 May 2018 00:02:10 +0200 Subject: [PATCH] Simplify comments extraction --- src/JsFunctionsScanner.php | 53 ++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/src/JsFunctionsScanner.php b/src/JsFunctionsScanner.php index 1d4db3f6..861cae8b 100644 --- a/src/JsFunctionsScanner.php +++ b/src/JsFunctionsScanner.php @@ -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; } @@ -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. @@ -156,7 +147,15 @@ 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 ); @@ -164,6 +163,10 @@ public function saveGettextFunctions( Translations $translations, array $options $translation->addExtractedComment( $parsed_comment->getComment() ); } } + + if ( isset( $parsed_comment ) ) { + $all_comments = []; + } } } );