Skip to content

Commit c381deb

Browse files
committed
Rule to drop 'Rule' suffix from rule names
ghstack-source-id: eef0464 Pull Request resolved: #350
1 parent a7f1c60 commit c381deb

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

docs/guide/builtins.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,6 +1269,7 @@ Built-in Rules
12691269
.. automodule:: fixit.upgrade
12701270

12711271
- :class:`FixitDeprecatedImport`
1272+
- :class:`FixitRemoveRuleSuffix`
12721273

12731274
.. class:: FixitDeprecatedImport
12741275

@@ -1307,4 +1308,34 @@ Built-in Rules
13071308
# suggested fix
13081309
from fixit import LintRule
13091310
1311+
.. class:: FixitRemoveRuleSuffix
1312+
1313+
Remove the "Rule" suffix from lint rule class names
1314+
1315+
.. attribute:: MESSAGE
1316+
1317+
Do not end lint rule subclasses with 'Rule'
1318+
1319+
1320+
.. attribute:: VALID
1321+
1322+
.. code:: python
1323+
1324+
import fixit
1325+
class DontTryThisAtHome(fixit.LintRule): ...
1326+
.. code:: python
1327+
1328+
from fixit import LintRule
1329+
class CatsRuleDogsDrool(LintRule): ...
1330+
1331+
.. attribute:: INVALID
1332+
1333+
.. code:: python
1334+
1335+
import fixit
1336+
class DontTryThisAtHomeRule(fixit.LintRule): ...
1337+
.. code:: python
1338+
1339+
from fixit import LintRule
1340+
class CatsRuleDogsDroolRule(LintRule): ...
13101341
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
#
3+
# This source code is licensed under the MIT license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
import libcst
7+
from libcst.metadata import FullyQualifiedNameProvider
8+
9+
from fixit import Invalid, LintRule, Valid
10+
11+
12+
class FixitRemoveRuleSuffix(LintRule):
13+
"""
14+
Remove the "Rule" suffix from lint rule class names
15+
"""
16+
17+
MESSAGE = "Do not end lint rule subclasses with 'Rule'"
18+
METADATA_DEPENDENCIES = (FullyQualifiedNameProvider,)
19+
20+
VALID = [
21+
Valid(
22+
"""
23+
import fixit
24+
class DontTryThisAtHome(fixit.LintRule): ...
25+
"""
26+
),
27+
Valid(
28+
"""
29+
from fixit import LintRule
30+
class CatsRuleDogsDrool(LintRule): ...
31+
"""
32+
),
33+
Valid(
34+
"""
35+
class NotALintRule: ...
36+
"""
37+
),
38+
]
39+
INVALID = [
40+
Invalid(
41+
"""
42+
import fixit
43+
class DontTryThisAtHomeRule(fixit.LintRule): ...
44+
"""
45+
),
46+
Invalid(
47+
"""
48+
from fixit import LintRule
49+
class CatsRuleDogsDroolRule(LintRule): ...
50+
"""
51+
),
52+
]
53+
54+
def visit_ClassDef(self, node: libcst.ClassDef) -> None:
55+
for base in node.bases:
56+
metadata = self.get_metadata(FullyQualifiedNameProvider, base.value)
57+
if isinstance(metadata, set):
58+
qname = metadata.pop().name
59+
if qname == "fixit.LintRule":
60+
rule_name = node.name.value
61+
if rule_name.endswith("Rule"):
62+
rep = node.name.with_changes(value=rule_name[:-4])
63+
self.report(node.name, replacement=rep)

0 commit comments

Comments
 (0)