Check for missing or duplicated anchors, rework v2

pull/420/head
Sergei Mikhailov 3 years ago
parent 6ade0ee800
commit 542e05fe8c

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

@ -1,44 +0,0 @@
# 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))

@ -0,0 +1,165 @@
# Copyright (C) 2022 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
'''
NORMAL_ANCHOR_NO_DOC_START = '''---
key: &keyanchor a
otherkey: *keyanchor
'''
DUPLICATED_ANCHOR = '''---
key1: &keyanchor a
key2: &keyanchor b
otherkey: *keyanchor
'''
HIT_ANCHOR_POINTER = '''---
key: &keyanchor a
otherkey: *keyanchor
'''
MISS_ANCHOR_POINTER = '''---
key: &keyanchor a
otherkey: *missedkeyanchor
'''
HIT_ANCHOR_MERGE = '''---
block: &keyanchor
key: a
otherkey:
<<: *keyanchor
otherkey: b
'''
MISS_ANCHOR_MERGE = '''---
block: &keyanchor
key: a
otherkey:
<<: *missedkeyanchor
otherkey: b
'''
MULTI_HIT_ANCHOR_POINTER = '''---
key: &keyanchor a
otherkey: *keyanchor
otherotherkey: *keyanchor
'''
MULTI_MISS_ANCHOR_POINTER = '''---
key: &keyanchor a
otherkey: *missedkeyanchor
otherotherkey: *missedkeyanchor
'''
MULTI_DOC_HIT_ANCHOR_POINTER = '''---
key: &keyanchor a
otherkey: *keyanchor
---
key: &otherkeyanchor a
otherkey: *otherkeyanchor
'''
MULTI_DOC_MISS_ANCHOR_POINTER = '''---
key: &keyanchor a
otherkey: *missedkeyanchor
---
key: &otherkeyanchor a
otherkey: *othermissedkeyanchor
'''
DEFAULT = {'forbid-unknown-aliases': True,
'forbid-duplicated-anchors': False}
class AnchorsTestCase(RuleTestCase):
rule_id = 'anchors'
def test_disabled(self):
conf = ('anchors:\n'
' forbid-unknown-aliases: false\n'
' forbid-duplicated-anchors: false\n'
)
self.check(NORMAL_ANCHOR, conf)
self.check(NORMAL_ANCHOR_NO_DOC_START, conf)
self.check(DUPLICATED_ANCHOR, conf)
self.check(HIT_ANCHOR_POINTER, conf)
self.check(MISS_ANCHOR_POINTER, conf)
self.check(HIT_ANCHOR_MERGE, conf)
self.check(MISS_ANCHOR_MERGE, conf)
self.check(MULTI_HIT_ANCHOR_POINTER, conf)
self.check(MULTI_MISS_ANCHOR_POINTER, conf)
self.check(MULTI_DOC_HIT_ANCHOR_POINTER, conf)
self.check(MULTI_DOC_MISS_ANCHOR_POINTER, conf)
def test_unknown_aliases(self):
conf = ('anchors:\n'
' forbid-unknown-aliases: true\n'
' forbid-duplicated-anchors: false\n'
)
self.check(NORMAL_ANCHOR, conf)
self.check(DUPLICATED_ANCHOR, conf)
self.check(HIT_ANCHOR_POINTER, conf)
self.check(MISS_ANCHOR_POINTER, conf, problem=(3, 11))
self.check(HIT_ANCHOR_MERGE, conf)
self.check(MISS_ANCHOR_MERGE, conf, problem=(5, 7))
self.check(MULTI_HIT_ANCHOR_POINTER, conf)
self.check(MULTI_MISS_ANCHOR_POINTER, conf,
problem1=(3, 11), problem2=(4, 16))
self.check(MULTI_DOC_HIT_ANCHOR_POINTER, conf)
self.check(MULTI_DOC_MISS_ANCHOR_POINTER, conf,
problem1=(3, 11), problem2=(6, 11))
def test_duplicated_anchors(self):
conf = ('anchors:\n'
' forbid-unknown-aliases: false\n'
' forbid-duplicated-anchors: true\n'
)
self.check(NORMAL_ANCHOR, conf)
self.check(DUPLICATED_ANCHOR, conf, problem=(3, 7))
self.check(HIT_ANCHOR_POINTER, conf)
self.check(MISS_ANCHOR_POINTER, conf)
self.check(HIT_ANCHOR_MERGE, conf)
self.check(MISS_ANCHOR_MERGE, conf)
self.check(MULTI_HIT_ANCHOR_POINTER, conf)
self.check(MULTI_MISS_ANCHOR_POINTER, conf)
self.check(MULTI_DOC_HIT_ANCHOR_POINTER, conf)
self.check(MULTI_DOC_MISS_ANCHOR_POINTER, conf)
def test_enabled(self):
conf = ('anchors:\n'
' forbid-unknown-aliases: true\n'
' forbid-duplicated-anchors: true\n'
)
self.check(NORMAL_ANCHOR, conf)
self.check(DUPLICATED_ANCHOR, conf, problem=(3, 7))
self.check(HIT_ANCHOR_POINTER, conf)
self.check(MISS_ANCHOR_POINTER, conf, problem=(3, 11))
self.check(HIT_ANCHOR_MERGE, conf)
self.check(MISS_ANCHOR_MERGE, conf, problem=(5, 7))
self.check(MULTI_HIT_ANCHOR_POINTER, conf)
self.check(MULTI_MISS_ANCHOR_POINTER, conf,
problem1=(3, 11), problem2=(4, 16))
self.check(MULTI_DOC_HIT_ANCHOR_POINTER, conf)
self.check(MULTI_DOC_MISS_ANCHOR_POINTER, conf,
problem1=(3, 11), problem2=(6, 11))

@ -1,99 +0,0 @@
# 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
HIT_ANCHOR_POINTER = '''---
key: &keyanchor a
otherkey: *keyanchor
'''
MISS_ANCHOR_POINTER = '''---
key: &keyanchor a
otherkey: *missedkeyanchor
'''
HIT_ANCHOR_MERGE = '''---
block: &keyanchor
key: a
otherkey:
<<: *keyanchor
otherkey: b
'''
MISS_ANCHOR_MERGE = '''---
block: &keyanchor
key: a
otherkey:
<<: *missedkeyanchor
otherkey: b
'''
MULTI_HIT_ANCHOR_POINTER = '''---
key: &keyanchor a
otherkey: *keyanchor
otherotherkey: *keyanchor
'''
MULTI_MISS_ANCHOR_POINTER = '''---
key: &keyanchor a
otherkey: *missedkeyanchor
otherotherkey: *missedkeyanchor
'''
MULTI_DOC_HIT_ANCHOR_POINTER = '''---
key: &keyanchor a
otherkey: *keyanchor
---
key: &otherkeyanchor a
otherkey: *otherkeyanchor
'''
MULTI_DOC_MISS_ANCHOR_POINTER = '''---
key: &keyanchor a
otherkey: *missedkeyanchor
---
key: &otherkeyanchor a
otherkey: *othermissedkeyanchor
'''
class UnknownAliasesTestCase(RuleTestCase):
rule_id = 'unknown-aliases'
def test_disabled(self):
conf = 'unknown-aliases: disable'
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)
self.check(MISS_ANCHOR_MERGE, conf)
self.check(MULTI_MISS_ANCHOR_POINTER, conf)
self.check(MULTI_DOC_MISS_ANCHOR_POINTER, conf)
def test_enabled(self):
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=(5, 7))
self.check(MULTI_MISS_ANCHOR_POINTER, conf,
problem1=(3, 11), problem2=(4, 16))
self.check(MULTI_DOC_MISS_ANCHOR_POINTER, conf,
problem1=(3, 11), problem2=(6, 11))

@ -6,7 +6,7 @@ yaml-files:
- '.yamllint'
rules:
anchor-duplicates: disable
anchors: enable
braces: enable
brackets: enable
colons: enable
@ -33,4 +33,3 @@ 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 (
anchor_duplicates,
anchors,
braces,
brackets,
colons,
@ -37,11 +37,10 @@ from yamllint.rules import (
quoted_strings,
trailing_spaces,
truthy,
unknown_aliases,
)
_RULES = {
anchor_duplicates.ID: anchor_duplicates,
anchors.ID: anchors,
braces.ID: braces,
brackets.ID: brackets,
colons.ID: colons,
@ -64,7 +63,6 @@ _RULES = {
quoted_strings.ID: quoted_strings,
trailing_spaces.ID: trailing_spaces,
truthy.ID: truthy,
unknown_aliases.ID: unknown_aliases,
}

@ -1,65 +0,0 @@
# 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)

@ -0,0 +1,124 @@
# Copyright (C) 2022 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 duplicated anchors and referencing to non-existent anchors.
.. rubric:: Options
* Use ``forbid-unknown-aliases`` to prevent referencing to anchors before assigment or referencing to non-existent anchors.
* Use ``forbid-duplicated-anchors`` to prevent duplicated anchors.
.. rubric:: Examples
#. With ``anchors: {forbid-duplicated-anchors: true}``
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
#. With ``anchors: {forbid-unknown-aliases: true}``
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 StreamStartToken, DocumentStartToken, AnchorToken, AliasToken
from yamllint.linter import LintProblem
ID = 'anchors'
TYPE = 'token'
CONF = {'forbid-unknown-aliases': bool,
'forbid-duplicated-anchors': bool}
DEFAULT = {'forbid-unknown-aliases': True,
'forbid-duplicated-anchors': False}
def check(conf, token, prev, next, nextnext, context):
if conf['forbid-unknown-aliases'] or conf['forbid-duplicated-anchors']:
# In case of DocumentStartToken `---` is missing
if isinstance(token, StreamStartToken):
context['anchors'] = []
if isinstance(token, DocumentStartToken):
context['anchors'] = []
elif isinstance(token, AnchorToken):
context['anchors'].append(token.value)
if conf['forbid-unknown-aliases'] and 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')
if conf['forbid-duplicated-anchors'] and isinstance(token, AnchorToken):
anchors_count = context['anchors'].count(token.value)
if anchors_count == 2:
yield LintProblem(
token.start_mark.line + 1, token.start_mark.column + 1,
f'duplicated anchor "{token.value}')

@ -1,82 +0,0 @@
# 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