@ontrails/regrade 1.0.0-beta.32 → 1.0.0-beta.41

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.
@@ -0,0 +1,96 @@
1
+ import type { GovernedVocabularyTransition } from '@ontrails/warden';
2
+ import { listGovernedVocabularyTransitions } from '@ontrails/warden';
3
+
4
+ import type {
5
+ VocabularyPreserveRule,
6
+ VocabularyRegradePlan,
7
+ } from './vocabulary.js';
8
+
9
+ const pluralizeVocabularyForm = (value: string): string =>
10
+ value.endsWith('s') || value.endsWith('x') || value.endsWith('ch')
11
+ ? `${value}es`
12
+ : `${value}s`;
13
+
14
+ const defaultSourceForms = (value: string): readonly string[] =>
15
+ /^[A-Za-z]+$/.test(value) ? [value, pluralizeVocabularyForm(value)] : [value];
16
+
17
+ const preserveRulesFromTransition = (
18
+ transition: GovernedVocabularyTransition
19
+ ): readonly VocabularyPreserveRule[] =>
20
+ transition.preserve.map((rule) => ({
21
+ ...(rule.paths === undefined ? {} : { paths: rule.paths }),
22
+ pattern: rule.pattern,
23
+ reason: rule.reason,
24
+ }));
25
+
26
+ const scopeFromTransition = (
27
+ transition: GovernedVocabularyTransition
28
+ ): VocabularyRegradePlan['scope'] | undefined => {
29
+ const { scope } = transition;
30
+ if (scope === undefined) {
31
+ return undefined;
32
+ }
33
+ return {
34
+ ...(scope.exclude === undefined ? {} : { exclude: scope.exclude }),
35
+ ...(scope.extensions === undefined ? {} : { extensions: scope.extensions }),
36
+ ...(scope.ignoredDirectories === undefined
37
+ ? {}
38
+ : { ignoredDirectories: scope.ignoredDirectories }),
39
+ ...(scope.include === undefined ? {} : { include: scope.include }),
40
+ };
41
+ };
42
+
43
+ const defaultFormsAreRegistrySafe = (
44
+ transition: GovernedVocabularyTransition
45
+ ): boolean => {
46
+ if (transition.target.kind !== 'single') {
47
+ return false;
48
+ }
49
+
50
+ return defaultSourceForms(transition.from).every((form) => {
51
+ const replacement = transition.safeRewriteForms[form];
52
+ return replacement !== undefined && !transition.reviewForms.includes(form);
53
+ });
54
+ };
55
+
56
+ export const vocabularyRegradePlanFromTransition = (
57
+ transition: GovernedVocabularyTransition
58
+ ): VocabularyRegradePlan | null => {
59
+ if (
60
+ transition.target.kind !== 'single' ||
61
+ !defaultFormsAreRegistrySafe(transition)
62
+ ) {
63
+ return null;
64
+ }
65
+
66
+ const scope = scopeFromTransition(transition);
67
+ return {
68
+ caseSensitive: true,
69
+ deferForms: transition.reviewForms,
70
+ from: transition.from,
71
+ id: transition.id,
72
+ intent: transition.intent,
73
+ kind: 'vocabulary',
74
+ overrides: transition.safeRewriteForms,
75
+ preserve: preserveRulesFromTransition(transition),
76
+ ...(scope === undefined ? {} : { scope }),
77
+ to: transition.target.to,
78
+ };
79
+ };
80
+
81
+ export const listVocabularyRegradePlansFromRegistry =
82
+ (): readonly VocabularyRegradePlan[] =>
83
+ listGovernedVocabularyTransitions()
84
+ .map(vocabularyRegradePlanFromTransition)
85
+ .filter((plan): plan is VocabularyRegradePlan => plan !== null);
86
+
87
+ export const vocabularyRegradeTransitionForInput = (
88
+ from: string,
89
+ to: string
90
+ ): GovernedVocabularyTransition | undefined =>
91
+ listGovernedVocabularyTransitions().find(
92
+ (transition) =>
93
+ transition.from === from &&
94
+ transition.target.kind === 'single' &&
95
+ transition.target.to === to
96
+ );