Skip to content

Commit ef996ea

Browse files
committed
Add json5_to_json function
1 parent 0255e2c commit ef996ea

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

lib/ts_utils/utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ def strip_comments(text: str) -> str:
3535
return text.split("#")[0].strip()
3636

3737

38+
def json5_to_json(text: str) -> str:
39+
"""Incomplete conversion from JSON5-like input to valid JSON."""
40+
# Remove full-line // comments only
41+
# (Can not remove inline comments)
42+
text = re.sub(r"(?m)^\s*//.*\n?", "", text)
43+
# Remove trailing commas before } or ]
44+
text = re.sub(r",\s*([}\]])", r"\1", text)
45+
return text
46+
47+
3848
# ====================================================================
3949
# Printing utilities
4050
# ====================================================================

tests/check_typeshed_structure.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from ts_utils.utils import (
1818
get_all_testcase_directories,
1919
get_gitignore_spec,
20+
json5_to_json,
2021
parse_requirements,
2122
parse_stdlib_versions_file,
2223
spec_matches_path,
@@ -177,13 +178,7 @@ def check_requirement_pins() -> None:
177178
def check_pyright_exclude_order() -> None:
178179
"""Check that 'exclude' entries in pyrightconfig.stricter.json are sorted alphabetically."""
179180
text = PYRIGHT_CONFIG.read_text(encoding="utf-8")
180-
181-
# Remove full-line // comments only
182-
# (Can not remove inline comments)
183-
text = re.sub(r"(?m)^\s*//.*\n?", "", text)
184-
# Remove trailing commas before } or ]
185-
text = re.sub(r",\s*([}\]])", r"\1", text)
186-
181+
text = json5_to_json(text)
187182
data = json.loads(text)
188183
exclude: list[str] = data.get("exclude", [])
189184

0 commit comments

Comments
 (0)