Check for missing or duplicated anchors, rework

pull/420/head
Сергей Михайлов 3 years ago committed by Sergei Mikhailov
parent e105228e6c
commit 6ade0ee800

@ -14,10 +14,10 @@ This page describes the rules and their options.
:local:
:depth: 1
anchors
-------
anchor-duplicates
------
.. automodule:: yamllint.rules.anchors
.. automodule:: yamllint.rules.anchor_duplicates
braces
------
@ -129,3 +129,8 @@ truthy
---------------
.. automodule:: yamllint.rules.truthy
unknown-aliases
---------------
.. automodule:: yamllint.rules.unknown_aliases

@ -0,0 +1,44 @@
# Copyright (C) 2021 Sergei Mikhailov
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from tests.common import RuleTestCase
NORMAL_ANCHOR = '''---
key: &keyanchor a
otherkey: *keyanchor
'''
DUPLICATED_ANCHOR = '''---
key1: &keyanchor a
key2: &keyanchor b
otherkey: *keyanchor
'''
class AnchorDuplicatesTestCase(RuleTestCase):
rule_id = 'anchor-duplicates'
def test_disabled(self):
conf = 'anchor-duplicates: disable'
self.check(NORMAL_ANCHOR, conf)
self.check(DUPLICATED_ANCHOR, conf)
def test_enabled(self):
conf = 'anchor-duplicates: enable'
self.check(NORMAL_ANCHOR, conf)
self.check(DUPLICATED_ANCHOR, conf, problem=(3,7))

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2021 Sergei Mikhailov
#
# This program is free software: you can redistribute it and/or modify
@ -67,22 +66,12 @@ key: &otherkeyanchor a
otherkey: *othermissedkeyanchor
'''
DUPLICATE_ANCHORS = '''---
first_block: &keyanchor
key: a
second_block: &keyanchor
key: b
target_block: *keyanchor
'''
class AnchorsTestCase(RuleTestCase):
rule_id = 'anchors'
class UnknownAliasesTestCase(RuleTestCase):
rule_id = 'unknown-aliases'
def test_disabled(self):
conf = 'anchors: disable'
conf = 'unknown-aliases: disable'
self.check(HIT_ANCHOR_POINTER, conf)
self.check(HIT_ANCHOR_MERGE, conf)
@ -93,25 +82,18 @@ class AnchorsTestCase(RuleTestCase):
self.check(MISS_ANCHOR_MERGE, conf)
self.check(MULTI_MISS_ANCHOR_POINTER, conf)
self.check(MULTI_DOC_MISS_ANCHOR_POINTER, conf)
self.check(DUPLICATE_ANCHORS, conf)
def test_enabled(self):
conf = 'anchors: enable'
conf = 'unknown-aliases: enable'
self.check(HIT_ANCHOR_POINTER, conf)
self.check(HIT_ANCHOR_MERGE, conf)
self.check(MULTI_HIT_ANCHOR_POINTER, conf)
self.check(MULTI_DOC_HIT_ANCHOR_POINTER, conf)
self.check(MISS_ANCHOR_POINTER, conf,
problem=(3, 11))
self.check(MISS_ANCHOR_MERGE, conf,
problem_first=(5, 7),
problem_second=(5, 7))
self.check(MISS_ANCHOR_POINTER, conf, problem=(3,11))
self.check(MISS_ANCHOR_MERGE, conf, problem=(5, 7))
self.check(MULTI_MISS_ANCHOR_POINTER, conf,
problem_first=(3, 11),
problem_second=(4, 16))
problem1=(3, 11), problem2=(4, 16))
self.check(MULTI_DOC_MISS_ANCHOR_POINTER, conf,
problem_first=(3, 11), problem_second=(6, 11))
self.check(DUPLICATE_ANCHORS, conf,
problem=(5, 15))
problem1=(3, 11), problem2=(6, 11))

@ -6,7 +6,7 @@ yaml-files:
- '.yamllint'
rules:
anchors: enable
anchor-duplicates: disable
braces: enable
brackets: enable
colons: enable
@ -33,3 +33,4 @@ rules:
trailing-spaces: enable
truthy:
level: warning
unknown-aliases: enable

@ -14,7 +14,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from yamllint.rules import (
anchors,
anchor_duplicates,
braces,
brackets,
colons,
@ -37,10 +37,11 @@ from yamllint.rules import (
quoted_strings,
trailing_spaces,
truthy,
unknown_aliases,
)
_RULES = {
anchors.ID: anchors,
anchor_duplicates.ID: anchor_duplicates,
braces.ID: braces,
brackets.ID: brackets,
colons.ID: colons,
@ -63,6 +64,7 @@ _RULES = {
quoted_strings.ID: quoted_strings,
trailing_spaces.ID: trailing_spaces,
truthy.ID: truthy,
unknown_aliases.ID: unknown_aliases,
}

@ -0,0 +1,65 @@
# Copyright (C) 2021 Sergei Mikhailov
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Use this rule to forbid duplicated anchors.
.. rubric:: Examples
#. With ``anchor-duplicates: {}``
the following code snippet would **PASS**:
::
clients:
jack: &jack_client
billing_id: 1234
bill: &bill_client
billing_id: 5678
target_client: *jack_client
the following code snippet would **FAIL**:
::
clients:
jack: &jack_client
billing_id: 1234
bill: &jack_client
billing_id: 5678
target_client: *jack_client
"""
from yaml import DocumentStartToken, AnchorToken, AliasToken
from yamllint.linter import LintProblem
ID = 'anchor-duplicates'
TYPE = 'token'
CONF = {'anchor-duplicates': bool}
DEFAULT = {'anchor-duplicates': True}
def check(conf, token, __prev, __next, __nextnext, context):
if conf['anchor-duplicates']:
if isinstance(token, DocumentStartToken):
context['anchors'] = []
elif 'anchors' in context:
if isinstance(token, AnchorToken):
if token.value in context['anchors']:
yield LintProblem(
token.start_mark.line + 1, token.start_mark.column + 1,
f'duplicated anchor "{token.value}"')
context['anchors'].append(token.value)

@ -1,117 +0,0 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2021 Sergei Mikhailov
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Use this rule to prevent aliases pointing to pointers that don't exist.
.. rubric:: Examples
#. With ``anchors: {}``
the following code snippet would **PASS**:
::
address: &address |
Williams St. 13
target_address: *address
the following code snippet would **FAIL**:
::
address: &address |
Williams St. 13
target_address: *wrong_address
#. This appends to merges as well:
the following code snippet would **PASS**:
::
default_address: &address
state: South Carolina
city: Barnwell
target_address:
<<: *address
city: Barnaul
the following code snippet would **FAIL**:
::
default_address: &address
state: South Carolina
city: Barnwell
target_address:
<<: *wrong_address
city: Barnaul
#. Duplicate anchors are not allowed.
the following code snippet would **PASS**:
::
clients:
jack: &jack_client
billing_id: 1234
bill: &bill_client
billing_id: 5678
target_client: *jack_client
the following code snippet would **FAIL**:
::
clients:
jack: &jack_client
billing_id: 1234
bill: &jack_client
billing_id: 5678
target_client: *jack_client
"""
from yaml import DocumentStartToken, AnchorToken, AliasToken, BlockEndToken
from yamllint.linter import LintProblem
ID = 'anchors'
TYPE = 'token'
def check(__conf, token, __prev, __next, __nextnext, context):
if isinstance(token, (DocumentStartToken)):
context['anchors'] = set()
context['aliases'] = {}
elif ('anchors' in context and 'aliases' in context):
if isinstance(token, (AnchorToken)):
if token.value in context['anchors']:
yield LintProblem(
token.start_mark.line + 1, token.start_mark.column + 1,
f"anchor '{token.value}' duplicates in document")
context['anchors'].add(token.value)
elif isinstance(token, (AliasToken)):
if token.value not in context['aliases']:
context['aliases'][token.value] = []
alias_obj = {"line": token.start_mark.line,
"column": token.start_mark.column}
context['aliases'][token.value].append(alias_obj)
elif isinstance(token, (BlockEndToken)):
missing_anchors = [alias for alias in list(context['aliases'])
if alias not in context['anchors']]
if len(missing_anchors) > 0:
for miss_anchor in missing_anchors:
for location in context['aliases'][miss_anchor]:
yield LintProblem(
location['line'] + 1, location['column'] + 1,
f"anchor '{miss_anchor}' is not found in document")

@ -0,0 +1,82 @@
# Copyright (C) 2021 Sergei Mikhailov
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Use this rule to prevent aliases pointing to pointers that don't exist.
.. rubric:: Examples
#. With ``unknown-aliases: {}``
the following code snippet would **PASS**:
::
address: &address |
Williams St. 13
target_address: *address
the following code snippet would **PASS**:
::
default_address: &address
state: South Carolina
city: Barnwell
target_address:
<<: *address
city: Barnaul
the following code snippet would **FAIL**:
::
address: &address |
Williams St. 13
target_address: *wrong_address
the following code snippet would **FAIL**:
::
default_address: &address
state: South Carolina
city: Barnwell
target_address:
<<: *wrong_address
city: Barnaul
"""
from yaml import DocumentStartToken, AnchorToken, AliasToken
from yamllint.linter import LintProblem
ID = 'unknown-aliases'
TYPE = 'token'
CONF = {'unknown-aliases': bool}
DEFAULT = {'unknown-aliases': True}
def check(conf, token, __prev, __next, __nextnext, context):
if conf['unknown-aliases']:
if isinstance(token, DocumentStartToken):
context['anchors'] = []
elif 'anchors' in context:
if isinstance(token, AnchorToken):
context['anchors'].append(token.value)
elif isinstance(token, AliasToken):
if token.value not in context['anchors']:
yield LintProblem(
token.start_mark.line + 1, token.start_mark.column + 1,
f'anchor "{token.value}" is used before assignment')
Loading…
Cancel
Save