@terra-symposium/hygiene 0.0.0-stage → 2.4.4
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 +70 -2
- package/dist/accessibility.d.ts +39 -0
- package/dist/accessibility.d.ts.map +1 -0
- package/dist/accessibility.js +524 -0
- package/dist/accessibility.js.map +1 -0
- package/dist/app/workspace-test-runner.d.ts +6 -0
- package/dist/app/workspace-test-runner.d.ts.map +1 -0
- package/dist/app/workspace-test-runner.js +39 -0
- package/dist/app/workspace-test-runner.js.map +1 -0
- package/dist/capabilities/core/analysis.d.ts +22 -0
- package/dist/capabilities/core/analysis.d.ts.map +1 -0
- package/dist/capabilities/core/analysis.js +44 -0
- package/dist/capabilities/core/analysis.js.map +1 -0
- package/dist/capabilities/core/cli.d.ts +19 -0
- package/dist/capabilities/core/cli.d.ts.map +1 -0
- package/dist/capabilities/core/cli.js +58 -0
- package/dist/capabilities/core/cli.js.map +1 -0
- package/dist/capabilities/core/config.d.ts +49 -0
- package/dist/capabilities/core/config.d.ts.map +1 -0
- package/dist/capabilities/core/config.js +88 -0
- package/dist/capabilities/core/config.js.map +1 -0
- package/dist/capabilities/core/index.d.ts +11 -0
- package/dist/capabilities/core/index.d.ts.map +1 -0
- package/dist/capabilities/core/index.js +11 -0
- package/dist/capabilities/core/index.js.map +1 -0
- package/dist/capabilities/core/lib/files.d.ts +55 -0
- package/dist/capabilities/core/lib/files.d.ts.map +1 -0
- package/dist/capabilities/core/lib/files.js +287 -0
- package/dist/capabilities/core/lib/files.js.map +1 -0
- package/dist/capabilities/core/lib/progress.d.ts +25 -0
- package/dist/capabilities/core/lib/progress.d.ts.map +1 -0
- package/dist/capabilities/core/lib/progress.js +40 -0
- package/dist/capabilities/core/lib/progress.js.map +1 -0
- package/dist/capabilities/core/lib/source-kind.d.ts +14 -0
- package/dist/capabilities/core/lib/source-kind.d.ts.map +1 -0
- package/dist/capabilities/core/lib/source-kind.js +29 -0
- package/dist/capabilities/core/lib/source-kind.js.map +1 -0
- package/dist/capabilities/core/types.d.ts +156 -0
- package/dist/capabilities/core/types.d.ts.map +1 -0
- package/dist/capabilities/core/types.js +35 -0
- package/dist/capabilities/core/types.js.map +1 -0
- package/dist/capabilities/documentation/cli.d.ts +3 -0
- package/dist/capabilities/documentation/cli.d.ts.map +1 -0
- package/dist/capabilities/documentation/cli.js +14 -0
- package/dist/capabilities/documentation/cli.js.map +1 -0
- package/dist/capabilities/documentation/index.d.ts +5 -0
- package/dist/capabilities/documentation/index.d.ts.map +1 -0
- package/dist/capabilities/documentation/index.js +5 -0
- package/dist/capabilities/documentation/index.js.map +1 -0
- package/dist/capabilities/documentation/lib/documentation.d.ts +108 -0
- package/dist/capabilities/documentation/lib/documentation.d.ts.map +1 -0
- package/dist/capabilities/documentation/lib/documentation.js +1007 -0
- package/dist/capabilities/documentation/lib/documentation.js.map +1 -0
- package/dist/capabilities/formatting/cli.d.ts +3 -0
- package/dist/capabilities/formatting/cli.d.ts.map +1 -0
- package/dist/capabilities/formatting/cli.js +15 -0
- package/dist/capabilities/formatting/cli.js.map +1 -0
- package/dist/capabilities/formatting/index.d.ts +8 -0
- package/dist/capabilities/formatting/index.d.ts.map +1 -0
- package/dist/capabilities/formatting/index.js +8 -0
- package/dist/capabilities/formatting/index.js.map +1 -0
- package/dist/capabilities/formatting/lib/collapse.d.ts +13 -0
- package/dist/capabilities/formatting/lib/collapse.d.ts.map +1 -0
- package/dist/capabilities/formatting/lib/collapse.js +179 -0
- package/dist/capabilities/formatting/lib/collapse.js.map +1 -0
- package/dist/capabilities/formatting/lib/inline-if.d.ts +13 -0
- package/dist/capabilities/formatting/lib/inline-if.d.ts.map +1 -0
- package/dist/capabilities/formatting/lib/inline-if.js +49 -0
- package/dist/capabilities/formatting/lib/inline-if.js.map +1 -0
- package/dist/capabilities/formatting/lib/run-formatting.d.ts +14 -0
- package/dist/capabilities/formatting/lib/run-formatting.d.ts.map +1 -0
- package/dist/capabilities/formatting/lib/run-formatting.js +134 -0
- package/dist/capabilities/formatting/lib/run-formatting.js.map +1 -0
- package/dist/capabilities/imports/cli.d.ts +3 -0
- package/dist/capabilities/imports/cli.d.ts.map +1 -0
- package/dist/capabilities/imports/cli.js +9 -0
- package/dist/capabilities/imports/cli.js.map +1 -0
- package/dist/capabilities/imports/index.d.ts +6 -0
- package/dist/capabilities/imports/index.d.ts.map +1 -0
- package/dist/capabilities/imports/index.js +6 -0
- package/dist/capabilities/imports/index.js.map +1 -0
- package/dist/capabilities/imports/lib/imports.d.ts +19 -0
- package/dist/capabilities/imports/lib/imports.d.ts.map +1 -0
- package/dist/capabilities/imports/lib/imports.js +88 -0
- package/dist/capabilities/imports/lib/imports.js.map +1 -0
- package/dist/capabilities/languages/cli.d.ts +3 -0
- package/dist/capabilities/languages/cli.d.ts.map +1 -0
- package/dist/capabilities/languages/cli.js +9 -0
- package/dist/capabilities/languages/cli.js.map +1 -0
- package/dist/capabilities/languages/index.d.ts +6 -0
- package/dist/capabilities/languages/index.d.ts.map +1 -0
- package/dist/capabilities/languages/index.js +6 -0
- package/dist/capabilities/languages/index.js.map +1 -0
- package/dist/capabilities/languages/lib/language-profiles.d.ts +110 -0
- package/dist/capabilities/languages/lib/language-profiles.d.ts.map +1 -0
- package/dist/capabilities/languages/lib/language-profiles.js +509 -0
- package/dist/capabilities/languages/lib/language-profiles.js.map +1 -0
- package/dist/capabilities/languages/lib/run-language-checks.d.ts +12 -0
- package/dist/capabilities/languages/lib/run-language-checks.d.ts.map +1 -0
- package/dist/capabilities/languages/lib/run-language-checks.js +109 -0
- package/dist/capabilities/languages/lib/run-language-checks.js.map +1 -0
- package/dist/capabilities/naming/cli.d.ts +3 -0
- package/dist/capabilities/naming/cli.d.ts.map +1 -0
- package/dist/capabilities/naming/cli.js +15 -0
- package/dist/capabilities/naming/cli.js.map +1 -0
- package/dist/capabilities/naming/index.d.ts +5 -0
- package/dist/capabilities/naming/index.d.ts.map +1 -0
- package/dist/capabilities/naming/index.js +5 -0
- package/dist/capabilities/naming/index.js.map +1 -0
- package/dist/capabilities/naming/lib/naming.d.ts +169 -0
- package/dist/capabilities/naming/lib/naming.d.ts.map +1 -0
- package/dist/capabilities/naming/lib/naming.js +908 -0
- package/dist/capabilities/naming/lib/naming.js.map +1 -0
- package/dist/capabilities/patterns/cli.d.ts +3 -0
- package/dist/capabilities/patterns/cli.d.ts.map +1 -0
- package/dist/capabilities/patterns/cli.js +30 -0
- package/dist/capabilities/patterns/cli.js.map +1 -0
- package/dist/capabilities/patterns/index.d.ts +5 -0
- package/dist/capabilities/patterns/index.d.ts.map +1 -0
- package/dist/capabilities/patterns/index.js +5 -0
- package/dist/capabilities/patterns/index.js.map +1 -0
- package/dist/capabilities/patterns/lib/patterns.d.ts +102 -0
- package/dist/capabilities/patterns/lib/patterns.d.ts.map +1 -0
- package/dist/capabilities/patterns/lib/patterns.js +1584 -0
- package/dist/capabilities/patterns/lib/patterns.js.map +1 -0
- package/dist/capabilities/refactoring/cli.d.ts +3 -0
- package/dist/capabilities/refactoring/cli.d.ts.map +1 -0
- package/dist/capabilities/refactoring/cli.js +15 -0
- package/dist/capabilities/refactoring/cli.js.map +1 -0
- package/dist/capabilities/refactoring/index.d.ts +6 -0
- package/dist/capabilities/refactoring/index.d.ts.map +1 -0
- package/dist/capabilities/refactoring/index.js +6 -0
- package/dist/capabilities/refactoring/index.js.map +1 -0
- package/dist/capabilities/refactoring/lib/object-forwarding.d.ts +42 -0
- package/dist/capabilities/refactoring/lib/object-forwarding.d.ts.map +1 -0
- package/dist/capabilities/refactoring/lib/object-forwarding.js +166 -0
- package/dist/capabilities/refactoring/lib/object-forwarding.js.map +1 -0
- package/dist/capabilities/refactoring/lib/single-use.d.ts +61 -0
- package/dist/capabilities/refactoring/lib/single-use.d.ts.map +1 -0
- package/dist/capabilities/refactoring/lib/single-use.js +1791 -0
- package/dist/capabilities/refactoring/lib/single-use.js.map +1 -0
- package/dist/capabilities/reports/cli.d.ts +3 -0
- package/dist/capabilities/reports/cli.d.ts.map +1 -0
- package/dist/capabilities/reports/cli.js +34 -0
- package/dist/capabilities/reports/cli.js.map +1 -0
- package/dist/capabilities/reports/index.d.ts +10 -0
- package/dist/capabilities/reports/index.d.ts.map +1 -0
- package/dist/capabilities/reports/index.js +10 -0
- package/dist/capabilities/reports/index.js.map +1 -0
- package/dist/capabilities/reports/lib/github-report.d.ts +94 -0
- package/dist/capabilities/reports/lib/github-report.d.ts.map +1 -0
- package/dist/capabilities/reports/lib/github-report.js +499 -0
- package/dist/capabilities/reports/lib/github-report.js.map +1 -0
- package/dist/capabilities/reports/lib/history-report.d.ts +58 -0
- package/dist/capabilities/reports/lib/history-report.d.ts.map +1 -0
- package/dist/capabilities/reports/lib/history-report.js +300 -0
- package/dist/capabilities/reports/lib/history-report.js.map +1 -0
- package/dist/capabilities/reports/lib/node-reporter.d.ts +79 -0
- package/dist/capabilities/reports/lib/node-reporter.d.ts.map +1 -0
- package/dist/capabilities/reports/lib/node-reporter.js +244 -0
- package/dist/capabilities/reports/lib/node-reporter.js.map +1 -0
- package/dist/capabilities/reports/lib/playwright-reporter.d.ts +100 -0
- package/dist/capabilities/reports/lib/playwright-reporter.d.ts.map +1 -0
- package/dist/capabilities/reports/lib/playwright-reporter.js +122 -0
- package/dist/capabilities/reports/lib/playwright-reporter.js.map +1 -0
- package/dist/capabilities/security/cli.d.ts +3 -0
- package/dist/capabilities/security/cli.d.ts.map +1 -0
- package/dist/capabilities/security/cli.js +9 -0
- package/dist/capabilities/security/cli.js.map +1 -0
- package/dist/capabilities/security/index.d.ts +5 -0
- package/dist/capabilities/security/index.d.ts.map +1 -0
- package/dist/capabilities/security/index.js +5 -0
- package/dist/capabilities/security/index.js.map +1 -0
- package/dist/capabilities/security/lib/security.d.ts +19 -0
- package/dist/capabilities/security/lib/security.d.ts.map +1 -0
- package/dist/capabilities/security/lib/security.js +115 -0
- package/dist/capabilities/security/lib/security.js.map +1 -0
- package/dist/capabilities/ui/cli.d.ts +3 -0
- package/dist/capabilities/ui/cli.d.ts.map +1 -0
- package/dist/capabilities/ui/cli.js +22 -0
- package/dist/capabilities/ui/cli.js.map +1 -0
- package/dist/capabilities/ui/index.d.ts +8 -0
- package/dist/capabilities/ui/index.d.ts.map +1 -0
- package/dist/capabilities/ui/index.js +8 -0
- package/dist/capabilities/ui/index.js.map +1 -0
- package/dist/capabilities/ui/lib/accessibility.d.ts +37 -0
- package/dist/capabilities/ui/lib/accessibility.d.ts.map +1 -0
- package/dist/capabilities/ui/lib/accessibility.js +676 -0
- package/dist/capabilities/ui/lib/accessibility.js.map +1 -0
- package/dist/capabilities/ui/lib/display-name.d.ts +42 -0
- package/dist/capabilities/ui/lib/display-name.d.ts.map +1 -0
- package/dist/capabilities/ui/lib/display-name.js +277 -0
- package/dist/capabilities/ui/lib/display-name.js.map +1 -0
- package/dist/capabilities/ui/lib/error-boundaries.d.ts +30 -0
- package/dist/capabilities/ui/lib/error-boundaries.d.ts.map +1 -0
- package/dist/capabilities/ui/lib/error-boundaries.js +401 -0
- package/dist/capabilities/ui/lib/error-boundaries.js.map +1 -0
- package/dist/capabilities/ui/lib/ui-recommendations.d.ts +45 -0
- package/dist/capabilities/ui/lib/ui-recommendations.d.ts.map +1 -0
- package/dist/capabilities/ui/lib/ui-recommendations.js +183 -0
- package/dist/capabilities/ui/lib/ui-recommendations.js.map +1 -0
- package/dist/display-name.d.ts +44 -0
- package/dist/display-name.d.ts.map +1 -0
- package/dist/display-name.js +256 -0
- package/dist/display-name.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/accessibility.d.ts +5 -0
- package/dist/lib/accessibility.d.ts.map +1 -0
- package/dist/lib/accessibility.js +5 -0
- package/dist/lib/accessibility.js.map +1 -0
- package/dist/lib/collapse.d.ts +5 -0
- package/dist/lib/collapse.d.ts.map +1 -0
- package/dist/lib/collapse.js +5 -0
- package/dist/lib/collapse.js.map +1 -0
- package/dist/lib/dev.d.ts +3 -0
- package/dist/lib/dev.d.ts.map +1 -0
- package/dist/lib/dev.js +126 -0
- package/dist/lib/dev.js.map +1 -0
- package/dist/lib/display-name.d.ts +5 -0
- package/dist/lib/display-name.d.ts.map +1 -0
- package/dist/lib/display-name.js +5 -0
- package/dist/lib/display-name.js.map +1 -0
- package/dist/lib/documentation.d.ts +5 -0
- package/dist/lib/documentation.d.ts.map +1 -0
- package/dist/lib/documentation.js +5 -0
- package/dist/lib/documentation.js.map +1 -0
- package/dist/lib/error-boundaries-astro.d.ts +6 -0
- package/dist/lib/error-boundaries-astro.d.ts.map +1 -0
- package/dist/lib/error-boundaries-astro.js +6 -0
- package/dist/lib/error-boundaries-astro.js.map +1 -0
- package/dist/lib/error-boundaries-react.d.ts +6 -0
- package/dist/lib/error-boundaries-react.d.ts.map +1 -0
- package/dist/lib/error-boundaries-react.js +6 -0
- package/dist/lib/error-boundaries-react.js.map +1 -0
- package/dist/lib/error-boundaries.d.ts +5 -0
- package/dist/lib/error-boundaries.d.ts.map +1 -0
- package/dist/lib/error-boundaries.js +5 -0
- package/dist/lib/error-boundaries.js.map +1 -0
- package/dist/lib/eslint-api.d.ts +13 -0
- package/dist/lib/eslint-api.d.ts.map +1 -0
- package/dist/lib/eslint-api.js +24 -0
- package/dist/lib/eslint-api.js.map +1 -0
- package/dist/lib/files.d.ts +5 -0
- package/dist/lib/files.d.ts.map +1 -0
- package/dist/lib/files.js +5 -0
- package/dist/lib/files.js.map +1 -0
- package/dist/lib/formatting.d.ts +5 -0
- package/dist/lib/formatting.d.ts.map +1 -0
- package/dist/lib/formatting.js +5 -0
- package/dist/lib/formatting.js.map +1 -0
- package/dist/lib/github-report.d.ts +5 -0
- package/dist/lib/github-report.d.ts.map +1 -0
- package/dist/lib/github-report.js +5 -0
- package/dist/lib/github-report.js.map +1 -0
- package/dist/lib/history-report.d.ts +5 -0
- package/dist/lib/history-report.d.ts.map +1 -0
- package/dist/lib/history-report.js +5 -0
- package/dist/lib/history-report.js.map +1 -0
- package/dist/lib/imports.d.ts +5 -0
- package/dist/lib/imports.d.ts.map +1 -0
- package/dist/lib/imports.js +5 -0
- package/dist/lib/imports.js.map +1 -0
- package/dist/lib/index.d.ts +20 -0
- package/dist/lib/index.d.ts.map +1 -0
- package/dist/lib/index.js +20 -0
- package/dist/lib/index.js.map +1 -0
- package/dist/lib/inline-if.d.ts +5 -0
- package/dist/lib/inline-if.d.ts.map +1 -0
- package/dist/lib/inline-if.js +5 -0
- package/dist/lib/inline-if.js.map +1 -0
- package/dist/lib/language-checks.d.ts +5 -0
- package/dist/lib/language-checks.d.ts.map +1 -0
- package/dist/lib/language-checks.js +5 -0
- package/dist/lib/language-checks.js.map +1 -0
- package/dist/lib/language-profiles.d.ts +5 -0
- package/dist/lib/language-profiles.d.ts.map +1 -0
- package/dist/lib/language-profiles.js +5 -0
- package/dist/lib/language-profiles.js.map +1 -0
- package/dist/lib/naming.d.ts +5 -0
- package/dist/lib/naming.d.ts.map +1 -0
- package/dist/lib/naming.js +5 -0
- package/dist/lib/naming.js.map +1 -0
- package/dist/lib/node-reporter.d.ts +6 -0
- package/dist/lib/node-reporter.d.ts.map +1 -0
- package/dist/lib/node-reporter.js +6 -0
- package/dist/lib/node-reporter.js.map +1 -0
- package/dist/lib/object-forwarding.d.ts +5 -0
- package/dist/lib/object-forwarding.d.ts.map +1 -0
- package/dist/lib/object-forwarding.js +5 -0
- package/dist/lib/object-forwarding.js.map +1 -0
- package/dist/lib/patterns.d.ts +5 -0
- package/dist/lib/patterns.d.ts.map +1 -0
- package/dist/lib/patterns.js +5 -0
- package/dist/lib/patterns.js.map +1 -0
- package/dist/lib/playwright-reporter.d.ts +6 -0
- package/dist/lib/playwright-reporter.d.ts.map +1 -0
- package/dist/lib/playwright-reporter.js +6 -0
- package/dist/lib/playwright-reporter.js.map +1 -0
- package/dist/lib/port.d.ts +21 -0
- package/dist/lib/port.d.ts.map +1 -0
- package/dist/lib/port.js +81 -0
- package/dist/lib/port.js.map +1 -0
- package/dist/lib/progress.d.ts +5 -0
- package/dist/lib/progress.d.ts.map +1 -0
- package/dist/lib/progress.js +5 -0
- package/dist/lib/progress.js.map +1 -0
- package/dist/lib/reports.d.ts +5 -0
- package/dist/lib/reports.d.ts.map +1 -0
- package/dist/lib/reports.js +5 -0
- package/dist/lib/reports.js.map +1 -0
- package/dist/lib/runner.d.ts +150 -0
- package/dist/lib/runner.d.ts.map +1 -0
- package/dist/lib/runner.js +4522 -0
- package/dist/lib/runner.js.map +1 -0
- package/dist/lib/security.d.ts +5 -0
- package/dist/lib/security.d.ts.map +1 -0
- package/dist/lib/security.js +5 -0
- package/dist/lib/security.js.map +1 -0
- package/dist/lib/single-use.d.ts +6 -0
- package/dist/lib/single-use.d.ts.map +1 -0
- package/dist/lib/single-use.js +5 -0
- package/dist/lib/single-use.js.map +1 -0
- package/dist/lib/source-kind.d.ts +5 -0
- package/dist/lib/source-kind.d.ts.map +1 -0
- package/dist/lib/source-kind.js +5 -0
- package/dist/lib/source-kind.js.map +1 -0
- package/dist/lib/spinner-worker.d.ts +6 -0
- package/dist/lib/spinner-worker.d.ts.map +1 -0
- package/dist/lib/spinner-worker.js +33 -0
- package/dist/lib/spinner-worker.js.map +1 -0
- package/dist/lib/ui-quality.d.ts +5 -0
- package/dist/lib/ui-quality.d.ts.map +1 -0
- package/dist/lib/ui-quality.js +5 -0
- package/dist/lib/ui-quality.js.map +1 -0
- package/dist/lib/ui-recommendations.d.ts +5 -0
- package/dist/lib/ui-recommendations.d.ts.map +1 -0
- package/dist/lib/ui-recommendations.js +5 -0
- package/dist/lib/ui-recommendations.js.map +1 -0
- package/dist/spinner-worker.d.ts +6 -0
- package/dist/spinner-worker.d.ts.map +1 -0
- package/dist/spinner-worker.js +39 -0
- package/dist/spinner-worker.js.map +1 -0
- package/package.json +160 -5
- package/quality-configs/checkstyle.xml +29 -0
- package/quality-configs/pmd-rules.xml +11 -0
- package/quality.schema.json +392 -0
|
@@ -0,0 +1,908 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* packages/hygiene/src/capabilities/naming/lib/naming.ts
|
|
3
|
+
* Renames identifiers according to the configured replacement rules.
|
|
4
|
+
*/
|
|
5
|
+
/*//////////////////////////////////////////////////
|
|
6
|
+
IMPORTS
|
|
7
|
+
//////////////////////////////////////////////////*/
|
|
8
|
+
// API
|
|
9
|
+
import ts from 'typescript';
|
|
10
|
+
import { displayPath, hasGeneratedSourceHeader, srcFiles } from '@terra-symposium/hygiene/core';
|
|
11
|
+
import { readFileSync, writeFileSync } from 'node:fs';
|
|
12
|
+
import { resolve } from 'node:path';
|
|
13
|
+
const R_U_REGEX = /\r$/u;
|
|
14
|
+
/*//////////////////////////////////////////////////
|
|
15
|
+
REGEX
|
|
16
|
+
//////////////////////////////////////////////////*/
|
|
17
|
+
const C_M_JS_JSX_TS_TSX_U_REGEX = /\.(?:astro|(?:c|m)?(?:js|jsx|ts|tsx))$/u;
|
|
18
|
+
/*//////////////////////////////////////////////////
|
|
19
|
+
REGEX
|
|
20
|
+
//////////////////////////////////////////////////*/
|
|
21
|
+
const A_Z_REGEX = /^[A-Z]/;
|
|
22
|
+
export const IDENTIFIER_REPLACEMENTS = replacementMap([
|
|
23
|
+
[['administrator', 'administration'], 'admin'],
|
|
24
|
+
['application', 'app'],
|
|
25
|
+
['argument', 'arg'],
|
|
26
|
+
['attribute', 'attr'],
|
|
27
|
+
['intelligence', 'intel'],
|
|
28
|
+
['image', 'img'],
|
|
29
|
+
['command', 'cmd'],
|
|
30
|
+
['column', 'col'],
|
|
31
|
+
['component', 'comp'],
|
|
32
|
+
['configuration', 'config'],
|
|
33
|
+
[['compare', 'comp'], 'comp'],
|
|
34
|
+
['difference', 'diff'],
|
|
35
|
+
['directory', 'dir'],
|
|
36
|
+
['description', 'desc'],
|
|
37
|
+
['destination', 'dest'],
|
|
38
|
+
['document', 'doc'],
|
|
39
|
+
['element', 'el'],
|
|
40
|
+
['environment', 'env'],
|
|
41
|
+
['error', 'err'],
|
|
42
|
+
['expression', 'expr'],
|
|
43
|
+
['extension', 'ext'],
|
|
44
|
+
[['format', 'formatted', 'formatting', 'formatter'], 'fmt'],
|
|
45
|
+
['function', 'fn'],
|
|
46
|
+
[['generation', 'generate', 'generated', 'generating'], 'gen'],
|
|
47
|
+
[['identify', 'indentifier'], 'id'],
|
|
48
|
+
[['initialise', 'initialised', 'initialises', 'initialising', 'initialisation', 'initialize', 'initialized', 'initializes', 'initializing', 'initialization'], 'init'],
|
|
49
|
+
[['initialiser', 'initializer'], 'init'],
|
|
50
|
+
['language', 'lang'],
|
|
51
|
+
['maximum', 'max'],
|
|
52
|
+
['message', 'msg'],
|
|
53
|
+
['minimum', 'min'],
|
|
54
|
+
['number', 'num'],
|
|
55
|
+
[['organization', 'organisation'], 'org'],
|
|
56
|
+
['operation', 'op'],
|
|
57
|
+
['original', 'orig'],
|
|
58
|
+
['parameter', 'param'],
|
|
59
|
+
['previous', 'prev'],
|
|
60
|
+
['property', 'prop'],
|
|
61
|
+
['reference', 'ref'],
|
|
62
|
+
['repository', 'repo'],
|
|
63
|
+
['source', 'src'],
|
|
64
|
+
['temporary', 'temp'],
|
|
65
|
+
]);
|
|
66
|
+
export const PREFIX_REPLACEMENTS = replacementMap([
|
|
67
|
+
[['normalise', 'normalised', 'normalises', 'normalize', 'normalized', 'normalizes'], 'norm'],
|
|
68
|
+
[['calculate', 'calculated'], 'calc'],
|
|
69
|
+
[['validate', 'validated', 'validation'], 'valid'],
|
|
70
|
+
[['sanitize', 'sanitized'], 'san'],
|
|
71
|
+
]);
|
|
72
|
+
export const CALLBACK_ALIASES = replacementMap([['index', 'i']], false);
|
|
73
|
+
export const CALLBACK_METHODS = new Set([
|
|
74
|
+
'addEventListener',
|
|
75
|
+
'every',
|
|
76
|
+
'filter',
|
|
77
|
+
'find',
|
|
78
|
+
'findIndex',
|
|
79
|
+
'findLast',
|
|
80
|
+
'findLastIndex',
|
|
81
|
+
'flatMap',
|
|
82
|
+
'forEach',
|
|
83
|
+
'forEachChild',
|
|
84
|
+
'map',
|
|
85
|
+
'reduce',
|
|
86
|
+
'reduceRight',
|
|
87
|
+
'some',
|
|
88
|
+
'sort',
|
|
89
|
+
]);
|
|
90
|
+
export const PROTECTED_IDENTIFIERS = new Set([
|
|
91
|
+
'GET',
|
|
92
|
+
'HEAD',
|
|
93
|
+
'OPTIONS',
|
|
94
|
+
'POST',
|
|
95
|
+
'PUT',
|
|
96
|
+
'PATCH',
|
|
97
|
+
'DELETE',
|
|
98
|
+
'default',
|
|
99
|
+
'FormData',
|
|
100
|
+
'Headers',
|
|
101
|
+
'NextRequest',
|
|
102
|
+
'NextResponse',
|
|
103
|
+
'Request',
|
|
104
|
+
'Response',
|
|
105
|
+
'URL',
|
|
106
|
+
'URLSearchParams',
|
|
107
|
+
'WebSocket',
|
|
108
|
+
'CarouselContextProps',
|
|
109
|
+
'ChartContextProps',
|
|
110
|
+
'FormSelectOptionProps',
|
|
111
|
+
'SidebarContextProps',
|
|
112
|
+
]);
|
|
113
|
+
export const PROTECTED_PATH_PARTS = new Set(['node_modules', '.next', 'out', 'build', 'coverage', 'src/components/ui']);
|
|
114
|
+
export const PROTECTED_FILENAMES = new Set(['page.tsx', 'page.ts', 'layout.tsx', 'layout.ts', 'route.ts', 'route.tsx', 'error.tsx', 'loading.tsx', 'not-found.tsx', 'index.ts', 'index.tsx']);
|
|
115
|
+
export const CODE_EXTS = new Set(['.js', '.jsx', '.mjs', '.cjs', '.ts', '.tsx', '.mts', '.cts']);
|
|
116
|
+
/**
|
|
117
|
+
* Narrows an initializer to a property or element access that can be aliased safely.
|
|
118
|
+
*
|
|
119
|
+
* @param n Expression to inspect.
|
|
120
|
+
*
|
|
121
|
+
* @return Stable identifier or access expression, otherwise `undefined`.
|
|
122
|
+
*/
|
|
123
|
+
const aliasTgt = (n) => ts.isIdentifier(n) || ts.isPropertyAccessExpression(n) || ts.isElementAccessExpression(n) ? n : undefined;
|
|
124
|
+
/**
|
|
125
|
+
* Confirms that an identifier keeps the same local binding after its declaration.
|
|
126
|
+
*
|
|
127
|
+
* @param symbol Symbol resolved for the identifier.
|
|
128
|
+
*
|
|
129
|
+
* @return Whether the target remains a stable local binding.
|
|
130
|
+
*/
|
|
131
|
+
function isStableLocalBinding(symbol) {
|
|
132
|
+
if (symbol.flags & ts.SymbolFlags.Alias)
|
|
133
|
+
return false;
|
|
134
|
+
const declaration = symbol.valueDeclaration;
|
|
135
|
+
const list = (declaration && ts.isVariableDeclaration(declaration)
|
|
136
|
+
? declaration
|
|
137
|
+
: declaration && ts.isBindingElement(declaration) && ts.isVariableDeclaration(declaration.parent.parent)
|
|
138
|
+
? declaration.parent.parent
|
|
139
|
+
: undefined)?.parent;
|
|
140
|
+
return Boolean(list && ts.isVariableDeclarationList(list) && (list.flags & ts.NodeFlags.Const) !== 0);
|
|
141
|
+
}
|
|
142
|
+
/*//////////////////////////////////////////////////
|
|
143
|
+
HELPERS
|
|
144
|
+
//////////////////////////////////////////////////*/
|
|
145
|
+
/**
|
|
146
|
+
* Builds a frozen lookup table of long identifier words and their concise replacements.
|
|
147
|
+
*
|
|
148
|
+
* @param groups Replacement groups where each left-hand word maps to one replacement.
|
|
149
|
+
* @param pluralReplacements Whether inferred plural keys should receive plural replacements.
|
|
150
|
+
*
|
|
151
|
+
* @return Immutable replacement map including supported plural forms.
|
|
152
|
+
*/
|
|
153
|
+
function replacementMap(groups, pluralReplacements = true) {
|
|
154
|
+
const explicit = new Map(groups.flatMap(([n, r]) => (typeof n === 'string' ? [n] : n).map(n => [n, r])));
|
|
155
|
+
/**
|
|
156
|
+
* Infers the plural spelling used for replacement-map lookup keys.
|
|
157
|
+
*
|
|
158
|
+
* @param word Singular replacement key.
|
|
159
|
+
*
|
|
160
|
+
* @return Pluralized spelling using the supported English suffix rules.
|
|
161
|
+
*/
|
|
162
|
+
const plural = (word) => {
|
|
163
|
+
if (/[^aeiou]y$/i.test(word))
|
|
164
|
+
return `${word.slice(0, -1)}ies`;
|
|
165
|
+
if (/(?:s|x|z|ch|sh)$/i.test(word))
|
|
166
|
+
return word.length <= 4 ? word : `${word}es`;
|
|
167
|
+
return `${word}s`;
|
|
168
|
+
};
|
|
169
|
+
const entries = [...explicit];
|
|
170
|
+
for (const [name, replacement] of explicit) {
|
|
171
|
+
const pluralName = plural(name);
|
|
172
|
+
if (!explicit.has(pluralName))
|
|
173
|
+
entries.push([pluralName, pluralReplacements ? plural(replacement) : replacement]);
|
|
174
|
+
}
|
|
175
|
+
return Object.freeze(Object.fromEntries(entries));
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Splits an identifier into words while preserving acronym and numeric boundaries.
|
|
179
|
+
*
|
|
180
|
+
* @param name Identifier to tokenize.
|
|
181
|
+
*
|
|
182
|
+
* @return Identifier word tokens in source order.
|
|
183
|
+
*/
|
|
184
|
+
export function identifierTokens(name) {
|
|
185
|
+
return name
|
|
186
|
+
.split(/[_-]/)
|
|
187
|
+
.flatMap(p => p.match(/[A-Z]+(?=[A-Z][a-z]|\d|$)|[A-Z]?[a-z]+|\d+/g) ?? [p])
|
|
188
|
+
.filter(Boolean);
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Applies an explicit callback alias when its target is available.
|
|
192
|
+
*
|
|
193
|
+
* @param name Existing callback parameter name.
|
|
194
|
+
* @param reserved Names already used in the callback scope.
|
|
195
|
+
*
|
|
196
|
+
* @return The configured alias, or the original descriptive name.
|
|
197
|
+
*/
|
|
198
|
+
export function callbackAlias(name, reserved = []) {
|
|
199
|
+
if (!name || name.startsWith('_'))
|
|
200
|
+
return name;
|
|
201
|
+
const preferred = CALLBACK_ALIASES[name.toLowerCase()];
|
|
202
|
+
return preferred && !reserved.includes(preferred) ? preferred : name;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Produces the configured concise form of an identifier, including callback and numeric-index aliases.
|
|
206
|
+
*
|
|
207
|
+
* @param name Identifier to shorten.
|
|
208
|
+
* @param kind Context flags controlling callback, index, and reserved-name handling.
|
|
209
|
+
*
|
|
210
|
+
* @return Concise identifier, or the original name when no safe replacement applies.
|
|
211
|
+
*/
|
|
212
|
+
export function renameId(name, kind = {}) {
|
|
213
|
+
if (kind.disabled || PROTECTED_IDENTIFIERS.has(name) || name === '_')
|
|
214
|
+
return name;
|
|
215
|
+
if (kind.numericIndex && name === 'index')
|
|
216
|
+
return callbackAlias(name, kind.reserved);
|
|
217
|
+
if (kind.callback) {
|
|
218
|
+
const alias = callbackAlias(name, kind.reserved);
|
|
219
|
+
if (alias !== name)
|
|
220
|
+
return alias;
|
|
221
|
+
}
|
|
222
|
+
const tokens = identifierTokens(name);
|
|
223
|
+
if (!tokens.some(t => Object.hasOwn(IDENTIFIER_REPLACEMENTS, t.toLowerCase()) || Object.hasOwn(PREFIX_REPLACEMENTS, t.toLowerCase())))
|
|
224
|
+
return name;
|
|
225
|
+
const replacement = (function renderIdentifier(orig, tokens) {
|
|
226
|
+
if (/^[A-Z][A-Z0-9_]*$/.test(orig))
|
|
227
|
+
return tokens.join(orig.includes('_') ? '_' : '').toUpperCase();
|
|
228
|
+
if (orig.includes('_'))
|
|
229
|
+
return tokens.join('_');
|
|
230
|
+
if (orig.includes('-'))
|
|
231
|
+
return tokens.join('-');
|
|
232
|
+
if (A_Z_REGEX.test(orig))
|
|
233
|
+
return tokens.map(t => t.charAt(0).toUpperCase() + t.slice(1)).join('');
|
|
234
|
+
return tokens.map((t, i) => (i === 0 ? t.toLowerCase() : t.charAt(0).toUpperCase() + t.slice(1))).join('');
|
|
235
|
+
})(name, tokens.map((t, i) => {
|
|
236
|
+
const lower = t.toLowerCase();
|
|
237
|
+
const replacement = i === 0 && Object.hasOwn(PREFIX_REPLACEMENTS, lower)
|
|
238
|
+
? PREFIX_REPLACEMENTS[lower]
|
|
239
|
+
: Object.hasOwn(IDENTIFIER_REPLACEMENTS, lower)
|
|
240
|
+
? IDENTIFIER_REPLACEMENTS[lower]
|
|
241
|
+
: t;
|
|
242
|
+
return replacement === t
|
|
243
|
+
? t
|
|
244
|
+
: (function tokenCase(orig, replacement) {
|
|
245
|
+
if (orig === orig.toUpperCase())
|
|
246
|
+
return replacement.toUpperCase();
|
|
247
|
+
if (orig.charAt(0) === orig.charAt(0).toUpperCase())
|
|
248
|
+
return replacement.charAt(0).toUpperCase() + replacement.slice(1);
|
|
249
|
+
return replacement;
|
|
250
|
+
})(t, replacement);
|
|
251
|
+
}));
|
|
252
|
+
return kind.callback && kind.reserved?.includes(replacement) ? name : replacement;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Determines whether an identifier parameter belongs to an inline or named callback.
|
|
256
|
+
*
|
|
257
|
+
* @param n Parameter identifier to classify.
|
|
258
|
+
*
|
|
259
|
+
* @return Callback rename context when the parameter is safely concise-able; otherwise `undefined`.
|
|
260
|
+
*/
|
|
261
|
+
function callbackKind(n) {
|
|
262
|
+
// JSX component bindings are syntax-bearing component names, not local callback
|
|
263
|
+
// values. Leave them intact while shortening ordinary callback parameters.
|
|
264
|
+
if (A_Z_REGEX.test(n.text))
|
|
265
|
+
return undefined;
|
|
266
|
+
const param = ts.findAncestor(n, ts.isParameter);
|
|
267
|
+
if (!param)
|
|
268
|
+
return undefined;
|
|
269
|
+
const callback = ts.findAncestor(param, (n) => ts.isArrowFunction(n) || ts.isFunctionExpression(n) || ts.isFunctionDeclaration(n));
|
|
270
|
+
if (!callback)
|
|
271
|
+
return undefined;
|
|
272
|
+
let isNamedCallback = false;
|
|
273
|
+
if (ts.isFunctionDeclaration(callback) && callback.name) {
|
|
274
|
+
const scan = (n) => {
|
|
275
|
+
if (isNamedCallback)
|
|
276
|
+
return;
|
|
277
|
+
const jsxAttr = ts.isJsxAttribute(n) ? n : undefined;
|
|
278
|
+
const jsxExpr = jsxAttr?.initializer && ts.isJsxExpression(jsxAttr.initializer) ? jsxAttr.initializer.expression : undefined;
|
|
279
|
+
if (ts.isCallExpression(n) &&
|
|
280
|
+
ts.isPropertyAccessExpression(n.expression) &&
|
|
281
|
+
CALLBACK_METHODS.has(n.expression.name.text) &&
|
|
282
|
+
n.arguments.some(a => ts.isIdentifier(a) && a.text === callback.name?.text)) {
|
|
283
|
+
isNamedCallback = true;
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
if (jsxAttr &&
|
|
287
|
+
ts.isIdentifier(jsxAttr.name) &&
|
|
288
|
+
/^on[A-Z]/.test(jsxAttr.name.text) &&
|
|
289
|
+
jsxExpr &&
|
|
290
|
+
(() => {
|
|
291
|
+
let refs = false;
|
|
292
|
+
const findRef = (c) => {
|
|
293
|
+
if (refs)
|
|
294
|
+
return;
|
|
295
|
+
if (ts.isIdentifier(c) && c.text === callback.name?.text) {
|
|
296
|
+
refs = true;
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
ts.forEachChild(c, findRef);
|
|
300
|
+
};
|
|
301
|
+
findRef(jsxExpr);
|
|
302
|
+
return refs;
|
|
303
|
+
})()) {
|
|
304
|
+
isNamedCallback = true;
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
ts.forEachChild(n, scan);
|
|
308
|
+
};
|
|
309
|
+
scan(callback.parent);
|
|
310
|
+
}
|
|
311
|
+
if (!(!ts.isFunctionDeclaration(callback) && ts.isCallExpression(callback.parent) && callback.parent.arguments.includes(callback)) &&
|
|
312
|
+
!isNamedCallback &&
|
|
313
|
+
!(ts.isJsxExpression(callback.parent) && callback.parent.expression === callback && ts.isJsxAttribute(callback.parent.parent)) &&
|
|
314
|
+
!(!ts.isFunctionDeclaration(callback) && ts.isPropertyAssignment(callback.parent) && callback.parent.initializer === callback))
|
|
315
|
+
return undefined;
|
|
316
|
+
for (let parent = callback.parent; parent; parent = parent.parent) {
|
|
317
|
+
if (!ts.isArrowFunction(parent) && !ts.isFunctionExpression(parent))
|
|
318
|
+
continue;
|
|
319
|
+
if ((ts.isCallExpression(parent.parent) && parent.parent.arguments.includes(parent)) ||
|
|
320
|
+
(ts.isPropertyAssignment(parent.parent) && parent.parent.initializer === parent) ||
|
|
321
|
+
(ts.isJsxExpression(parent.parent) && parent.parent.expression === parent && ts.isJsxAttribute(parent.parent.parent)))
|
|
322
|
+
return { disabled: true };
|
|
323
|
+
}
|
|
324
|
+
const declarations = [];
|
|
325
|
+
const collectBinding = (name) => {
|
|
326
|
+
if (ts.isIdentifier(name))
|
|
327
|
+
declarations.push(name);
|
|
328
|
+
else
|
|
329
|
+
for (const el of name.elements)
|
|
330
|
+
if (ts.isBindingElement(el))
|
|
331
|
+
collectBinding(el.name);
|
|
332
|
+
};
|
|
333
|
+
for (const p of callback.parameters)
|
|
334
|
+
collectBinding(p.name);
|
|
335
|
+
// hygiene-ignore single-use -- Build declaration names once before the recursive reserved-name visitor runs.
|
|
336
|
+
const declarationNames = new Set(declarations.map(d => d.text));
|
|
337
|
+
const reserved = new Set();
|
|
338
|
+
const collect = (child) => {
|
|
339
|
+
if (ts.isIdentifier(child) && !declarationNames.has(child.text))
|
|
340
|
+
reserved.add(child.text);
|
|
341
|
+
ts.forEachChild(child, collect);
|
|
342
|
+
};
|
|
343
|
+
collect(callback);
|
|
344
|
+
const paramIndex = (declaration) => {
|
|
345
|
+
const declarationParam = ts.findAncestor(declaration, ts.isParameter);
|
|
346
|
+
return !!declarationParam && callback.parameters.indexOf(declarationParam) === 1 && declaration.text === 'index';
|
|
347
|
+
};
|
|
348
|
+
const initialAlias = (declaration) => renameId(declaration.text, { callback: true, numericIndex: paramIndex(declaration) });
|
|
349
|
+
// Keep unshortened names and names already used in the callback body available
|
|
350
|
+
// before assigning initials, so collisions are resolved consistently by order.
|
|
351
|
+
const fixedDeclarations = new Set(declarations.filter(d => initialAlias(d) === d.text));
|
|
352
|
+
for (const declaration of fixedDeclarations)
|
|
353
|
+
reserved.add(declaration.text);
|
|
354
|
+
for (const declaration of declarations) {
|
|
355
|
+
const numericIndex = paramIndex(declaration);
|
|
356
|
+
if (declaration === n) {
|
|
357
|
+
const targetReserved = new Set(reserved);
|
|
358
|
+
if (fixedDeclarations.has(declaration))
|
|
359
|
+
targetReserved.delete(declaration.text);
|
|
360
|
+
return { callback: true, numericIndex, reserved: [...targetReserved] };
|
|
361
|
+
}
|
|
362
|
+
if (!fixedDeclarations.has(declaration))
|
|
363
|
+
reserved.add(renameId(declaration.text, { callback: true, numericIndex, reserved: [...reserved] }));
|
|
364
|
+
}
|
|
365
|
+
return undefined;
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Returns whether a declaration belongs to a supported callback.
|
|
369
|
+
*
|
|
370
|
+
* @param node Identifier declaration to inspect.
|
|
371
|
+
*
|
|
372
|
+
* @return `true` when the declaration is a supported callback parameter.
|
|
373
|
+
*/
|
|
374
|
+
export function isCallbackDeclaration(node) {
|
|
375
|
+
return callbackKind(node) !== undefined;
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* Returns the context-aware concise suggestion for one repository-owned declaration.
|
|
379
|
+
*
|
|
380
|
+
* @param n Identifier declaration whose context determines the suggestion.
|
|
381
|
+
*
|
|
382
|
+
* @return Concise name appropriate for the declaration's scope and callback role.
|
|
383
|
+
*/
|
|
384
|
+
export function renameDeclaration(n) {
|
|
385
|
+
return renameId(n.text, callbackKind(n));
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* Checks whether a parameter rename would capture a free reference in its default initializer.
|
|
389
|
+
*
|
|
390
|
+
* @param declaration Parameter identifier being considered for renaming.
|
|
391
|
+
* @param suggestion Proposed parameter name.
|
|
392
|
+
* @param checker TypeScript checker used to distinguish outer references from initializer-local bindings.
|
|
393
|
+
*
|
|
394
|
+
* @return Whether the initializer contains a reference that the proposed parameter would shadow.
|
|
395
|
+
*/
|
|
396
|
+
export function parameterRenameCapturesDefault(declaration, suggestion, checker) {
|
|
397
|
+
if (!ts.isParameter(declaration.parent) || !declaration.parent.initializer)
|
|
398
|
+
return false;
|
|
399
|
+
const init = declaration.parent.initializer;
|
|
400
|
+
const withinInit = (node) => {
|
|
401
|
+
for (let current = node; current; current = current.parent) {
|
|
402
|
+
if (current === init)
|
|
403
|
+
return true;
|
|
404
|
+
}
|
|
405
|
+
return false;
|
|
406
|
+
};
|
|
407
|
+
// hygiene-ignore single-use -- Reuse the property-name classifier across every identifier visited below.
|
|
408
|
+
const isPropName = (node) => (ts.isPropertyAccessExpression(node.parent) && node.parent.name === node) ||
|
|
409
|
+
(ts.isQualifiedName(node.parent) && node.parent.right === node) ||
|
|
410
|
+
((ts.isPropertyAssignment(node.parent) || ts.isMethodDeclaration(node.parent) || ts.isMethodSignature(node.parent) || ts.isPropertyDeclaration(node.parent)) && node.parent.name === node) ||
|
|
411
|
+
(ts.isLabeledStatement(node.parent) && node.parent.label === node) ||
|
|
412
|
+
((ts.isBreakStatement(node.parent) || ts.isContinueStatement(node.parent)) && node.parent.label === node);
|
|
413
|
+
let captures = false;
|
|
414
|
+
const visit = (node) => {
|
|
415
|
+
if (captures)
|
|
416
|
+
return;
|
|
417
|
+
if (ts.isIdentifier(node) && node.text === suggestion && !isPropName(node) && !ts.findAncestor(node.parent, ts.isTypeNode)) {
|
|
418
|
+
const symbol = ts.isShorthandPropertyAssignment(node.parent) ? checker.getShorthandAssignmentValueSymbol(node.parent) : checker.getSymbolAtLocation(node);
|
|
419
|
+
const declarations = symbol?.declarations ?? [];
|
|
420
|
+
if (!symbol || !declarations.length || declarations.some(srcDeclaration => !withinInit(srcDeclaration)))
|
|
421
|
+
captures = true;
|
|
422
|
+
}
|
|
423
|
+
ts.forEachChild(node, visit);
|
|
424
|
+
};
|
|
425
|
+
visit(init);
|
|
426
|
+
return captures;
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* Checks whether an identifier starts with a configured prefix replacement.
|
|
430
|
+
*
|
|
431
|
+
* @param name Identifier to inspect.
|
|
432
|
+
*
|
|
433
|
+
* @return `true` when its first token has a configured concise prefix.
|
|
434
|
+
*/
|
|
435
|
+
export function hasPrefixReplacement(name) {
|
|
436
|
+
return Object.hasOwn(PREFIX_REPLACEMENTS, identifierTokens(name)[0]?.toLowerCase() ?? '');
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* Checks whether a path is protected from symbol renaming because it is generated or framework-owned.
|
|
440
|
+
*
|
|
441
|
+
* @param fileName Path to inspect.
|
|
442
|
+
*
|
|
443
|
+
* @return `true` for declaration files and configured protected path segments.
|
|
444
|
+
*/
|
|
445
|
+
export function isProtectedPath(fileName) {
|
|
446
|
+
const norm = fileName.replaceAll('\\', '/');
|
|
447
|
+
return (norm.endsWith('.d.ts') ||
|
|
448
|
+
[...PROTECTED_PATH_PARTS].some(p => (p.includes('/') ? norm === p || norm.endsWith(`/${p}`) || norm.startsWith(`${p}/`) || norm.includes(`/${p}/`) : norm.split('/').includes(p))));
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* Checks whether a filename is a framework convention whose declarations must retain their names.
|
|
452
|
+
*
|
|
453
|
+
* @param fileName Path or basename to inspect.
|
|
454
|
+
*
|
|
455
|
+
* @return `true` when the basename is a protected route or component filename.
|
|
456
|
+
*/
|
|
457
|
+
export function isProtectedFilename(fileName) {
|
|
458
|
+
return PROTECTED_FILENAMES.has(fileName.split(/[\\/]/).at(-1) ?? fileName);
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* Reports whether an identifier already satisfies the configured concise-name rules.
|
|
462
|
+
*
|
|
463
|
+
* @param name Identifier to validate.
|
|
464
|
+
* @param kind Context flags used when the identifier is a callback or index parameter.
|
|
465
|
+
*
|
|
466
|
+
* @return `true` when renaming would produce the same identifier.
|
|
467
|
+
*/
|
|
468
|
+
export function isApprovedId(name, kind = {}) {
|
|
469
|
+
return renameId(name, kind) === name;
|
|
470
|
+
}
|
|
471
|
+
/**
|
|
472
|
+
* Returns whether an identifier declares a repository-owned symbol safe to rename with all references.
|
|
473
|
+
*
|
|
474
|
+
* @param n Identifier declaration to classify.
|
|
475
|
+
*
|
|
476
|
+
* @return `true` when all references can be updated as a repository-local rename.
|
|
477
|
+
*/
|
|
478
|
+
export function isRenamableDeclaration(n) {
|
|
479
|
+
if (ts.isParameter(n.parent) &&
|
|
480
|
+
n.parent.modifiers?.some(m => [ts.SyntaxKind.PublicKeyword, ts.SyntaxKind.PrivateKeyword, ts.SyntaxKind.ProtectedKeyword, ts.SyntaxKind.ReadonlyKeyword].includes(m.kind)))
|
|
481
|
+
return false;
|
|
482
|
+
const declaration = ts.findAncestor(n.parent, (a) => ts.isVariableDeclaration(a) || ts.isParameter(a));
|
|
483
|
+
const varStatement = declaration && ts.isVariableDeclaration(declaration) && ts.isVariableDeclarationList(declaration.parent) && ts.isVariableStatement(declaration.parent.parent)
|
|
484
|
+
? declaration.parent.parent
|
|
485
|
+
: undefined;
|
|
486
|
+
return ((ts.isVariableDeclaration(n.parent) && n.parent.name === n && !varStatement?.modifiers?.some(m => m.kind === ts.SyntaxKind.ExportKeyword)) ||
|
|
487
|
+
(ts.isParameter(n.parent) && n.parent.name === n) ||
|
|
488
|
+
(ts.isBindingElement(n.parent) && n.parent.name === n && !!declaration && !varStatement?.modifiers?.some(m => m.kind === ts.SyntaxKind.ExportKeyword)) ||
|
|
489
|
+
(ts.isFunctionDeclaration(n.parent) && n.parent.name === n && !n.parent.modifiers?.some(m => m.kind === ts.SyntaxKind.ExportKeyword)) ||
|
|
490
|
+
(ts.isFunctionExpression(n.parent) && n.parent.name === n) ||
|
|
491
|
+
(ts.isClassDeclaration(n.parent) && n.parent.name === n && !n.parent.modifiers?.some(m => m.kind === ts.SyntaxKind.ExportKeyword)) ||
|
|
492
|
+
(ts.isClassExpression(n.parent) && n.parent.name === n) ||
|
|
493
|
+
(ts.isInterfaceDeclaration(n.parent) && n.parent.name === n && !n.parent.modifiers?.some(m => m.kind === ts.SyntaxKind.ExportKeyword)) ||
|
|
494
|
+
(ts.isTypeAliasDeclaration(n.parent) && n.parent.name === n && !n.parent.modifiers?.some(m => m.kind === ts.SyntaxKind.ExportKeyword)));
|
|
495
|
+
}
|
|
496
|
+
/**
|
|
497
|
+
* Returns the function or source-file scope that owns a declaration.
|
|
498
|
+
*
|
|
499
|
+
* @param n Identifier whose owning function or source-file scope is required.
|
|
500
|
+
*
|
|
501
|
+
* @return The nearest function-like scope, or the source file when no function owns it.
|
|
502
|
+
*/
|
|
503
|
+
export function declarationScope(n) {
|
|
504
|
+
let scope = (ts.isFunctionDeclaration(n.parent) || ts.isClassDeclaration(n.parent) || ts.isInterfaceDeclaration(n.parent) || ts.isTypeAliasDeclaration(n.parent)) && n.parent.name === n
|
|
505
|
+
? n.parent.parent
|
|
506
|
+
: n.parent;
|
|
507
|
+
while (!ts.isSourceFile(scope)) {
|
|
508
|
+
if (ts.isArrowFunction(scope) ||
|
|
509
|
+
ts.isFunctionDeclaration(scope) ||
|
|
510
|
+
ts.isFunctionExpression(scope) ||
|
|
511
|
+
ts.isMethodDeclaration(scope) ||
|
|
512
|
+
ts.isConstructorDeclaration(scope) ||
|
|
513
|
+
ts.isGetAccessorDeclaration(scope) ||
|
|
514
|
+
ts.isSetAccessorDeclaration(scope))
|
|
515
|
+
return scope;
|
|
516
|
+
scope = scope.parent;
|
|
517
|
+
}
|
|
518
|
+
return scope;
|
|
519
|
+
}
|
|
520
|
+
/**
|
|
521
|
+
* Reports concise-name drift without mutating the consumer repository.
|
|
522
|
+
*
|
|
523
|
+
* @param cfg Quality configuration defining source scope and naming protections.
|
|
524
|
+
* @param analysis Optional shared program and source cache for the current quality run.
|
|
525
|
+
*
|
|
526
|
+
* @return Concise-name findings for declarations that can be renamed safely.
|
|
527
|
+
*/
|
|
528
|
+
export function namingFindings(cfg, analysis) {
|
|
529
|
+
const protectedNames = new Set([...PROTECTED_IDENTIFIERS, ...(cfg.protectedIdentifiers ?? [])]);
|
|
530
|
+
const findings = [];
|
|
531
|
+
const files = (analysis?.files ?? srcFiles(cfg)).filter(file => C_M_JS_JSX_TS_TSX_U_REGEX.test(file));
|
|
532
|
+
const program = analysis?.program ??
|
|
533
|
+
ts.createProgram({
|
|
534
|
+
rootNames: files.filter(file => CODE_EXTS.has(file.slice(file.lastIndexOf('.')))),
|
|
535
|
+
options: {
|
|
536
|
+
allowNonTsExtensions: true,
|
|
537
|
+
allowJs: true,
|
|
538
|
+
checkJs: false,
|
|
539
|
+
jsx: ts.JsxEmit.Preserve,
|
|
540
|
+
module: ts.ModuleKind.ESNext,
|
|
541
|
+
moduleResolution: ts.ModuleResolutionKind.Bundler,
|
|
542
|
+
skipLibCheck: true,
|
|
543
|
+
target: ts.ScriptTarget.ESNext,
|
|
544
|
+
},
|
|
545
|
+
});
|
|
546
|
+
const checker = analysis?.checker ?? program.getTypeChecker();
|
|
547
|
+
for (const f of files) {
|
|
548
|
+
if (isProtectedPath(displayPath(cfg, f)))
|
|
549
|
+
continue;
|
|
550
|
+
const srcText = analysis?.texts.get(f) ?? readFileSync(f, 'utf8');
|
|
551
|
+
const frontmatter = f.endsWith('.astro') ? astroFrontmatter(srcText) : undefined;
|
|
552
|
+
if (f.endsWith('.astro') && !frontmatter)
|
|
553
|
+
continue;
|
|
554
|
+
const file = frontmatter
|
|
555
|
+
? ts.createSourceFile(f, frontmatter.source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS)
|
|
556
|
+
: (analysis?.sources.get(f) ?? program.getSourceFile(f) ?? ts.createSourceFile(f, srcText, ts.ScriptTarget.Latest, true, f.endsWith('.tsx') ? ts.ScriptKind.TSX : ts.ScriptKind.TS));
|
|
557
|
+
const srcOffset = frontmatter?.offset ?? 0;
|
|
558
|
+
// hygiene-ignore single-use -- Build the exported-name set once before visiting each declaration in this file.
|
|
559
|
+
const exportedNames = new Set(file.statements.flatMap(s => (ts.isExportDeclaration(s) && s.exportClause && ts.isNamedExports(s.exportClause) ? s.exportClause.elements.map(e => (e.propertyName ?? e.name).text) : [])));
|
|
560
|
+
const safetyByScope = new Map();
|
|
561
|
+
const declaredNames = new Set();
|
|
562
|
+
const collect = (n) => {
|
|
563
|
+
if (ts.isIdentifier(n) && isRenamableDeclaration(n)) {
|
|
564
|
+
const scope = declarationScope(n);
|
|
565
|
+
const safety = safetyByScope.get(scope) ?? { declared: new Set(), proposals: new Map() };
|
|
566
|
+
safety.declared.add(n.text);
|
|
567
|
+
declaredNames.add(n.text);
|
|
568
|
+
const suggestion = renameDeclaration(n);
|
|
569
|
+
if (!n.text.startsWith('_') && suggestion !== n.text && !isCallbackDeclaration(n))
|
|
570
|
+
safety.proposals.set(suggestion, (safety.proposals.get(suggestion) ?? 0) + 1);
|
|
571
|
+
safetyByScope.set(scope, safety);
|
|
572
|
+
}
|
|
573
|
+
ts.forEachChild(n, collect);
|
|
574
|
+
};
|
|
575
|
+
collect(file);
|
|
576
|
+
const inspect = (n) => {
|
|
577
|
+
if (ts.isIdentifier(n) && isRenamableDeclaration(n) && !n.text.startsWith('_') && !protectedNames.has(n.text) && !exportedNames.has(n.text)) {
|
|
578
|
+
const suggestion = renameDeclaration(n);
|
|
579
|
+
const safety = safetyByScope.get(declarationScope(n));
|
|
580
|
+
if (suggestion !== n.text && parameterRenameCapturesDefault(n, suggestion, checker)) {
|
|
581
|
+
findings.push({
|
|
582
|
+
file: displayPath(cfg, f),
|
|
583
|
+
name: n.text,
|
|
584
|
+
suggestion,
|
|
585
|
+
severity: 'error',
|
|
586
|
+
start: srcOffset + n.getStart(file),
|
|
587
|
+
safe: false,
|
|
588
|
+
reason: `renaming would capture the free identifier ${suggestion} in this parameter's default initializer`,
|
|
589
|
+
});
|
|
590
|
+
return;
|
|
591
|
+
}
|
|
592
|
+
if (!isCallbackDeclaration(n) && (safety?.declared.has(suggestion) || declaredNames.has(suggestion) || (safety?.proposals.get(suggestion) ?? 0) > 1))
|
|
593
|
+
return;
|
|
594
|
+
if (suggestion !== n.text)
|
|
595
|
+
findings.push({ file: displayPath(cfg, f), name: n.text, suggestion, severity: 'error', start: srcOffset + n.getStart(file) });
|
|
596
|
+
}
|
|
597
|
+
ts.forEachChild(n, inspect);
|
|
598
|
+
};
|
|
599
|
+
inspect(file);
|
|
600
|
+
}
|
|
601
|
+
return findings;
|
|
602
|
+
}
|
|
603
|
+
/**
|
|
604
|
+
* Extracts an Astro frontmatter script and its source offset for TypeScript's rename service.
|
|
605
|
+
*
|
|
606
|
+
* @param src Complete Astro page source.
|
|
607
|
+
*
|
|
608
|
+
* @return Frontmatter source and its start offset, or `undefined` when delimiters are invalid.
|
|
609
|
+
*/
|
|
610
|
+
function astroFrontmatter(src) {
|
|
611
|
+
const firstLineEnd = src.indexOf('\n');
|
|
612
|
+
if (firstLineEnd < 0 || src.slice(0, firstLineEnd).replace(R_U_REGEX, '').trim() !== '---')
|
|
613
|
+
return undefined;
|
|
614
|
+
const offset = firstLineEnd + 1;
|
|
615
|
+
let lineStart = offset;
|
|
616
|
+
while (lineStart <= src.length) {
|
|
617
|
+
const nextLineEnd = src.indexOf('\n', lineStart);
|
|
618
|
+
if (src
|
|
619
|
+
.slice(lineStart, nextLineEnd < 0 ? src.length : nextLineEnd)
|
|
620
|
+
.replace(R_U_REGEX, '')
|
|
621
|
+
.trim() === '---')
|
|
622
|
+
return { offset, source: src.slice(offset, lineStart) };
|
|
623
|
+
if (nextLineEnd < 0)
|
|
624
|
+
break;
|
|
625
|
+
lineStart = nextLineEnd + 1;
|
|
626
|
+
}
|
|
627
|
+
return undefined;
|
|
628
|
+
}
|
|
629
|
+
/**
|
|
630
|
+
* Confirms that an Astro naming finding belongs to a frontmatter callback parameter.
|
|
631
|
+
*
|
|
632
|
+
* @param file Virtual TypeScript filename used for the frontmatter.
|
|
633
|
+
* @param src Frontmatter source containing the declaration.
|
|
634
|
+
* @param start Declaration offset relative to that frontmatter.
|
|
635
|
+
*
|
|
636
|
+
* @return `true` when renaming stays inside a callback and cannot change template bindings.
|
|
637
|
+
*/
|
|
638
|
+
function isAstroCallbackFinding(file, src, start) {
|
|
639
|
+
const ast = ts.createSourceFile(file, src, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
|
|
640
|
+
let found = false;
|
|
641
|
+
const inspect = (node) => {
|
|
642
|
+
if (ts.isIdentifier(node) && node.getStart(ast) === start && isCallbackDeclaration(node))
|
|
643
|
+
found = true;
|
|
644
|
+
if (!found)
|
|
645
|
+
ts.forEachChild(node, inspect);
|
|
646
|
+
};
|
|
647
|
+
inspect(ast);
|
|
648
|
+
return found;
|
|
649
|
+
}
|
|
650
|
+
/**
|
|
651
|
+
* Applies safe renames identified by namingFindings and updates bound references and parameter docs.
|
|
652
|
+
*
|
|
653
|
+
* @param cfg Quality configuration defining the source files and protected names eligible for safe renaming.
|
|
654
|
+
*
|
|
655
|
+
* @return Number of source files whose declarations or parameter documentation changed.
|
|
656
|
+
*/
|
|
657
|
+
export function fixNaming(cfg) {
|
|
658
|
+
const files = srcFiles(cfg).filter(f => C_M_JS_JSX_TS_TSX_U_REGEX.test(f));
|
|
659
|
+
const findings = namingFindings(cfg).filter(f => f.safe !== false && C_M_JS_JSX_TS_TSX_U_REGEX.test(f.file));
|
|
660
|
+
if (!findings.length)
|
|
661
|
+
return 0;
|
|
662
|
+
const astroByFile = new Map();
|
|
663
|
+
const virtualFiles = new Map();
|
|
664
|
+
for (const file of files.filter(f => f.endsWith('.astro'))) {
|
|
665
|
+
const absoluteFile = resolve(cfg.root, file);
|
|
666
|
+
const frontmatter = astroFrontmatter(readFileSync(absoluteFile, 'utf8'));
|
|
667
|
+
if (!frontmatter)
|
|
668
|
+
continue;
|
|
669
|
+
const virtualFile = `${absoluteFile}.__terra_astro_frontmatter__.ts`;
|
|
670
|
+
astroByFile.set(absoluteFile, { file: absoluteFile, virtualFile, offset: frontmatter.offset, source: frontmatter.source });
|
|
671
|
+
virtualFiles.set(resolve(virtualFile), { file: absoluteFile, offset: frontmatter.offset, source: frontmatter.source });
|
|
672
|
+
}
|
|
673
|
+
const virtualSrcs = new Map([...virtualFiles].map(([file, entry]) => [file, entry.source]));
|
|
674
|
+
const fileNames = new Set(files.map(file => {
|
|
675
|
+
const absoluteFile = resolve(cfg.root, file);
|
|
676
|
+
return astroByFile.get(absoluteFile)?.virtualFile ?? absoluteFile;
|
|
677
|
+
}));
|
|
678
|
+
// hygiene-ignore single-use -- Keep one compiler-options object for every language-service host callback.
|
|
679
|
+
const options = {
|
|
680
|
+
allowJs: true,
|
|
681
|
+
checkJs: false,
|
|
682
|
+
jsx: ts.JsxEmit.Preserve,
|
|
683
|
+
module: ts.ModuleKind.ESNext,
|
|
684
|
+
moduleResolution: ts.ModuleResolutionKind.Bundler,
|
|
685
|
+
skipLibCheck: true,
|
|
686
|
+
target: ts.ScriptTarget.ESNext,
|
|
687
|
+
};
|
|
688
|
+
const service = ts.createLanguageService({
|
|
689
|
+
getCompilationSettings: () => options,
|
|
690
|
+
getScriptFileNames: () => [...fileNames],
|
|
691
|
+
getScriptVersion: () => '0',
|
|
692
|
+
getScriptSnapshot: f => {
|
|
693
|
+
const text = virtualSrcs.get(resolve(f)) ?? ts.sys.readFile(f);
|
|
694
|
+
return text === undefined ? undefined : ts.ScriptSnapshot.fromString(text);
|
|
695
|
+
},
|
|
696
|
+
getCurrentDirectory: () => cfg.root,
|
|
697
|
+
getDefaultLibFileName: ts.getDefaultLibFilePath,
|
|
698
|
+
fileExists: f => virtualSrcs.has(resolve(f)) || ts.sys.fileExists(f),
|
|
699
|
+
readFile: f => virtualSrcs.get(resolve(f)) ?? ts.sys.readFile(f),
|
|
700
|
+
readDirectory: ts.sys.readDirectory,
|
|
701
|
+
}, ts.createDocumentRegistry());
|
|
702
|
+
const edits = new Map();
|
|
703
|
+
const identifiersByFile = new Map();
|
|
704
|
+
for (const finding of findings) {
|
|
705
|
+
const srcFile = resolve(cfg.root, finding.file);
|
|
706
|
+
const astro = astroByFile.get(srcFile);
|
|
707
|
+
if (finding.file.endsWith('.astro') && !astro)
|
|
708
|
+
continue;
|
|
709
|
+
const fileName = astro?.virtualFile ?? srcFile;
|
|
710
|
+
const start = astro ? finding.start - astro.offset : finding.start;
|
|
711
|
+
if (astro && !isAstroCallbackFinding(fileName, astro.source, start))
|
|
712
|
+
continue;
|
|
713
|
+
if (!fileNames.has(fileName))
|
|
714
|
+
continue;
|
|
715
|
+
const locations = service.findRenameLocations(fileName, start, false, false, true) ?? [];
|
|
716
|
+
for (const location of locations) {
|
|
717
|
+
const locationFile = resolve(location.fileName);
|
|
718
|
+
const virtual = virtualFiles.get(locationFile);
|
|
719
|
+
const file = virtual?.file ?? locationFile;
|
|
720
|
+
let identifierSpans = identifiersByFile.get(locationFile);
|
|
721
|
+
if (!identifierSpans) {
|
|
722
|
+
identifierSpans = new Set();
|
|
723
|
+
const src = ts.createSourceFile(locationFile, virtual?.source ?? readFileSync(file, 'utf8'), ts.ScriptTarget.Latest, true, /\.(?:tsx|jsx)$/u.test(locationFile) ? ts.ScriptKind.TSX : /\.(?:mjs|cjs|js)$/u.test(locationFile) ? ts.ScriptKind.JS : ts.ScriptKind.TS);
|
|
724
|
+
const collectIdentifiers = (node) => {
|
|
725
|
+
if (ts.isJSDoc(node))
|
|
726
|
+
return;
|
|
727
|
+
if (ts.isIdentifier(node)) {
|
|
728
|
+
const start = node.getStart(src);
|
|
729
|
+
identifierSpans.add(`${start}:${node.end - start}`);
|
|
730
|
+
}
|
|
731
|
+
ts.forEachChild(node, collectIdentifiers);
|
|
732
|
+
};
|
|
733
|
+
collectIdentifiers(src);
|
|
734
|
+
identifiersByFile.set(locationFile, identifierSpans);
|
|
735
|
+
}
|
|
736
|
+
if (!identifierSpans.has(`${location.textSpan.start}:${location.textSpan.length}`))
|
|
737
|
+
continue;
|
|
738
|
+
const keyed = edits.get(file) ?? new Map();
|
|
739
|
+
const edit = { start: location.textSpan.start + (virtual?.offset ?? 0), length: location.textSpan.length, text: `${location.prefixText ?? ''}${finding.suggestion}${location.suffixText ?? ''}` };
|
|
740
|
+
keyed.set(`${edit.start}:${edit.length}`, edit);
|
|
741
|
+
edits.set(file, keyed);
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
let changedFiles = 0;
|
|
745
|
+
for (const [file, replacements] of edits) {
|
|
746
|
+
const orig = readFileSync(file, 'utf8');
|
|
747
|
+
if (hasGeneratedSourceHeader(orig))
|
|
748
|
+
continue;
|
|
749
|
+
const updated = [...replacements.values()].sort((a, b) => b.start - a.start).reduce((s, e) => `${s.slice(0, e.start)}${e.text}${s.slice(e.start + e.length)}`, orig);
|
|
750
|
+
if (updated === orig)
|
|
751
|
+
continue;
|
|
752
|
+
writeFileSync(file, updated);
|
|
753
|
+
changedFiles += 1;
|
|
754
|
+
}
|
|
755
|
+
service.dispose();
|
|
756
|
+
return changedFiles;
|
|
757
|
+
}
|
|
758
|
+
/**
|
|
759
|
+
* Finds and optionally removes declarations that only rename another symbol.
|
|
760
|
+
*
|
|
761
|
+
* @param cfg Quality configuration defining the source scope and protected identifiers.
|
|
762
|
+
* @param analysisOrFix Shared analysis context, or the legacy boolean autofix flag.
|
|
763
|
+
* @param legacyFix Autofix flag used when an analysis context is supplied.
|
|
764
|
+
*
|
|
765
|
+
* @return Safe local-binding aliases that can be removed, whether or not autofix is enabled.
|
|
766
|
+
*/
|
|
767
|
+
export function aliasFindings(cfg, analysisOrFix = false, legacyFix = false) {
|
|
768
|
+
const analysis = typeof analysisOrFix === 'object' ? analysisOrFix : undefined;
|
|
769
|
+
const files = (analysis?.files ?? srcFiles(cfg)).filter(f => /\.(?:c|m)?(?:js|ts)x?$/.test(f));
|
|
770
|
+
const srcTexts = analysis?.texts ?? new Map(files.map(f => [f, readFileSync(f, 'utf8')]));
|
|
771
|
+
const program = analysis?.program ??
|
|
772
|
+
ts.createProgram(files, {
|
|
773
|
+
allowJs: true,
|
|
774
|
+
checkJs: false,
|
|
775
|
+
jsx: ts.JsxEmit.Preserve,
|
|
776
|
+
module: ts.ModuleKind.ESNext,
|
|
777
|
+
moduleResolution: ts.ModuleResolutionKind.Bundler,
|
|
778
|
+
skipLibCheck: true,
|
|
779
|
+
target: ts.ScriptTarget.ESNext,
|
|
780
|
+
});
|
|
781
|
+
const checker = analysis?.checker ?? program.getTypeChecker();
|
|
782
|
+
const exportedSymbols = new Set();
|
|
783
|
+
const exportedNames = new Set();
|
|
784
|
+
for (const file of program.getSourceFiles()) {
|
|
785
|
+
const module = checker.getSymbolAtLocation(file);
|
|
786
|
+
if (module)
|
|
787
|
+
for (const symbol of checker.getExportsOfModule(module)) {
|
|
788
|
+
exportedNames.add(symbol.name);
|
|
789
|
+
exportedSymbols.add(symbol.flags & ts.SymbolFlags.Alias ? checker.getAliasedSymbol(symbol) : symbol);
|
|
790
|
+
}
|
|
791
|
+
for (const statement of file.statements) {
|
|
792
|
+
if (!ts.isExportDeclaration(statement) || !statement.exportClause || !ts.isNamedExports(statement.exportClause))
|
|
793
|
+
continue;
|
|
794
|
+
for (const specifier of statement.exportClause.elements)
|
|
795
|
+
exportedNames.add(specifier.name.text);
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
const findings = [];
|
|
799
|
+
for (const src of program.getSourceFiles()) {
|
|
800
|
+
if (!files.includes(src.fileName))
|
|
801
|
+
continue;
|
|
802
|
+
const aliases = [];
|
|
803
|
+
const inspect = (node) => {
|
|
804
|
+
if (ts.isVariableStatement(node) &&
|
|
805
|
+
(node.declarationList.flags & ts.NodeFlags.Const) !== 0 &&
|
|
806
|
+
node.declarationList.declarations.length === 1 &&
|
|
807
|
+
ts.isIdentifier(node.declarationList.declarations[0].name)) {
|
|
808
|
+
const declaration = node.declarationList.declarations[0];
|
|
809
|
+
const name = declaration.name;
|
|
810
|
+
const tgt = declaration.initializer && aliasTgt(declaration.initializer);
|
|
811
|
+
const symbol = tgt && checker.getSymbolAtLocation(name);
|
|
812
|
+
const targetSymbol = tgt
|
|
813
|
+
? (checker.getSymbolAtLocation(tgt) ??
|
|
814
|
+
(ts.isElementAccessExpression(tgt) && ts.isStringLiteral(tgt.argumentExpression)
|
|
815
|
+
? checker.getPropertyOfType(checker.getTypeAtLocation(tgt.expression), tgt.argumentExpression.text)
|
|
816
|
+
: undefined))
|
|
817
|
+
: undefined;
|
|
818
|
+
if (tgt &&
|
|
819
|
+
ts.isIdentifier(tgt) &&
|
|
820
|
+
symbol &&
|
|
821
|
+
targetSymbol &&
|
|
822
|
+
symbol !== targetSymbol &&
|
|
823
|
+
isStableLocalBinding(targetSymbol) &&
|
|
824
|
+
!exportedSymbols.has(symbol) &&
|
|
825
|
+
!exportedNames.has(name.text) &&
|
|
826
|
+
!node.modifiers?.some(m => m.kind === ts.SyntaxKind.ExportKeyword))
|
|
827
|
+
aliases.push({ statement: node, name, symbol, replacement: tgt.getText(src), kind: 'const', targetSymbol, targetName: tgt.text });
|
|
828
|
+
}
|
|
829
|
+
if (ts.isTypeAliasDeclaration(node) && node.typeParameters === undefined && ts.isTypeReferenceNode(node.type) && ts.isIdentifier(node.type.typeName) && node.type.typeArguments === undefined) {
|
|
830
|
+
const symbol = checker.getSymbolAtLocation(node.name);
|
|
831
|
+
const targetSymbol = checker.getSymbolAtLocation(node.type.typeName);
|
|
832
|
+
if (symbol &&
|
|
833
|
+
targetSymbol &&
|
|
834
|
+
symbol !== targetSymbol &&
|
|
835
|
+
!exportedSymbols.has(symbol) &&
|
|
836
|
+
!exportedNames.has(node.name.text) &&
|
|
837
|
+
!(function externallyReferenced(files, srcTexts, src, name) {
|
|
838
|
+
const pattern = new RegExp(`\\b${name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\b`);
|
|
839
|
+
return files.some(f => f !== src && pattern.test(srcTexts.get(f) ?? ''));
|
|
840
|
+
})(files, srcTexts, src.fileName, node.name.text))
|
|
841
|
+
aliases.push({ statement: node, name: node.name, symbol, replacement: node.type.getText(src), kind: 'type', targetSymbol, targetName: node.type.typeName.getText(src) });
|
|
842
|
+
}
|
|
843
|
+
ts.forEachChild(node, inspect);
|
|
844
|
+
};
|
|
845
|
+
inspect(src);
|
|
846
|
+
if (!aliases.length)
|
|
847
|
+
continue;
|
|
848
|
+
const edits = [];
|
|
849
|
+
const symbols = new Map(aliases.map(a => [a.symbol, a]));
|
|
850
|
+
const shadowed = new Set();
|
|
851
|
+
const chained = new Set();
|
|
852
|
+
for (const alias of aliases) {
|
|
853
|
+
const tgtAlias = alias.targetSymbol && symbols.get(alias.targetSymbol);
|
|
854
|
+
if (tgtAlias) {
|
|
855
|
+
chained.add(alias);
|
|
856
|
+
chained.add(tgtAlias);
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
const visit = (node) => {
|
|
860
|
+
if (aliases.some(a => a.statement === node))
|
|
861
|
+
return;
|
|
862
|
+
if (ts.isIdentifier(node)) {
|
|
863
|
+
const alias = symbols.get((ts.isShorthandPropertyAssignment(node.parent) && node.parent.name === node
|
|
864
|
+
? (checker.getShorthandAssignmentValueSymbol(node.parent) ?? checker.getSymbolAtLocation(node))
|
|
865
|
+
: checker.getSymbolAtLocation(node)));
|
|
866
|
+
if (alias && node !== alias.name) {
|
|
867
|
+
if (alias.targetSymbol && checker.getSymbolsInScope(node, ts.SymbolFlags.Type | ts.SymbolFlags.Value).find(c => c.name === alias.targetName) !== alias.targetSymbol) {
|
|
868
|
+
shadowed.add(alias);
|
|
869
|
+
return;
|
|
870
|
+
}
|
|
871
|
+
edits.push({
|
|
872
|
+
start: node.getStart(src),
|
|
873
|
+
end: node.end,
|
|
874
|
+
text: ts.isShorthandPropertyAssignment(node.parent) && node.parent.name === node ? `${node.text}: ${alias.replacement}` : alias.replacement,
|
|
875
|
+
alias,
|
|
876
|
+
});
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
ts.forEachChild(node, visit);
|
|
880
|
+
};
|
|
881
|
+
visit(src);
|
|
882
|
+
const sAliases = aliases.filter(a => !shadowed.has(a) && !chained.has(a));
|
|
883
|
+
if (!sAliases.length)
|
|
884
|
+
continue;
|
|
885
|
+
const sAliasSet = new Set(sAliases);
|
|
886
|
+
edits.splice(0, edits.length, ...edits.filter(e => sAliasSet.has(e.alias)));
|
|
887
|
+
for (const alias of sAliases) {
|
|
888
|
+
findings.push({
|
|
889
|
+
file: displayPath(cfg, src.fileName),
|
|
890
|
+
line: src.getLineAndCharacterOfPosition(alias.name.getStart(src)).line + 1,
|
|
891
|
+
kind: alias.kind,
|
|
892
|
+
name: alias.name.text,
|
|
893
|
+
replacement: alias.replacement,
|
|
894
|
+
});
|
|
895
|
+
edits.push({ start: alias.statement.getFullStart(), end: alias.statement.end, text: '', alias });
|
|
896
|
+
}
|
|
897
|
+
if (typeof analysisOrFix === 'boolean' ? analysisOrFix : legacyFix) {
|
|
898
|
+
const orig = readFileSync(src.fileName, 'utf8');
|
|
899
|
+
if (hasGeneratedSourceHeader(orig))
|
|
900
|
+
continue;
|
|
901
|
+
const updated = edits.sort((a, b) => b.start - a.start).reduce((t, e) => `${t.slice(0, e.start)}${e.text}${t.slice(e.end)}`, orig);
|
|
902
|
+
if (updated !== orig)
|
|
903
|
+
writeFileSync(src.fileName, updated);
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
return findings;
|
|
907
|
+
}
|
|
908
|
+
//# sourceMappingURL=naming.js.map
|