ts_languages.py¶
clew/guard/ts_languages.py
Tree-sitter grammar loading and language-specific node type maps.
_language_from_config
str | None clew.guard.ts_languages._language_from_config(str ext, dict[str, Any] config)
Look up language name from user config extensions.
clew/guard/ts_languages.py:175
Source · 8 lines
def _language_from_config(ext: str, config: dict[str, Any]) -> str | None:
languages = config.get("validate", {}).get("languages", {})
for lang_name, lang_config in languages.items():
if ext in lang_config.get("extensions", []) and lang_name in LANGUAGE_SPECS:
return lang_name
return None
Activity · 5 steps
Flows
_looks_like_cpp_header
bool clew.guard.ts_languages._looks_like_cpp_header(str file_path)
Detect C++ constructs in a .h file to route to cpp grammar.
clew/guard/ts_languages.py:188
Source · 15 lines
def _looks_like_cpp_header(file_path: str) -> bool:
try:
with open(file_path, encoding="utf-8", errors="replace") as f:
content = f.read(8192)
except OSError:
return False
import re
patterns = (
r"^\s*namespace\s+\w",
r"^\s*template\s*<",
r"^\s*class\s+\w",
r"^\s*(?:public|private|protected)\s*:",
)
return any(re.search(p, content, re.MULTILINE) for p in patterns)1 more lines, not shown
Activity · 8 steps
Flows
_substrate_extension_table
dict[str, str] clew.guard.ts_languages._substrate_extension_table()
Build the extension -> language table from the parsing substrate's registry.
clew/guard/ts_languages.py:104
Source · 12 lines
def _substrate_extension_table() -> dict[str, str]:
import lang_parsing_substrate
table: dict[str, str] = {}
for info in lang_parsing_substrate.languages():
if info.key not in LANGUAGE_SPECS:
continue
for ext in (*info.extensions, *info.explicit_only):
table.setdefault(f".{ext}", info.key)
return table
Activity · 8 steps
Requirements
get_language_spec
LanguageSpec | None clew.guard.ts_languages.get_language_spec(str lang_name)
Get the LanguageSpec for a named language.
clew/guard/ts_languages.py:140
Source · 4 lines
def get_language_spec(lang_name: str) -> LanguageSpec | None:
return LANGUAGE_SPECS.get(lang_name)
Activity · 1 steps
Requirements
Flows
get_parser_for_language
Any clew.guard.ts_languages.get_parser_for_language(str lang_name)
Get a tree-sitter Parser for a named language.
clew/guard/ts_languages.py:126
Source · 10 lines
def get_parser_for_language(lang_name: str) -> Any:
spec = LANGUAGE_SPECS.get(lang_name)
if spec is None:
logger.warning("No tree-sitter spec for language: %s", lang_name)
return None
from ..tsnode import Parser as SubstrateParser
return SubstrateParser(spec.grammar_module.removeprefix("substrate:"))
Activity · 6 steps
Requirements
Flows
language_for_extension
str | None clew.guard.ts_languages.language_for_extension(str ext)
Resolve a file extension to a language name.
clew/guard/ts_languages.py:148
Source · 4 lines
def language_for_extension(ext: str) -> str | None:
return EXTENSION_TO_LANGUAGE.get(ext)
Activity · 1 steps
Requirements
Flows
language_for_file
str | None clew.guard.ts_languages.language_for_file(str file_path, dict[str, Any] config)
Resolve a file path to a language name using config extensions.
clew/guard/ts_languages.py:158
Called by parse_content_for_file
Tested by 9 tests
test_h_file_with_class_detected_as_cpp, test_h_file_with_namespace_detected_as_cpp, test_h_file_with_template_detected_as_cpp, test_language_for_file_unknown, test_language_for_file_with_config, test_languages_without_a_spec_are_not_mapped, test_plain_c_h_file_stays_c, test_tsx_file_uses_typescript_config, test_tsx_is_routed_to_the_jsx_aware_grammar
Calls _language_from_config, _looks_like_cpp_header, language_for_extension
Source · 14 lines
def language_for_file(file_path: str, config: dict[str, Any]) -> str | None:
from pathlib import Path
ext = Path(file_path).suffix
lang = language_for_extension(ext)
if lang == "c" and ext == ".h" and _looks_like_cpp_header(file_path):
lang = "cpp"
if lang == "typescript" and ext == ".tsx":
lang = "tsx"
if lang is None:
lang = _language_from_config(ext, config)
return lang