feature #23294 [Yaml][Lint] Add line numbers to JSON output. (WybrenKoelmans)

This PR was merged into the 4.0-dev branch.

Discussion
----------

[Yaml][Lint] Add line numbers to JSON output.

| Q             | A
| ------------- | ---
| Branch?       | 2.7?
| Bug fix?      | no?
| New feature?  | yes?
| BC breaks?    | no?
| Deprecations? | no?
| Tests pass?   | Hopefully
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | TODO?

- [ ] Run tests?
- [ ] Check if it will break BC?
- [ ] Update changelog?

The JSON output is not very useful for me without the line number. I don't want to have to parse it from the message.

Is this the right way of doing it?

With PR:
```
[
    {
        "file": "",
        "line": 13,
        "valid": false,
        "message": "Unable to parse at line 13 (near \"sdf \")."
    }
]
```

Before:
```
[
    {
        "file": "",
        "valid": false,
        "message": "Unable to parse at line 13 (near \"sdf \")."
    }
]
```

Commits
-------

c6d19b1976 [Yaml][Twig][Lint] Added line numbers to JSON output.
This commit is contained in:
Fabien Potencier 2017-06-29 23:22:16 +02:00
commit 09321838da
3 changed files with 13 additions and 3 deletions

View File

@ -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);

View File

@ -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();
}

View File

@ -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;