code-prune 1.2.0__tar.gz → 1.3.0__tar.gz
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-1.2.0 → code_prune-1.3.0}/CHANGELOG.md +21 -0
- {code_prune-1.2.0 → code_prune-1.3.0}/PKG-INFO +10 -7
- {code_prune-1.2.0 → code_prune-1.3.0}/README.md +9 -6
- {code_prune-1.2.0 → code_prune-1.3.0}/code_prune/__init__.py +1 -1
- {code_prune-1.2.0 → code_prune-1.3.0}/code_prune/cli.py +1 -1
- code_prune-1.3.0/code_prune/language_tables.py +170 -0
- {code_prune-1.2.0 → code_prune-1.3.0}/code_prune/logging.ini +2 -2
- {code_prune-1.2.0 → code_prune-1.3.0}/code_prune/scanner.py +255 -190
- {code_prune-1.2.0 → code_prune-1.3.0}/code_prune/searcher.py +6 -12
- {code_prune-1.2.0 → code_prune-1.3.0}/code_prune/store.py +2 -1
- {code_prune-1.2.0 → code_prune-1.3.0}/code_prune/symbol_ignore +1 -0
- {code_prune-1.2.0 → code_prune-1.3.0}/pyproject.toml +1 -1
- {code_prune-1.2.0 → code_prune-1.3.0}/LICENSE +0 -0
- {code_prune-1.2.0 → code_prune-1.3.0}/code_prune/.codepruneignore +0 -0
- {code_prune-1.2.0 → code_prune-1.3.0}/code_prune/config.ini +0 -0
- {code_prune-1.2.0 → code_prune-1.3.0}/code_prune/config.py +0 -0
- {code_prune-1.2.0 → code_prune-1.3.0}/code_prune/parser.py +0 -0
- {code_prune-1.2.0 → code_prune-1.3.0}/code_prune/reporter.py +0 -0
|
@@ -1,5 +1,26 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 1.3.0 - 2026-11-16
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- `unused-symbol` now also reports unused fields/attributes (kind `field`):
|
|
8
|
+
class-level fields in Python, JavaScript (including `#private`), TypeScript
|
|
9
|
+
(including interface properties and constructor parameter properties), Java
|
|
10
|
+
(including enum constants) and Gosu (`var` fields without an `as` alias), plus
|
|
11
|
+
Python `self.x = ...` attributes assigned in methods.
|
|
12
|
+
- `--search-path` applies to fields with the same rule as methods: the field
|
|
13
|
+
name and its enclosing class's name must both be found in the same file.
|
|
14
|
+
- `serialVersionUID` is ignored by the default `symbol_ignore`.
|
|
15
|
+
|
|
16
|
+
### Changed
|
|
17
|
+
|
|
18
|
+
- Private JavaScript/TypeScript property usages (`this.#x`) are now counted as
|
|
19
|
+
identifier usages.
|
|
20
|
+
- `code_prune.log` now uses a `RotatingFileHandler` (10 MB per file, 10 backups)
|
|
21
|
+
with UTF-8 encoding, instead of an unbounded, platform-default-encoded
|
|
22
|
+
`FileHandler`.
|
|
23
|
+
|
|
3
24
|
## 1.2.0 - 2026-11-15
|
|
4
25
|
|
|
5
26
|
### Added
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: code-prune
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.3.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
|
-
[](https://github.com/rcw3bb/code-prune/blob/main/LICENSE) [](https://github.com/rcw3bb/code-prune/blob/main/LICENSE) [](https://github.com/rcw3bb/code-prune/blob/main/CHANGELOG.md) [](https://www.python.org/) [](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,
|
|
@@ -73,8 +73,11 @@ TypeScript, Java, and Gosu**:
|
|
|
73
73
|
(`False`/`false`/`0`). Comments are never reported as unreachable.
|
|
74
74
|
- **Unused imports & local variables** — per-file, syntactic detection of
|
|
75
75
|
bindings that are never referenced again in the same file.
|
|
76
|
-
- **Unused functions/classes/methods** — cross-file detection of
|
|
77
|
-
that are never referenced anywhere in the scanned project.
|
|
76
|
+
- **Unused functions/classes/methods/fields** — cross-file detection of
|
|
77
|
+
definitions that are never referenced anywhere in the scanned project. Fields
|
|
78
|
+
cover class-level fields in every language, Python `self.x` attributes,
|
|
79
|
+
JavaScript `#private` fields, TypeScript interface properties and constructor
|
|
80
|
+
parameter properties, and Java enum constants.
|
|
78
81
|
- **Obsolete markers** — `@deprecated`/`@Deprecated`-style decorators and
|
|
79
82
|
annotations, plus comments mentioning `deprecated`.
|
|
80
83
|
- **TODO markers** — `TODO`/`FIXME`/`XXX` comment tags (not necessarily
|
|
@@ -120,7 +123,7 @@ Common options:
|
|
|
120
123
|
| `--workers` | Number of parallel worker threads used to scan files. |
|
|
121
124
|
| `--db` | Path to the SQLite scratch database used during a scan run. |
|
|
122
125
|
| `--target-list` | Treat `path` as a file listing target paths, one per line. |
|
|
123
|
-
| `--search-path` | Files and/or directories (separated by `;`) searched textually for each `unused-symbol` candidate; a symbol found there is not reported. Classes
|
|
126
|
+
| `--search-path` | Files and/or directories (separated by `;`) searched textually for each `unused-symbol` candidate; a symbol found there is not reported. Classes, functions/methods and fields are all matched by a whole-word regex; a method or field additionally requires its enclosing class's name to appear in the same file. The symbol's own file is skipped and `.codepruneignore` rules apply. |
|
|
124
127
|
| `--search-ext` | Comma-separated extensions of the files searched in `--search-path` (e.g. `xml,json`). Omit to search every text file. Requires `--search-path`. |
|
|
125
128
|
|
|
126
129
|
Example:
|
|
@@ -161,8 +164,8 @@ It always uses that fixed name and is not affected by `config.ini`.
|
|
|
161
164
|
### Ignoring symbol names
|
|
162
165
|
|
|
163
166
|
`symbol_ignore` holds one regex pattern per line, matched against symbol
|
|
164
|
-
names to suppress from "unused function/class/method" findings — useful for
|
|
165
|
-
dunder methods, `main`, test hooks (`test_*`, `setUp`), and similar
|
|
167
|
+
names to suppress from "unused function/class/method/field" findings — useful for
|
|
168
|
+
dunder methods, `main`, test hooks (`test_*`, `setUp`), `serialVersionUID`, and similar
|
|
166
169
|
intentionally-unreferenced symbols.
|
|
167
170
|
|
|
168
171
|
## Development
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# code-prune
|
|
2
2
|
|
|
3
|
-
[](https://github.com/rcw3bb/code-prune/blob/main/LICENSE) [](https://github.com/rcw3bb/code-prune/blob/main/LICENSE) [](https://github.com/rcw3bb/code-prune/blob/main/CHANGELOG.md) [](https://www.python.org/) [](https://pypi.org/project/code-prune/)
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
A CLI tool that scans a codebase for unreachable, unused, and obsolete code,
|
|
@@ -17,8 +17,11 @@ TypeScript, Java, and Gosu**:
|
|
|
17
17
|
(`False`/`false`/`0`). Comments are never reported as unreachable.
|
|
18
18
|
- **Unused imports & local variables** — per-file, syntactic detection of
|
|
19
19
|
bindings that are never referenced again in the same file.
|
|
20
|
-
- **Unused functions/classes/methods** — cross-file detection of
|
|
21
|
-
that are never referenced anywhere in the scanned project.
|
|
20
|
+
- **Unused functions/classes/methods/fields** — cross-file detection of
|
|
21
|
+
definitions that are never referenced anywhere in the scanned project. Fields
|
|
22
|
+
cover class-level fields in every language, Python `self.x` attributes,
|
|
23
|
+
JavaScript `#private` fields, TypeScript interface properties and constructor
|
|
24
|
+
parameter properties, and Java enum constants.
|
|
22
25
|
- **Obsolete markers** — `@deprecated`/`@Deprecated`-style decorators and
|
|
23
26
|
annotations, plus comments mentioning `deprecated`.
|
|
24
27
|
- **TODO markers** — `TODO`/`FIXME`/`XXX` comment tags (not necessarily
|
|
@@ -64,7 +67,7 @@ Common options:
|
|
|
64
67
|
| `--workers` | Number of parallel worker threads used to scan files. |
|
|
65
68
|
| `--db` | Path to the SQLite scratch database used during a scan run. |
|
|
66
69
|
| `--target-list` | Treat `path` as a file listing target paths, one per line. |
|
|
67
|
-
| `--search-path` | Files and/or directories (separated by `;`) searched textually for each `unused-symbol` candidate; a symbol found there is not reported. Classes
|
|
70
|
+
| `--search-path` | Files and/or directories (separated by `;`) searched textually for each `unused-symbol` candidate; a symbol found there is not reported. Classes, functions/methods and fields are all matched by a whole-word regex; a method or field additionally requires its enclosing class's name to appear in the same file. The symbol's own file is skipped and `.codepruneignore` rules apply. |
|
|
68
71
|
| `--search-ext` | Comma-separated extensions of the files searched in `--search-path` (e.g. `xml,json`). Omit to search every text file. Requires `--search-path`. |
|
|
69
72
|
|
|
70
73
|
Example:
|
|
@@ -105,8 +108,8 @@ It always uses that fixed name and is not affected by `config.ini`.
|
|
|
105
108
|
### Ignoring symbol names
|
|
106
109
|
|
|
107
110
|
`symbol_ignore` holds one regex pattern per line, matched against symbol
|
|
108
|
-
names to suppress from "unused function/class/method" findings — useful for
|
|
109
|
-
dunder methods, `main`, test hooks (`test_*`, `setUp`), and similar
|
|
111
|
+
names to suppress from "unused function/class/method/field" findings — useful for
|
|
112
|
+
dunder methods, `main`, test hooks (`test_*`, `setUp`), `serialVersionUID`, and similar
|
|
110
113
|
intentionally-unreferenced symbols.
|
|
111
114
|
|
|
112
115
|
## Development
|
|
@@ -34,12 +34,12 @@ from . import CONF_DIR
|
|
|
34
34
|
from . import PATH_IGNORE_PATH
|
|
35
35
|
from . import SYMBOL_IGNORE_PATH
|
|
36
36
|
from .config import Config
|
|
37
|
+
from .language_tables import SUPPORTED_EXTENSIONS
|
|
37
38
|
from .reporter import write_outputs
|
|
38
39
|
from .scanner import (
|
|
39
40
|
CATEGORY_UNUSED_ALIAS,
|
|
40
41
|
CATEGORY_UNUSED_SYMBOL,
|
|
41
42
|
CLI_CATEGORIES,
|
|
42
|
-
SUPPORTED_EXTENSIONS,
|
|
43
43
|
UNUSED_CATEGORIES,
|
|
44
44
|
load_name_ignore_patterns,
|
|
45
45
|
scan_file,
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
"""Per-language tree-sitter node-type tables used by the scanner.
|
|
2
|
+
|
|
3
|
+
The node types were verified empirically against each tree-sitter grammar
|
|
4
|
+
before being hard-coded here.
|
|
5
|
+
|
|
6
|
+
Author: Ron Webb
|
|
7
|
+
Since: 1.3.0
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
EXTENSION_TO_LANGUAGE: dict[str, str] = {
|
|
13
|
+
".py": "Python",
|
|
14
|
+
".pyi": "Python",
|
|
15
|
+
".js": "JavaScript",
|
|
16
|
+
".mjs": "JavaScript",
|
|
17
|
+
".cjs": "JavaScript",
|
|
18
|
+
".ts": "TypeScript",
|
|
19
|
+
".tsx": "TypeScript",
|
|
20
|
+
".java": "Java",
|
|
21
|
+
".gs": "Gosu",
|
|
22
|
+
".gsx": "Gosu",
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
#: File extensions the scanner can parse.
|
|
26
|
+
SUPPORTED_EXTENSIONS = frozenset(EXTENSION_TO_LANGUAGE)
|
|
27
|
+
|
|
28
|
+
#: Function/method definition node types, per language.
|
|
29
|
+
FUNCTION_NODE_TYPES: dict[str, frozenset[str]] = {
|
|
30
|
+
"Python": frozenset({"function_definition"}),
|
|
31
|
+
"JavaScript": frozenset(
|
|
32
|
+
{
|
|
33
|
+
"function_declaration",
|
|
34
|
+
"function_expression",
|
|
35
|
+
"arrow_function",
|
|
36
|
+
"method_definition",
|
|
37
|
+
}
|
|
38
|
+
),
|
|
39
|
+
"TypeScript": frozenset(
|
|
40
|
+
{
|
|
41
|
+
"function_declaration",
|
|
42
|
+
"function_expression",
|
|
43
|
+
"arrow_function",
|
|
44
|
+
"method_definition",
|
|
45
|
+
}
|
|
46
|
+
),
|
|
47
|
+
"Java": frozenset({"method_declaration", "constructor_declaration"}),
|
|
48
|
+
"Gosu": frozenset({"function_declaration", "constructor_declaration"}),
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
#: Class/interface definition node types, per language.
|
|
52
|
+
CLASS_NODE_TYPES: dict[str, frozenset[str]] = {
|
|
53
|
+
"Python": frozenset({"class_definition"}),
|
|
54
|
+
"JavaScript": frozenset({"class_declaration"}),
|
|
55
|
+
"TypeScript": frozenset({"class_declaration", "interface_declaration"}),
|
|
56
|
+
"Java": frozenset(
|
|
57
|
+
{"class_declaration", "interface_declaration", "enum_declaration"}
|
|
58
|
+
),
|
|
59
|
+
"Gosu": frozenset({"class_declaration"}),
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
#: Field/attribute declaration node types, per language. Python class-level
|
|
63
|
+
#: fields are plain ``assignment`` nodes; ``self.x = ...`` attributes are found
|
|
64
|
+
#: separately inside methods.
|
|
65
|
+
FIELD_NODE_TYPES: dict[str, frozenset[str]] = {
|
|
66
|
+
"Python": frozenset({"assignment"}),
|
|
67
|
+
"JavaScript": frozenset({"field_definition"}),
|
|
68
|
+
"TypeScript": frozenset({"public_field_definition", "property_signature"}),
|
|
69
|
+
"Java": frozenset({"field_declaration", "enum_constant"}),
|
|
70
|
+
"Gosu": frozenset({"field_declaration"}),
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
#: Grammar field holding the declared name of a field node (default ``name``).
|
|
74
|
+
FIELD_NAME_FIELD: dict[str, str] = {"Python": "left", "JavaScript": "property"}
|
|
75
|
+
|
|
76
|
+
#: Node types a field name may have.
|
|
77
|
+
FIELD_NAME_NODE_TYPES = frozenset(
|
|
78
|
+
{"identifier", "property_identifier", "private_property_identifier"}
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
#: Modifiers that turn a TypeScript constructor parameter into a field.
|
|
82
|
+
PARAMETER_PROPERTY_MODIFIERS = frozenset({"accessibility_modifier", "readonly"})
|
|
83
|
+
|
|
84
|
+
#: Import/uses-clause node types, per language.
|
|
85
|
+
IMPORT_NODE_TYPES: dict[str, frozenset[str]] = {
|
|
86
|
+
"Python": frozenset({"import_statement", "import_from_statement"}),
|
|
87
|
+
"JavaScript": frozenset({"import_statement"}),
|
|
88
|
+
"TypeScript": frozenset({"import_statement"}),
|
|
89
|
+
"Java": frozenset({"import_declaration"}),
|
|
90
|
+
"Gosu": frozenset({"uses_declaration"}),
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
#: Local-variable declaration/assignment node types, per language.
|
|
94
|
+
LOCAL_DECL_NODE_TYPES: dict[str, frozenset[str]] = {
|
|
95
|
+
"Python": frozenset({"assignment"}),
|
|
96
|
+
"JavaScript": frozenset({"variable_declarator"}),
|
|
97
|
+
"TypeScript": frozenset({"variable_declarator"}),
|
|
98
|
+
"Java": frozenset({"variable_declarator"}),
|
|
99
|
+
"Gosu": frozenset({"local_variable_declaration"}),
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
#: Statements that terminate control flow within a block, per language.
|
|
103
|
+
TERMINATING_STATEMENT_TYPES: dict[str, frozenset[str]] = {
|
|
104
|
+
"Python": frozenset(
|
|
105
|
+
{"return_statement", "raise_statement", "break_statement", "continue_statement"}
|
|
106
|
+
),
|
|
107
|
+
"JavaScript": frozenset(
|
|
108
|
+
{"return_statement", "throw_statement", "break_statement", "continue_statement"}
|
|
109
|
+
),
|
|
110
|
+
"TypeScript": frozenset(
|
|
111
|
+
{"return_statement", "throw_statement", "break_statement", "continue_statement"}
|
|
112
|
+
),
|
|
113
|
+
"Java": frozenset(
|
|
114
|
+
{"return_statement", "throw_statement", "break_statement", "continue_statement"}
|
|
115
|
+
),
|
|
116
|
+
"Gosu": frozenset(
|
|
117
|
+
{"return_statement", "throw_statement", "break_statement", "continue_statement"}
|
|
118
|
+
),
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
#: Node types whose direct named children form a sequential statement list.
|
|
122
|
+
BLOCK_NODE_TYPES: dict[str, frozenset[str]] = {
|
|
123
|
+
"Python": frozenset({"block"}),
|
|
124
|
+
"JavaScript": frozenset({"statement_block"}),
|
|
125
|
+
"TypeScript": frozenset({"statement_block"}),
|
|
126
|
+
"Java": frozenset({"block"}),
|
|
127
|
+
"Gosu": frozenset({"statement_block"}),
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
#: Comment node types, per language.
|
|
131
|
+
COMMENT_NODE_TYPES: dict[str, frozenset[str]] = {
|
|
132
|
+
"Python": frozenset({"comment"}),
|
|
133
|
+
"JavaScript": frozenset({"comment"}),
|
|
134
|
+
"TypeScript": frozenset({"comment"}),
|
|
135
|
+
"Java": frozenset({"line_comment", "block_comment"}),
|
|
136
|
+
"Gosu": frozenset({"line_comment", "block_comment"}),
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
#: Decorator/annotation node types consulted for deprecation markers.
|
|
140
|
+
DECORATOR_NODE_TYPES: dict[str, frozenset[str]] = {
|
|
141
|
+
"Python": frozenset({"decorator"}),
|
|
142
|
+
"JavaScript": frozenset(),
|
|
143
|
+
"TypeScript": frozenset({"decorator"}),
|
|
144
|
+
"Java": frozenset({"marker_annotation", "annotation"}),
|
|
145
|
+
"Gosu": frozenset({"annotation"}),
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
#: Identifier-like node types counted as a "usage" of a name.
|
|
149
|
+
USAGE_NODE_TYPES: dict[str, frozenset[str]] = {
|
|
150
|
+
"Python": frozenset({"identifier"}),
|
|
151
|
+
"JavaScript": frozenset(
|
|
152
|
+
{
|
|
153
|
+
"identifier",
|
|
154
|
+
"property_identifier",
|
|
155
|
+
"private_property_identifier",
|
|
156
|
+
"shorthand_property_identifier",
|
|
157
|
+
}
|
|
158
|
+
),
|
|
159
|
+
"TypeScript": frozenset(
|
|
160
|
+
{
|
|
161
|
+
"identifier",
|
|
162
|
+
"property_identifier",
|
|
163
|
+
"private_property_identifier",
|
|
164
|
+
"shorthand_property_identifier",
|
|
165
|
+
"type_identifier",
|
|
166
|
+
}
|
|
167
|
+
),
|
|
168
|
+
"Java": frozenset({"identifier", "type_identifier"}),
|
|
169
|
+
"Gosu": frozenset({"identifier"}),
|
|
170
|
+
}
|
|
@@ -17,9 +17,9 @@ formatter=consoleFormatter
|
|
|
17
17
|
args=(sys.stderr,)
|
|
18
18
|
|
|
19
19
|
[handler_fileHandler]
|
|
20
|
-
class=
|
|
20
|
+
class=handlers.RotatingFileHandler
|
|
21
21
|
formatter=logFormatter
|
|
22
|
-
args=('code_prune.log', 'a')
|
|
22
|
+
args=('code_prune.log', 'a', 10485760, 10, 'utf-8')
|
|
23
23
|
|
|
24
24
|
[formatter_logFormatter]
|
|
25
25
|
format=%(asctime)s [%(levelname)s] %(name)s - %(message)s
|
|
@@ -15,11 +15,27 @@ import logging
|
|
|
15
15
|
import re
|
|
16
16
|
from collections import Counter
|
|
17
17
|
from collections.abc import Generator, Iterable
|
|
18
|
-
from dataclasses import dataclass, field
|
|
18
|
+
from dataclasses import dataclass, field, replace
|
|
19
19
|
from pathlib import Path
|
|
20
20
|
|
|
21
21
|
from tree_sitter import Node, Tree # type: ignore[import-untyped]
|
|
22
22
|
|
|
23
|
+
from .language_tables import (
|
|
24
|
+
BLOCK_NODE_TYPES,
|
|
25
|
+
CLASS_NODE_TYPES,
|
|
26
|
+
COMMENT_NODE_TYPES,
|
|
27
|
+
DECORATOR_NODE_TYPES,
|
|
28
|
+
EXTENSION_TO_LANGUAGE,
|
|
29
|
+
FIELD_NAME_FIELD,
|
|
30
|
+
FIELD_NAME_NODE_TYPES,
|
|
31
|
+
FIELD_NODE_TYPES,
|
|
32
|
+
FUNCTION_NODE_TYPES,
|
|
33
|
+
IMPORT_NODE_TYPES,
|
|
34
|
+
LOCAL_DECL_NODE_TYPES,
|
|
35
|
+
PARAMETER_PROPERTY_MODIFIERS,
|
|
36
|
+
TERMINATING_STATEMENT_TYPES,
|
|
37
|
+
USAGE_NODE_TYPES,
|
|
38
|
+
)
|
|
23
39
|
from .parser import parse
|
|
24
40
|
|
|
25
41
|
_logger = logging.getLogger(__name__)
|
|
@@ -34,6 +50,7 @@ CATEGORY_UNUSED_LOCAL = "unused-local"
|
|
|
34
50
|
CATEGORY_UNUSED_SYMBOL = "unused-symbol"
|
|
35
51
|
SYMBOL_KIND_CLASS = "class"
|
|
36
52
|
SYMBOL_KIND_FUNCTION = "function"
|
|
53
|
+
SYMBOL_KIND_FIELD = "field"
|
|
37
54
|
CATEGORY_OBSOLETE = "obsolete"
|
|
38
55
|
CATEGORY_TODO_MARKER = "todo-marker"
|
|
39
56
|
CATEGORY_COMMENTED_OUT = "commented-out"
|
|
@@ -68,143 +85,6 @@ CLI_CATEGORIES = (
|
|
|
68
85
|
CATEGORY_COMMENTED_OUT,
|
|
69
86
|
)
|
|
70
87
|
|
|
71
|
-
# --------------------------------------------------------------------------
|
|
72
|
-
# Per-language grammar node-type tables (verified empirically against each
|
|
73
|
-
# tree-sitter grammar before being hard-coded here).
|
|
74
|
-
# --------------------------------------------------------------------------
|
|
75
|
-
|
|
76
|
-
EXTENSION_TO_LANGUAGE: dict[str, str] = {
|
|
77
|
-
".py": "Python",
|
|
78
|
-
".pyi": "Python",
|
|
79
|
-
".js": "JavaScript",
|
|
80
|
-
".mjs": "JavaScript",
|
|
81
|
-
".cjs": "JavaScript",
|
|
82
|
-
".ts": "TypeScript",
|
|
83
|
-
".tsx": "TypeScript",
|
|
84
|
-
".java": "Java",
|
|
85
|
-
".gs": "Gosu",
|
|
86
|
-
".gsx": "Gosu",
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
#: File extensions the scanner can parse.
|
|
90
|
-
SUPPORTED_EXTENSIONS = frozenset(EXTENSION_TO_LANGUAGE)
|
|
91
|
-
|
|
92
|
-
#: Function/method definition node types, per language.
|
|
93
|
-
FUNCTION_NODE_TYPES: dict[str, frozenset[str]] = {
|
|
94
|
-
"Python": frozenset({"function_definition"}),
|
|
95
|
-
"JavaScript": frozenset(
|
|
96
|
-
{
|
|
97
|
-
"function_declaration",
|
|
98
|
-
"function_expression",
|
|
99
|
-
"arrow_function",
|
|
100
|
-
"method_definition",
|
|
101
|
-
}
|
|
102
|
-
),
|
|
103
|
-
"TypeScript": frozenset(
|
|
104
|
-
{
|
|
105
|
-
"function_declaration",
|
|
106
|
-
"function_expression",
|
|
107
|
-
"arrow_function",
|
|
108
|
-
"method_definition",
|
|
109
|
-
}
|
|
110
|
-
),
|
|
111
|
-
"Java": frozenset({"method_declaration", "constructor_declaration"}),
|
|
112
|
-
"Gosu": frozenset({"function_declaration", "constructor_declaration"}),
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
#: Class/interface definition node types, per language.
|
|
116
|
-
CLASS_NODE_TYPES: dict[str, frozenset[str]] = {
|
|
117
|
-
"Python": frozenset({"class_definition"}),
|
|
118
|
-
"JavaScript": frozenset({"class_declaration"}),
|
|
119
|
-
"TypeScript": frozenset({"class_declaration", "interface_declaration"}),
|
|
120
|
-
"Java": frozenset(
|
|
121
|
-
{"class_declaration", "interface_declaration", "enum_declaration"}
|
|
122
|
-
),
|
|
123
|
-
"Gosu": frozenset({"class_declaration"}),
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
#: Import/uses-clause node types, per language.
|
|
127
|
-
IMPORT_NODE_TYPES: dict[str, frozenset[str]] = {
|
|
128
|
-
"Python": frozenset({"import_statement", "import_from_statement"}),
|
|
129
|
-
"JavaScript": frozenset({"import_statement"}),
|
|
130
|
-
"TypeScript": frozenset({"import_statement"}),
|
|
131
|
-
"Java": frozenset({"import_declaration"}),
|
|
132
|
-
"Gosu": frozenset({"uses_declaration"}),
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
#: Local-variable declaration/assignment node types, per language.
|
|
136
|
-
LOCAL_DECL_NODE_TYPES: dict[str, frozenset[str]] = {
|
|
137
|
-
"Python": frozenset({"assignment"}),
|
|
138
|
-
"JavaScript": frozenset({"variable_declarator"}),
|
|
139
|
-
"TypeScript": frozenset({"variable_declarator"}),
|
|
140
|
-
"Java": frozenset({"variable_declarator"}),
|
|
141
|
-
"Gosu": frozenset({"local_variable_declaration"}),
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
#: Statements that terminate control flow within a block, per language.
|
|
145
|
-
TERMINATING_STATEMENT_TYPES: dict[str, frozenset[str]] = {
|
|
146
|
-
"Python": frozenset(
|
|
147
|
-
{"return_statement", "raise_statement", "break_statement", "continue_statement"}
|
|
148
|
-
),
|
|
149
|
-
"JavaScript": frozenset(
|
|
150
|
-
{"return_statement", "throw_statement", "break_statement", "continue_statement"}
|
|
151
|
-
),
|
|
152
|
-
"TypeScript": frozenset(
|
|
153
|
-
{"return_statement", "throw_statement", "break_statement", "continue_statement"}
|
|
154
|
-
),
|
|
155
|
-
"Java": frozenset(
|
|
156
|
-
{"return_statement", "throw_statement", "break_statement", "continue_statement"}
|
|
157
|
-
),
|
|
158
|
-
"Gosu": frozenset(
|
|
159
|
-
{"return_statement", "throw_statement", "break_statement", "continue_statement"}
|
|
160
|
-
),
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
#: Node types whose direct named children form a sequential statement list.
|
|
164
|
-
BLOCK_NODE_TYPES: dict[str, frozenset[str]] = {
|
|
165
|
-
"Python": frozenset({"block"}),
|
|
166
|
-
"JavaScript": frozenset({"statement_block"}),
|
|
167
|
-
"TypeScript": frozenset({"statement_block"}),
|
|
168
|
-
"Java": frozenset({"block"}),
|
|
169
|
-
"Gosu": frozenset({"statement_block"}),
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
#: Comment node types, per language.
|
|
173
|
-
COMMENT_NODE_TYPES: dict[str, frozenset[str]] = {
|
|
174
|
-
"Python": frozenset({"comment"}),
|
|
175
|
-
"JavaScript": frozenset({"comment"}),
|
|
176
|
-
"TypeScript": frozenset({"comment"}),
|
|
177
|
-
"Java": frozenset({"line_comment", "block_comment"}),
|
|
178
|
-
"Gosu": frozenset({"line_comment", "block_comment"}),
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
#: Decorator/annotation node types consulted for deprecation markers.
|
|
182
|
-
DECORATOR_NODE_TYPES: dict[str, frozenset[str]] = {
|
|
183
|
-
"Python": frozenset({"decorator"}),
|
|
184
|
-
"JavaScript": frozenset(),
|
|
185
|
-
"TypeScript": frozenset({"decorator"}),
|
|
186
|
-
"Java": frozenset({"marker_annotation", "annotation"}),
|
|
187
|
-
"Gosu": frozenset({"annotation"}),
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
#: Identifier-like node types counted as a "usage" of a name.
|
|
191
|
-
USAGE_NODE_TYPES: dict[str, frozenset[str]] = {
|
|
192
|
-
"Python": frozenset({"identifier"}),
|
|
193
|
-
"JavaScript": frozenset(
|
|
194
|
-
{"identifier", "property_identifier", "shorthand_property_identifier"}
|
|
195
|
-
),
|
|
196
|
-
"TypeScript": frozenset(
|
|
197
|
-
{
|
|
198
|
-
"identifier",
|
|
199
|
-
"property_identifier",
|
|
200
|
-
"shorthand_property_identifier",
|
|
201
|
-
"type_identifier",
|
|
202
|
-
}
|
|
203
|
-
),
|
|
204
|
-
"Java": frozenset({"identifier", "type_identifier"}),
|
|
205
|
-
"Gosu": frozenset({"identifier"}),
|
|
206
|
-
}
|
|
207
|
-
|
|
208
88
|
#: Regex matching a condition expression that is always false (after
|
|
209
89
|
#: optionally stripping surrounding parentheses added by some grammars).
|
|
210
90
|
_FALSY_CONDITION_RE = re.compile(r"^\(*\s*(False|false|0)\s*\)*$")
|
|
@@ -242,11 +122,11 @@ class Finding:
|
|
|
242
122
|
|
|
243
123
|
@dataclass(frozen=True)
|
|
244
124
|
class Definition:
|
|
245
|
-
"""A function/class/method definition discovered in one file.
|
|
125
|
+
"""A function/class/method/field definition discovered in one file.
|
|
246
126
|
|
|
247
|
-
``enclosing_class`` is the name of the class a method is directly
|
|
127
|
+
``enclosing_class`` is the name of the class a method or field is directly
|
|
248
128
|
declared in (``None`` for classes and free functions); it lets search-path
|
|
249
|
-
matching require both the class and
|
|
129
|
+
matching require both the class and member names to appear together.
|
|
250
130
|
|
|
251
131
|
Author: Ron Webb
|
|
252
132
|
Since: 1.0.0
|
|
@@ -506,64 +386,249 @@ def _local_bound_name_node(decl_node: Node, language: str) -> Node | None:
|
|
|
506
386
|
return None
|
|
507
387
|
|
|
508
388
|
|
|
509
|
-
def
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
389
|
+
def _field_name_nodes(node: Node, language: str) -> list[Node]:
|
|
390
|
+
"""Return the name nodes declared by the field *node*.
|
|
391
|
+
|
|
392
|
+
Java ``field_declaration`` may declare several variables. A Gosu field with
|
|
393
|
+
an ``as`` alias is exposed as a property and is therefore never a candidate.
|
|
513
394
|
|
|
514
395
|
Author: Ron Webb
|
|
515
|
-
Since: 1.
|
|
396
|
+
Since: 1.3.0
|
|
516
397
|
"""
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
398
|
+
if language == "Java" and node.type == "field_declaration":
|
|
399
|
+
names = [
|
|
400
|
+
declarator.child_by_field_name("name")
|
|
401
|
+
for declarator in node.children_by_field_name("declarator")
|
|
402
|
+
]
|
|
403
|
+
elif language == "Gosu" and node.child_by_field_name("alias") is not None:
|
|
404
|
+
names = []
|
|
405
|
+
else:
|
|
406
|
+
names = [node.child_by_field_name(FIELD_NAME_FIELD.get(language, "name"))]
|
|
407
|
+
return [n for n in names if n is not None and n.type in FIELD_NAME_NODE_TYPES]
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
def _parameter_property_name_nodes(function_node: Node) -> list[Node]:
|
|
411
|
+
"""Return the names of TypeScript constructor parameter properties.
|
|
412
|
+
|
|
413
|
+
``constructor(private q: number)`` declares the field ``q``.
|
|
414
|
+
|
|
415
|
+
Author: Ron Webb
|
|
416
|
+
Since: 1.3.0
|
|
417
|
+
"""
|
|
418
|
+
parameters = function_node.child_by_field_name("parameters")
|
|
419
|
+
if parameters is None:
|
|
420
|
+
return []
|
|
421
|
+
names: list[Node] = []
|
|
422
|
+
for param in parameters.named_children:
|
|
423
|
+
is_property = any(
|
|
424
|
+
c.type in PARAMETER_PROPERTY_MODIFIERS for c in param.children
|
|
425
|
+
)
|
|
426
|
+
pattern = param.child_by_field_name("pattern")
|
|
427
|
+
if is_property and pattern is not None and pattern.type == "identifier":
|
|
428
|
+
names.append(pattern)
|
|
429
|
+
return names
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
def _self_attribute_name_node(assign_node: Node, source_bytes: bytes) -> Node | None:
|
|
433
|
+
"""Return the ``x`` node of a Python ``self.x = ...`` assignment, if any.
|
|
434
|
+
|
|
435
|
+
Author: Ron Webb
|
|
436
|
+
Since: 1.3.0
|
|
437
|
+
"""
|
|
438
|
+
left = assign_node.child_by_field_name("left")
|
|
439
|
+
if left is None or left.type != "attribute":
|
|
440
|
+
return None
|
|
441
|
+
target = left.child_by_field_name("object")
|
|
442
|
+
if target is None or _decode(source_bytes, target) != "self":
|
|
443
|
+
return None
|
|
444
|
+
return left.child_by_field_name("attribute")
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
@dataclass(frozen=True)
|
|
448
|
+
class _Scope:
|
|
449
|
+
"""Lexical position of the node being visited during the binding walk.
|
|
450
|
+
|
|
451
|
+
``enclosing_class`` is the class a node is a *direct* member of (reset by
|
|
452
|
+
nested functions); ``owner_class`` is the nearest lexically enclosing class
|
|
453
|
+
regardless of functions, used for Python ``self.x`` attributes.
|
|
454
|
+
|
|
455
|
+
Author: Ron Webb
|
|
456
|
+
Since: 1.3.0
|
|
457
|
+
"""
|
|
458
|
+
|
|
459
|
+
inside_function: bool = False
|
|
460
|
+
enclosing_class: str | None = None
|
|
461
|
+
owner_class: str | None = None
|
|
462
|
+
|
|
463
|
+
|
|
464
|
+
class _BindingCollector:
|
|
465
|
+
"""Walk a tree once, collecting import/local/definition bindings.
|
|
466
|
+
|
|
467
|
+
Author: Ron Webb
|
|
468
|
+
Since: 1.3.0
|
|
469
|
+
"""
|
|
470
|
+
|
|
471
|
+
def __init__(self, source_bytes: bytes, language: str, file_path: Path) -> None:
|
|
472
|
+
"""Store the file being walked.
|
|
473
|
+
|
|
474
|
+
Author: Ron Webb
|
|
475
|
+
Since: 1.3.0
|
|
476
|
+
"""
|
|
477
|
+
self._source = source_bytes
|
|
478
|
+
self._language = language
|
|
479
|
+
self._file_path = file_path
|
|
480
|
+
self._seen_fields: set[tuple[str, str]] = set()
|
|
481
|
+
self.bindings = _Bindings()
|
|
482
|
+
|
|
483
|
+
def walk(self, node: Node, scope: _Scope) -> None:
|
|
484
|
+
"""Visit *node* and its descendants, recording bindings.
|
|
485
|
+
|
|
486
|
+
Author: Ron Webb
|
|
487
|
+
Since: 1.3.0
|
|
488
|
+
"""
|
|
489
|
+
language = self._language
|
|
490
|
+
if node.type in IMPORT_NODE_TYPES.get(language, frozenset()):
|
|
491
|
+
self._record_imports(node)
|
|
530
492
|
return
|
|
531
|
-
if
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
493
|
+
if (
|
|
494
|
+
node.type in LOCAL_DECL_NODE_TYPES.get(language, frozenset())
|
|
495
|
+
and scope.inside_function
|
|
496
|
+
):
|
|
497
|
+
self._record_local(node)
|
|
498
|
+
self._record_fields(node, scope)
|
|
499
|
+
child_scope = scope
|
|
500
|
+
if node.type in FUNCTION_NODE_TYPES.get(
|
|
501
|
+
language, frozenset()
|
|
502
|
+
) or node.type in CLASS_NODE_TYPES.get(language, frozenset()):
|
|
503
|
+
child_scope = self._record_definition(node, scope)
|
|
504
|
+
elif node.type in FIELD_NODE_TYPES.get(language, frozenset()):
|
|
505
|
+
child_scope = replace(scope, enclosing_class=None)
|
|
506
|
+
for child in node.children:
|
|
507
|
+
self.walk(child, child_scope)
|
|
508
|
+
|
|
509
|
+
def _record_imports(self, node: Node) -> None:
|
|
510
|
+
"""Record the names bound by the import *node*.
|
|
511
|
+
|
|
512
|
+
Author: Ron Webb
|
|
513
|
+
Since: 1.3.0
|
|
514
|
+
"""
|
|
515
|
+
for name_node in _import_bound_name_nodes(node, self._language):
|
|
516
|
+
self.bindings.import_bindings.append(
|
|
517
|
+
(name_node, _decode(self._source, name_node))
|
|
518
|
+
)
|
|
519
|
+
self.bindings.excluded_positions.add(name_node.start_byte)
|
|
520
|
+
|
|
521
|
+
def _record_local(self, node: Node) -> None:
|
|
522
|
+
"""Record the local variable bound by the declaration *node*.
|
|
523
|
+
|
|
524
|
+
Author: Ron Webb
|
|
525
|
+
Since: 1.3.0
|
|
526
|
+
"""
|
|
527
|
+
name_node = _local_bound_name_node(node, self._language)
|
|
528
|
+
if name_node is not None:
|
|
529
|
+
self.bindings.local_bindings.append(
|
|
530
|
+
(name_node, _decode(self._source, name_node))
|
|
531
|
+
)
|
|
532
|
+
self.bindings.excluded_positions.add(name_node.start_byte)
|
|
533
|
+
|
|
534
|
+
def _record_definition(self, node: Node, scope: _Scope) -> _Scope:
|
|
535
|
+
"""Record a function/class definition and return the scope of its body.
|
|
536
|
+
|
|
537
|
+
A method's body is local scope: nested definitions are not themselves
|
|
538
|
+
direct members of the enclosing class.
|
|
539
|
+
|
|
540
|
+
Author: Ron Webb
|
|
541
|
+
Since: 1.3.0
|
|
542
|
+
"""
|
|
543
|
+
is_class = node.type in CLASS_NODE_TYPES.get(self._language, frozenset())
|
|
544
|
+
name_node = node.child_by_field_name("name")
|
|
545
|
+
class_name: str | None = None
|
|
546
|
+
if name_node is not None:
|
|
547
|
+
decoded_name = _decode(self._source, name_node)
|
|
548
|
+
class_name = decoded_name if is_class else None
|
|
549
|
+
self.bindings.definitions.append(
|
|
550
|
+
Definition(
|
|
551
|
+
name=decoded_name,
|
|
552
|
+
kind=SYMBOL_KIND_CLASS if is_class else SYMBOL_KIND_FUNCTION,
|
|
553
|
+
file_path=self._file_path,
|
|
554
|
+
line=name_node.start_point[0] + 1,
|
|
555
|
+
column=name_node.start_point[1],
|
|
556
|
+
enclosing_class=None if is_class else scope.enclosing_class,
|
|
536
557
|
)
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
558
|
+
)
|
|
559
|
+
self.bindings.excluded_positions.add(name_node.start_byte)
|
|
560
|
+
return _Scope(
|
|
561
|
+
inside_function=scope.inside_function or not is_class,
|
|
562
|
+
enclosing_class=class_name,
|
|
563
|
+
owner_class=class_name if is_class and class_name else scope.owner_class,
|
|
564
|
+
)
|
|
565
|
+
|
|
566
|
+
def _record_fields(self, node: Node, scope: _Scope) -> None:
|
|
567
|
+
"""Record any field/attribute declared by *node*.
|
|
568
|
+
|
|
569
|
+
Author: Ron Webb
|
|
570
|
+
Since: 1.3.0
|
|
571
|
+
"""
|
|
572
|
+
language = self._language
|
|
573
|
+
if scope.enclosing_class is not None:
|
|
574
|
+
if node.type in FIELD_NODE_TYPES.get(language, frozenset()):
|
|
575
|
+
self._add_fields(
|
|
576
|
+
_field_name_nodes(node, language), scope.enclosing_class
|
|
577
|
+
)
|
|
578
|
+
elif language == "TypeScript" and node.type in FUNCTION_NODE_TYPES.get(
|
|
579
|
+
language, frozenset()
|
|
580
|
+
):
|
|
581
|
+
self._add_fields(
|
|
582
|
+
_parameter_property_name_nodes(node), scope.enclosing_class
|
|
583
|
+
)
|
|
584
|
+
if (
|
|
585
|
+
language == "Python"
|
|
586
|
+
and node.type == "assignment"
|
|
587
|
+
and scope.inside_function
|
|
588
|
+
and scope.owner_class is not None
|
|
589
|
+
):
|
|
590
|
+
name_node = _self_attribute_name_node(node, self._source)
|
|
543
591
|
if name_node is not None:
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
592
|
+
self._add_fields([name_node], scope.owner_class)
|
|
593
|
+
|
|
594
|
+
def _add_fields(self, name_nodes: list[Node], class_name: str) -> None:
|
|
595
|
+
"""Record each of *name_nodes* as a field of *class_name*.
|
|
596
|
+
|
|
597
|
+
Every declaration position is excluded from usages; only the first
|
|
598
|
+
declaration of a field yields a :class:`Definition`.
|
|
599
|
+
|
|
600
|
+
Author: Ron Webb
|
|
601
|
+
Since: 1.3.0
|
|
602
|
+
"""
|
|
603
|
+
for name_node in name_nodes:
|
|
604
|
+
self.bindings.excluded_positions.add(name_node.start_byte)
|
|
605
|
+
decoded_name = _decode(self._source, name_node)
|
|
606
|
+
if (class_name, decoded_name) in self._seen_fields:
|
|
607
|
+
continue
|
|
608
|
+
self._seen_fields.add((class_name, decoded_name))
|
|
609
|
+
self.bindings.definitions.append(
|
|
610
|
+
Definition(
|
|
611
|
+
name=decoded_name,
|
|
612
|
+
kind=SYMBOL_KIND_FIELD,
|
|
613
|
+
file_path=self._file_path,
|
|
614
|
+
line=name_node.start_point[0] + 1,
|
|
615
|
+
column=name_node.start_point[1],
|
|
616
|
+
enclosing_class=class_name,
|
|
554
617
|
)
|
|
555
|
-
bindings.excluded_positions.add(name_node.start_byte)
|
|
556
|
-
# A method's body is local scope: nested definitions are not
|
|
557
|
-
# themselves direct members of the enclosing class.
|
|
558
|
-
child_enclosing_class = (
|
|
559
|
-
decoded_name if is_class and name_node is not None else None
|
|
560
618
|
)
|
|
561
|
-
inside_function = inside_function or node.type in function_types
|
|
562
|
-
for child in node.children:
|
|
563
|
-
walk(child, inside_function, child_enclosing_class)
|
|
564
619
|
|
|
565
|
-
|
|
566
|
-
|
|
620
|
+
|
|
621
|
+
def _collect_bindings(
|
|
622
|
+
tree: Tree, source_bytes: bytes, language: str, file_path: Path
|
|
623
|
+
) -> _Bindings:
|
|
624
|
+
"""Walk *tree* once, collecting import/local/definition/field bindings.
|
|
625
|
+
|
|
626
|
+
Author: Ron Webb
|
|
627
|
+
Since: 1.0.0
|
|
628
|
+
"""
|
|
629
|
+
collector = _BindingCollector(source_bytes, language, file_path)
|
|
630
|
+
collector.walk(tree.root_node, _Scope())
|
|
631
|
+
return collector.bindings
|
|
567
632
|
|
|
568
633
|
|
|
569
634
|
def _collect_usage_names(
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
Symbols that are only referenced from files outside the scanned project
|
|
4
4
|
(templates, configuration, other code bases) would otherwise be reported as
|
|
5
5
|
unused. :class:`SymbolSearcher` checks such files purely textually, without
|
|
6
|
-
parsing them.
|
|
7
|
-
whole-word regular expression; a method defined inside a class is only
|
|
6
|
+
parsing them. Class, function/method and field names are matched with the same
|
|
7
|
+
whole-word regular expression; a method or field defined inside a class is only
|
|
8
8
|
considered referenced when its own name *and* its enclosing class's name are
|
|
9
9
|
both found in the same search file.
|
|
10
10
|
|
|
@@ -17,8 +17,6 @@ from __future__ import annotations
|
|
|
17
17
|
import re
|
|
18
18
|
from pathlib import Path
|
|
19
19
|
|
|
20
|
-
from .scanner import SYMBOL_KIND_FUNCTION
|
|
21
|
-
|
|
22
20
|
#: ``(name, kind, defining_file, enclosing_class)``. ``enclosing_class`` is
|
|
23
21
|
#: ``None`` for classes and free functions.
|
|
24
22
|
Candidate = tuple[str, str, str, str | None]
|
|
@@ -98,24 +96,20 @@ class SymbolSearcher:
|
|
|
98
96
|
"""Return the *pending* candidates referenced in *text*.
|
|
99
97
|
|
|
100
98
|
A plain class or free function matches once its own name is found. A
|
|
101
|
-
method (``enclosing_class`` set) additionally requires its
|
|
102
|
-
class's name to be found in the same *text*.
|
|
99
|
+
method or field (``enclosing_class`` set) additionally requires its
|
|
100
|
+
enclosing class's name to be found in the same *text*.
|
|
103
101
|
|
|
104
102
|
Author: Ron Webb
|
|
105
103
|
Since: 1.2.0
|
|
106
104
|
"""
|
|
107
105
|
hits: set[Candidate] = set()
|
|
108
106
|
for candidate in pending:
|
|
109
|
-
name,
|
|
107
|
+
name, _, defining_file, enclosing_class = candidate
|
|
110
108
|
if _resolve(Path(defining_file)) == resolved_file:
|
|
111
109
|
continue
|
|
112
110
|
if not _name_matches(text, name, patterns):
|
|
113
111
|
continue
|
|
114
|
-
if (
|
|
115
|
-
kind == SYMBOL_KIND_FUNCTION
|
|
116
|
-
and enclosing_class
|
|
117
|
-
and not _name_matches(text, enclosing_class, patterns)
|
|
118
|
-
):
|
|
112
|
+
if enclosing_class and not _name_matches(text, enclosing_class, patterns):
|
|
119
113
|
continue
|
|
120
114
|
hits.add(candidate)
|
|
121
115
|
return hits
|
|
@@ -231,7 +231,8 @@ class SessionStore:
|
|
|
231
231
|
) -> list[dict[str, object]]:
|
|
232
232
|
"""Return a finding for every definition never referenced elsewhere.
|
|
233
233
|
|
|
234
|
-
A definition is "unused" when its
|
|
234
|
+
A definition (class, function/method or field) is "unused" when its
|
|
235
|
+
name never appears in the
|
|
235
236
|
project-wide ``identifiers`` table collected for *session_id*. Names
|
|
236
237
|
matching any of *ignore_patterns* (e.g. dunder methods, ``main``,
|
|
237
238
|
test hooks) are suppressed. When *searcher* is given, definitions that
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|