From c6d19b197670cc3b3bf119d46af9fd43d33e064b Mon Sep 17 00:00:00 2001 From: Wybren Koelmans Date: Thu, 29 Jun 2017 22:55:41 +0200 Subject: [PATCH] [Yaml][Twig][Lint] Added line numbers to JSON output. --- src/Symfony/Bridge/Twig/Command/LintCommand.php | 2 +- src/Symfony/Component/Yaml/Command/LintCommand.php | 4 ++-- src/Symfony/Component/Yaml/Parser.php | 10 ++++++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Command/LintCommand.php b/src/Symfony/Bridge/Twig/Command/LintCommand.php index d20b9cf193..01857fc1af 100644 --- a/src/Symfony/Bridge/Twig/Command/LintCommand.php +++ b/src/Symfony/Bridge/Twig/Command/LintCommand.php @@ -145,7 +145,7 @@ EOF } catch (Error $e) { $twig->setLoader($realLoader); - return array('template' => $template, 'file' => $file, 'valid' => false, 'exception' => $e); + return array('template' => $template, 'file' => $file, 'line' => $e->getTemplateLine(), 'valid' => false, 'exception' => $e); } return array('template' => $template, 'file' => $file, 'valid' => true); diff --git a/src/Symfony/Component/Yaml/Command/LintCommand.php b/src/Symfony/Component/Yaml/Command/LintCommand.php index 271f2d8474..f51d4bb383 100644 --- a/src/Symfony/Component/Yaml/Command/LintCommand.php +++ b/src/Symfony/Component/Yaml/Command/LintCommand.php @@ -105,7 +105,7 @@ EOF { $prevErrorHandler = set_error_handler(function ($level, $message, $file, $line) use (&$prevErrorHandler) { if (E_USER_DEPRECATED === $level) { - throw new ParseException($message); + throw new ParseException($message, $this->getParser()->getLastLineNumberBeforeDeprecation()); } return $prevErrorHandler ? $prevErrorHandler($level, $message, $file, $line) : false; @@ -114,7 +114,7 @@ EOF try { $this->getParser()->parse($content, Yaml::PARSE_CONSTANT); } catch (ParseException $e) { - return array('file' => $file, 'valid' => false, 'message' => $e->getMessage()); + return array('file' => $file, 'line' => $e->getParsedLine(), 'valid' => false, 'message' => $e->getMessage()); } finally { restore_error_handler(); } diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index d24179d134..820ed75ad1 100644 --- a/src/Symfony/Component/Yaml/Parser.php +++ b/src/Symfony/Component/Yaml/Parser.php @@ -75,6 +75,16 @@ class Parser return $data; } + /** + * @internal + * + * @return int + */ + public function getLastLineNumberBeforeDeprecation() + { + return $this->getRealCurrentLineNb(); + } + private function doParse($value, $flags) { $this->currentLineNb = -1;