code-prune 2.0.0__py3-none-any.whl → 2.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
code_prune/__init__.py CHANGED
@@ -10,7 +10,7 @@ Since: 1.0.0
10
10
  from env_dir_bootstrap import EnvDirBootstrap
11
11
 
12
12
  __app_name__ = "code-prune"
13
- __version__ = "2.0.0"
13
+ __version__ = "2.1.0"
14
14
 
15
15
  _bootstrapper = EnvDirBootstrap(
16
16
  env_var="CODE_PRUNE_CONFIG_DIR",
@@ -1,9 +1,9 @@
1
1
  """Usages hidden inside Gosu string interpolation.
2
2
 
3
3
  Gosu parses ``"${expr}"`` into real identifier nodes, but a simple ``"$name"``
4
- stays plain string text. Those names are real usages, so they are extracted here
5
- and counted when deciding whether a symbol is unused. Single-quoted Gosu strings
6
- are not interpolated and are therefore ignored.
4
+ stays plain string text, and a single-quoted string is one opaque literal
5
+ (``'${expr}'`` is not parsed at all). Those names are real usages, so they are
6
+ extracted here and counted when deciding whether a symbol is unused.
7
7
 
8
8
  Author: Ron Webb
9
9
  Since: 2.0.0
@@ -16,30 +16,65 @@ from collections.abc import Iterable
16
16
 
17
17
  from tree_sitter import Node # type: ignore[import-untyped]
18
18
 
19
- from .language_tables import INTERPOLATED_TEXT_NODE_TYPES
19
+ from .language_tables import (
20
+ INTERPOLATED_LITERAL_NODE_TYPES,
21
+ INTERPOLATED_TEXT_NODE_TYPES,
22
+ )
20
23
 
21
24
  _SIMPLE_INTERPOLATION = re.compile(r"\$([A-Za-z_][A-Za-z0-9_]*)")
25
+ _EXPRESSION_INTERPOLATION = re.compile(r"\$\{([^}]*)\}")
26
+ _IDENTIFIER = re.compile(r"[A-Za-z_][A-Za-z0-9_]*")
27
+ _ESCAPED_DOLLAR = re.compile(r"\\\$")
28
+
29
+
30
+ def _text_names(text: str) -> list[str]:
31
+ """Return the names referenced by ``$name`` / ``${expr}`` in *text*.
32
+
33
+ Author: Ron Webb
34
+ Since: 2.1.0
35
+ """
36
+ text = _ESCAPED_DOLLAR.sub("", text)
37
+ names = _SIMPLE_INTERPOLATION.findall(text)
38
+ for expression in _EXPRESSION_INTERPOLATION.findall(text):
39
+ names.extend(_IDENTIFIER.findall(expression))
40
+ return names
22
41
 
23
42
 
24
43
  def interpolated_names(
25
44
  nodes: Iterable[Node], source_bytes: bytes, language: str
26
45
  ) -> list[str]:
27
- """Return the names referenced as ``$name`` within string text of *nodes*.
46
+ """Return the names referenced through interpolation within *nodes*.
28
47
 
29
- Escaped dollars (``\\$name``) are separate escape-sequence nodes in the
30
- grammar, so they never reach the text checked here.
48
+ Two shapes are inspected: ``string_content`` text of a double-quoted string
49
+ (only ``$name`` there, as ``${expr}`` is already parsed into identifier
50
+ nodes and ``\\$`` is a separate escape-sequence node) and childless
51
+ single-quoted string literals, whose ``$name`` and ``${expr}`` are both raw
52
+ text.
31
53
 
32
54
  Author: Ron Webb
33
55
  Since: 2.0.0
34
56
  """
35
- text_types = INTERPOLATED_TEXT_NODE_TYPES.get(language)
36
- if not text_types:
37
- return []
57
+ text_types = INTERPOLATED_TEXT_NODE_TYPES.get(language, frozenset())
58
+ literal_types = INTERPOLATED_LITERAL_NODE_TYPES.get(language, frozenset())
38
59
  names: list[str] = []
39
60
  for node in nodes:
40
61
  if node.type in text_types:
41
- text = source_bytes[node.start_byte : node.end_byte].decode(
42
- "utf-8", errors="replace"
62
+ names.extend(
63
+ _SIMPLE_INTERPOLATION.findall(_decode_node(node, source_bytes))
43
64
  )
44
- names.extend(_SIMPLE_INTERPOLATION.findall(text))
65
+ elif node.type in literal_types and node.child_count == 0:
66
+ text = _decode_node(node, source_bytes)
67
+ if text.startswith("'"):
68
+ names.extend(_text_names(text))
45
69
  return names
70
+
71
+
72
+ def _decode_node(node: Node, source_bytes: bytes) -> str:
73
+ """Return the source text covered by *node*.
74
+
75
+ Author: Ron Webb
76
+ Since: 2.1.0
77
+ """
78
+ return source_bytes[node.start_byte : node.end_byte].decode(
79
+ "utf-8", errors="replace"
80
+ )
@@ -186,6 +186,12 @@ INTERPOLATED_TEXT_NODE_TYPES: dict[str, frozenset[str]] = {
186
186
  "Gosu": frozenset({"string_content"}),
187
187
  }
188
188
 
189
+ #: Childless literal node types whose raw text may contain interpolation
190
+ #: (Gosu single-quoted strings), per language.
191
+ INTERPOLATED_LITERAL_NODE_TYPES: dict[str, frozenset[str]] = {
192
+ "Gosu": frozenset({"string_literal"}),
193
+ }
194
+
189
195
  #: Identifier-like node types counted as a "usage" of a name.
190
196
  USAGE_NODE_TYPES: dict[str, frozenset[str]] = {
191
197
  "Python": frozenset({"identifier"}),
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: code-prune
3
- Version: 2.0.0
3
+ Version: 2.1.0
4
4
  Summary: Find unreachable, unused, and obsolete code before it becomes technical debt.
5
5
  License: MIT License
6
6
 
@@ -56,7 +56,7 @@ Description-Content-Type: text/markdown
56
56
 
57
57
  # code-prune
58
58
 
59
- [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/rcw3bb/code-prune/blob/main/LICENSE) [![Version](https://img.shields.io/badge/Version-2.0.0-green.svg)](https://github.com/rcw3bb/code-prune/blob/main/CHANGELOG.md) [![Python](https://img.shields.io/badge/Python-3.14%2B-blue)](https://www.python.org/) [![PyPI](https://img.shields.io/badge/PyPI-code--prune-orange)](https://pypi.org/project/code-prune/)
59
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/rcw3bb/code-prune/blob/main/LICENSE) [![Version](https://img.shields.io/badge/Version-2.1.0-green.svg)](https://github.com/rcw3bb/code-prune/blob/main/CHANGELOG.md) [![Python](https://img.shields.io/badge/Python-3.14%2B-blue)](https://www.python.org/) [![PyPI](https://img.shields.io/badge/PyPI-code--prune-orange)](https://pypi.org/project/code-prune/)
60
60
 
61
61
 
62
62
  A CLI tool that scans a codebase for unreachable, unused, and obsolete code,
@@ -174,8 +174,8 @@ Overriding members are also never reported as unused symbols: Java `@Override`,
174
174
  Gosu `override` / `@Override`, TypeScript `override`, and Python `@override` /
175
175
  `@typing.override`.
176
176
 
177
- Gosu `$name` and `${expr}` string interpolation in double-quoted strings counts
178
- as a usage of `name` (single-quoted Gosu strings are not interpolated).
177
+ Gosu `$name` and `${expr}` string interpolation counts as a usage of `name` in
178
+ both double- and single-quoted strings.
179
179
 
180
180
  ### Suppression annotations
181
181
 
@@ -1,11 +1,11 @@
1
1
  code_prune/.codepruneignore,sha256=9mRAQiaYZdmlMXazShKy5pjbWEfMfFOTUaHOVC8irJI,218
2
- code_prune/__init__.py,sha256=Cx0OLkONDjyjNEqAx4dPncTm-IojxiondAuky8ZLL5g,765
2
+ code_prune/__init__.py,sha256=zYQ7qhV5JIY7_ssl4gQxdPxWfsjdhBYk3VFQ9ojg4Co,765
3
3
  code_prune/annotation_config.py,sha256=k4-4h3bhzpHptLOy2_UF494dUP3IU1VoMHrBYo8jEew,9105
4
4
  code_prune/cli.py,sha256=d8vxxvspLoT673lfkeBiFOUz1g0lRM0__4cGM0OLiyQ,20348
5
5
  code_prune/config.ini,sha256=URW6VcIlLXCuV5MHBIh32ANoLEIRLSdPszWtIxao5Qs,42
6
6
  code_prune/config.py,sha256=DcNIfQrGNQK2D7F7aoS5nLN3hJsud0BSVzorHJSBieg,1105
7
- code_prune/interpolation.py,sha256=7ssuq9DJ69_XyyqaZkNuAn65HUFagqj7C83CtVWD8s4,1405
8
- code_prune/language_tables.py,sha256=zkCH12qAVDVdr4wTjj36eFrJVoB2dL0lDBEEav8ff4A,7482
7
+ code_prune/interpolation.py,sha256=Rgx_ySAQNhqMGrIDkdSHZNrohTJPpxKBURwQ-zdQFws,2621
8
+ code_prune/language_tables.py,sha256=4laPzUaVF1Ypvw6z4561Bl9sQJvEggdOav9_TiYT2f8,7711
9
9
  code_prune/logging.ini,sha256=dyYxzO4aWrO7zs9C17kBDcYuezdYtM_B5oDrxZzi8Ts,574
10
10
  code_prune/parser.py,sha256=fnMQK7_xY8_uCDL75ZDbmZ6MgFV3sfF73gwnNzZWj50,2219
11
11
  code_prune/reporter.py,sha256=NHCCV8gKjyVWET3r-TH9tT6aVsfhprOyOdl_Yo-GWmc,13355
@@ -15,8 +15,8 @@ code_prune/store.py,sha256=Wnfcryvjl7jt4xeyW7Tys0JnqIvd43D0OAbrvm0u1vw,11731
15
15
  code_prune/suppression.py,sha256=TIZqjTxGydioG6kMBSAO7tvlgX3JamAFaHKA_9ivk6A,8216
16
16
  code_prune/suppression_annotations.json,sha256=WvO5PCWgfhWTKZfvwQTZsBaoK9oD-mUawB2L7KW9cxc,294
17
17
  code_prune/symbol_ignore,sha256=VT4MRMVfjvN-7N97g6Sx4tmqk1Hf4ZSeA06abE2wLak,402
18
- code_prune-2.0.0.dist-info/METADATA,sha256=N6ZkwHEQOIoI5SWsBzNTBMkZzdM4WYSpPZUtmBEhuYg,11534
19
- code_prune-2.0.0.dist-info/WHEEL,sha256=M5asmiAlL6HEcOq52Yi5mmk9KmTVjY2RDPtO4p9DMrc,88
20
- code_prune-2.0.0.dist-info/entry_points.txt,sha256=iyShO9vHCgkDexXC9rYhzNlERG6xRZNXNXg2lOWIZE0,50
21
- code_prune-2.0.0.dist-info/licenses/LICENSE,sha256=Fs1jiRzMbn9F-lRZC-Z219JKSDzhFFCNibLKwGkRtCY,1065
22
- code_prune-2.0.0.dist-info/RECORD,,
18
+ code_prune-2.1.0.dist-info/METADATA,sha256=SXlW6qXzbSqpUNvDH9U8_7XseIj1fwILEAYQmpE6m08,11501
19
+ code_prune-2.1.0.dist-info/WHEEL,sha256=M5asmiAlL6HEcOq52Yi5mmk9KmTVjY2RDPtO4p9DMrc,88
20
+ code_prune-2.1.0.dist-info/entry_points.txt,sha256=iyShO9vHCgkDexXC9rYhzNlERG6xRZNXNXg2lOWIZE0,50
21
+ code_prune-2.1.0.dist-info/licenses/LICENSE,sha256=Fs1jiRzMbn9F-lRZC-Z219JKSDzhFFCNibLKwGkRtCY,1065
22
+ code_prune-2.1.0.dist-info/RECORD,,