@codeperfect/eslint-plugin 3.7.0 → 3.9.0
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.
- package/README.md +11 -19
- package/dist/adapters/angular/angular-lifecycle-usage-adapter.d.ts +15 -0
- package/dist/adapters/angular/angular-lifecycle-usage-adapter.d.ts.map +1 -0
- package/dist/adapters/angular/angular-lifecycle-usage-adapter.js +87 -0
- package/dist/adapters/angular/angular-template-expression-utilities.js +2 -2
- package/dist/adapters/angular/angular-template-usage-context.d.ts +1 -1
- package/dist/adapters/angular/angular-template-usage-context.d.ts.map +1 -1
- package/dist/adapters/angular/angular-template-usage-context.js +2 -2
- package/dist/adapters/angular/no-unused-public-members-template-adapter.js +2 -2
- package/dist/adapters/angular/plugin.d.ts.map +1 -1
- package/dist/adapters/angular/plugin.js +7 -3
- package/dist/codeperfect-eslint-plugin-1.0.0.tgz +0 -0
- package/dist/domain/architecture/consistent-barrel-files/exports-only-rule-utilities.d.ts +39 -0
- package/dist/domain/architecture/consistent-barrel-files/exports-only-rule-utilities.d.ts.map +1 -0
- package/dist/domain/architecture/consistent-barrel-files/exports-only-rule-utilities.js +131 -0
- package/dist/domain/architecture/consistent-barrel-files/exports-only-rule.d.ts.map +1 -1
- package/dist/domain/architecture/consistent-barrel-files/exports-only-rule.js +12 -1
- package/dist/domain/architecture/index.d.ts +2 -0
- package/dist/domain/architecture/index.d.ts.map +1 -1
- package/dist/domain/architecture/index.js +3 -1
- package/dist/domain/architecture/no-directory-only-imports/index.d.ts +2 -0
- package/dist/domain/architecture/no-directory-only-imports/index.d.ts.map +1 -0
- package/dist/domain/architecture/no-directory-only-imports/index.js +2 -0
- package/dist/domain/architecture/no-directory-only-imports/no-directory-only-imports-rule.d.ts +5 -0
- package/dist/domain/architecture/no-directory-only-imports/no-directory-only-imports-rule.d.ts.map +1 -0
- package/dist/domain/architecture/no-directory-only-imports/no-directory-only-imports-rule.js +83 -0
- package/dist/domain/architecture/no-unused-exports/no-unused-exports-identifier-utilities.d.ts +1 -1
- package/dist/domain/architecture/no-unused-exports/no-unused-exports-identifier-utilities.d.ts.map +1 -1
- package/dist/domain/architecture/no-unused-exports/no-unused-exports-identifier-utilities.js +2 -2
- package/dist/domain/architecture/no-unused-exports/no-unused-exports-symbol-utilities.d.ts +1 -1
- package/dist/domain/architecture/no-unused-exports/no-unused-exports-symbol-utilities.d.ts.map +1 -1
- package/dist/domain/architecture/no-unused-exports/no-unused-exports-symbol-utilities.js +2 -2
- package/dist/domain/architecture/no-unused-exports/no-unused-exports-usage-index-builder.d.ts +1 -1
- package/dist/domain/architecture/no-unused-exports/no-unused-exports-usage-index-builder.d.ts.map +1 -1
- package/dist/domain/architecture/no-unused-exports/no-unused-exports-usage-index-builder.js +2 -2
- package/dist/domain/architecture/no-unused-exports/no-unused-exports-usage-index.d.ts +1 -1
- package/dist/domain/architecture/no-unused-exports/no-unused-exports-usage-index.d.ts.map +1 -1
- package/dist/domain/architecture/no-unused-exports/no-unused-exports-usage-index.js +2 -2
- package/dist/domain/architecture/no-unused-exports/rule.d.ts +14 -1
- package/dist/domain/architecture/no-unused-exports/rule.d.ts.map +1 -1
- package/dist/domain/architecture/no-unused-exports/rule.js +17 -4
- package/dist/domain/architecture/no-unused-public-members/member-api-reachability-utilities.d.ts +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-api-reachability-utilities.d.ts.map +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-api-reachability-utilities.js +2 -2
- package/dist/domain/architecture/no-unused-public-members/member-api-relevance-utilities.d.ts +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-api-relevance-utilities.d.ts.map +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-api-relevance-utilities.js +2 -2
- package/dist/domain/architecture/no-unused-public-members/member-api-type-utilities.d.ts +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-api-type-utilities.d.ts.map +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-api-type-utilities.js +2 -2
- package/dist/domain/architecture/no-unused-public-members/member-contract-utilities.d.ts +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-contract-utilities.d.ts.map +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-contract-utilities.js +13 -2
- package/dist/domain/architecture/no-unused-public-members/member-declaration-utilities.d.ts +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-declaration-utilities.d.ts.map +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-declaration-utilities.js +9 -4
- package/dist/domain/architecture/no-unused-public-members/member-factory-utilities.d.ts +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-factory-utilities.d.ts.map +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-factory-utilities.js +2 -2
- package/dist/domain/architecture/no-unused-public-members/member-function-utilities.d.ts +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-function-utilities.d.ts.map +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-function-utilities.js +2 -2
- package/dist/domain/architecture/no-unused-public-members/member-host-utilities.d.ts +3 -3
- package/dist/domain/architecture/no-unused-public-members/member-host-utilities.d.ts.map +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-host-utilities.js +40 -25
- package/dist/domain/architecture/no-unused-public-members/member-operation-utilities.d.ts +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-operation-utilities.d.ts.map +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-operation-utilities.js +2 -2
- package/dist/domain/architecture/no-unused-public-members/member-parameter-flow-utilities.d.ts +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-parameter-flow-utilities.d.ts.map +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-parameter-flow-utilities.js +2 -2
- package/dist/domain/architecture/no-unused-public-members/member-public-api-utilities.d.ts +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-public-api-utilities.d.ts.map +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-public-api-utilities.js +2 -2
- package/dist/domain/architecture/no-unused-public-members/member-request-body-utilities.d.ts +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-request-body-utilities.d.ts.map +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-request-body-utilities.js +2 -2
- package/dist/domain/architecture/no-unused-public-members/member-scope-utilities.d.ts +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-scope-utilities.d.ts.map +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-scope-utilities.js +39 -15
- package/dist/domain/architecture/no-unused-public-members/member-usage-call-utilities.d.ts +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-usage-call-utilities.d.ts.map +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-usage-call-utilities.js +2 -2
- package/dist/domain/architecture/no-unused-public-members/member-usage-expression-reference-utilities.d.ts +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-usage-expression-reference-utilities.d.ts.map +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-usage-expression-reference-utilities.js +2 -2
- package/dist/domain/architecture/no-unused-public-members/member-usage-handoff-utilities.d.ts +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-usage-handoff-utilities.d.ts.map +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-usage-handoff-utilities.js +2 -2
- package/dist/domain/architecture/no-unused-public-members/member-usage-index-utilities.d.ts +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-usage-index-utilities.d.ts.map +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-usage-index-utilities.js +2 -2
- package/dist/domain/architecture/no-unused-public-members/member-usage-local-flow-utilities.d.ts +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-usage-local-flow-utilities.d.ts.map +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-usage-local-flow-utilities.js +2 -2
- package/dist/domain/architecture/no-unused-public-members/member-usage-reference-utilities.d.ts +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-usage-reference-utilities.d.ts.map +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-usage-reference-utilities.js +2 -2
- package/dist/domain/architecture/no-unused-public-members/member-usage-serialization-utilities.d.ts +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-usage-serialization-utilities.d.ts.map +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-usage-serialization-utilities.js +2 -2
- package/dist/domain/architecture/no-unused-public-members/member-usage-type-utilities.d.ts +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-usage-type-utilities.d.ts.map +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-usage-type-utilities.js +2 -2
- package/dist/domain/architecture/no-unused-public-members/member-usage-utilities.d.ts +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-usage-utilities.d.ts.map +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-usage-utilities.js +13 -2
- package/dist/domain/architecture/no-unused-public-members/member-usage-value-utilities.d.ts +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-usage-value-utilities.d.ts.map +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-usage-value-utilities.js +2 -2
- package/dist/domain/architecture/no-unused-public-members/member-usage-whole-object-utilities.d.ts +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-usage-whole-object-utilities.d.ts.map +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-usage-whole-object-utilities.js +2 -2
- package/dist/domain/architecture/no-unused-public-members/member-utilities.d.ts +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-utilities.d.ts.map +1 -1
- package/dist/domain/architecture/no-unused-public-members/member-utilities.js +2 -2
- package/dist/domain/architecture/no-unused-public-members/rule.d.ts +14 -1
- package/dist/domain/architecture/no-unused-public-members/rule.d.ts.map +1 -1
- package/dist/domain/architecture/no-unused-public-members/rule.js +16 -3
- package/dist/domain/architecture/package-subpath-imports/index.d.ts +2 -0
- package/dist/domain/architecture/package-subpath-imports/index.d.ts.map +1 -0
- package/dist/domain/architecture/package-subpath-imports/index.js +2 -0
- package/dist/domain/architecture/package-subpath-imports/package-subpath-import-alias-utilities.d.ts +12 -0
- package/dist/domain/architecture/package-subpath-imports/package-subpath-import-alias-utilities.d.ts.map +1 -0
- package/dist/domain/architecture/package-subpath-imports/package-subpath-import-alias-utilities.js +215 -0
- package/dist/domain/architecture/package-subpath-imports/package-subpath-import-matching-utilities.d.ts +24 -0
- package/dist/domain/architecture/package-subpath-imports/package-subpath-import-matching-utilities.d.ts.map +1 -0
- package/dist/domain/architecture/package-subpath-imports/package-subpath-import-matching-utilities.js +46 -0
- package/dist/domain/architecture/package-subpath-imports/package-subpath-import-path-utilities.d.ts +36 -0
- package/dist/domain/architecture/package-subpath-imports/package-subpath-import-path-utilities.d.ts.map +1 -0
- package/dist/domain/architecture/package-subpath-imports/package-subpath-import-path-utilities.js +103 -0
- package/dist/domain/architecture/package-subpath-imports/package-subpath-import-types.d.ts +44 -0
- package/dist/domain/architecture/package-subpath-imports/package-subpath-import-types.d.ts.map +1 -0
- package/dist/domain/architecture/package-subpath-imports/package-subpath-import-types.js +1 -0
- package/dist/domain/architecture/package-subpath-imports/package-subpath-import-utilities.d.ts +13 -0
- package/dist/domain/architecture/package-subpath-imports/package-subpath-import-utilities.d.ts.map +1 -0
- package/dist/domain/architecture/package-subpath-imports/package-subpath-import-utilities.js +194 -0
- package/dist/domain/architecture/package-subpath-imports/prefer-package-subpath-imports-rule.d.ts +5 -0
- package/dist/domain/architecture/package-subpath-imports/prefer-package-subpath-imports-rule.d.ts.map +1 -0
- package/dist/domain/architecture/package-subpath-imports/prefer-package-subpath-imports-rule.js +106 -0
- package/dist/domain/architecture/program-analysis/source-dependency-utilities.d.ts +1 -1
- package/dist/domain/architecture/program-analysis/source-dependency-utilities.d.ts.map +1 -1
- package/dist/domain/architecture/program-analysis/source-dependency-utilities.js +2 -2
- package/dist/domain/architecture/program-analysis/source-file-facts.d.ts +1 -1
- package/dist/domain/architecture/program-analysis/source-file-facts.d.ts.map +1 -1
- package/dist/domain/architecture/program-analysis/source-file-facts.js +2 -2
- package/dist/domain/docs/single-line-jsdoc/rule.js +2 -2
- package/dist/domain/index.d.ts +1 -1
- package/dist/domain/index.d.ts.map +1 -1
- package/dist/domain/index.js +2 -2
- package/dist/domain/testing/require-act-result-capture/rule.js +2 -2
- package/dist/index.d.ts +1 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -3
- package/dist/infrastructure/codeperfect-plugin.d.ts +4 -0
- package/dist/infrastructure/codeperfect-plugin.d.ts.map +1 -1
- package/dist/infrastructure/codeperfect-plugin.js +4 -2
- package/dist/infrastructure/index.d.ts +0 -1
- package/dist/infrastructure/index.d.ts.map +1 -1
- package/dist/infrastructure/index.js +1 -2
- package/dist/infrastructure/presets.d.ts.map +1 -1
- package/dist/infrastructure/presets.js +5 -1
- package/docs/README.md +20 -0
- package/docs/api/index.md +19 -0
- package/docs/api/type-aliases/CodeperfectPreset.md +13 -0
- package/docs/api/variables/aaa.md +13 -0
- package/docs/api/variables/all.md +13 -0
- package/docs/api/variables/architecture.md +13 -0
- package/docs/api/variables/codeperfectPlugin.md +111 -0
- package/docs/api/variables/core.md +13 -0
- package/docs/api/variables/documentation.md +13 -0
- package/docs/api/variables/testing.md +13 -0
- package/docs/design/README.md +6 -0
- package/docs/design/architecture.md +218 -0
- package/docs/design/release.md +30 -0
- package/docs/rules/assert-actual-expected-names.md +163 -0
- package/docs/rules/barrel-files-exports-only.md +88 -0
- package/docs/rules/consistent-barrel-files.md +107 -0
- package/docs/rules/enforce-aaa-structure.md +220 -0
- package/docs/rules/no-directory-only-imports.md +72 -0
- package/docs/rules/no-import-export-aliases.md +54 -0
- package/docs/rules/no-import-export-extensions.md +63 -0
- package/docs/rules/no-interface-member-docs.md +101 -0
- package/docs/rules/no-multiple-declarators.md +105 -0
- package/docs/rules/no-reexports-outside-barrels.md +106 -0
- package/docs/rules/no-unused-exports.md +283 -0
- package/docs/rules/no-unused-public-members.md +389 -0
- package/docs/rules/no-useless-delegation.md +56 -0
- package/docs/rules/prefer-interface-types.md +138 -0
- package/docs/rules/prefer-package-subpath-imports.md +82 -0
- package/docs/rules/prefer-vi-mocked-import.md +129 -0
- package/docs/rules/require-act-result-capture.md +183 -0
- package/docs/rules/require-example-language.md +172 -0
- package/docs/rules/require-test-companion.md +125 -0
- package/docs/rules/single-act-statement.md +133 -0
- package/docs/rules/single-line-jsdoc.md +140 -0
- package/package.json +27 -26
- package/dist/application/custom-rules.d.ts +0 -70
- package/dist/application/custom-rules.d.ts.map +0 -1
- package/dist/application/custom-rules.js +0 -37
- package/dist/application/index.d.ts +0 -4
- package/dist/application/index.d.ts.map +0 -1
- package/dist/application/index.js +0 -3
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
# `codeperfect/no-unused-exports`
|
|
2
|
+
|
|
3
|
+
## Summary
|
|
4
|
+
|
|
5
|
+
Disallow exports that are never consumed by production code.
|
|
6
|
+
|
|
7
|
+
The rule reports:
|
|
8
|
+
|
|
9
|
+
- exports with no consumers at all
|
|
10
|
+
- exports consumed only by test files
|
|
11
|
+
|
|
12
|
+
## Enabled by
|
|
13
|
+
|
|
14
|
+
- internal `codeperfect` plugin registry
|
|
15
|
+
- `architecture` preset
|
|
16
|
+
|
|
17
|
+
## Why this rule exists
|
|
18
|
+
|
|
19
|
+
`no-unused-vars` intentionally ignores exported bindings. That behavior is useful for public APIs, but it can hide dead exports and test-only exports that should not remain in production modules.
|
|
20
|
+
|
|
21
|
+
This rule complements `no-unused-vars` by checking cross-file usage and ensuring exports are consumed by non-test code.
|
|
22
|
+
|
|
23
|
+
For this rule, consumption means one concrete symbol reference in another non-test file. Bare imports and ordinary forwarding exports are not concrete usage.
|
|
24
|
+
|
|
25
|
+
Classification rules:
|
|
26
|
+
|
|
27
|
+
- references from files matching `testFilePatterns` never satisfy production usage
|
|
28
|
+
- exports exposed by files matching `publicApiFiles` satisfy production usage
|
|
29
|
+
- `publicApiFiles` exposure can be direct or transitive through barrel files
|
|
30
|
+
- `import { X }` without any concrete reference to `X` does not count
|
|
31
|
+
- type exports must be referenced in type positions
|
|
32
|
+
- value exports must be referenced in value positions
|
|
33
|
+
- `typeof X` counts as a concrete value reference to `X` even when it appears inside a type
|
|
34
|
+
|
|
35
|
+
## The Problem with Exporting for Testing Only
|
|
36
|
+
|
|
37
|
+
Exporting an internal helper, type, or function solely so a test file can import it is a common anti-pattern:
|
|
38
|
+
|
|
39
|
+
- **Breaks Encapsulation**: Exposing internal details pollutes your public API and makes it unclear what parts of the module are meant to be used by the rest of the application. Consumers of the module cannot distinguish between intentional public interfaces and accidental implementation details.
|
|
40
|
+
- **Ties Tests to Implementation**: If you test private functions directly, refactoring, restructuring, or renaming those internal functions will break your unit tests, even if the public behavior of the module remains completely unchanged. Tests should verify observable behavior and contracts rather than private mechanics.
|
|
41
|
+
- **Smell of Design Issues**: Needing to export a private function often means the module is doing too much (violating the Single Responsibility Principle) and should be split into smaller, independent services or utility classes.
|
|
42
|
+
|
|
43
|
+
### Better Alternatives
|
|
44
|
+
|
|
45
|
+
- **Test Through Public Methods**: Write your tests against the public API (the functions or classes that are already exported and meant to be consumed). If the public methods work correctly, the internal helper functions are verified by extension.
|
|
46
|
+
- **Extract a Separate Helper Module**: If a private function is complex enough that it needs its own dedicated unit tests, extract it into its own separate module or utility file and export it properly from there (where the original module imports and consumes it in production).
|
|
47
|
+
- **Move to Test Utilities**: If the symbol is genuinely only needed for testing (such as mock data factories, fixtures, or test assertion helpers), move it to a test file or a dedicated test utilities folder outside production source code.
|
|
48
|
+
|
|
49
|
+
## Options
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
type Options = {
|
|
53
|
+
publicApiFiles?: string[]; // default: ["**/src/index.ts", "**/__tests__/**/*.ts"]
|
|
54
|
+
testFilePatterns?: string[]; // default: ["**/*.{test,spec,e2e}.ts", "**/__tests__/**/*.ts"]
|
|
55
|
+
};
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
- `publicApiFiles`
|
|
59
|
+
- File globs that define public API export surfaces
|
|
60
|
+
- Exports and re-exports reachable from these files count as production usage
|
|
61
|
+
- `testFilePatterns`
|
|
62
|
+
- File globs treated as tests when classifying consumers
|
|
63
|
+
|
|
64
|
+
## Rule Details
|
|
65
|
+
|
|
66
|
+
The rule checks these export kinds:
|
|
67
|
+
|
|
68
|
+
- named value exports
|
|
69
|
+
- named type exports
|
|
70
|
+
- default exports
|
|
71
|
+
- local export specifiers backed by declarations in the same file
|
|
72
|
+
|
|
73
|
+
By default, the rule analyzes files matching `**/src/**/*.ts`.
|
|
74
|
+
|
|
75
|
+
The rule currently detects production consumption from:
|
|
76
|
+
|
|
77
|
+
- concrete cross-file symbol references in non-test files
|
|
78
|
+
- direct or transitive export exposure through files matching `publicApiFiles`
|
|
79
|
+
|
|
80
|
+
Ordinary pass-through re-export statements (`export ... from` and `export * from`) are treated as API forwarding only:
|
|
81
|
+
|
|
82
|
+
- they do not count as consumption of declaration files
|
|
83
|
+
- they do not create declaration diagnostics in the forwarding file
|
|
84
|
+
|
|
85
|
+
The exception is a pass-through re-export that is reachable from a `publicApiFiles` file. In that case, the public API file intentionally exposes the original symbol, so the original export is considered consumed by production code.
|
|
86
|
+
|
|
87
|
+
Dynamic runtime loading patterns are not considered consumers.
|
|
88
|
+
|
|
89
|
+
A reported export falls into one of two categories:
|
|
90
|
+
|
|
91
|
+
- **unusedExport**: no concrete non-test usage consumes it
|
|
92
|
+
- **usedOnlyInTests**: only test files consume it
|
|
93
|
+
|
|
94
|
+
One additional non-reported path exists:
|
|
95
|
+
|
|
96
|
+
- **publicApiExposure**: the export is reachable from a file matching `publicApiFiles`
|
|
97
|
+
|
|
98
|
+
### Performance
|
|
99
|
+
|
|
100
|
+
The rule scans each TypeScript Program once and shares a symbol usage index
|
|
101
|
+
across exports and linted files. Public API exposure is indexed alongside
|
|
102
|
+
concrete references. File classifications are cached separately for each
|
|
103
|
+
repository root and option set.
|
|
104
|
+
|
|
105
|
+
Source syntax catalogs and native reference-symbol lookups are shared with
|
|
106
|
+
`no-unused-public-members` when both rules run. These facts contain no API,
|
|
107
|
+
test-file, or ownership classifications; each rule applies its own policy.
|
|
108
|
+
Import/dependency edges do not turn bare imports or dynamic loading into
|
|
109
|
+
concrete export usage.
|
|
110
|
+
|
|
111
|
+
Caches are scoped to the parser's Program object through weak references. A new
|
|
112
|
+
Program receives a fresh index, so editor updates do not reuse an old Program's
|
|
113
|
+
analysis. Separate TypeScript projects each require their own scan.
|
|
114
|
+
|
|
115
|
+
The member rule's analysis folders do not restrict this rule's consumers.
|
|
116
|
+
Both rules work independently and in either execution order. See the
|
|
117
|
+
[measured export controls](../design/architecture.md#measured-performance)
|
|
118
|
+
for diagnostic equivalence and timing limits of the reference workloads.
|
|
119
|
+
|
|
120
|
+
## Invalid
|
|
121
|
+
|
|
122
|
+
### Fully unused export
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
// src/feature.ts
|
|
126
|
+
// Reported: Export 'orphan' is never consumed by any file in the project;
|
|
127
|
+
// remove the export or delete the symbol if it is no longer needed.
|
|
128
|
+
export const orphan = 1;
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Export consumed only by test files
|
|
132
|
+
|
|
133
|
+
```typescript
|
|
134
|
+
// src/feature.ts
|
|
135
|
+
export const feature = 1;
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
// src/feature.spec.ts
|
|
140
|
+
import { feature } from "./feature";
|
|
141
|
+
void feature;
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Internal helper exported solely for tests
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
// src/order.ts
|
|
148
|
+
// BAD: calculateTax is internal logic exported solely for test files
|
|
149
|
+
export function calculateTax(amount: number): number {
|
|
150
|
+
return amount * 0.2;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function createOrder(amount: number): { total: number } {
|
|
154
|
+
return { total: amount + calculateTax(amount) };
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
// src/order.spec.ts
|
|
160
|
+
import { calculateTax } from "./order";
|
|
161
|
+
|
|
162
|
+
// Reported: Export 'calculateTax' is consumed only by test files.
|
|
163
|
+
// Avoid exporting for tests: test through the public API, extract to a separate helper module, or move to test utilities.
|
|
164
|
+
test("calculates tax", () => {
|
|
165
|
+
expect(calculateTax(100)).toBe(20);
|
|
166
|
+
});
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### Forwarded only by a non-public barrel
|
|
170
|
+
|
|
171
|
+
```typescript
|
|
172
|
+
// src/feature.ts
|
|
173
|
+
export const feature = 1;
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
// src/reexport.ts
|
|
178
|
+
export { feature } from "./feature";
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## Valid
|
|
182
|
+
|
|
183
|
+
### Export consumed by production code
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
// src/feature.ts
|
|
187
|
+
export const feature = 1;
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
```typescript
|
|
191
|
+
// src/consumer.ts
|
|
192
|
+
import { feature } from "./feature";
|
|
193
|
+
console.log(feature);
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
### Alternative 1: Test through public API (keep helper unexported)
|
|
197
|
+
|
|
198
|
+
```typescript
|
|
199
|
+
// src/order.ts
|
|
200
|
+
// GOOD: helper remains unexported and private to the module
|
|
201
|
+
function calculateTax(amount: number): number {
|
|
202
|
+
return amount * 0.2;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export function createOrder(amount: number): { total: number } {
|
|
206
|
+
return { total: amount + calculateTax(amount) };
|
|
207
|
+
}
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
```typescript
|
|
211
|
+
// src/order.spec.ts
|
|
212
|
+
import { createOrder } from "./order";
|
|
213
|
+
|
|
214
|
+
// GOOD: Unit test verifies the observable public API
|
|
215
|
+
test("creates order with tax included", () => {
|
|
216
|
+
expect(createOrder(100)).toEqual({ total: 120 });
|
|
217
|
+
});
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
### Alternative 2: Extract a separate helper module
|
|
221
|
+
|
|
222
|
+
```typescript
|
|
223
|
+
// src/tax-calculator.ts
|
|
224
|
+
// GOOD: Complex logic extracted to its own module and exported for production use
|
|
225
|
+
export function calculateTax(amount: number): number {
|
|
226
|
+
return amount * 0.2;
|
|
227
|
+
}
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
```typescript
|
|
231
|
+
// src/order.ts
|
|
232
|
+
import { calculateTax } from "./tax-calculator";
|
|
233
|
+
|
|
234
|
+
export function createOrder(amount: number): { total: number } {
|
|
235
|
+
return { total: amount + calculateTax(amount) };
|
|
236
|
+
}
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
```typescript
|
|
240
|
+
// src/tax-calculator.spec.ts
|
|
241
|
+
import { calculateTax } from "./tax-calculator";
|
|
242
|
+
|
|
243
|
+
test("calculates tax", () => {
|
|
244
|
+
expect(calculateTax(100)).toBe(20);
|
|
245
|
+
});
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
### Export exposed by a public API file
|
|
249
|
+
|
|
250
|
+
```typescript
|
|
251
|
+
// src/presets.ts
|
|
252
|
+
export const aaa = {};
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
```typescript
|
|
256
|
+
// src/infrastructure.ts
|
|
257
|
+
export { aaa } from "./presets";
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
```typescript
|
|
261
|
+
// src/index.ts
|
|
262
|
+
export { aaa } from "./infrastructure";
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
### Public API file declaration
|
|
266
|
+
|
|
267
|
+
```typescript
|
|
268
|
+
// src/index.ts
|
|
269
|
+
export const barrel = 1;
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
### Value export referenced through `typeof`
|
|
273
|
+
|
|
274
|
+
```typescript
|
|
275
|
+
// src/feature.ts
|
|
276
|
+
export const feature = { id: 1 } as const;
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
```typescript
|
|
280
|
+
// src/consumer.ts
|
|
281
|
+
import { feature } from "./feature";
|
|
282
|
+
export type FeatureShape = typeof feature;
|
|
283
|
+
```
|
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
# `codeperfect/no-unused-public-members`
|
|
2
|
+
|
|
3
|
+
## Summary
|
|
4
|
+
|
|
5
|
+
Disallow public methods, properties, and fields that are never used by production code, unless their host is exposed by the public API.
|
|
6
|
+
|
|
7
|
+
The rule checks direct members of:
|
|
8
|
+
|
|
9
|
+
- classes
|
|
10
|
+
- interfaces
|
|
11
|
+
- object-shaped type aliases
|
|
12
|
+
- const object literals
|
|
13
|
+
|
|
14
|
+
Private and protected class members are ignored. Nested object members, constructors, and index signatures are outside the rule's scope.
|
|
15
|
+
|
|
16
|
+
## Enabled by
|
|
17
|
+
|
|
18
|
+
- internal `codeperfect` plugin registry
|
|
19
|
+
- `architecture` preset
|
|
20
|
+
|
|
21
|
+
## Why this rule exists
|
|
22
|
+
|
|
23
|
+
Exported classes and types can stay in use while individual methods or properties become dead. `no-unused-exports` checks whether exported hosts are used; this rule checks whether their public members are used.
|
|
24
|
+
|
|
25
|
+
Hosts exposed by configured public API files are exempt because those members may be consumed by downstream package users.
|
|
26
|
+
|
|
27
|
+
## The Problem with Public Visibility for Testing Only
|
|
28
|
+
|
|
29
|
+
Making class methods, properties, or interface fields `public` solely so a unit test can reach them is an anti-pattern:
|
|
30
|
+
|
|
31
|
+
- **Breaks Encapsulation**: Exposing internal details pollutes the class or type contract and makes it unclear what parts are intended to be consumed by other parts of the application.
|
|
32
|
+
- **Ties Tests to Implementation**: If you test private methods directly by making them public, refactoring, restructuring, or renaming those internal methods will break your unit tests, even if the public behavior of the class remains unchanged.
|
|
33
|
+
- **Smell of Design Issues**: Needing to expose a private method often means the class is doing too much (violating the Single Responsibility Principle) and should be split into smaller, independent services, collaborator classes, or utility modules.
|
|
34
|
+
|
|
35
|
+
### Better Alternatives
|
|
36
|
+
|
|
37
|
+
- **Test Through Public Methods**: Write your tests against the legitimate public API of the class. If the public methods work correctly, internal helper methods and state transitions are verified by extension.
|
|
38
|
+
- **Extract a Separate Service or Helper**: If a private method contains complex logic that warrants dedicated unit testing, extract it into its own separate service, collaborator class, or helper module with its own contract and inject or call it in production code.
|
|
39
|
+
- **Keep Internal Members Private or Protected**: If a member is only used internally by the declaring class, declare it as `private` (or `#private`). The rule ignores private and protected class members.
|
|
40
|
+
|
|
41
|
+
## Options
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
type Options = {
|
|
45
|
+
publicApiFiles?: string[]; // default: ["**/src/index.ts", "**/__tests__/**/*.ts"]
|
|
46
|
+
testFilePatterns?: string[]; // default: ["**/*.{test,spec,e2e}.ts", "**/__tests__/**/*.ts"]
|
|
47
|
+
};
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
- `publicApiFiles` selects public API entries and their transitive exports, as in `no-unused-exports`. For the member rule, each eligible entry's exact parent directory also defines an analysis folder.
|
|
51
|
+
- `testFilePatterns` identifies files whose references do not satisfy production use.
|
|
52
|
+
|
|
53
|
+
By default, analysis is limited to production files matching `**/src/**/*.ts`; files matching `testFilePatterns` are excluded as both hosts and consumers. The rule requires TypeScript parser services with a project program; it does not report when those services are unavailable.
|
|
54
|
+
|
|
55
|
+
### Analysis folders
|
|
56
|
+
|
|
57
|
+
Only actual, non-test, non-declaration API files in the TypeScript program define
|
|
58
|
+
folders. A host's production consumers must be inside its own folder; references
|
|
59
|
+
in sibling packages do not count. Same-file uses count even when the host is
|
|
60
|
+
never imported. Public API exemptions still apply.
|
|
61
|
+
|
|
62
|
+
For example, API entries at `packages/a/src/index.ts` and
|
|
63
|
+
`packages/b/src/index.ts` create separate folders. An ordinary
|
|
64
|
+
`packages/a/src/feature/index.ts` barrel creates no boundary unless explicitly
|
|
65
|
+
selected by `publicApiFiles`. Multiple entries in one folder share that folder.
|
|
66
|
+
|
|
67
|
+
Distinct nested folders are configuration errors. An API entry at
|
|
68
|
+
`src/api/public.ts` covers only `src/api`, not all of `src`. If an eligible host
|
|
69
|
+
has no containing folder, the rule throws an actionable `publicApiFiles`
|
|
70
|
+
configuration error instead of searching elsewhere or reporting unsupported
|
|
71
|
+
unused-member findings. Paths are normalized using filesystem case sensitivity;
|
|
72
|
+
containment follows directory components, not string prefixes.
|
|
73
|
+
|
|
74
|
+
Effective per-file ESLint options remain isolated. This restriction does not
|
|
75
|
+
change the consumer scope of `no-unused-exports`, and neither rule requires the
|
|
76
|
+
other to be enabled. Use the existing `publicApiFiles` option; no additional
|
|
77
|
+
scope option or boundaries-plugin integration is required.
|
|
78
|
+
|
|
79
|
+
## Rule Details
|
|
80
|
+
|
|
81
|
+
A member is considered used when a production TypeScript file in its analysis folder contains a concrete reference to it. References in the declaring file count. References only in test files do not.
|
|
82
|
+
|
|
83
|
+
The rule resolves uses through explicitly implemented or inherited class and interface contracts, and through the declared type of a const object. Public API exposure is based on exported hosts reachable from `publicApiFiles`, including through barrels.
|
|
84
|
+
|
|
85
|
+
- Object spreads count as reads of the source object's own members, including interface fields.
|
|
86
|
+
- Passing a value to an external typed call counts for members accepted by its parameter contract, whether the argument is an inline literal or a variable. Excess properties and calls to local implementations do not automatically count.
|
|
87
|
+
- External string-index contracts also accept dictionary entries, including union-valued contracts such as request headers. Index-based handoffs require the forwarded value to carry the member's declaration; unrelated matching dictionaries and incompatible value types remain reportable.
|
|
88
|
+
- Standard-library `JSON.stringify(value)` counts as consumption of the value's own members. Calls with a replacer and shadowed serializers do not automatically count.
|
|
89
|
+
- JSON consumption follows nested data properties and array elements through union/intersection types, up to five nested data levels, with cycle detection. Shallow object spreads do not consume nested fields. Const values retain their own member identity when a type annotation changes their property symbols.
|
|
90
|
+
- Whole-object reads and dictionary handoffs follow resolved local factory return expressions when return annotations erase member identity. Local call arguments also count when the corresponding parameter is forwarded through object-rest/spread aliases and nested local calls to standard `JSON.stringify`; unrelated serialization in the same function does not count. Recursive calls terminate, and returns inside nested functions are not treated as factory results.
|
|
91
|
+
- Factory and forwarding searches stay within the host's eligible production files, including at recursive call steps. Calling a sibling implementation does not search that implementation's body; external type contracts remain usable.
|
|
92
|
+
- Values carried by an object property typed `unknown` count as used when handed to a declaration-only method contract, such as an opaque event publisher. The first opaque argument to a declaration-only `.json(...)` method and an opaque `body` property in a declaration-only `.request(...)` method count as serialized transport data; other unknown-typed method and function arguments do not count.
|
|
93
|
+
- Framework templates do not count as usage in the framework-neutral rule. Use the optional Angular adapter when Angular template expressions should count.
|
|
94
|
+
- Prototype methods and accessors are not exempted by spreading or serializing a class instance.
|
|
95
|
+
- Dynamic string-key reads count for the entries of the same const registry, including registries annotated with `Record<string, ...>`. A lookup on a different registry does not exempt its entries.
|
|
96
|
+
- Reads through smaller structural contracts count when the host is assignable to that contract, including lifecycle calls through an `OnDestroy`-style interface. Incompatible method signatures remain reportable.
|
|
97
|
+
- Object destructuring in `for...of` and `for await...of` loops counts as a read of the selected members, including renamed bindings and optional fields with defaults. Unselected members remain reportable.
|
|
98
|
+
- Members exposed through exported type aliases, union/intersection constituents, nested API properties, or exported API call/construct signatures are public API members even when their declaring interfaces are not exported directly. Signature inputs, return values, and array elements are included.
|
|
99
|
+
|
|
100
|
+
Class members without an explicit visibility modifier are treated as public. Private, protected, and `#private` members are ignored.
|
|
101
|
+
|
|
102
|
+
## Performance
|
|
103
|
+
|
|
104
|
+
The rule partitions eligible source files by API folder before indexing production
|
|
105
|
+
reference symbols and candidate operations. Usage indexes are isolated by
|
|
106
|
+
TypeScript program, folder, and test-pattern configuration. Direct references use symbol
|
|
107
|
+
and declaration-site lookups; only unresolved members require structural,
|
|
108
|
+
whole-object, or local-flow checks. Heritage declarations are also indexed.
|
|
109
|
+
|
|
110
|
+
For each member name, eager reference resolution follows observed imports back
|
|
111
|
+
from its declaration files, including transitive barrels and cycles within the
|
|
112
|
+
owner. Same-file declarations remain eligible without imports. Global scripts
|
|
113
|
+
and augmentations receive conservative treatment. Name-matching references
|
|
114
|
+
without a known dependency path are deferred, not discarded: structural reads
|
|
115
|
+
and declaration identity can still establish usage when needed. Whole-object
|
|
116
|
+
operations and framework adapters remain eligible independently of imports.
|
|
117
|
+
Folder names and forbidden architecture directions never hide actual consumers.
|
|
118
|
+
|
|
119
|
+
Operation classification is lazy: files whose candidates already have reference
|
|
120
|
+
or adapter uses do not trigger spread, serializer, computed-key, or external-call
|
|
121
|
+
type/signature analysis. Remaining candidates are checked in batches with
|
|
122
|
+
operations as the outer loop, dropping consumed members from later operations.
|
|
123
|
+
Literal computed keys skip unrelated names without resolving their key types.
|
|
124
|
+
Completed usage answers are cached by member declaration and analysis scope;
|
|
125
|
+
negative results are stored once per candidate, not once per candidate-operation
|
|
126
|
+
pair. Node/contextual types and property lists are reused within the current
|
|
127
|
+
TypeScript checker, including absent contextual types. Declaration identity,
|
|
128
|
+
shallow versus nested consumption, and member-specific contract compatibility
|
|
129
|
+
still require their existing checks.
|
|
130
|
+
|
|
131
|
+
Local forwarding and factory returns use separate, completed summaries.
|
|
132
|
+
Parameter forwarding is summarized independently of candidate members, with
|
|
133
|
+
distinct results for standard JSON serialization and opaque request bodies.
|
|
134
|
+
Factory results retain candidate declaration identity. Mutually recursive flow
|
|
135
|
+
states are grouped and completed from callees to callers: a reachable consumption
|
|
136
|
+
exit makes the group used, and a closed group without an exit completes unused.
|
|
137
|
+
Both results are reusable; encountering an in-progress cycle is not a completed
|
|
138
|
+
negative answer. Function calls and returns are cataloged once per immutable
|
|
139
|
+
syntax snapshot, while semantic answers remain owner/configuration-specific.
|
|
140
|
+
Factory returns inside nested functions remain excluded unless that function is
|
|
141
|
+
itself reached as a local factory. Completed groups release their temporary
|
|
142
|
+
graph records immediately, and remaining traversal queues leave scope when the
|
|
143
|
+
query returns. TypeScript-owned trees and types remain owned by the active
|
|
144
|
+
program.
|
|
145
|
+
|
|
146
|
+
Syntax catalogs and native reference-symbol lookups are shared with
|
|
147
|
+
`no-unused-exports`, regardless of execution order. Catalogs are collected lazily
|
|
148
|
+
for requested source files; the member rule does not build the export rule's
|
|
149
|
+
whole-program usage index. Shared dependency edges include imports, re-exports,
|
|
150
|
+
import types, and triple-slash references, with conservative global and
|
|
151
|
+
augmentation roots. File classifications and member ownership stay rule-local.
|
|
152
|
+
|
|
153
|
+
Direct public API exports are checked without expanding their types. Nested API
|
|
154
|
+
types are searched only for members without production use, stopping when the
|
|
155
|
+
member is found rather than expanding every export. Only candidate answers are
|
|
156
|
+
retained, not a catalog of every reachable type member. Completed positive and
|
|
157
|
+
negative type queries are reused by program, candidate, API location, and
|
|
158
|
+
remaining depth. Before expanding an export, a
|
|
159
|
+
cached source-dependency graph checks whether its API file and declaring file
|
|
160
|
+
can reach the member's declaration. Imports, re-exports, import types,
|
|
161
|
+
triple-slash references, and global/module augmentations are included
|
|
162
|
+
conservatively.
|
|
163
|
+
|
|
164
|
+
Nested queries check member declaration identity before resolving child types.
|
|
165
|
+
Index elements and call/construct signature types are visited lazily, then
|
|
166
|
+
properties; finding the candidate stops later branches. A conservative
|
|
167
|
+
declaration-reference graph and visible generic arguments can exclude unrelated
|
|
168
|
+
primitive, library, and closed recursive builder branches without instantiating
|
|
169
|
+
their children. Unavailable substitutions, captured parameters, and potentially
|
|
170
|
+
relevant ambient augmentations fall back to semantic traversal rather than
|
|
171
|
+
proving absence. The existing six-level nested API type budget is unchanged;
|
|
172
|
+
union/intersection edges do not consume a level. Revisiting a type with more
|
|
173
|
+
remaining depth is allowed, and incomplete queries are never cached as absent.
|
|
174
|
+
|
|
175
|
+
Option-dependent caches weakly retain values for at most one TypeScript program;
|
|
176
|
+
switching programs evicts the previous program's entries even if it is still
|
|
177
|
+
alive. Each named cache retains only its current configuration. Production usage
|
|
178
|
+
indexes additionally follow their scope's lifetime, not a history of folder
|
|
179
|
+
names. Public API patterns, test patterns, and project roots remain isolated.
|
|
180
|
+
Reusable syntax facts follow immutable syntax snapshots; semantic facts follow
|
|
181
|
+
their program, checker, or analysis scope. See
|
|
182
|
+
[cache ownership](../design/architecture.md#cache-ownership) for the lifetime
|
|
183
|
+
boundaries. Releasing plugin-owned caches does not release trees or types still
|
|
184
|
+
held by TypeScript, parser services, or callers.
|
|
185
|
+
|
|
186
|
+
Framework adapter results are not shared with the framework-neutral rule.
|
|
187
|
+
Angular component metadata is isolated by checker, and parsed templates follow
|
|
188
|
+
their metadata snapshot. External templates are read once per component/checker
|
|
189
|
+
snapshot; replacing the checker rereads them even when its class syntax is reused.
|
|
190
|
+
|
|
191
|
+
Large projects with many unused members and complex whole-object handoffs can
|
|
192
|
+
still require more analysis than projects dominated by direct references.
|
|
193
|
+
Increasing Node's heap limit does not remove this analysis cost.
|
|
194
|
+
|
|
195
|
+
The rule's usage index only scans eligible production TypeScript files inside the
|
|
196
|
+
declaring host's API folder. Angular template adapters receive the same scoped
|
|
197
|
+
source files. The TypeScript parser still constructs the full configured
|
|
198
|
+
program; reducing that baseline memory requires smaller typed-linting projects
|
|
199
|
+
or TypeScript project references.
|
|
200
|
+
|
|
201
|
+
See the [measured performance and limits](../design/architecture.md#measured-performance)
|
|
202
|
+
for the reference workloads, default/constrained-heap results, export-only
|
|
203
|
+
controls, and diagnostic comparisons. Those observations are not a runtime or
|
|
204
|
+
memory guarantee for every project.
|
|
205
|
+
|
|
206
|
+
## Angular adapter
|
|
207
|
+
|
|
208
|
+
Angular analysis is opt-in and is not loaded by the default rule. The `@codeperfect/eslint-config` Angular profile enables the adapter automatically for its scoped files when `angular-eslint` is selected. The adapter recognizes known lifecycle hook methods on Angular-managed classes, including concrete methods inherited from base classes, when their decorators are imported from `@angular/core`; implementing the corresponding lifecycle interface is optional. For component templates, it recognizes a `Component` decorator imported from `@angular/core`, parses static inline and external templates with the Angular compiler, respects template-local variables and `$event`, and resolves member reads through the component's TypeScript types.
|
|
209
|
+
|
|
210
|
+
When configuring the plugin directly, register the Angular entry point under a separate namespace and replace the framework-neutral rule for the Angular files:
|
|
211
|
+
|
|
212
|
+
```typescript
|
|
213
|
+
import { codeperfectPlugin } from "@codeperfect/eslint-plugin";
|
|
214
|
+
import { codeperfectAngularPlugin } from "@codeperfect/eslint-plugin/angular";
|
|
215
|
+
|
|
216
|
+
export default [
|
|
217
|
+
{
|
|
218
|
+
files: ["apps/front/**/*.ts"],
|
|
219
|
+
plugins: {
|
|
220
|
+
codeperfect: codeperfectPlugin,
|
|
221
|
+
"codeperfect-angular": codeperfectAngularPlugin,
|
|
222
|
+
},
|
|
223
|
+
rules: {
|
|
224
|
+
"codeperfect/no-unused-public-members": "off",
|
|
225
|
+
"codeperfect-angular/no-unused-public-members": "error",
|
|
226
|
+
},
|
|
227
|
+
},
|
|
228
|
+
];
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
The adapter uses the project's `@angular/compiler` installation and requires version 22.2 or newer. Projects that enable it must provide that optional peer dependency.
|
|
232
|
+
|
|
233
|
+
## Invalid
|
|
234
|
+
|
|
235
|
+
### Unused class method and field
|
|
236
|
+
|
|
237
|
+
```typescript
|
|
238
|
+
// src/service.ts
|
|
239
|
+
export class Service {
|
|
240
|
+
unusedMethod(): void {}
|
|
241
|
+
|
|
242
|
+
unusedValue = 1;
|
|
243
|
+
}
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
### Unused interface or type members
|
|
247
|
+
|
|
248
|
+
```typescript
|
|
249
|
+
// src/model.ts
|
|
250
|
+
export interface Model {
|
|
251
|
+
used: string;
|
|
252
|
+
unused: number;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
export type Options = { enabled: boolean; legacy: boolean };
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
### Test-only use
|
|
259
|
+
|
|
260
|
+
```typescript
|
|
261
|
+
// src/service.ts
|
|
262
|
+
export class Service {
|
|
263
|
+
// BAD: calculateDiscount is internal logic made public solely for tests
|
|
264
|
+
calculateDiscount(price: number): number {
|
|
265
|
+
return price * 0.1;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
checkout(price: number): number {
|
|
269
|
+
return price - this.calculateDiscount(price);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
```typescript
|
|
275
|
+
// src/service.spec.ts
|
|
276
|
+
import { Service } from "./service";
|
|
277
|
+
|
|
278
|
+
// Reported: Member 'calculateDiscount' of 'Service' is never used by production code.
|
|
279
|
+
// Test through public methods, extract to a separate service/helper, or make it private.
|
|
280
|
+
test("calculates discount", () => {
|
|
281
|
+
expect(new Service().calculateDiscount(100)).toBe(10);
|
|
282
|
+
});
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
## Valid
|
|
286
|
+
|
|
287
|
+
### Production use
|
|
288
|
+
|
|
289
|
+
```typescript
|
|
290
|
+
// src/service.ts
|
|
291
|
+
export class Service {
|
|
292
|
+
run(): void {}
|
|
293
|
+
}
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
```typescript
|
|
297
|
+
// src/consumer.ts
|
|
298
|
+
import { Service } from "./service";
|
|
299
|
+
|
|
300
|
+
new Service().run();
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
### Alternative 1: Test through public methods (keep member private)
|
|
304
|
+
|
|
305
|
+
```typescript
|
|
306
|
+
// src/service.ts
|
|
307
|
+
export class Service {
|
|
308
|
+
// GOOD: internal helper remains private
|
|
309
|
+
private calculateDiscount(price: number): number {
|
|
310
|
+
return price * 0.1;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
checkout(price: number): number {
|
|
314
|
+
return price - this.calculateDiscount(price);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
```typescript
|
|
320
|
+
// src/service.spec.ts
|
|
321
|
+
import { Service } from "./service";
|
|
322
|
+
|
|
323
|
+
// GOOD: Tests verify the observable public method
|
|
324
|
+
test("checks out with discount applied", () => {
|
|
325
|
+
expect(new Service().checkout(100)).toBe(90);
|
|
326
|
+
});
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
### Alternative 2: Extract a separate collaborator service
|
|
330
|
+
|
|
331
|
+
```typescript
|
|
332
|
+
// src/discount-calculator.ts
|
|
333
|
+
export class DiscountCalculator {
|
|
334
|
+
calculateDiscount(price: number): number {
|
|
335
|
+
return price * 0.1;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
```typescript
|
|
341
|
+
// src/service.ts
|
|
342
|
+
import { DiscountCalculator } from "./discount-calculator";
|
|
343
|
+
|
|
344
|
+
export class Service {
|
|
345
|
+
constructor(private readonly discounts = new DiscountCalculator()) {}
|
|
346
|
+
|
|
347
|
+
checkout(price: number): number {
|
|
348
|
+
return price - this.discounts.calculateDiscount(price);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
```typescript
|
|
354
|
+
// src/discount-calculator.spec.ts
|
|
355
|
+
import { DiscountCalculator } from "./discount-calculator";
|
|
356
|
+
|
|
357
|
+
test("calculates discount", () => {
|
|
358
|
+
expect(new DiscountCalculator().calculateDiscount(100)).toBe(10);
|
|
359
|
+
});
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
### Public API host
|
|
363
|
+
|
|
364
|
+
```typescript
|
|
365
|
+
// src/service.ts
|
|
366
|
+
export class Service {
|
|
367
|
+
downstreamOnly(): void {}
|
|
368
|
+
}
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
```typescript
|
|
372
|
+
// src/index.ts
|
|
373
|
+
export { Service } from "./service";
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
### Use through an implemented contract
|
|
377
|
+
|
|
378
|
+
```typescript
|
|
379
|
+
interface Runner {
|
|
380
|
+
run(): void;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
class Service implements Runner {
|
|
384
|
+
run(): void {}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
const runner: Runner = new Service();
|
|
388
|
+
runner.run();
|
|
389
|
+
```
|