Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions lib/ts_utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,40 @@ def colored(text: str, color: str | None = None, **kwargs: Any) -> str: # type:
return text


_REMOVE_COMMENT_RE = re.compile(
r"""
(\"(?:\\.|[^\\\"])*?\")
|
(\/\*.*?\*\/ | \/\/[^\r\n]*?(?:[\r\n]))
""",
re.DOTALL | re.VERBOSE,
)
_REMOVE_TRAILING_COMMA_RE = re.compile(
r"""
(\"(?:\\.|[^\\\"])*?\")
|
,\s*([\]}])
""",
re.DOTALL | re.VERBOSE,
)


PYTHON_VERSION: Final = f"{sys.version_info.major}.{sys.version_info.minor}"


def strip_comments(text: str) -> str:
return text.split("#")[0].strip()


def json5_to_json(text: str) -> str:
"""Incomplete conversion from JSON5-like input to valid JSON."""
# Remove full-line // comments only
# (Can not remove inline comments)
text = re.sub(r"(?m)^\s*//.*\n?", "", text)
def jsonc_to_json(text: str) -> str:
"""Conversion from JSONC format input to valid JSON."""
# Remove comments
if not text.endswith("\n"):
text += "\n"
text = _REMOVE_COMMENT_RE.sub(lambda m: m.group(1) or "", text)

# Remove trailing commas before } or ]
text = re.sub(r",\s*([}\]])", r"\1", text)
text = _REMOVE_TRAILING_COMMA_RE.sub(lambda m: m.group(1) or m.group(2), text)
return text


Expand Down
4 changes: 2 additions & 2 deletions tests/check_typeshed_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from ts_utils.utils import (
get_all_testcase_directories,
get_gitignore_spec,
json5_to_json,
jsonc_to_json,
parse_requirements,
parse_stdlib_versions_file,
spec_matches_path,
Expand Down Expand Up @@ -178,7 +178,7 @@ def check_requirement_pins() -> None:
def check_pyright_exclude_order() -> None:
"""Check that 'exclude' entries in pyrightconfig.stricter.json are sorted alphabetically."""
text = PYRIGHT_CONFIG.read_text(encoding="utf-8")
text = json5_to_json(text)
text = jsonc_to_json(text)
data = json.loads(text)
exclude: list[str] = data.get("exclude", [])

Expand Down