line-length: Generalize ...-inline-mappings for corner cases
This commit refactors the `allow-non-breakable-inline-mappings` logic to
use YAML tokens and avoid crashes or erroneous reports on cases like:
```yaml
- {a: "http://localhost/very/very/very/very/very/very/long/url"
}
```
```yaml
dict:
{a: long long long long long long long, b: nospace}
```
```yaml
- long_line: http://localhost/very/very/long/url
```
```yaml
long_line: and+some+space+at+the+end <-- extra spaces
```
For reference see:
https://github.com/adrienverge/yamllint/pull/17#issuecomment-247805799
This commit is contained in:
committed by
Allan Lewis
parent
e56a7c788c
commit
d3cd8ba332
@@ -113,7 +113,35 @@ class LineLengthTestCase(RuleTestCase):
|
|||||||
'long_line: http://localhost/very/very/long/url\n'
|
'long_line: http://localhost/very/very/long/url\n'
|
||||||
'...\n', conf, problem=(2, 21))
|
'...\n', conf, problem=(2, 21))
|
||||||
|
|
||||||
|
conf = ('line-length: {max: 20, allow-non-breakable-words: yes}\n'
|
||||||
|
'trailing-spaces: disable')
|
||||||
|
self.check('---\n'
|
||||||
|
'loooooooooong+word+and+some+space+at+the+end \n',
|
||||||
|
conf, problem=(2, 21))
|
||||||
|
|
||||||
|
def test_non_breakable_inline_mappings(self):
|
||||||
conf = 'line-length: {max: 20, ' \
|
conf = 'line-length: {max: 20, ' \
|
||||||
'allow-non-breakable-inline-mappings: yes}'
|
'allow-non-breakable-inline-mappings: yes}'
|
||||||
self.check('---\n'
|
self.check('---\n'
|
||||||
'long_line: http://localhost/very/very/long/url\n', conf)
|
'long_line: http://localhost/very/very/long/url\n'
|
||||||
|
'long line: http://localhost/very/very/long/url\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'- long line: http://localhost/very/very/long/url\n', conf)
|
||||||
|
|
||||||
|
self.check('---\n'
|
||||||
|
'long_line: http://localhost/short/url + word\n'
|
||||||
|
'long line: http://localhost/short/url + word\n',
|
||||||
|
conf, problem1=(2, 21), problem2=(3, 21))
|
||||||
|
|
||||||
|
conf = ('line-length: {max: 20,'
|
||||||
|
' allow-non-breakable-inline-mappings: yes}\n'
|
||||||
|
'trailing-spaces: disable')
|
||||||
|
self.check('---\n'
|
||||||
|
'long_line: and+some+space+at+the+end \n',
|
||||||
|
conf, problem=(2, 21))
|
||||||
|
self.check('---\n'
|
||||||
|
'long line: and+some+space+at+the+end \n',
|
||||||
|
conf, problem=(2, 21))
|
||||||
|
self.check('---\n'
|
||||||
|
'- long line: and+some+space+at+the+end \n',
|
||||||
|
conf, problem=(2, 21))
|
||||||
|
|||||||
@@ -87,6 +87,7 @@ Use this rule to set a limit to lines length.
|
|||||||
http://localhost/very/very/very/very/very/very/very/very/long/url
|
http://localhost/very/very/very/very/very/very/very/very/long/url
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from yamllint.linter import LintProblem
|
from yamllint.linter import LintProblem
|
||||||
@@ -99,6 +100,18 @@ CONF = {'max': int,
|
|||||||
'allow-non-breakable-inline-mappings': bool}
|
'allow-non-breakable-inline-mappings': bool}
|
||||||
|
|
||||||
|
|
||||||
|
def check_inline_mapping(line):
|
||||||
|
loader = yaml.SafeLoader(line.content)
|
||||||
|
while loader.peek_token():
|
||||||
|
if isinstance(loader.get_token(), yaml.BlockMappingStartToken):
|
||||||
|
while loader.peek_token():
|
||||||
|
if isinstance(loader.get_token(), yaml.ValueToken):
|
||||||
|
t = loader.get_token()
|
||||||
|
if isinstance(t, yaml.ScalarToken):
|
||||||
|
return ' ' not in line.content[t.start_mark.column:]
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def check(conf, line):
|
def check(conf, line):
|
||||||
if line.end - line.start > conf['max']:
|
if line.end - line.start > conf['max']:
|
||||||
conf['allow-non-breakable-words'] |= \
|
conf['allow-non-breakable-words'] |= \
|
||||||
@@ -115,10 +128,8 @@ def check(conf, line):
|
|||||||
if line.buffer.find(' ', start, line.end) == -1:
|
if line.buffer.find(' ', start, line.end) == -1:
|
||||||
return
|
return
|
||||||
|
|
||||||
if conf['allow-non-breakable-inline-mappings']:
|
if (conf['allow-non-breakable-inline-mappings'] and
|
||||||
line_yaml = yaml.safe_load(line.content)
|
check_inline_mapping(line)):
|
||||||
if (isinstance(line_yaml, dict) and
|
|
||||||
' ' not in line_yaml.popitem()[1]):
|
|
||||||
return
|
return
|
||||||
|
|
||||||
yield LintProblem(line.line_no, conf['max'] + 1,
|
yield LintProblem(line.line_no, conf['max'] + 1,
|
||||||
|
|||||||
Reference in New Issue
Block a user