Rewrite syntax errors handling and test them
If a syntax errors occurs at the same place than a regular yamllint rule error, only the yamllint one is issued.
This commit is contained in:
@@ -14,7 +14,10 @@
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import yaml
|
||||
|
||||
from yamllint import config
|
||||
from yamllint.errors import LintProblem
|
||||
from yamllint import parser
|
||||
|
||||
|
||||
@@ -28,23 +31,14 @@ __license__ = 'GPLv3'
|
||||
__version__ = APP_VERSION
|
||||
|
||||
|
||||
def _lint(buffer, conf):
|
||||
def get_costemic_problems(buffer, conf):
|
||||
rules = config.get_enabled_rules(conf)
|
||||
|
||||
# Split token rules from line rules
|
||||
token_rules = [r for r in rules if r.TYPE == 'token']
|
||||
line_rules = [r for r in rules if r.TYPE == 'line']
|
||||
|
||||
# If the document contains a syntax error, save it and yield it at the
|
||||
# right line
|
||||
syntax_error = parser.get_syntax_error(buffer)
|
||||
|
||||
for elem in parser.token_or_line_generator(buffer):
|
||||
if syntax_error and syntax_error.line <= elem.line_no:
|
||||
syntax_error.level = 'error'
|
||||
yield syntax_error
|
||||
syntax_error = None
|
||||
|
||||
if isinstance(elem, parser.Token):
|
||||
for rule in token_rules:
|
||||
rule_conf = conf[rule.ID]
|
||||
@@ -62,6 +56,39 @@ def _lint(buffer, conf):
|
||||
yield problem
|
||||
|
||||
|
||||
def get_syntax_error(buffer):
|
||||
try:
|
||||
list(yaml.safe_load_all(buffer))
|
||||
except yaml.error.MarkedYAMLError as e:
|
||||
problem = LintProblem(e.problem_mark.line + 1,
|
||||
e.problem_mark.column + 1,
|
||||
'syntax error: ' + e.problem)
|
||||
problem.level = 'error'
|
||||
return problem
|
||||
|
||||
|
||||
def _lint(buffer, conf):
|
||||
# If the document contains a syntax error, save it and yield it at the
|
||||
# right line
|
||||
syntax_error = get_syntax_error(buffer)
|
||||
|
||||
for problem in get_costemic_problems(buffer, conf):
|
||||
# Insert the syntax error (if any) at the right place...
|
||||
if (syntax_error and syntax_error.line <= problem.line and
|
||||
syntax_error.column <= problem.column):
|
||||
# ... unless there is already a yamllint error, in which case the
|
||||
# syntax error is probably redundant.
|
||||
if (syntax_error.line != problem.line or
|
||||
syntax_error.column != problem.column):
|
||||
yield syntax_error
|
||||
syntax_error = None
|
||||
|
||||
yield problem
|
||||
|
||||
if syntax_error:
|
||||
yield syntax_error
|
||||
|
||||
|
||||
def lint(input, conf):
|
||||
"""Lints a YAML source.
|
||||
|
||||
|
||||
@@ -16,8 +16,6 @@
|
||||
|
||||
import yaml
|
||||
|
||||
from yamllint.errors import LintProblem
|
||||
|
||||
|
||||
class Line(object):
|
||||
def __init__(self, line_no, buffer, start, end):
|
||||
@@ -85,11 +83,3 @@ def token_or_line_generator(buffer):
|
||||
else:
|
||||
yield token
|
||||
token = next(token_gen, None)
|
||||
|
||||
|
||||
def get_syntax_error(buffer):
|
||||
try:
|
||||
yaml.safe_load_all(buffer)
|
||||
except yaml.error.MarkedYAMLError as e:
|
||||
return LintProblem(e.problem_mark.line + 1, e.problem_mark.column + 1,
|
||||
'syntax error: ' + e.problem)
|
||||
|
||||
Reference in New Issue
Block a user