Skip to content

Commit

Permalink
feat(yaml): autofix YAML files (#431)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreoliwa authored Dec 31, 2021
1 parent 101ece9 commit d8cc4b1
Show file tree
Hide file tree
Showing 35 changed files with 491 additions and 124 deletions.
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ index.html
_includes/*
*.min.css
README.md

# Prettier uses double quotes, ruamel.yaml uses single quotes
tests/test_yaml/*.yaml
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ output-format=colorized
# --disable=W"
# Configurations for the black formatter
disable=bad-continuation,bad-whitespace,
fixme,cyclic-import
fixme,cyclic-import,line-too-long

[BASIC]

Expand Down
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ Implemented
* - `Any TOML file <https://nitpick.rtfd.io/en/latest/plugins.html#toml-files>`_
- ✅
- ✅
* - `Any YAML file (except .pre-commit-config.yaml) <https://nitpick.rtfd.io/en/latest/plugins.html#yaml-files>`_
- ✅
- ✅
* - `.editorconfig <https://nitpick.rtfd.io/en/latest/examples.html#example-editorconfig>`_
- ✅
- ✅
Expand Down
5 changes: 3 additions & 2 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,8 @@ Contents of `resources/python/mypy.toml <https://github.com/andreoliwa/nitpick/b
# Lint-style cleanliness for typing
warn_redundant_casts = true
warn_unused_ignores = true
# False positives when running on local machine... it works on pre-commit.ci ¯\_(ツ)_/¯
warn_unused_ignores = false
[[".pre-commit-config.yaml".repos]]
yaml = """
Expand Down Expand Up @@ -368,7 +369,7 @@ Contents of `resources/python/pylint.toml <https://github.com/andreoliwa/nitpick
# comma_separated_values = ["MESSAGES CONTROL.disable"]
# This syntax will be deprecated anyway, so I won't make it work now
# Configurations for the black formatter
#disable = "bad-continuation,bad-whitespace,fixme,cyclic-import"
#disable = "bad-continuation,bad-whitespace,fixme,cyclic-import,line-too-long"
[".pylintrc".BASIC]
# List of builtins function names that should not be used, separated by a comma
Expand Down
3 changes: 3 additions & 0 deletions docs/generate_rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ def row(self) -> Tuple[str, str, str]:
FileType("Any JSON file", f"{READ_THE_DOCS_URL}plugins.html#json-files", True, True),
FileType("Any text file", f"{READ_THE_DOCS_URL}plugins.html#text-files", True, False),
FileType("Any TOML file", f"{READ_THE_DOCS_URL}plugins.html#toml-files", True, True),
FileType(
f"Any YAML file (except {PRE_COMMIT_CONFIG_YAML})", f"{READ_THE_DOCS_URL}plugins.html#yaml-files", True, True
),
FileType(EDITOR_CONFIG, f"{READ_THE_DOCS_URL}examples.html#example-editorconfig", True, True),
FileType(PRE_COMMIT_CONFIG_YAML, f"{READ_THE_DOCS_URL}plugins.html#pre-commit-config-yaml", True, 282),
FileType(PYLINTRC, f"{READ_THE_DOCS_URL}plugins.html#ini-files", True, True),
Expand Down
25 changes: 0 additions & 25 deletions docs/ideas/yaml/jmespath-on-section.toml

This file was deleted.

7 changes: 0 additions & 7 deletions docs/ideas/yaml/jmespath-simple.toml

This file was deleted.

25 changes: 0 additions & 25 deletions docs/ideas/yaml/jmespath-table.toml

This file was deleted.

39 changes: 39 additions & 0 deletions docs/ideas/yaml/jmespath.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# JMESPath as part of the section name, after the file name.
# Everything after the file name is considered a JMESPath https://jmespath.org/
# Format: ["path/to/file.ext".jmes.path.expression]
# The values below were taken from .github/workflows/python.yaml in this repo

# 1. Complex JMESPath expressions should be quoted
# (I still don't know how to deal with JMESPath that matches multiple items)
[[".github/workflows/python.yaml"."jobs.build.steps[].{name: name, uses: uses}"]]
uses = "actions/checkout@v2"

# 2. JMESPath expression that has double quotes, wrapped in single quotes for TOML
[[".github/workflows/python.yaml".'jobs.build.strategy.matrix."python-version"']]
name = "Set up Python ${{ matrix.python-version }}"
uses = "actions/setup-python@v2"
with = {"python-version" = "${{ matrix.python-version }}"}

# 3. It allows Jinja tuning in https://github.com/andreoliwa/nitpick/issues/283
name__jinja = "Set up Python ${{ matrix.python-version }}"
name__no_jinja = "Set up Python ${{ matrix.python-version }}"
name__jinja_off = "Set up Python ${{ matrix.python-version }}"

# 4. "{{" and "}}" will conflict with Jinja https://github.com/andreoliwa/nitpick/issues/283
# So we need a way to turn on/off Jinja templating.
# Probably "false" will be the default, to keep compatibility.
# Whoever wants to use Jinja will need to set "true" either here or as a global config on .nitpick.toml
[".github/workflows/python.yaml".jobs.build]
__jinja = false

# 5. Another way to turn off Jinja for a specific key only, not the whole dict
# (using the "__" syntax from Django filters, SQLAlchemy, factoryboy...)
"runs-on__no_jinja" = "${{ matrix.os }}"

# 6. Simplified API, having JMESPath as direct keys
# Read the discussion: https://github.com/andreoliwa/nitpick/pull/353/files#r613816390
[".github/workflows/jmespath-simple.yaml"]
"jobs.build.strategy.matrix.os" = "foo"
"jobs.build.steps" = ["bar"]
"jobs.build.steps.regex" = "baz d+"
"jobs.build.steps.contains" = "baz"
13 changes: 10 additions & 3 deletions docs/plugins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Style example: :ref:`the default pre-commit hooks <example-pre-commit-hooks>`.
INI files
---------

Enforce config on INI files.
Enforce configurations and autofix INI files.

Examples of ``.ini`` files handled by this plugin:

Expand All @@ -42,7 +42,7 @@ Style examples enforcing values on INI files: :ref:`flake8 configuration <exampl
JSON files
----------

Enforce configurations for any JSON file.
Enforce configurations and autofix JSON files.

Add the configurations for the file name you wish to check.
Style example: :ref:`the default config for package.json <example-package-json>`.
Expand All @@ -69,7 +69,7 @@ To check if ``some.txt`` file contains the lines ``abc`` and ``def`` (in any ord
TOML files
----------

Enforce config on TOML files.
Enforce configurations and autofix TOML files.

E.g.: `pyproject.toml (PEP 518) <https://www.python.org/dev/peps/pep-0518/#file-format>`_.

Expand All @@ -78,3 +78,10 @@ See also `the [tool.poetry] section of the pyproject.toml file

Style example: :ref:`Python 3.8 version constraint <example-python-3-8>`.
There are :ref:`many other examples here <examples>`.

.. _yamlplugin:

YAML files
----------

Enforce configurations and autofix YAML files.
6 changes: 3 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ NIP = "nitpick.flake8:NitpickFlake8Extension"
[tool.poetry.plugins.nitpick]
text = "nitpick.plugins.text"
json = "nitpick.plugins.json"
pre_commit = "nitpick.plugins.pre_commit"
pre_commit = "nitpick.plugins.pre_commit" # TODO: remove this when removing the plugin class
ini = "nitpick.plugins.ini"
toml = "nitpick.plugins.toml"
yaml = "nitpick.plugins.yaml"

[tool.poetry.dependencies]
python = "^3.6.1"
Expand Down
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ follow_imports = normal
strict_optional = True
warn_no_return = True
warn_redundant_casts = True
warn_unused_ignores = True
# False positives when running on local machine... it works on pre-commit.ci ¯\_(ツ)_/¯
warn_unused_ignores = false
exclude =
src/nitpick/compat.py

Expand Down
2 changes: 1 addition & 1 deletion src/nitpick/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
DOUBLE_QUOTE = '"'

#: Special unique separator for :py:meth:`flatten()` and :py:meth:`unflatten()`,
# to avoid collision with existing key values (e.g. the default dot separator "." can be part of a pyproject.toml key).
# to avoid collision with existing key values (e.g. the default dot separator "." can be part of a TOML key).
SEPARATOR_FLATTEN = "$#@"

#: Special unique separator for :py:meth:`nitpick.generic.quoted_split()`.
Expand Down
Loading

0 comments on commit d8cc4b1

Please sign in to comment.