mirror of
https://github.com/rennf93/roboco.git
synced 2026-08-03 07:23:24 +02:00
[F030] conventions: typescript-scoped custom rules now apply to .tsx files
The validator tags a .tsx file as language 'tsx' (the JSX grammar needs
that tag, distinct from plain 'typescript'), but a custom rule scoped to
'typescript' — the language the scan reports for a React+TS repo — silently
skipped every .tsx file. The two suffix maps were NOT unified: the 'tsx'
tag is load-bearing (grammars.py picks the JSX grammar on it; hygiene.py
keys on it), so unifying would make .tsx fail to parse.
Fix is in check_custom: a one-directional dialect map _DIALECT_OF =
{'tsx': 'typescript'} — a typescript-scoped rule fires on a .tsx file,
but a tsx-scoped (JSX-only) rule still does not fire on plain .ts.
TDD; ruff/mypy clean; 80 unit + 38 integration conventions tests green.
This commit is contained in:
@@ -47,3 +47,60 @@ def test_bad_regex_abstains_without_crashing() -> None:
|
||||
rule = CustomRule(id="bad", pattern=r"(unclosed", message="m", level="block")
|
||||
std = ConventionsStandard(custom=[rule])
|
||||
assert check_custom("a.py", b"anything\n", "python", std) == []
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# .tsx is a TypeScript dialect: a rule scoped to ``typescript`` must fire on a
|
||||
# ``.tsx`` file. The validator tags a .tsx file as language ``tsx`` (the JSX
|
||||
# grammar needs that tag), so without dialect-awareness in the scoping check a
|
||||
# ``languages: [typescript]`` rule — the language the scan *reports* for a
|
||||
# React+TS repo — silently skipped every .tsx file.
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
_NO_CONSOLE = CustomRule(
|
||||
id="no-console",
|
||||
pattern=r"console\.",
|
||||
message="no console in app code",
|
||||
level="warn",
|
||||
languages=["typescript"],
|
||||
)
|
||||
|
||||
_JSX_ONLY = CustomRule(
|
||||
id="jsx-only",
|
||||
pattern=r"jsx",
|
||||
message="jsx-specific",
|
||||
level="warn",
|
||||
languages=["tsx"],
|
||||
)
|
||||
|
||||
|
||||
def test_typescript_scoped_rule_fires_on_tsx_file() -> None:
|
||||
"""A ``typescript``-scoped rule fires on a ``.tsx`` (language ``tsx``)
|
||||
file — tsx is a typescript dialect, and the scan reports the project as
|
||||
``typescript`` so operators scope rules to that."""
|
||||
std = ConventionsStandard(custom=[_NO_CONSOLE])
|
||||
findings = check_custom("a.tsx", b"console.log(1)\n", "tsx", std)
|
||||
assert len(findings) == 1
|
||||
assert findings[0].rule == "no-console"
|
||||
|
||||
|
||||
def test_typescript_scoped_rule_still_fires_on_ts_file() -> None:
|
||||
"""Sanity: the typescript-scoped rule still fires on a plain ``.ts``
|
||||
(language ``typescript``) file — the dialect fix must not regress the
|
||||
direct-match case."""
|
||||
std = ConventionsStandard(custom=[_NO_CONSOLE])
|
||||
assert len(check_custom("a.ts", b"console.log(1)\n", "typescript", std)) == 1
|
||||
|
||||
|
||||
def test_tsx_scoped_rule_does_not_fire_on_plain_typescript() -> None:
|
||||
"""A ``tsx``-scoped rule (JSX-only) must NOT fire on a plain ``.ts``
|
||||
file — the dialect relation is one-directional (tsx ⊂ typescript), so a
|
||||
JSX-specific rule does not apply to non-JSX TypeScript."""
|
||||
std = ConventionsStandard(custom=[_JSX_ONLY])
|
||||
assert check_custom("a.ts", b"var jsx = 1\n", "typescript", std) == []
|
||||
|
||||
|
||||
def test_tsx_scoped_rule_fires_on_tsx_file() -> None:
|
||||
"""Sanity: a ``tsx``-scoped rule still fires on a ``.tsx`` file directly."""
|
||||
std = ConventionsStandard(custom=[_JSX_ONLY])
|
||||
assert len(check_custom("a.tsx", b"var jsx = 1\n", "tsx", std)) == 1
|
||||
|
||||
Reference in New Issue
Block a user