omnicoach 0.3.0

Lightweight, high-signal static analysis and command linter for Python and Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
//! Flags private functions, methods, and types defined above their owning or calling public entrypoints.

use crate::code_lint::ast::{self, CallClusterRule, ParsedFile};
use crate::code_lint::contract::{CodeRule, RuleTarget};
use crate::diagnostic::{Diagnostic, Language, RuleName, ViolationTemplate, violation_template};
use crate::rule_declaration::{
    Classification, Consensus, Declaration, Example, ImpactedQuality, Precision, Reference,
    RuleDoc, RuleOptions, Topic,
};
use std::path::Path;

const TEMPLATE: ViolationTemplate = violation_template! {
    summary: "Private helper `{function}` is defined before public entrypoint `{caller}`.",
    rationale: "Placing lower-abstraction private helpers above `{caller}` buries the public contract of the scope behind implementation details.",
    suggestion: "Move `{function}` below `{caller}`, for example below `{anchor}` (ends on line {line}).",
};

/// The rule's declaration.
pub const RULE: CodeRule = CodeRule {
    declaration: Declaration {
        name: RuleName("private-before-public"),
        template: &TEMPLATE,
        languages: &[Language::Python, Language::Rust],
        options: RuleOptions::code_rule(()),
        classification: Classification {
            topics: &[Topic::DECLARATION_ORDER],
            precision: Precision::Exact,
            consensus: Consensus::Opinionated,
            impacted_quality: ImpactedQuality::Maintainability,
        },
        doc: RuleDoc {
            summary: "Flags private functions, methods, and types defined above their owning or calling public entrypoints.",
            what_it_does: indoc::indoc! {r"
                Flags a private function, method, or type (`_name` in Python, or a non-`pub` `fn`,
                `struct`, `enum`, `union`, or `trait` in Rust) in a module, Python `class`, or Rust
                inherent `impl` block declared above a public declaration that references it, or,
                when no public declaration reaches it, above the last public one. In Python, items
                evaluated when the module or class body executes (such as top-level calls, decorators,
                parameter defaults, base classes, eager annotations without
                `from __future__ import annotations`), or PEP 593 `Annotated` metadata expressions
                resolved by runtime introspection (such as Pydantic or FastAPI), and their transitive
                body callees are exempt when defined before a consumer. Helpers below their callers
                are checked by `uncolocated-helper` and private-only call order by `callee-before-caller`,
                so each misplaced declaration is reported once."},
            why_is_this_bad: indoc::indoc! {r"
                A private helper function or type is a lower-abstraction building block than the
                public entrypoint that uses it, and a helper shared across multiple public
                entrypoints is at a lower abstraction level still. Placing private helpers above
                their public consumers inverts the abstraction hierarchy and forces readers to wade
                through internal mechanics before seeing the scope's public interface.

                Place single-use helpers either immediately below their owning public entrypoint or
                in the trailing private helper section, and place shared helpers in the trailing
                private helper section below all public declarations."},
            known_problems: Some(indoc::indoc! {r"
                - Module-level constants (`const`, `static`, `UPPER_CASE = ...`) and type aliases
                  are not ordered.
                - In Rust macro arguments, a pattern binding or named argument that shares a sibling
                  declaration's name counts as a reference."}),
            references: &[
                Reference {
                    title: "Robert C. Martin: Clean Code — Chapter 5 & Chapter 10 (The Newspaper Metaphor, The Stepdown Rule, Class Organization)",
                    url: "https://www.oreilly.com/library/view/clean-code-a/9780136083238/",
                },
                Reference {
                    title: "wemake-python-styleguide: WPS338 WrongMethodOrderViolation",
                    url: "https://wemake-python-styleguide.readthedocs.io/en/latest/pages/usage/violations/consistency.html",
                },
            ],
            examples: &[
                Example {
                    language: Language::Python,
                    flagged: indoc::indoc! {r"
                        class TokenVerifier:
                            def _normalize(self, raw: str) -> str:
                                return raw.strip()

                            def verify(self, raw: str) -> bool:
                                return bool(self._normalize(raw))
                    "},
                    flagged_span: "_normalize",
                    fixed: indoc::indoc! {r"
                        class TokenVerifier:
                            def verify(self, raw: str) -> bool:
                                return bool(self._normalize(raw))

                            def _normalize(self, raw: str) -> str:
                                return raw.strip()
                    "},
                },
                Example {
                    language: Language::Rust,
                    flagged: indoc::indoc! {r"
                        fn trim_ascii(input: &str) -> &str {
                            input.trim()
                        }

                        pub fn parse_token(input: &str) -> bool {
                            !trim_ascii(input).is_empty()
                        }
                    "},
                    flagged_span: "trim_ascii",
                    fixed: indoc::indoc! {r"
                        pub fn parse_token(input: &str) -> bool {
                            !trim_ascii(input).is_empty()
                        }

                        fn trim_ascii(input: &str) -> &str {
                            input.trim()
                        }
                    "},
                },
            ],
        },
    },
    target: RuleTarget::SourceOnly,
    check: check_file,
};

fn check_file(rule: &CodeRule, path: &Path, file: &ParsedFile, (): ()) -> Vec<Diagnostic> {
    ast::collect_call_cluster_findings(file, CallClusterRule::PrivateBeforePublic)
        .iter()
        .map(|finding| {
            let line = finding.anchor.line.to_string();
            rule.diagnostic_at_node(
                path,
                &finding.name_node,
                &[
                    ("function", &finding.item),
                    ("caller", &finding.related),
                    ("anchor", &finding.anchor.name),
                    ("line", &line),
                ],
            )
        })
        .collect()
}

#[cfg(test)]
crate::test_utils::rule_test!(
    RULE,
    {
        Python => {
            pass: [
                colocated_exclusive_helpers_between_public_entrypoints_allowed => r#"
                    def parse_header(raw: str) -> str:
                        return _clean_header(raw)

                    def _clean_header(raw: str) -> str:
                        return raw.strip()

                    def parse_body(raw: str) -> str:
                        return _clean_body(raw)

                    def _clean_body(raw: str) -> str:
                        return raw.lower()
                "#,
                shared_helper_below_all_its_public_callers_allowed => r#"
                    class Formatter:
                        def format_title(self, text: str) -> str:
                            return self._normalize(text)

                        def format_summary(self, text: str) -> str:
                            return self._normalize(text)

                        def _normalize(self, text: str) -> str:
                            return text.strip()
                "#,
                dunder_method_is_public => r#"
                    class Formatter:
                        def __repr__(self) -> str:
                            return "Formatter"

                        def format_title(self, text: str) -> str:
                            return text
                "#,
                private_only_scope_exempt => r#"
                    def _first_internal(value: int) -> int:
                        return _second_internal(value)

                    def _second_internal(value: int) -> int:
                        return value + 1
                "#,
                property_accessors_grouped_at_first_definition => r#"
                    from typing import overload

                    class Session:
                        @property
                        def token(self) -> str:
                            return "ok"

                        def _reset(self) -> None:
                            pass

                        @overload
                        def token(self, value: str) -> None: ...

                        @overload
                        def token(self, value: bytes) -> None: ...

                        @token.setter
                        def token(self, value: str | bytes) -> None:
                            pass
                "#,
                import_time_pinned_helper_exempt => r#"
                    def _build_table() -> dict[str, int]:
                        return {"a": _default_weight()}

                    def _default_weight() -> int:
                        return 1

                    TABLE = _build_table()

                    def lookup(key: str) -> int:
                        return TABLE.get(key, _default_weight())
                "#,
                decorator_pinned_helper_exempt => r#"
                    def _trace(fn):
                        return fn

                    @_trace
                    def execute(value: int) -> int:
                        return value
                "#,
                base_class_pinned_before_subclass_exempt => r#"
                    class _BaseClient:
                        def _send(self) -> int:
                            return 1

                    class HttpClient(_BaseClient):
                        def fetch(self) -> int:
                            return self._send()
                "#,
                runtime_annotation_pinned_without_future_annotations => r#"
                    class _Config:
                        pass

                    def configure(cfg: _Config) -> None:
                        pass
                "#,
                annotated_validator_on_pydantic_model_pinned_with_future_annotations => r#"
                    from __future__ import annotations
                    from typing import Annotated
                    from pydantic import BaseModel, AfterValidator

                    def _strip_whitespace(v: str) -> str:
                        return v.strip()

                    def _validate(v: str) -> str:
                        return _strip_whitespace(v)

                    class User(BaseModel):
                        name: Annotated[str, AfterValidator(_validate)]
                "#,
                annotated_validator_in_function_signature_pinned_with_future_annotations => r#"
                    from __future__ import annotations
                    from typing import Annotated

                    def _check(v: str) -> str:
                        return v

                    def endpoint(param: Annotated[str, _check]) -> None:
                        pass
                "#,
                annotated_validator_in_pep695_type_alias_pinned => r#"
                    from __future__ import annotations
                    from typing import Annotated

                    def _check(v: int) -> int:
                        return v

                    type Checked = Annotated[int, _check]

                    def public_fn() -> None:
                        pass
                "#,
            ],
            fail: [
                method_helper_on_pinned_base_class_still_flagged => r#"
                    def _helper() -> int:
                        return 1

                    class _Base:
                        def method(self) -> int:
                            return _helper()

                    class Public(_Base):
                        pass
                "# => "_helper",
                postponed_submodel_type_above_parent_model => r#"
                    from __future__ import annotations

                    class _SubConfig:
                        host: str

                    class AppConfig:
                        sub: _SubConfig
                "# => "_SubConfig",
                unpinned_method_return_type_still_flagged => r#"
                    from __future__ import annotations
                    from pydantic import BaseModel

                    class _Result:
                        pass

                    class Service(BaseModel):
                        def run(self) -> _Result:
                            return _Result()
                "# => "_Result",
                exclusive_helper_before_owning_public_function => r#"
                    def _prepare(value: int) -> int:
                        return value + 1

                    def compute(value: int) -> int:
                        return _prepare(value)
                "# => "_prepare",
                shared_helper_between_its_two_public_callers => r#"
                    class Verifier:
                        def verify_header(self, raw: str) -> bool:
                            return bool(self._strip(raw))

                        def _strip(self, raw: str) -> str:
                            return raw.strip()

                        def verify_footer(self, raw: str) -> bool:
                            return bool(self._strip(raw))
                "# => "_strip",
                uncalled_private_method_before_public_method => r#"
                    class Formatter:
                        def __trim(self) -> str:
                            return "x"

                        def __repr__(self) -> str:
                            return "Formatter"
                "# => "__trim",
                private_class_before_public_class_using_it => r#"
                    class _Entry:
                        pass

                    class Cache:
                        def create(self) -> object:
                            return _Entry()
                "# => "_Entry",
                future_annotations_unpins_private_annotation_type => r#"
                    from __future__ import annotations

                    class _Config:
                        pass

                    def configure(cfg: _Config) -> None:
                        pass
                "# => "_Config",
            ],
        },
        Rust => {
            pass: [
                colocated_exclusive_helper_between_public_methods_allowed => r#"
                    pub struct SpanNode {
                        offset: usize,
                    }

                    impl SpanNode {
                        pub fn shifted(&self) -> usize {
                            self.compute_shift()
                        }

                        fn compute_shift(&self) -> usize {
                            self.offset + 1
                        }

                        pub fn raw_offset(&self) -> usize {
                            self.offset
                        }
                    }
                "#,
                restricted_visibility_is_public => r#"
                    pub struct SpanNode {
                        offset: usize,
                    }

                    impl SpanNode {
                        pub(crate) fn from_offset(offset: usize) -> Self {
                            Self { offset }
                        }

                        pub(super) fn raw_offset(&self) -> usize {
                            self.offset
                        }

                        pub fn shifted(&self) -> usize {
                            self.offset + 1
                        }
                    }
                "#,
                inline_test_fn_exempt => r#"
                    pub struct Worker;

                    impl Worker {
                        /// Test-only helper method.
                        #[cfg(test)]
                        fn test_only_helper(&self) {}

                        pub fn validate(&self) -> bool {
                            true
                        }
                    }
                "#,
                private_helper_after_pub_struct_impl_allowed => r#"
                    pub struct Service;

                    impl Service {
                        pub fn execute(&self) -> usize {
                            compute_step()
                        }
                    }

                    fn compute_step() -> usize {
                        1
                    }
                "#,
            ],
            fail: [
                module_private_helper_before_pub_caller => r#"
                    fn normalize_key(key: &str) -> &str {
                        key.trim()
                    }

                    pub fn lookup_key(key: &str) -> usize {
                        normalize_key(key).len()
                    }
                "# => "normalize_key",
                shared_impl_helper_before_second_pub_caller => r#"
                    pub struct Parser;

                    impl Parser {
                        pub fn parse_left(&self, input: &str) -> usize {
                            Self::trimmed_len(input)
                        }

                        fn trimmed_len(input: &str) -> usize {
                            input.trim().len()
                        }

                        pub(crate) fn parse_right(&self, input: &str) -> usize {
                            Self::trimmed_len(input)
                        }
                    }
                "# => "trimmed_len",
                uncalled_private_method_before_pub_method => r#"
                    pub struct Tracker {
                        count: usize,
                    }

                    impl Tracker {
                        fn reset_internal(&mut self) {
                            self.count = 0;
                        }

                        pub fn total(&self) -> usize {
                            self.count
                        }
                    }
                "# => "reset_internal",
                private_struct_before_pub_struct_using_it => r#"
                    struct RawEntry {
                        count: usize,
                    }

                    pub struct Table {
                        entry: RawEntry,
                    }
                "# => "RawEntry",
                private_fn_before_pub_struct_impl_caller => r#"
                    fn compute_hash(input: &str) -> usize {
                        input.len()
                    }

                    pub struct Hasher;

                    impl Hasher {
                        pub fn hash(&self, input: &str) -> usize {
                            compute_hash(input)
                        }
                    }
                "# => "compute_hash",
            ],
        },
    }
);