Skip to content

Commit

Permalink
feature #23294 [Yaml][Lint] Add line numbers to JSON output. (WybrenK…
Browse files Browse the repository at this point in the history
…oelmans)

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

c6d19b1 [Yaml][Twig][Lint] Added line numbers to JSON output.
  • Loading branch information
fabpot authored and xabbuh committed Jun 30, 2017
1 parent e891d55 commit 1baac5a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Twig/Command/LintCommand.php
Expand Up @@ -145,7 +145,7 @@ private function validate(Environment $twig, $template, $file)
} 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);
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Yaml/Command/LintCommand.php
Expand Up @@ -105,7 +105,7 @@ private function validate($content, $file = null)
{
$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;
Expand All @@ -114,7 +114,7 @@ private function validate($content, $file = null)
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();
}
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/Yaml/Parser.php
Expand Up @@ -124,6 +124,16 @@ public function parse($value, $flags = 0)
return $data;
}

/**
* @internal
*
* @return int
*/
public function getLastLineNumberBeforeDeprecation()
{
return $this->getRealCurrentLineNb();
}

private function doParse($value, $flags)
{
$this->currentLineNb = -1;
Expand Down

0 comments on commit 1baac5a

Please sign in to comment.