diff --git a/docs/rules.rst b/docs/rules.rst
index 15416bf3d..9fdb39911 100644
--- a/docs/rules.rst
+++ b/docs/rules.rst
@@ -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
------
@@ -123,8 +123,3 @@ truthy
---------------
.. automodule:: yamllint.rules.truthy
-
-unknown-aliases
----------------
-
-.. automodule:: yamllint.rules.unknown_aliases
diff --git a/tests/rules/test_anchor_duplicates.py b/tests/rules/test_anchor_duplicates.py
deleted file mode 100644
index d21367396..000000000
--- a/tests/rules/test_anchor_duplicates.py
+++ /dev/null
@@ -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 .
-
-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))
diff --git a/tests/rules/test_unknown_aliases.py b/tests/rules/test_anchors.py
similarity index 50%
rename from tests/rules/test_unknown_aliases.py
rename to tests/rules/test_anchors.py
index eee9964b3..7af9d9c52 100644
--- a/tests/rules/test_unknown_aliases.py
+++ b/tests/rules/test_anchors.py
@@ -16,6 +16,22 @@
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
@@ -67,33 +83,83 @@
'''
-class UnknownAliasesTestCase(RuleTestCase):
- rule_id = 'unknown-aliases'
+
+DEFAULT = {'forbid-unknown-aliases': True,
+ 'forbid-duplicated-anchors': False}
+
+
+class AnchorsTestCase(RuleTestCase):
+ rule_id = 'anchors'
def test_disabled(self):
- conf = 'unknown-aliases: disable'
+ 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 = 'unknown-aliases: enable'
+ 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(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_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))
diff --git a/yamllint/conf/default.yaml b/yamllint/conf/default.yaml
index 021160496..597acbdaa 100644
--- a/yamllint/conf/default.yaml
+++ b/yamllint/conf/default.yaml
@@ -6,7 +6,7 @@ yaml-files:
- '.yamllint'
rules:
- anchor-duplicates: disable
+ anchors: enable
braces: enable
brackets: enable
colons: enable
@@ -32,4 +32,3 @@ rules:
trailing-spaces: enable
truthy:
level: warning
- unknown-aliases: enable
diff --git a/yamllint/rules/__init__.py b/yamllint/rules/__init__.py
index 8d18f8a7b..f33d699c5 100644
--- a/yamllint/rules/__init__.py
+++ b/yamllint/rules/__init__.py
@@ -15,7 +15,7 @@
# along with this program. If not, see .
from yamllint.rules import (
- anchor_duplicates,
+ anchors,
braces,
brackets,
colons,
@@ -37,11 +37,10 @@
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,
@@ -63,7 +62,6 @@
quoted_strings.ID: quoted_strings,
trailing_spaces.ID: trailing_spaces,
truthy.ID: truthy,
- unknown_aliases.ID: unknown_aliases,
}
diff --git a/yamllint/rules/anchor_duplicates.py b/yamllint/rules/anchor_duplicates.py
deleted file mode 100644
index cb0d9e878..000000000
--- a/yamllint/rules/anchor_duplicates.py
+++ /dev/null
@@ -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 .
-
-"""
-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)
diff --git a/yamllint/rules/anchors.py b/yamllint/rules/anchors.py
new file mode 100644
index 000000000..275b61599
--- /dev/null
+++ b/yamllint/rules/anchors.py
@@ -0,0 +1,124 @@
+# 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 .
+
+"""
+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}')
diff --git a/yamllint/rules/unknown_aliases.py b/yamllint/rules/unknown_aliases.py
deleted file mode 100644
index b4264f8ba..000000000
--- a/yamllint/rules/unknown_aliases.py
+++ /dev/null
@@ -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 .
-
-"""
-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')