Compare commits
57 Commits
Author | SHA1 | Date |
---|---|---|
|
a68c3aa69e | 2 years ago |
|
7f2c071545 | 2 years ago |
|
b05e028c58 | 2 years ago |
|
e636848ddc | 2 years ago |
|
019c87d36d | 2 years ago |
|
977f4908b5 | 2 years ago |
|
f874b6607c | 2 years ago |
|
98f2281f56 | 2 years ago |
|
15eafeb80a | 2 years ago |
|
16eae28a50 | 2 years ago |
|
771c3a0412 | 2 years ago |
|
b92fc9cb31 | 2 years ago |
|
e90e0a0eb5 | 2 years ago |
|
6bfd6756e2 | 2 years ago |
|
6b45be1afc | 2 years ago |
|
9d0f59876d | 2 years ago |
|
ebd6b90d3e | 2 years ago |
|
8aaa226830 | 2 years ago |
|
15f8204427 | 2 years ago |
|
404656394c | 2 years ago |
|
06db2af9b0 | 2 years ago |
|
b9e1fd18c1 | 2 years ago |
|
fa0bb03f9a | 2 years ago |
|
2a904f8fc1 | 2 years ago |
|
6194a282fc | 2 years ago |
|
5ac3ed4490 | 3 years ago |
|
5b21a3d9ea | 3 years ago |
|
5fbf44c203 | 3 years ago |
|
c9c5e0b1c7 | 3 years ago |
|
a6e0e1213a | 3 years ago |
|
eb7b7ca627 | 3 years ago |
|
cec4f3383a | 3 years ago |
|
52234b7a46 | 3 years ago |
|
151b1c4086 | 3 years ago |
|
764586d836 | 3 years ago |
|
47cd8f2e9e | 3 years ago |
|
4d271f3daf | 3 years ago |
|
22ddf4c8e5 | 3 years ago |
|
b8c85f0dfd | 3 years ago |
|
e0f749bf5d | 3 years ago |
|
19d00809d1 | 3 years ago |
|
008db4aa09 | 3 years ago |
|
e8391de711 | 3 years ago |
|
a5adec1570 | 3 years ago |
|
9cce294041 | 3 years ago |
|
2f8ad7003a | 3 years ago |
|
fb0c0a5247 | 3 years ago |
|
352e1a975e | 3 years ago |
|
e319a17344 | 3 years ago |
|
6b6fdba3bf | 3 years ago |
|
868350681a | 3 years ago |
|
94c1c2bcf2 | 3 years ago |
|
0130e15c8c | 3 years ago |
|
ae3158cd1f | 3 years ago |
|
4c7b47daf3 | 3 years ago |
|
3346843edc | 3 years ago |
|
ea70520216 | 3 years ago |
@ -0,0 +1,4 @@
|
|||||||
|
[flake8]
|
||||||
|
import-order-style = pep8
|
||||||
|
application-import-names = yamllint
|
||||||
|
ignore = W503,W504
|
@ -1,4 +0,0 @@
|
|||||||
include LICENSE
|
|
||||||
include README.rst
|
|
||||||
include docs/*
|
|
||||||
include tests/*.py tests/rules/*.py tests/yaml-1.2-spec-examples/*
|
|
Binary file not shown.
Before Width: | Height: | Size: 41 KiB After Width: | Height: | Size: 31 KiB |
@ -0,0 +1,54 @@
|
|||||||
|
[project]
|
||||||
|
name = "yamllint"
|
||||||
|
description = "A linter for YAML files."
|
||||||
|
readme = {file = "README.rst", content-type = "text/x-rst"}
|
||||||
|
requires-python = ">=3.7"
|
||||||
|
license = {text = "GPL-3.0-or-later"}
|
||||||
|
authors = [{name = "Adrien Vergé"}]
|
||||||
|
keywords = ["yaml", "lint", "linter", "syntax", "checker"]
|
||||||
|
classifiers = [
|
||||||
|
"Development Status :: 5 - Production/Stable",
|
||||||
|
"Environment :: Console",
|
||||||
|
"Intended Audience :: Developers",
|
||||||
|
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
|
||||||
|
"Programming Language :: Python",
|
||||||
|
"Topic :: Software Development",
|
||||||
|
"Topic :: Software Development :: Debuggers",
|
||||||
|
"Topic :: Software Development :: Quality Assurance",
|
||||||
|
"Topic :: Software Development :: Testing",
|
||||||
|
]
|
||||||
|
dependencies = [
|
||||||
|
"pathspec >= 0.5.3",
|
||||||
|
"pyyaml",
|
||||||
|
]
|
||||||
|
dynamic = ["version"]
|
||||||
|
|
||||||
|
[project.optional-dependencies]
|
||||||
|
dev = [
|
||||||
|
"doc8",
|
||||||
|
"flake8",
|
||||||
|
"flake8-import-order",
|
||||||
|
"rstcheck[sphinx]",
|
||||||
|
"sphinx",
|
||||||
|
]
|
||||||
|
|
||||||
|
[project.scripts]
|
||||||
|
yamllint = "yamllint.cli:run"
|
||||||
|
|
||||||
|
[project.urls]
|
||||||
|
homepage = "https://github.com/adrienverge/yamllint"
|
||||||
|
repository = "https://github.com/adrienverge/yamllint"
|
||||||
|
documentation = "https://yamllint.readthedocs.io"
|
||||||
|
|
||||||
|
[build-system]
|
||||||
|
build-backend = "setuptools.build_meta"
|
||||||
|
requires = ["setuptools >= 61"]
|
||||||
|
|
||||||
|
[tool.setuptools]
|
||||||
|
packages = ["yamllint", "yamllint.conf", "yamllint.rules"]
|
||||||
|
|
||||||
|
[tool.setuptools.package-data]
|
||||||
|
yamllint = ["conf/*.yaml"]
|
||||||
|
|
||||||
|
[tool.setuptools.dynamic]
|
||||||
|
version = {attr = "yamllint.__version__"}
|
@ -1,72 +0,0 @@
|
|||||||
[bdist_wheel]
|
|
||||||
universal = 1
|
|
||||||
|
|
||||||
[flake8]
|
|
||||||
import-order-style = pep8
|
|
||||||
application-import-names = yamllint
|
|
||||||
ignore = W503,W504
|
|
||||||
|
|
||||||
[build_sphinx]
|
|
||||||
all-files = 1
|
|
||||||
source-dir = docs
|
|
||||||
build-dir = docs/_build
|
|
||||||
warning-is-error = 1
|
|
||||||
|
|
||||||
[metadata]
|
|
||||||
keywords =
|
|
||||||
yaml
|
|
||||||
lint
|
|
||||||
linter
|
|
||||||
syntax
|
|
||||||
checker
|
|
||||||
|
|
||||||
url = https://github.com/adrienverge/yamllint
|
|
||||||
classifiers =
|
|
||||||
Development Status :: 5 - Production/Stable
|
|
||||||
Environment :: Console
|
|
||||||
Intended Audience :: Developers
|
|
||||||
License :: OSI Approved :: GNU General Public License v3 (GPLv3)
|
|
||||||
Programming Language :: Python :: 3
|
|
||||||
Programming Language :: Python :: 3.6
|
|
||||||
Programming Language :: Python :: 3.7
|
|
||||||
Programming Language :: Python :: 3.8
|
|
||||||
Programming Language :: Python :: 3.9
|
|
||||||
Programming Language :: Python :: 3.10
|
|
||||||
Topic :: Software Development
|
|
||||||
Topic :: Software Development :: Debuggers
|
|
||||||
Topic :: Software Development :: Quality Assurance
|
|
||||||
Topic :: Software Development :: Testing
|
|
||||||
|
|
||||||
project_urls =
|
|
||||||
Documentation = https://yamllint.readthedocs.io
|
|
||||||
Download = https://pypi.org/project/yamllint/#files
|
|
||||||
Bug Tracker = https://github.com/adrienverge/yamllint/issues
|
|
||||||
Source Code = https://github.com/adrienverge/yamllint
|
|
||||||
|
|
||||||
[options]
|
|
||||||
packages = find:
|
|
||||||
|
|
||||||
python_requires = >=3.6
|
|
||||||
|
|
||||||
include_package_data = True
|
|
||||||
install_requires =
|
|
||||||
pathspec >= 0.5.3
|
|
||||||
pyyaml
|
|
||||||
setuptools
|
|
||||||
|
|
||||||
test_suite = tests
|
|
||||||
|
|
||||||
[options.packages.find]
|
|
||||||
exclude =
|
|
||||||
tests
|
|
||||||
tests.*
|
|
||||||
|
|
||||||
[options.package_data]
|
|
||||||
yamllint = conf/*.yaml
|
|
||||||
|
|
||||||
[options.entry_points]
|
|
||||||
console_scripts =
|
|
||||||
yamllint = yamllint.cli:run
|
|
||||||
|
|
||||||
[coverage:run]
|
|
||||||
relative_files = True
|
|
@ -0,0 +1,281 @@
|
|||||||
|
# Copyright (C) 2023 Adrien Vergé
|
||||||
|
#
|
||||||
|
# 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
|
||||||
|
|
||||||
|
|
||||||
|
class AnchorsTestCase(RuleTestCase):
|
||||||
|
rule_id = 'anchors'
|
||||||
|
|
||||||
|
def test_disabled(self):
|
||||||
|
conf = 'anchors: disable'
|
||||||
|
self.check('---\n'
|
||||||
|
'- &b true\n'
|
||||||
|
'- &i 42\n'
|
||||||
|
'- &s hello\n'
|
||||||
|
'- &f_m {k: v}\n'
|
||||||
|
'- &f_s [1, 2]\n'
|
||||||
|
'- *b\n'
|
||||||
|
'- *i\n'
|
||||||
|
'- *s\n'
|
||||||
|
'- *f_m\n'
|
||||||
|
'- *f_s\n'
|
||||||
|
'---\n' # redeclare anchors in a new document
|
||||||
|
'- &b true\n'
|
||||||
|
'- &i 42\n'
|
||||||
|
'- &s hello\n'
|
||||||
|
'- *b\n'
|
||||||
|
'- *i\n'
|
||||||
|
'- *s\n'
|
||||||
|
'---\n'
|
||||||
|
'block mapping: &b_m\n'
|
||||||
|
' key: value\n'
|
||||||
|
'extended:\n'
|
||||||
|
' <<: *b_m\n'
|
||||||
|
' foo: bar\n'
|
||||||
|
'---\n'
|
||||||
|
'{a: 1, &x b: 2, c: &y 3, *x : 4, e: *y}\n'
|
||||||
|
'...\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'- &i 42\n'
|
||||||
|
'---\n'
|
||||||
|
'- &b true\n'
|
||||||
|
'- &b true\n'
|
||||||
|
'- &b true\n'
|
||||||
|
'- &s hello\n'
|
||||||
|
'- *b\n'
|
||||||
|
'- *i\n' # declared in a previous document
|
||||||
|
'- *f_m\n' # never declared
|
||||||
|
'- *f_m\n'
|
||||||
|
'- *f_m\n'
|
||||||
|
'- *f_s\n' # declared after
|
||||||
|
'- &f_s [1, 2]\n'
|
||||||
|
'---\n'
|
||||||
|
'block mapping: &b_m\n'
|
||||||
|
' key: value\n'
|
||||||
|
'---\n'
|
||||||
|
'block mapping 1: &b_m_bis\n'
|
||||||
|
' key: value\n'
|
||||||
|
'block mapping 2: &b_m_bis\n'
|
||||||
|
' key: value\n'
|
||||||
|
'extended:\n'
|
||||||
|
' <<: *b_m\n'
|
||||||
|
' foo: bar\n'
|
||||||
|
'---\n'
|
||||||
|
'{a: 1, &x b: 2, c: &x 3, *x : 4, e: *y}\n'
|
||||||
|
'...\n', conf)
|
||||||
|
|
||||||
|
def test_forbid_undeclared_aliases(self):
|
||||||
|
conf = ('anchors:\n'
|
||||||
|
' forbid-undeclared-aliases: true\n'
|
||||||
|
' forbid-duplicated-anchors: false\n'
|
||||||
|
' forbid-unused-anchors: false\n')
|
||||||
|
self.check('---\n'
|
||||||
|
'- &b true\n'
|
||||||
|
'- &i 42\n'
|
||||||
|
'- &s hello\n'
|
||||||
|
'- &f_m {k: v}\n'
|
||||||
|
'- &f_s [1, 2]\n'
|
||||||
|
'- *b\n'
|
||||||
|
'- *i\n'
|
||||||
|
'- *s\n'
|
||||||
|
'- *f_m\n'
|
||||||
|
'- *f_s\n'
|
||||||
|
'---\n' # redeclare anchors in a new document
|
||||||
|
'- &b true\n'
|
||||||
|
'- &i 42\n'
|
||||||
|
'- &s hello\n'
|
||||||
|
'- *b\n'
|
||||||
|
'- *i\n'
|
||||||
|
'- *s\n'
|
||||||
|
'---\n'
|
||||||
|
'block mapping: &b_m\n'
|
||||||
|
' key: value\n'
|
||||||
|
'extended:\n'
|
||||||
|
' <<: *b_m\n'
|
||||||
|
' foo: bar\n'
|
||||||
|
'---\n'
|
||||||
|
'{a: 1, &x b: 2, c: &y 3, *x : 4, e: *y}\n'
|
||||||
|
'...\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'- &i 42\n'
|
||||||
|
'---\n'
|
||||||
|
'- &b true\n'
|
||||||
|
'- &b true\n'
|
||||||
|
'- &b true\n'
|
||||||
|
'- &s hello\n'
|
||||||
|
'- *b\n'
|
||||||
|
'- *i\n' # declared in a previous document
|
||||||
|
'- *f_m\n' # never declared
|
||||||
|
'- *f_m\n'
|
||||||
|
'- *f_m\n'
|
||||||
|
'- *f_s\n' # declared after
|
||||||
|
'- &f_s [1, 2]\n'
|
||||||
|
'...\n'
|
||||||
|
'---\n'
|
||||||
|
'block mapping: &b_m\n'
|
||||||
|
' key: value\n'
|
||||||
|
'---\n'
|
||||||
|
'block mapping 1: &b_m_bis\n'
|
||||||
|
' key: value\n'
|
||||||
|
'block mapping 2: &b_m_bis\n'
|
||||||
|
' key: value\n'
|
||||||
|
'extended:\n'
|
||||||
|
' <<: *b_m\n'
|
||||||
|
' foo: bar\n'
|
||||||
|
'---\n'
|
||||||
|
'{a: 1, &x b: 2, c: &x 3, *x : 4, e: *y}\n'
|
||||||
|
'...\n', conf,
|
||||||
|
problem1=(9, 3),
|
||||||
|
problem2=(10, 3),
|
||||||
|
problem3=(11, 3),
|
||||||
|
problem4=(12, 3),
|
||||||
|
problem5=(13, 3),
|
||||||
|
problem6=(25, 7),
|
||||||
|
problem7=(28, 37))
|
||||||
|
|
||||||
|
def test_forbid_duplicated_anchors(self):
|
||||||
|
conf = ('anchors:\n'
|
||||||
|
' forbid-undeclared-aliases: false\n'
|
||||||
|
' forbid-duplicated-anchors: true\n'
|
||||||
|
' forbid-unused-anchors: false\n')
|
||||||
|
self.check('---\n'
|
||||||
|
'- &b true\n'
|
||||||
|
'- &i 42\n'
|
||||||
|
'- &s hello\n'
|
||||||
|
'- &f_m {k: v}\n'
|
||||||
|
'- &f_s [1, 2]\n'
|
||||||
|
'- *b\n'
|
||||||
|
'- *i\n'
|
||||||
|
'- *s\n'
|
||||||
|
'- *f_m\n'
|
||||||
|
'- *f_s\n'
|
||||||
|
'---\n' # redeclare anchors in a new document
|
||||||
|
'- &b true\n'
|
||||||
|
'- &i 42\n'
|
||||||
|
'- &s hello\n'
|
||||||
|
'- *b\n'
|
||||||
|
'- *i\n'
|
||||||
|
'- *s\n'
|
||||||
|
'---\n'
|
||||||
|
'block mapping: &b_m\n'
|
||||||
|
' key: value\n'
|
||||||
|
'extended:\n'
|
||||||
|
' <<: *b_m\n'
|
||||||
|
' foo: bar\n'
|
||||||
|
'---\n'
|
||||||
|
'{a: 1, &x b: 2, c: &y 3, *x : 4, e: *y}\n'
|
||||||
|
'...\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'- &i 42\n'
|
||||||
|
'---\n'
|
||||||
|
'- &b true\n'
|
||||||
|
'- &b true\n'
|
||||||
|
'- &b true\n'
|
||||||
|
'- &s hello\n'
|
||||||
|
'- *b\n'
|
||||||
|
'- *i\n' # declared in a previous document
|
||||||
|
'- *f_m\n' # never declared
|
||||||
|
'- *f_m\n'
|
||||||
|
'- *f_m\n'
|
||||||
|
'- *f_s\n' # declared after
|
||||||
|
'- &f_s [1, 2]\n'
|
||||||
|
'...\n'
|
||||||
|
'---\n'
|
||||||
|
'block mapping: &b_m\n'
|
||||||
|
' key: value\n'
|
||||||
|
'---\n'
|
||||||
|
'block mapping 1: &b_m_bis\n'
|
||||||
|
' key: value\n'
|
||||||
|
'block mapping 2: &b_m_bis\n'
|
||||||
|
' key: value\n'
|
||||||
|
'extended:\n'
|
||||||
|
' <<: *b_m\n'
|
||||||
|
' foo: bar\n'
|
||||||
|
'---\n'
|
||||||
|
'{a: 1, &x b: 2, c: &x 3, *x : 4, e: *y}\n'
|
||||||
|
'...\n', conf,
|
||||||
|
problem1=(5, 3),
|
||||||
|
problem2=(6, 3),
|
||||||
|
problem3=(22, 18),
|
||||||
|
problem4=(28, 20))
|
||||||
|
|
||||||
|
def test_forbid_unused_anchors(self):
|
||||||
|
conf = ('anchors:\n'
|
||||||
|
' forbid-undeclared-aliases: false\n'
|
||||||
|
' forbid-duplicated-anchors: false\n'
|
||||||
|
' forbid-unused-anchors: true\n')
|
||||||
|
|
||||||
|
self.check('---\n'
|
||||||
|
'- &b true\n'
|
||||||
|
'- &i 42\n'
|
||||||
|
'- &s hello\n'
|
||||||
|
'- &f_m {k: v}\n'
|
||||||
|
'- &f_s [1, 2]\n'
|
||||||
|
'- *b\n'
|
||||||
|
'- *i\n'
|
||||||
|
'- *s\n'
|
||||||
|
'- *f_m\n'
|
||||||
|
'- *f_s\n'
|
||||||
|
'---\n' # redeclare anchors in a new document
|
||||||
|
'- &b true\n'
|
||||||
|
'- &i 42\n'
|
||||||
|
'- &s hello\n'
|
||||||
|
'- *b\n'
|
||||||
|
'- *i\n'
|
||||||
|
'- *s\n'
|
||||||
|
'---\n'
|
||||||
|
'block mapping: &b_m\n'
|
||||||
|
' key: value\n'
|
||||||
|
'extended:\n'
|
||||||
|
' <<: *b_m\n'
|
||||||
|
' foo: bar\n'
|
||||||
|
'---\n'
|
||||||
|
'{a: 1, &x b: 2, c: &y 3, *x : 4, e: *y}\n'
|
||||||
|
'...\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'- &i 42\n'
|
||||||
|
'---\n'
|
||||||
|
'- &b true\n'
|
||||||
|
'- &b true\n'
|
||||||
|
'- &b true\n'
|
||||||
|
'- &s hello\n'
|
||||||
|
'- *b\n'
|
||||||
|
'- *i\n' # declared in a previous document
|
||||||
|
'- *f_m\n' # never declared
|
||||||
|
'- *f_m\n'
|
||||||
|
'- *f_m\n'
|
||||||
|
'- *f_s\n' # declared after
|
||||||
|
'- &f_s [1, 2]\n'
|
||||||
|
'...\n'
|
||||||
|
'---\n'
|
||||||
|
'block mapping: &b_m\n'
|
||||||
|
' key: value\n'
|
||||||
|
'---\n'
|
||||||
|
'block mapping 1: &b_m_bis\n'
|
||||||
|
' key: value\n'
|
||||||
|
'block mapping 2: &b_m_bis\n'
|
||||||
|
' key: value\n'
|
||||||
|
'extended:\n'
|
||||||
|
' <<: *b_m\n'
|
||||||
|
' foo: bar\n'
|
||||||
|
'---\n'
|
||||||
|
'{a: 1, &x b: 2, c: &x 3, *x : 4, e: *y}\n'
|
||||||
|
'...\n', conf,
|
||||||
|
problem1=(2, 3),
|
||||||
|
problem2=(7, 3),
|
||||||
|
problem3=(14, 3),
|
||||||
|
problem4=(17, 16),
|
||||||
|
problem5=(22, 18))
|
@ -0,0 +1,174 @@
|
|||||||
|
# Copyright (C) 2023 Adrien Vergé
|
||||||
|
#
|
||||||
|
# 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 report duplicated anchors and aliases referencing undeclared
|
||||||
|
anchors.
|
||||||
|
|
||||||
|
.. rubric:: Options
|
||||||
|
|
||||||
|
* Set ``forbid-undeclared-aliases`` to ``true`` to avoid aliases that reference
|
||||||
|
an anchor that hasn't been declared (either not declared at all, or declared
|
||||||
|
later in the document).
|
||||||
|
* Set ``forbid-duplicated-anchors`` to ``true`` to avoid duplications of a same
|
||||||
|
anchor.
|
||||||
|
* Set ``forbid-unused-anchors`` to ``true`` to avoid anchors being declared but
|
||||||
|
not used anywhere in the YAML document via alias.
|
||||||
|
|
||||||
|
.. rubric:: Default values (when enabled)
|
||||||
|
|
||||||
|
.. code-block:: yaml
|
||||||
|
|
||||||
|
rules:
|
||||||
|
anchors:
|
||||||
|
forbid-undeclared-aliases: true
|
||||||
|
forbid-duplicated-anchors: false
|
||||||
|
forbid-unused-anchors: false
|
||||||
|
|
||||||
|
.. rubric:: Examples
|
||||||
|
|
||||||
|
#. With ``anchors: {forbid-undeclared-aliases: true}``
|
||||||
|
|
||||||
|
the following code snippet would **PASS**:
|
||||||
|
::
|
||||||
|
|
||||||
|
---
|
||||||
|
- &anchor
|
||||||
|
foo: bar
|
||||||
|
- *anchor
|
||||||
|
|
||||||
|
the following code snippet would **FAIL**:
|
||||||
|
::
|
||||||
|
|
||||||
|
---
|
||||||
|
- &anchor
|
||||||
|
foo: bar
|
||||||
|
- *unknown
|
||||||
|
|
||||||
|
the following code snippet would **FAIL**:
|
||||||
|
::
|
||||||
|
|
||||||
|
---
|
||||||
|
- &anchor
|
||||||
|
foo: bar
|
||||||
|
- <<: *unknown
|
||||||
|
extra: value
|
||||||
|
|
||||||
|
#. With ``anchors: {forbid-duplicated-anchors: true}``
|
||||||
|
|
||||||
|
the following code snippet would **PASS**:
|
||||||
|
::
|
||||||
|
|
||||||
|
---
|
||||||
|
- &anchor1 Foo Bar
|
||||||
|
- &anchor2 [item 1, item 2]
|
||||||
|
|
||||||
|
the following code snippet would **FAIL**:
|
||||||
|
::
|
||||||
|
|
||||||
|
---
|
||||||
|
- &anchor Foo Bar
|
||||||
|
- &anchor [item 1, item 2]
|
||||||
|
|
||||||
|
#. With ``anchors: {forbid-unused-anchors: true}``
|
||||||
|
|
||||||
|
the following code snippet would **PASS**:
|
||||||
|
::
|
||||||
|
|
||||||
|
---
|
||||||
|
- &anchor
|
||||||
|
foo: bar
|
||||||
|
- *anchor
|
||||||
|
|
||||||
|
the following code snippet would **FAIL**:
|
||||||
|
::
|
||||||
|
|
||||||
|
---
|
||||||
|
- &anchor
|
||||||
|
foo: bar
|
||||||
|
- items:
|
||||||
|
- item1
|
||||||
|
- item2
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
from yamllint.linter import LintProblem
|
||||||
|
|
||||||
|
|
||||||
|
ID = 'anchors'
|
||||||
|
TYPE = 'token'
|
||||||
|
CONF = {'forbid-undeclared-aliases': bool,
|
||||||
|
'forbid-duplicated-anchors': bool,
|
||||||
|
'forbid-unused-anchors': bool}
|
||||||
|
DEFAULT = {'forbid-undeclared-aliases': True,
|
||||||
|
'forbid-duplicated-anchors': False,
|
||||||
|
'forbid-unused-anchors': False}
|
||||||
|
|
||||||
|
|
||||||
|
def check(conf, token, prev, next, nextnext, context):
|
||||||
|
if (conf['forbid-undeclared-aliases'] or
|
||||||
|
conf['forbid-duplicated-anchors'] or
|
||||||
|
conf['forbid-unused-anchors']):
|
||||||
|
if isinstance(token, (
|
||||||
|
yaml.StreamStartToken,
|
||||||
|
yaml.DocumentStartToken,
|
||||||
|
yaml.DocumentEndToken)):
|
||||||
|
context['anchors'] = {}
|
||||||
|
|
||||||
|
if (conf['forbid-undeclared-aliases'] and
|
||||||
|
isinstance(token, yaml.AliasToken) and
|
||||||
|
token.value not in context['anchors']):
|
||||||
|
yield LintProblem(
|
||||||
|
token.start_mark.line + 1, token.start_mark.column + 1,
|
||||||
|
f'found undeclared alias "{token.value}"')
|
||||||
|
|
||||||
|
if (conf['forbid-duplicated-anchors'] and
|
||||||
|
isinstance(token, yaml.AnchorToken) and
|
||||||
|
token.value in context['anchors']):
|
||||||
|
yield LintProblem(
|
||||||
|
token.start_mark.line + 1, token.start_mark.column + 1,
|
||||||
|
f'found duplicated anchor "{token.value}"')
|
||||||
|
|
||||||
|
if conf['forbid-unused-anchors']:
|
||||||
|
# Unused anchors can only be detected at the end of Document.
|
||||||
|
# End of document can be either
|
||||||
|
# - end of stream
|
||||||
|
# - end of document sign '...'
|
||||||
|
# - start of a new document sign '---'
|
||||||
|
# If next token indicates end of document,
|
||||||
|
# check if the anchors have been used or not.
|
||||||
|
# If they haven't been used, report problem on those anchors.
|
||||||
|
if isinstance(next, (yaml.StreamEndToken,
|
||||||
|
yaml.DocumentStartToken,
|
||||||
|
yaml.DocumentEndToken)):
|
||||||
|
for anchor, info in context['anchors'].items():
|
||||||
|
if not info['used']:
|
||||||
|
yield LintProblem(info['line'] + 1,
|
||||||
|
info['column'] + 1,
|
||||||
|
f'found unused anchor "{anchor}"')
|
||||||
|
elif isinstance(token, yaml.AliasToken):
|
||||||
|
context['anchors'].get(token.value, {})['used'] = True
|
||||||
|
|
||||||
|
if (conf['forbid-undeclared-aliases'] or
|
||||||
|
conf['forbid-duplicated-anchors'] or
|
||||||
|
conf['forbid-unused-anchors']):
|
||||||
|
if isinstance(token, yaml.AnchorToken):
|
||||||
|
context['anchors'][token.value] = {
|
||||||
|
'line': token.start_mark.line,
|
||||||
|
'column': token.start_mark.column,
|
||||||
|
'used': False
|
||||||
|
}
|
Loading…
Reference in New Issue