Skip to content

Commit

Permalink
Throw a more useful exception on token unexpected in raw text (#31)
Browse files Browse the repository at this point in the history
* Debug Token to string conversion error
* Check each chunk in lines analysis
  • Loading branch information
kylekatarnls authored Mar 19, 2019
1 parent 37e584a commit a487840
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Phug/Lexer/Analyzer/LineAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Phug\Lexer\Scanner\InterpolationScanner;
use Phug\Lexer\State;
use Phug\Lexer\Token\TextToken;
use Phug\Lexer\TokenInterface;
use Phug\Reader;

class LineAnalyzer
Expand Down Expand Up @@ -175,6 +176,12 @@ public function getLines()
public function getFlatLines()
{
return array_map(function ($line) {
foreach ($line as $chunk) {
if ($chunk instanceof TokenInterface) {
$this->state->throwException('Unexpected '.get_class($chunk).' inside raw text.');
}
}

return implode('', $line);
}, $this->lines);
}
Expand Down
19 changes: 19 additions & 0 deletions tests/Phug/Lexer/Scanner/InterpolationScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Phug\Test\Lexer\Scanner;

use Phug\Lexer\Analyzer\LineAnalyzer;
use Phug\Lexer\State;
use Phug\Lexer\Token\ExpressionToken;
use Phug\Lexer\Token\IndentToken;
use Phug\Lexer\Token\InterpolationEndToken;
Expand All @@ -11,7 +13,9 @@
use Phug\Lexer\Token\TagInterpolationStartToken;
use Phug\Lexer\Token\TagToken;
use Phug\Lexer\Token\TextToken;
use Phug\Reader;
use Phug\Test\AbstractLexerTest;
use Phug\Util\SourceLocation;

class InterpolationScannerTest extends AbstractLexerTest
{
Expand Down Expand Up @@ -131,4 +135,19 @@ public function testScan()
TextToken::class,
]);
}

/**
* @expectedException \Phug\LexerException
* @expectedExceptionMessage Failed to lex: Unexpected Phug\Lexer\Token\InterpolationStartToken inside raw text.
*/
public function testTokenInLineAnalyzer()
{
$input = 'p #{42}';
$analyzer = new LineAnalyzer(new State($this->lexer, $input, []), new Reader($input), [
[
new InterpolationStartToken(new SourceLocation('foo.pug', 12, 43)),
],
]);
$analyzer->getFlatLines();
}
}

0 comments on commit a487840

Please sign in to comment.