mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-03-29 10:16:52 +00:00
fix sort_simple_yaml.py to handle comments with space before #
This commit is contained in:
parent
cc94673a3f
commit
4baf093f26
1 changed files with 12 additions and 2 deletions
|
|
@ -20,12 +20,22 @@ complicated YAML files.
|
|||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import re
|
||||
from typing import Sequence
|
||||
|
||||
|
||||
QUOTES = ["'", '"']
|
||||
|
||||
|
||||
def _is_comment(line: str) -> bool:
|
||||
"""Tell if a YAML line is a comment, starting with spaces and a #.
|
||||
|
||||
:param line: line of text
|
||||
:return: True if line is a yaml comment
|
||||
"""
|
||||
return re.search(r'^\s*#', line) is not None
|
||||
|
||||
|
||||
def sort(lines: list[str]) -> list[str]:
|
||||
"""Sort a YAML file in alphabetical order, keeping blocks together.
|
||||
|
||||
|
|
@ -55,7 +65,7 @@ def parse_block(lines: list[str], header: bool = False) -> list[str]:
|
|||
:return: list of lines that form the single block
|
||||
"""
|
||||
block_lines = []
|
||||
while lines and lines[0] and (not header or lines[0].startswith('#')):
|
||||
while lines and lines[0] and (not header or _is_comment(lines[0])):
|
||||
block_lines.append(lines.pop(0))
|
||||
return block_lines
|
||||
|
||||
|
|
@ -90,7 +100,7 @@ def first_key(lines: list[str]) -> str:
|
|||
'foo'
|
||||
"""
|
||||
for line in lines:
|
||||
if line.startswith('#'):
|
||||
if _is_comment(line):
|
||||
continue
|
||||
if any(line.startswith(quote) for quote in QUOTES):
|
||||
return line[1:]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue