feat: add JSON linter (jsonlint) to customlinter package

- Move JSON linter from src/json_lint/json_lint.py to src/customlinter/jsonlint.py
- Use JSN prefix for violation codes (JSN001-JSN006) matching other linters
- Rename class from JSONLinter to JsonLinter for consistency
- Update all imports across codebase
- Fix inline import violations for pycustomlint
- Integrate into both sanity checkers (run_sanity.py and wiki/sanity_manager.py)

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
nvaldez
2026-09-28 20:55:07 +02:00
parent 90e7055dbd
commit 814bad2866
8 changed files with 99 additions and 127 deletions
+1 -2
View File
@@ -46,7 +46,7 @@ where = ["src"]
# The hyphen makes it unimportable as a Python module (import llm-wiki
# is a SyntaxError), but setuptools allows it as a distribution package name.
# The __init__.py exists only to satisfy setuptools' package detection.
include = ["fiona", "librarian", "mdoctor", "python_ruff", "customlinter", "sanity_runner", "llm-wiki", "wiki", "gittool", "html_js_lint", "json_lint"]
include = ["fiona", "librarian", "mdoctor", "python_ruff", "customlinter", "sanity_runner", "llm-wiki", "wiki", "gittool", "html_js_lint"]
[tool.setuptools.package-data]
# Include all files under llm-wiki/ as package data (not Python code)
@@ -76,4 +76,3 @@ fiona-assistant = "fiona.__main__:main"
librarian = "librarian.librarian:main"
mdoctor = "mdoctor.mdoctor:main"
html-js-lint = "html_js_lint.html_js_lint:main"
json-lint = "json_lint.json_lint:main"
+3
View File
@@ -7,6 +7,7 @@ various code and content rules. It includes:
- FrontmatterLinter: Linter for markdown file frontmatter validation
- MarkdownLinter: Linter for markdown content rules (dead links)
- PythonCustomLinter: Linter for Python code rules (inline imports, etc.)
- JsonLinter: Linter for JSON syntax and style validation
- LintViolation: Standardized violation representation
- LinterError: Base exception for linting failures
- CustomLintError: Alias for Python linter exceptions
@@ -25,6 +26,7 @@ from customlinter.base import (
LintViolation,
)
from customlinter.fmlint import FrontmatterLinter
from customlinter.jsonlint import JsonLinter
from customlinter.mdlinter import MarkdownLinter
from customlinter.pycustomlint import PythonCustomLinter
@@ -35,6 +37,7 @@ __all__ = [
"CustomLintError",
"FrontmatterLintError",
"FrontmatterLinter",
"JsonLinter",
"LintViolation",
"LinterError",
"MarkdownLinter",
@@ -18,6 +18,7 @@ import json
import logging
import pathlib
import re
import sys
from typing import Any, ClassVar
from customlinter.base import BaseLinter, LintViolation, LinterError
@@ -26,7 +27,7 @@ from fiona.logging_config import setup_logging
logger = logging.getLogger(__name__)
class JSONLinter(BaseLinter):
class JsonLinter(BaseLinter):
"""JSON linter that extends BaseLinter.
Provides JSON syntax and style validation with configurable rules.
@@ -43,29 +44,35 @@ class JSONLinter(BaseLinter):
# Violation codes with their descriptions
VIOLATION_CODES: ClassVar[dict[str, dict[str, Any]]] = {
"JSON_SYNTAX_ERROR": {
"JSN001": {
"rule_name": "JSON Syntax Error",
"description": "Invalid JSON syntax that prevents parsing"
"description": "Invalid JSON syntax that prevents parsing",
"severity": "error"
},
"JSON_INDENTATION": {
"JSN002": {
"rule_name": "Indentation Error",
"description": "Incorrect indentation (spaces vs tabs, or wrong length)"
"description": "Incorrect indentation (spaces vs tabs, or wrong length)",
"severity": "error"
},
"JSON_TRAILING_COMMA": {
"JSN003": {
"rule_name": "Trailing Comma",
"description": "JSON does not allow trailing commas (RFC 8259)"
"description": "JSON does not allow trailing commas (RFC 8259)",
"severity": "error"
},
"JSON_DUPLICATE_KEYS": {
"JSN004": {
"rule_name": "Duplicate Keys",
"description": "Object has duplicate keys"
"description": "Object has duplicate keys",
"severity": "error"
},
"JSON_KEY_ORDER": {
"JSN005": {
"rule_name": "Key Order",
"description": "Keys are not in the required order"
"description": "Keys are not in the required order",
"severity": "warning"
},
"JSON_MAX_LINE_LENGTH": {
"JSN006": {
"rule_name": "Line Too Long",
"description": "JSON line exceeds maximum allowed length"
"description": "JSON line exceeds maximum allowed length",
"severity": "warning"
},
}
@@ -140,9 +147,9 @@ class JSONLinter(BaseLinter):
filepath=pathlib.Path("temp.json"), # Will be updated
line=line_num,
column=column,
code="JSON_TRAILING_COMMA",
code="JSN003",
message=f"{line_num}:{column} Trailing comma before {match.group(1)}",
rule_id="JSON_TRAILING_COMMA",
rule_id="JSN003",
severity="error"
))
@@ -185,9 +192,9 @@ class JSONLinter(BaseLinter):
filepath=filepath,
line=line_num,
column=column,
code="JSON_INDENTATION",
code="JSN002",
message=f"{line_num}:{column} Found tab but expected spaces",
rule_id="JSON_INDENTATION",
rule_id="JSN002",
severity="error"
))
elif expected_indent_char == '\t' and ' ' in leading_ws and '\t' not in leading_ws:
@@ -197,9 +204,9 @@ class JSONLinter(BaseLinter):
filepath=filepath,
line=line_num,
column=column,
code="JSON_INDENTATION",
code="JSN002",
message=f"{line_num}:{column} Found spaces but expected tabs",
rule_id="JSON_INDENTATION",
rule_id="JSN002",
severity="error"
))
elif expected_indent_char == '\t' and ' ' in leading_ws:
@@ -209,9 +216,9 @@ class JSONLinter(BaseLinter):
filepath=filepath,
line=line_num,
column=column,
code="JSON_INDENTATION",
code="JSN002",
message=f"{line_num}:{column} Mixed tabs and spaces in indentation",
rule_id="JSON_INDENTATION",
rule_id="JSN002",
severity="error"
))
@@ -224,9 +231,9 @@ class JSONLinter(BaseLinter):
filepath=filepath,
line=line_num,
column=column,
code="JSON_INDENTATION",
code="JSN002",
message=f"{line_num}:{column} Indentation {space_count} spaces is not a multiple of {expected_indent_len}",
rule_id="JSON_INDENTATION",
rule_id="JSN002",
severity="warning"
))
@@ -262,9 +269,9 @@ class JSONLinter(BaseLinter):
filepath=filepath,
line=line_num,
column=1,
code="JSON_DUPLICATE_KEYS",
code="JSN004",
message=f"Duplicate key '{key}' at {path}",
rule_id="JSON_DUPLICATE_KEYS",
rule_id="JSN004",
severity="error"
))
else:
@@ -303,9 +310,9 @@ class JSONLinter(BaseLinter):
filepath=filepath,
line=line_num,
column=self.max_line_length + 1,
code="JSON_MAX_LINE_LENGTH",
code="JSN006",
message=f"{line_num}:{self.max_line_length + 1} Line exceeds {self.max_line_length} characters",
rule_id="JSON_MAX_LINE_LENGTH",
rule_id="JSN006",
severity="warning"
))
@@ -338,9 +345,9 @@ class JSONLinter(BaseLinter):
filepath=filepath,
line=1, # Line number not easily available without parsing
column=1,
code="JSON_KEY_ORDER",
code="JSN005",
message=f"Keys not in alphabetical order at {path}",
rule_id="JSON_KEY_ORDER",
rule_id="JSN005",
severity="warning"
))
for key, value in obj.items():
@@ -413,9 +420,9 @@ class JSONLinter(BaseLinter):
filepath=resolved_filepath,
line=line_num,
column=column,
code="JSON_SYNTAX_ERROR",
code="JSN001",
message=f"{line_num}:{column} JSON syntax error: {e.msg}",
rule_id="JSON_SYNTAX_ERROR",
rule_id="JSN001",
severity="error"
))
# Can't do further checks if syntax is invalid
@@ -593,7 +600,7 @@ def main():
setup_logging()
logging.getLogger(__name__).debug("json-lint subcommand: %s", args.subcommand)
json_linter = JSONLinter()
json_linter = JsonLinter()
if args.subcommand is None:
# Default to chklint with current directory
@@ -607,7 +614,6 @@ def main():
# Return exit code 1 if there are violations
if violations and "No violations" not in violations:
import sys
sys.exit(1)
elif args.subcommand == "fix":
@@ -620,7 +626,7 @@ def main():
# Collect all JSON files and fix them
files = []
for ext in JSONLinter.file_extensions:
for ext in JsonLinter.file_extensions:
files.extend(directory_path.glob(f"**/*{ext}"))
fixed_count = 0
@@ -641,10 +647,8 @@ def main():
else:
parser.print_help()
import sys
sys.exit(1)
if __name__ == "__main__":
import sys
sys.exit(main())
+5 -5
View File
@@ -9,9 +9,9 @@ import logging
import sys
from customlinter.fmlint import main as fmlint_main
from customlinter.jsonlint import main as jsonlint_main
from fiona.logging_config import setup_logging
from gittool.git_tool import main as git_tool_main
from json_lint.json_lint import main as json_lint_main
from librarian.librarian import main as librarian_main
from librarian.toc_generator import main as toc_generator_main
from mdoctor.mdoctor import main as mdoctor_main
@@ -325,12 +325,12 @@ def main():
sys.argv = ["python-ruff"] + args.args
python_ruff_main()
elif args.command == "json-lint":
# Pass all remaining args to json-lint, default to chklint with current directory
# Pass all remaining args to jsonlint, default to chklint with current directory
if not args.args:
sys.argv = ["json-lint", "chklint", "."]
sys.argv = ["jsonlint", "chklint", "."]
else:
sys.argv = ["json-lint"] + args.args
json_lint_main()
sys.argv = ["jsonlint"] + args.args
jsonlint_main()
elif args.command == "git": # pragma: no cover
# Handle git subcommands
# Note: CLI routing is tested via integration tests
-4
View File
@@ -1,4 +0,0 @@
"""JSON Linter package.
Wraps JSON linting functionality for the Fiona-Assistant CLI.
"""
+3 -3
View File
@@ -19,8 +19,8 @@ from pathlib import Path
import sanity_runner
from customlinter.fmlint import FrontmatterLinter
from customlinter.jsonlint import JsonLinter
from customlinter.pycustomlint import PythonCustomLinter
from json_lint.json_lint import JSONLinter
from mdoctor.mdoctor import MDoctor
from python_ruff.python_ruff import PythonRuff
from run_tests import TestRunner
@@ -446,7 +446,7 @@ def run_frontmatter_lint():
def run_jsonlint():
"""Run JSON linter programmatically using JSONLinter.
"""Run JSON linter programmatically using JsonLinter.
Scans all JSON files in the repository for syntax and style violations.
@@ -457,7 +457,7 @@ def run_jsonlint():
- stdout: AI-friendly report naming each violating file,
the line:column position, and the rule
"""
json_linter = JSONLinter()
json_linter = JsonLinter()
# Lint all JSON files in the repository
violations = json_linter.lint_directory("src")
+44 -74
View File
@@ -6,73 +6,65 @@ from __future__ import annotations
import json
import pathlib
import subprocess
import sys
import tempfile
from unittest.mock import Mock, patch
import pytest
from customlinter.base import LintViolation
from customlinter.base import BaseLinter, LintViolation, LinterError
from customlinter.jsonlint import JsonLinter, main
def test_json_lint_module_exists():
"""Test that the json_lint module can be imported."""
try:
from json_lint import json_lint
assert json_lint is not None
except ImportError as e:
pytest.fail(f"Failed to import json_lint module: {e}")
"""Test that the jsonlint module can be imported."""
assert JsonLinter is not None
def test_json_linter_class_exists():
"""Test that the JSONLinter class exists and is a subclass of BaseLinter."""
from customlinter.base import BaseLinter
from json_lint.json_lint import JSONLinter
"""Test that the JsonLinter class exists and is a subclass of BaseLinter."""
assert issubclass(JSONLinter, BaseLinter)
assert issubclass(JsonLinter, BaseLinter)
def test_json_linter_file_extensions():
"""Test that JSONLinter has the correct file extensions."""
from json_lint.json_lint import JSONLinter
"""Test that JsonLinter has the correct file extensions."""
assert hasattr(JSONLinter, 'file_extensions')
assert '.json' in JSONLinter.file_extensions
assert hasattr(JsonLinter, 'file_extensions')
assert '.json' in JsonLinter.file_extensions
def test_json_linter_settings_key_path():
"""Test that JSONLinter has the correct settings key path."""
from json_lint.json_lint import JSONLinter
"""Test that JsonLinter has the correct settings key path."""
assert hasattr(JSONLinter, 'settings_key_path')
assert JSONLinter.settings_key_path == ['json']
assert hasattr(JsonLinter, 'settings_key_path')
assert JsonLinter.settings_key_path == ['json']
def test_json_linter_linter_name():
"""Test that JSONLinter has the correct linter name."""
from json_lint.json_lint import JSONLinter
"""Test that JsonLinter has the correct linter name."""
assert hasattr(JSONLinter, 'linter_name')
assert JSONLinter.linter_name == 'JSONLint'
assert hasattr(JsonLinter, 'linter_name')
assert JsonLinter.linter_name == 'JSONLint'
def test_json_linter_violation_codes():
"""Test that JSONLinter has violation codes defined."""
from json_lint.json_lint import JSONLinter
"""Test that JsonLinter has violation codes defined."""
assert hasattr(JSONLinter, 'VIOLATION_CODES')
assert isinstance(JSONLinter.VIOLATION_CODES, dict)
assert hasattr(JsonLinter, 'VIOLATION_CODES')
assert isinstance(JsonLinter.VIOLATION_CODES, dict)
# Check that expected violation codes are present
assert 'JSON_SYNTAX_ERROR' in JSONLinter.VIOLATION_CODES
assert 'JSON_INDENTATION' in JSONLinter.VIOLATION_CODES
assert 'JSON_TRAILING_COMMA' in JSONLinter.VIOLATION_CODES
assert 'JSON_DUPLICATE_KEYS' in JSONLinter.VIOLATION_CODES
assert 'JSN001' in JsonLinter.VIOLATION_CODES
assert 'JSN002' in JsonLinter.VIOLATION_CODES
assert 'JSN003' in JsonLinter.VIOLATION_CODES
assert 'JSN004' in JsonLinter.VIOLATION_CODES
def test_lint_file_valid_json():
"""Test linting a valid JSON file with no violations."""
from json_lint.json_lint import JSONLinter
linter = JSONLinter()
linter = JsonLinter()
# Create a temporary valid JSON file
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
@@ -90,9 +82,8 @@ def test_lint_file_valid_json():
def test_lint_file_invalid_json():
"""Test linting an invalid JSON file returns syntax error violation."""
from json_lint.json_lint import JSONLinter
linter = JSONLinter()
linter = JsonLinter()
# Create a temporary invalid JSON file
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
@@ -106,16 +97,15 @@ def test_lint_file_invalid_json():
assert len(violations) > 0
# Check that at least one violation has the syntax error code
codes = [v.code for v in violations]
assert 'JSON_SYNTAX_ERROR' in codes
assert 'JSN001' in codes
finally:
pathlib.Path(temp_file).unlink()
def test_lint_file_trailing_comma():
"""Test linting a JSON file with trailing commas."""
from json_lint.json_lint import JSONLinter
linter = JSONLinter()
linter = JsonLinter()
# Create a temporary JSON file with trailing comma (invalid in strict JSON)
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
@@ -133,9 +123,8 @@ def test_lint_file_trailing_comma():
def test_lint_file_duplicate_keys():
"""Test linting a JSON file with duplicate keys."""
from json_lint.json_lint import JSONLinter
linter = JSONLinter()
linter = JsonLinter()
# Create a temporary JSON file with duplicate keys
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
@@ -153,9 +142,8 @@ def test_lint_file_duplicate_keys():
def test_lint_file_indentation():
"""Test linting a JSON file with incorrect indentation."""
from json_lint.json_lint import JSONLinter
linter = JSONLinter()
linter = JsonLinter()
# Create a temporary JSON file with mixed indentation
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
@@ -173,9 +161,8 @@ def test_lint_file_indentation():
def test_lint_directory():
"""Test linting a directory of JSON files."""
from json_lint.json_lint import JSONLinter
linter = JSONLinter()
linter = JsonLinter()
# Create a temporary directory with multiple JSON files
with tempfile.TemporaryDirectory() as temp_dir:
@@ -199,9 +186,8 @@ def test_lint_directory():
def test_lint_directory_no_json_files():
"""Test linting a directory with no JSON files."""
from json_lint.json_lint import JSONLinter
linter = JSONLinter()
linter = JsonLinter()
# Create a temporary directory with no JSON files
with tempfile.TemporaryDirectory() as temp_dir:
@@ -220,9 +206,8 @@ def test_lint_directory_no_json_files():
def test_violation_is_lint_violation():
"""Test that violations are LintViolation instances."""
from json_lint.json_lint import JSONLinter
linter = JSONLinter()
linter = JsonLinter()
# Create a temporary invalid JSON file
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
@@ -247,9 +232,8 @@ def test_violation_is_lint_violation():
def test_compress_violations():
"""Test compressing violations into the shared format."""
from json_lint.json_lint import JSONLinter
linter = JSONLinter()
linter = JsonLinter()
# Create a temporary invalid JSON file
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
@@ -273,9 +257,8 @@ def test_compress_violations():
def test_format_violations():
"""Test formatting violations for display."""
from json_lint.json_lint import JSONLinter
linter = JSONLinter()
linter = JsonLinter()
# Create a temporary invalid JSON file
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
@@ -298,9 +281,8 @@ def test_format_violations():
def test_format_violations_empty():
"""Test formatting empty violations."""
from json_lint.json_lint import JSONLinter
linter = JSONLinter()
linter = JsonLinter()
# Create a temporary valid JSON file
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
@@ -320,9 +302,8 @@ def test_format_violations_empty():
def test_fix_file_valid_json():
"""Test fixing a valid JSON file (should return empty list)."""
from json_lint.json_lint import JSONLinter
linter = JSONLinter()
linter = JsonLinter()
# Create a temporary valid JSON file
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
@@ -351,7 +332,6 @@ def test_fix_file_valid_json():
def test_fix_file_with_settings():
"""Test fixing a JSON file with specific settings."""
from json_lint.json_lint import JSONLinter
# Create settings file
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
@@ -368,7 +348,7 @@ def test_fix_file_with_settings():
settings_file = f.name
try:
linter = JSONLinter(settings_file=pathlib.Path(settings_file))
linter = JsonLinter(settings_file=pathlib.Path(settings_file))
assert linter is not None
finally:
pathlib.Path(settings_file).unlink()
@@ -376,8 +356,6 @@ def test_fix_file_with_settings():
def test_main_function():
"""Test the main CLI function."""
from json_lint.json_lint import main
# This should not raise an exception when called without args
# (it should use defaults)
try:
@@ -391,12 +369,10 @@ def test_main_function():
def test_cli_chklint_subcommand():
"""Test the chklint CLI subcommand."""
import subprocess
import sys
# Test that the CLI can be invoked
# Test that the CLI can be invoked via fiona-assistant
result = subprocess.run(
[sys.executable, '-m', 'json_lint.json_lint', 'chklint', '.'],
[sys.executable, '-m', 'fiona.__main__', 'json-lint', 'chklint', 'src'],
capture_output=True,
text=True,
cwd='/home/user/repositories/fiona-assistant'
@@ -408,9 +384,8 @@ def test_cli_chklint_subcommand():
def test_readonly_zones_skipped():
"""Test that files in read-only zones are skipped."""
from json_lint.json_lint import JSONLinter
linter = JSONLinter()
linter = JsonLinter()
# Create a temporary directory structure with personal/ subdirectory
with tempfile.TemporaryDirectory() as temp_dir:
@@ -439,9 +414,8 @@ def test_readonly_zones_skipped():
def test_respects_gitignore():
"""Test that .gitignore files are respected."""
from json_lint.json_lint import JSONLinter
linter = JSONLinter()
linter = JsonLinter()
# Create a temporary directory with .gitignore
with tempfile.TemporaryDirectory() as temp_dir:
@@ -470,10 +444,8 @@ def test_respects_gitignore():
def test_lint_file_nonexistent():
"""Test linting a non-existent file raises appropriate error."""
from json_lint.json_lint import JSONLinter
from customlinter.base import LinterError
linter = JSONLinter()
linter = JsonLinter()
with pytest.raises(Exception):
linter.lint_file(pathlib.Path('/nonexistent/file.json'))
@@ -481,10 +453,8 @@ def test_lint_file_nonexistent():
def test_lint_directory_nonexistent():
"""Test linting a non-existent directory raises appropriate error."""
from json_lint.json_lint import JSONLinter
from customlinter.base import LinterError
linter = JSONLinter()
linter = JsonLinter()
with pytest.raises(LinterError):
linter.lint_directory(pathlib.Path('/nonexistent/directory'))
+3 -3
View File
@@ -27,8 +27,8 @@ import run_sanity
import sanity_runner
from customlinter.base import LintViolation
from customlinter.fmlint import FrontmatterLinter
from customlinter.jsonlint import JsonLinter
from customlinter.mdlinter import MarkdownLinter
from json_lint.json_lint import JSONLinter
from mdoctor.mdoctor import MDoctor
from python_ruff.python_ruff import PythonRuff
@@ -140,7 +140,7 @@ def run_frontmatter_lint(wiki_root: Path) -> dict:
def run_json_lint(wiki_root: Path) -> dict:
"""Run the JSON linter (json-lint) over the wiki.
"""Run the JSON linter (jsonlint) over the wiki.
Validates JSON files for syntax and style violations.
@@ -151,7 +151,7 @@ def run_json_lint(wiki_root: Path) -> dict:
dict: Check result with result, violation_count, stdout, and
LintViolation objects.
"""
linter = JSONLinter(settings_file=wiki_root / SETTINGS_FILE)
linter = JsonLinter(settings_file=wiki_root / SETTINGS_FILE)
violations = linter.lint_directory(wiki_root)
compressed = linter.compress_violations(violations, wiki_root)
report = linter.format_compressed_violations(compressed)