@featurevisor/core 3.1.0 → 3.3.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.
Files changed (213) hide show
  1. package/lib/datasource/datasource.d.ts +1 -1
  2. package/lib/datasource/datasource.js +4 -14
  3. package/lib/datasource/datasource.js.map +1 -1
  4. package/lib/datasource/requiredFeatures.d.ts +7 -0
  5. package/lib/datasource/requiredFeatures.js +38 -0
  6. package/lib/datasource/requiredFeatures.js.map +1 -0
  7. package/lib/linter/checkCircularDependency.d.ts +1 -1
  8. package/lib/linter/checkCircularDependency.js +3 -22
  9. package/lib/linter/checkCircularDependency.js.map +1 -1
  10. package/lib/linter/conditionSchema.js +27 -3
  11. package/lib/linter/conditionSchema.js.map +1 -1
  12. package/lib/linter/testSchema.d.ts +4 -0
  13. package/lib/linter/testSchema.js +46 -4
  14. package/lib/linter/testSchema.js.map +1 -1
  15. package/lib/promoter/index.js +62 -1
  16. package/lib/promoter/index.js.map +1 -1
  17. package/lib/tester/testFeature.js +17 -16
  18. package/lib/tester/testFeature.js.map +1 -1
  19. package/package.json +10 -7
  20. package/CHANGELOG.md +0 -2246
  21. package/coverage/clover.xml +0 -5456
  22. package/coverage/coverage-final.json +0 -64
  23. package/coverage/lcov-report/base.css +0 -224
  24. package/coverage/lcov-report/block-navigation.js +0 -87
  25. package/coverage/lcov-report/favicon.png +0 -0
  26. package/coverage/lcov-report/index.html +0 -371
  27. package/coverage/lcov-report/prettify.css +0 -1
  28. package/coverage/lcov-report/prettify.js +0 -2
  29. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  30. package/coverage/lcov-report/sorter.js +0 -210
  31. package/coverage/lcov-report/src/assess-distribution/index.html +0 -116
  32. package/coverage/lcov-report/src/assess-distribution/index.ts.html +0 -754
  33. package/coverage/lcov-report/src/benchmark/index.html +0 -116
  34. package/coverage/lcov-report/src/benchmark/index.ts.html +0 -793
  35. package/coverage/lcov-report/src/builder/allocator.ts.html +0 -220
  36. package/coverage/lcov-report/src/builder/applyContextToConditions.ts.html +0 -502
  37. package/coverage/lcov-report/src/builder/applyContextToDatafile.ts.html +0 -772
  38. package/coverage/lcov-report/src/builder/applyContextToSegments.ts.html +0 -580
  39. package/coverage/lcov-report/src/builder/buildDatafile.ts.html +0 -2053
  40. package/coverage/lcov-report/src/builder/buildProject.ts.html +0 -1117
  41. package/coverage/lcov-report/src/builder/buildRuntimeDatafiles.ts.html +0 -256
  42. package/coverage/lcov-report/src/builder/getFeatureRanges.ts.html +0 -268
  43. package/coverage/lcov-report/src/builder/hashes.ts.html +0 -412
  44. package/coverage/lcov-report/src/builder/index.html +0 -311
  45. package/coverage/lcov-report/src/builder/index.ts.html +0 -91
  46. package/coverage/lcov-report/src/builder/mutateVariables.ts.html +0 -421
  47. package/coverage/lcov-report/src/builder/mutator.ts.html +0 -796
  48. package/coverage/lcov-report/src/builder/revision.ts.html +0 -151
  49. package/coverage/lcov-report/src/builder/traffic.ts.html +0 -661
  50. package/coverage/lcov-report/src/cli/cli.ts.html +0 -400
  51. package/coverage/lcov-report/src/cli/index.html +0 -131
  52. package/coverage/lcov-report/src/cli/plugins.ts.html +0 -214
  53. package/coverage/lcov-report/src/config/index.html +0 -131
  54. package/coverage/lcov-report/src/config/index.ts.html +0 -88
  55. package/coverage/lcov-report/src/config/projectConfig.ts.html +0 -925
  56. package/coverage/lcov-report/src/datasource/adapter.ts.html +0 -253
  57. package/coverage/lcov-report/src/datasource/datasource.ts.html +0 -1000
  58. package/coverage/lcov-report/src/datasource/filesystemAdapter.ts.html +0 -1576
  59. package/coverage/lcov-report/src/datasource/index.html +0 -161
  60. package/coverage/lcov-report/src/datasource/index.ts.html +0 -94
  61. package/coverage/lcov-report/src/evaluate/index.html +0 -116
  62. package/coverage/lcov-report/src/evaluate/index.ts.html +0 -814
  63. package/coverage/lcov-report/src/find-duplicate-segments/findDuplicateSegments.ts.html +0 -313
  64. package/coverage/lcov-report/src/find-duplicate-segments/index.html +0 -131
  65. package/coverage/lcov-report/src/find-duplicate-segments/index.ts.html +0 -307
  66. package/coverage/lcov-report/src/find-usage/index.html +0 -116
  67. package/coverage/lcov-report/src/find-usage/index.ts.html +0 -1780
  68. package/coverage/lcov-report/src/generate-code/index.html +0 -131
  69. package/coverage/lcov-report/src/generate-code/index.ts.html +0 -403
  70. package/coverage/lcov-report/src/generate-code/typescript.ts.html +0 -1909
  71. package/coverage/lcov-report/src/index.html +0 -131
  72. package/coverage/lcov-report/src/info/index.html +0 -116
  73. package/coverage/lcov-report/src/info/index.ts.html +0 -472
  74. package/coverage/lcov-report/src/init/index.html +0 -116
  75. package/coverage/lcov-report/src/init/index.ts.html +0 -280
  76. package/coverage/lcov-report/src/linter/attributeSchema.ts.html +0 -1963
  77. package/coverage/lcov-report/src/linter/checkCircularDependency.ts.html +0 -220
  78. package/coverage/lcov-report/src/linter/checkPercentageExceedingSlot.ts.html +0 -268
  79. package/coverage/lcov-report/src/linter/conditionSchema.ts.html +0 -2224
  80. package/coverage/lcov-report/src/linter/featureSchema.ts.html +0 -5851
  81. package/coverage/lcov-report/src/linter/groupSchema.ts.html +0 -226
  82. package/coverage/lcov-report/src/linter/index.html +0 -326
  83. package/coverage/lcov-report/src/linter/index.ts.html +0 -88
  84. package/coverage/lcov-report/src/linter/lintProject.ts.html +0 -2704
  85. package/coverage/lcov-report/src/linter/mutationNotation.ts.html +0 -1411
  86. package/coverage/lcov-report/src/linter/printError.ts.html +0 -289
  87. package/coverage/lcov-report/src/linter/schema.ts.html +0 -1558
  88. package/coverage/lcov-report/src/linter/segmentSchema.ts.html +0 -139
  89. package/coverage/lcov-report/src/linter/targetSchema.ts.html +0 -223
  90. package/coverage/lcov-report/src/linter/testSchema.ts.html +0 -529
  91. package/coverage/lcov-report/src/linter/zodHelpers.ts.html +0 -133
  92. package/coverage/lcov-report/src/list/index.html +0 -131
  93. package/coverage/lcov-report/src/list/index.ts.html +0 -2065
  94. package/coverage/lcov-report/src/list/matrix.ts.html +0 -721
  95. package/coverage/lcov-report/src/promoter/index.html +0 -116
  96. package/coverage/lcov-report/src/promoter/index.ts.html +0 -3883
  97. package/coverage/lcov-report/src/sets.ts.html +0 -313
  98. package/coverage/lcov-report/src/targeting.ts.html +0 -403
  99. package/coverage/lcov-report/src/tester/cliFormat.ts.html +0 -133
  100. package/coverage/lcov-report/src/tester/helpers.ts.html +0 -313
  101. package/coverage/lcov-report/src/tester/index.html +0 -206
  102. package/coverage/lcov-report/src/tester/prettyDuration.ts.html +0 -187
  103. package/coverage/lcov-report/src/tester/printTestResult.ts.html +0 -337
  104. package/coverage/lcov-report/src/tester/testFeature.ts.html +0 -1186
  105. package/coverage/lcov-report/src/tester/testProject.ts.html +0 -1018
  106. package/coverage/lcov-report/src/tester/testSegment.ts.html +0 -316
  107. package/coverage/lcov-report/src/utils/extractKeys.ts.html +0 -493
  108. package/coverage/lcov-report/src/utils/git.ts.html +0 -559
  109. package/coverage/lcov-report/src/utils/index.html +0 -161
  110. package/coverage/lcov-report/src/utils/index.ts.html +0 -94
  111. package/coverage/lcov-report/src/utils/pretty.ts.html +0 -121
  112. package/coverage/lcov.info +0 -11615
  113. package/jest.config.js +0 -22
  114. package/json-schema/README.md +0 -5
  115. package/json-schema/attribute.json +0 -40
  116. package/json-schema/feature.json +0 -394
  117. package/json-schema/segment.json +0 -137
  118. package/src/assess-distribution/index.ts +0 -223
  119. package/src/benchmark/index.ts +0 -236
  120. package/src/builder/allocator.spec.ts +0 -105
  121. package/src/builder/allocator.ts +0 -45
  122. package/src/builder/applyContextToConditions.spec.ts +0 -2677
  123. package/src/builder/applyContextToConditions.ts +0 -139
  124. package/src/builder/applyContextToDatafile.spec.ts +0 -2626
  125. package/src/builder/applyContextToDatafile.ts +0 -229
  126. package/src/builder/applyContextToSegments.spec.ts +0 -1593
  127. package/src/builder/applyContextToSegments.ts +0 -165
  128. package/src/builder/buildDatafile.spec.ts +0 -445
  129. package/src/builder/buildDatafile.ts +0 -656
  130. package/src/builder/buildProject.ts +0 -344
  131. package/src/builder/buildRuntimeDatafiles.ts +0 -57
  132. package/src/builder/getFeatureRanges.ts +0 -61
  133. package/src/builder/hashes.ts +0 -109
  134. package/src/builder/index.ts +0 -2
  135. package/src/builder/mutateVariables.spec.ts +0 -1179
  136. package/src/builder/mutateVariables.ts +0 -112
  137. package/src/builder/mutator.spec.ts +0 -413
  138. package/src/builder/mutator.ts +0 -237
  139. package/src/builder/revision.spec.ts +0 -22
  140. package/src/builder/revision.ts +0 -22
  141. package/src/builder/traffic.spec.ts +0 -946
  142. package/src/builder/traffic.ts +0 -192
  143. package/src/cli/cli.ts +0 -105
  144. package/src/cli/index.ts +0 -2
  145. package/src/cli/plugins.ts +0 -43
  146. package/src/config/index.ts +0 -1
  147. package/src/config/projectConfig.spec.ts +0 -194
  148. package/src/config/projectConfig.ts +0 -280
  149. package/src/datasource/adapter.ts +0 -56
  150. package/src/datasource/datasource.ts +0 -305
  151. package/src/datasource/filesystemAdapter.spec.ts +0 -212
  152. package/src/datasource/filesystemAdapter.ts +0 -497
  153. package/src/datasource/index.ts +0 -3
  154. package/src/dependencies.ts +0 -13
  155. package/src/evaluate/index.ts +0 -243
  156. package/src/find-duplicate-segments/findDuplicateSegments.ts +0 -76
  157. package/src/find-duplicate-segments/index.ts +0 -74
  158. package/src/find-usage/index.ts +0 -565
  159. package/src/generate-code/index.ts +0 -106
  160. package/src/generate-code/typescript.spec.ts +0 -214
  161. package/src/generate-code/typescript.ts +0 -608
  162. package/src/index.ts +0 -20
  163. package/src/info/index.ts +0 -129
  164. package/src/init/index.ts +0 -65
  165. package/src/linter/attributeSchema.spec.ts +0 -342
  166. package/src/linter/attributeSchema.ts +0 -626
  167. package/src/linter/checkCircularDependency.ts +0 -45
  168. package/src/linter/checkPercentageExceedingSlot.ts +0 -61
  169. package/src/linter/conditionSchema.spec.ts +0 -510
  170. package/src/linter/conditionSchema.ts +0 -713
  171. package/src/linter/featureSchema.spec.ts +0 -1985
  172. package/src/linter/featureSchema.ts +0 -1922
  173. package/src/linter/groupSchema.ts +0 -47
  174. package/src/linter/index.ts +0 -1
  175. package/src/linter/lintProject.spec.ts +0 -336
  176. package/src/linter/lintProject.ts +0 -873
  177. package/src/linter/mutationNotation.spec.ts +0 -704
  178. package/src/linter/mutationNotation.ts +0 -442
  179. package/src/linter/printError.ts +0 -68
  180. package/src/linter/schema.spec.ts +0 -690
  181. package/src/linter/schema.ts +0 -491
  182. package/src/linter/segmentSchema.spec.ts +0 -358
  183. package/src/linter/segmentSchema.ts +0 -18
  184. package/src/linter/targetSchema.spec.ts +0 -159
  185. package/src/linter/targetSchema.ts +0 -46
  186. package/src/linter/testSchema.spec.ts +0 -258
  187. package/src/linter/testSchema.ts +0 -148
  188. package/src/linter/zodHelpers.ts +0 -16
  189. package/src/list/index.spec.ts +0 -135
  190. package/src/list/index.ts +0 -660
  191. package/src/list/matrix.spec.ts +0 -49
  192. package/src/list/matrix.ts +0 -212
  193. package/src/promoter/index.spec.ts +0 -617
  194. package/src/promoter/index.ts +0 -1266
  195. package/src/sets.ts +0 -76
  196. package/src/targeting.spec.ts +0 -74
  197. package/src/targeting.ts +0 -106
  198. package/src/tester/cliFormat.ts +0 -16
  199. package/src/tester/helpers.spec.ts +0 -149
  200. package/src/tester/helpers.ts +0 -76
  201. package/src/tester/index.ts +0 -2
  202. package/src/tester/prettyDuration.ts +0 -34
  203. package/src/tester/printTestResult.ts +0 -84
  204. package/src/tester/testFeature.ts +0 -367
  205. package/src/tester/testProject.spec.ts +0 -50
  206. package/src/tester/testProject.ts +0 -311
  207. package/src/tester/testSegment.ts +0 -77
  208. package/src/utils/extractKeys.ts +0 -136
  209. package/src/utils/git.ts +0 -158
  210. package/src/utils/index.ts +0 -3
  211. package/src/utils/pretty.ts +0 -12
  212. package/tsconfig.cjs.json +0 -13
  213. package/tsconfig.typecheck.json +0 -9
@@ -1,873 +0,0 @@
1
- // for use in node only
2
- import * as fs from "fs";
3
- import * as path from "path";
4
-
5
- import type { Attribute, Schema } from "@featurevisor/types";
6
- import type { ZodError } from "zod";
7
-
8
- import { getAttributeZodSchema } from "./attributeSchema";
9
- import { getConditionsZodSchema } from "./conditionSchema";
10
- import { getSegmentZodSchema } from "./segmentSchema";
11
- import { getGroupZodSchema } from "./groupSchema";
12
- import { getFeatureZodSchema } from "./featureSchema";
13
- import { getSchemaZodSchema } from "./schema";
14
- import { getTestsZodSchema } from "./testSchema";
15
- import { getTargetZodSchema } from "./targetSchema";
16
-
17
- import { checkForCircularDependencyInRequired } from "./checkCircularDependency";
18
- import { checkForFeatureExceedingGroupSlotPercentage } from "./checkPercentageExceedingSlot";
19
- import { getLintIssuesFromZodError, printZodError } from "./printError";
20
- import { Dependencies } from "../dependencies";
21
- import { CLI_FORMAT_BOLD_UNDERLINE, CLI_FORMAT_GREEN, CLI_FORMAT_RED } from "../tester/cliFormat";
22
- import { Plugin } from "../cli";
23
- import { assertProjectSetJsonSelection, getProjectSetExecutions, printSetHeader } from "../sets";
24
-
25
- export type LintEntityType =
26
- | "attribute"
27
- | "segment"
28
- | "feature"
29
- | "group"
30
- | "schema"
31
- | "target"
32
- | "test";
33
-
34
- export interface LintProjectOptions {
35
- keyPattern?: string;
36
- entityType?: string;
37
- authors?: boolean;
38
- json?: boolean;
39
- pretty?: boolean;
40
- }
41
-
42
- export interface LintErrorItem {
43
- filePath: string;
44
- entityType: LintEntityType;
45
- key: string;
46
- message: string;
47
- path: (string | number)[];
48
- code?: string;
49
- value?: unknown;
50
- }
51
-
52
- export interface LintResult {
53
- hasError: boolean;
54
- errors: LintErrorItem[];
55
- }
56
-
57
- const ENTITY_NAME_REGEX = /^[a-zA-Z0-9_\-./]+$/;
58
- const ENTITY_NAME_REGEX_ERROR =
59
- "Names must be alphanumeric and can contain _, -, /, ., and the configured namespace character";
60
-
61
- const ATTRIBUTE_NAME_REGEX_ERROR =
62
- "Names must be alphanumeric and can contain _, -, /, ., and the configured namespace character";
63
-
64
- function escapeRegExp(value: string): string {
65
- return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
66
- }
67
-
68
- function isValidEntityKey(key: string, namespaceCharacter: string): boolean {
69
- const keyWithoutNamespaceCharacter = key.replace(
70
- new RegExp(escapeRegExp(namespaceCharacter), "g"),
71
- "",
72
- );
73
-
74
- return ENTITY_NAME_REGEX.test(keyWithoutNamespaceCharacter);
75
- }
76
-
77
- function getPathSegmentsFromKey(
78
- key: string,
79
- namespaceCharacter: string,
80
- entityType?: LintEntityType,
81
- ): string[] {
82
- const pathSegments = key.split(namespaceCharacter);
83
-
84
- if (
85
- entityType === "test" &&
86
- pathSegments.length > 1 &&
87
- ["spec", "feature", "segment"].includes(pathSegments[pathSegments.length - 1])
88
- ) {
89
- const suffix = pathSegments[pathSegments.length - 1];
90
- pathSegments[pathSegments.length - 2] = `${pathSegments[pathSegments.length - 2]}.${suffix}`;
91
- pathSegments.pop();
92
- }
93
-
94
- return pathSegments;
95
- }
96
-
97
- function getReservedNameCheckValue(
98
- entityType: LintEntityType,
99
- name: string,
100
- isFile: boolean,
101
- extension: string,
102
- ): string {
103
- let value =
104
- isFile && name.endsWith(`.${extension}`) ? name.slice(0, -extension.length - 1) : name;
105
-
106
- if (entityType === "test" && isFile) {
107
- for (const suffix of [".spec", ".feature", ".segment"]) {
108
- if (value.endsWith(suffix)) {
109
- value = value.slice(0, -suffix.length);
110
- break;
111
- }
112
- }
113
- }
114
-
115
- return value;
116
- }
117
-
118
- async function getAuthorsOfEntity(datasource, entityType, entityKey): Promise<string[]> {
119
- const entries = await datasource.listHistoryEntries(entityType, entityKey);
120
- const authors: string[] = Array.from(new Set(entries.map((entry) => entry.author)));
121
-
122
- return authors;
123
- }
124
-
125
- function collectExplicitAttributePaths(
126
- attributeKey: string,
127
- attribute: Attribute,
128
- result: Set<string>,
129
- ): void {
130
- result.add(attributeKey);
131
-
132
- function visit(prefix: string, schema: Attribute | Attribute["properties"][string]) {
133
- if (!schema || schema.type !== "object" || !schema.properties) {
134
- return;
135
- }
136
-
137
- for (const propertyKey of Object.keys(schema.properties)) {
138
- const propertyPath = `${prefix}.${propertyKey}`;
139
- result.add(propertyPath);
140
- visit(propertyPath, schema.properties[propertyKey]);
141
- }
142
- }
143
-
144
- visit(attributeKey, attribute);
145
- }
146
-
147
- export async function lintProject(
148
- deps: Dependencies,
149
- options: LintProjectOptions = {},
150
- ): Promise<LintResult> {
151
- const { projectConfig, datasource } = deps;
152
-
153
- const isJsonMode = options.json === true;
154
- const errors: LintErrorItem[] = [];
155
-
156
- function log(...args: unknown[]) {
157
- if (!isJsonMode) {
158
- console.log(...args);
159
- }
160
- }
161
-
162
- function getFullPathFromKey(type: LintEntityType, key: string, relative = false) {
163
- const fileName = `${getPathSegmentsFromKey(key, projectConfig.namespaceCharacter, type).join(
164
- path.sep,
165
- )}.${datasource.getExtension()}`;
166
- let fullPath = "";
167
-
168
- if (type === "attribute") {
169
- fullPath = path.join(projectConfig.attributesDirectoryPath, fileName);
170
- } else if (type === "segment") {
171
- fullPath = path.join(projectConfig.segmentsDirectoryPath, fileName);
172
- } else if (type === "feature") {
173
- fullPath = path.join(projectConfig.featuresDirectoryPath, fileName);
174
- } else if (type === "group") {
175
- fullPath = path.join(projectConfig.groupsDirectoryPath, fileName);
176
- } else if (type === "schema") {
177
- fullPath = path.join(projectConfig.schemasDirectoryPath, fileName);
178
- } else if (type === "target") {
179
- fullPath = path.join(projectConfig.targetsDirectoryPath, fileName);
180
- } else {
181
- fullPath = path.join(projectConfig.testsDirectoryPath, fileName);
182
- }
183
-
184
- if (relative) {
185
- fullPath = path.relative(process.cwd(), fullPath);
186
- }
187
-
188
- return fullPath;
189
- }
190
-
191
- function getEntityDirectoryPath(type: LintEntityType): string {
192
- if (type === "attribute") {
193
- return projectConfig.attributesDirectoryPath;
194
- }
195
- if (type === "segment") {
196
- return projectConfig.segmentsDirectoryPath;
197
- }
198
- if (type === "feature") {
199
- return projectConfig.featuresDirectoryPath;
200
- }
201
- if (type === "group") {
202
- return projectConfig.groupsDirectoryPath;
203
- }
204
- if (type === "schema") {
205
- return projectConfig.schemasDirectoryPath;
206
- }
207
- if (type === "target") {
208
- return projectConfig.targetsDirectoryPath;
209
- }
210
-
211
- return projectConfig.testsDirectoryPath;
212
- }
213
-
214
- async function lintReservedNamespaceCharacterInPathNames(
215
- entityType: LintEntityType,
216
- directoryPath: string,
217
- ) {
218
- if (!fs.existsSync(directoryPath)) {
219
- return;
220
- }
221
-
222
- const extension = datasource.getExtension();
223
- const namespaceCharacter = projectConfig.namespaceCharacter;
224
- const entries = fs.readdirSync(directoryPath, { withFileTypes: true });
225
-
226
- for (const entry of entries) {
227
- const entryPath = path.join(directoryPath, entry.name);
228
- const isFile = entry.isFile();
229
- const isDirectory = entry.isDirectory();
230
-
231
- if (!isDirectory && !(isFile && entry.name.endsWith(`.${extension}`))) {
232
- continue;
233
- }
234
-
235
- const nameToCheck = getReservedNameCheckValue(entityType, entry.name, isFile, extension);
236
-
237
- if (nameToCheck.includes(namespaceCharacter)) {
238
- await reportSimpleError({
239
- entityType,
240
- key: path.relative(getEntityDirectoryPath(entityType), entryPath),
241
- fullPath: entryPath,
242
- message: `Invalid file or directory name: "${entry.name}"`,
243
- detail: `Namespace character "${namespaceCharacter}" is not allowed in entity file or directory names.`,
244
- code: "invalid_name",
245
- });
246
- }
247
-
248
- if (isDirectory) {
249
- await lintReservedNamespaceCharacterInPathNames(entityType, entryPath);
250
- }
251
- }
252
- }
253
-
254
- async function lintReservedNamespaceCharacterInEntityPaths(entityType: LintEntityType) {
255
- await lintReservedNamespaceCharacterInPathNames(entityType, getEntityDirectoryPath(entityType));
256
- }
257
-
258
- async function printEntityHeader(entityType: LintEntityType, key: string, fullPath: string) {
259
- if (isJsonMode) {
260
- return;
261
- }
262
-
263
- console.log(CLI_FORMAT_BOLD_UNDERLINE, fullPath);
264
-
265
- if (options.authors) {
266
- const authors = await getAuthorsOfEntity(datasource, entityType, key);
267
- console.log(` Authors: ${authors.join(", ")}\n`);
268
- }
269
- }
270
-
271
- function recordError(error: LintErrorItem) {
272
- errors.push(error);
273
- }
274
-
275
- async function reportSimpleError({
276
- entityType,
277
- key,
278
- fullPath,
279
- message,
280
- detail,
281
- pathParts = [],
282
- code,
283
- value,
284
- }: {
285
- entityType: LintEntityType;
286
- key: string;
287
- fullPath: string;
288
- message: string;
289
- detail?: string;
290
- pathParts?: (string | number)[];
291
- code?: string;
292
- value?: unknown;
293
- }) {
294
- recordError({
295
- filePath: path.relative(process.cwd(), fullPath),
296
- entityType,
297
- key,
298
- message,
299
- path: pathParts,
300
- code,
301
- value,
302
- });
303
-
304
- if (!isJsonMode) {
305
- await printEntityHeader(entityType, key, fullPath);
306
- console.log(CLI_FORMAT_RED, ` => Error: ${message}`);
307
- if (detail) {
308
- console.log(CLI_FORMAT_RED, ` ${detail}`);
309
- }
310
- console.log("");
311
- }
312
- }
313
-
314
- async function reportThrownError(
315
- entityType: LintEntityType,
316
- key: string,
317
- fullPath: string,
318
- error: unknown,
319
- ) {
320
- const pathFromError =
321
- error &&
322
- typeof error === "object" &&
323
- "featurevisorFilePath" in error &&
324
- typeof error.featurevisorFilePath === "string"
325
- ? error.featurevisorFilePath
326
- : undefined;
327
- const targetPath = pathFromError || fullPath;
328
- const message = error instanceof Error ? error.message : String(error);
329
-
330
- recordError({
331
- filePath: path.relative(process.cwd(), targetPath),
332
- entityType,
333
- key,
334
- message,
335
- path: [],
336
- code: error instanceof Error ? error.name : undefined,
337
- });
338
-
339
- if (!isJsonMode) {
340
- await printEntityHeader(entityType, key, targetPath);
341
- console.log("");
342
- console.log(error);
343
- }
344
- }
345
-
346
- async function reportZodValidationError(
347
- entityType: LintEntityType,
348
- key: string,
349
- fullPath: string,
350
- error: ZodError,
351
- ) {
352
- const issues = getLintIssuesFromZodError(error);
353
-
354
- const issuesWithTargetPath: {
355
- issue: (typeof issues)[0];
356
- targetPath: string;
357
- pathRelativeToFile: (string | number)[];
358
- }[] = [];
359
- for (const issue of issues) {
360
- const targetPath = fullPath;
361
- const pathRelativeToFile = issue.path;
362
- issuesWithTargetPath.push({ issue, targetPath, pathRelativeToFile });
363
-
364
- recordError({
365
- filePath: path.relative(process.cwd(), targetPath),
366
- entityType,
367
- key,
368
- message: issue.message,
369
- path: pathRelativeToFile,
370
- code: issue.code,
371
- value: issue.value,
372
- });
373
- }
374
-
375
- if (!isJsonMode) {
376
- if (entityType === "feature" && issuesWithTargetPath.length > 0) {
377
- const byPath = new Map<
378
- string,
379
- {
380
- issue: (typeof issuesWithTargetPath)[0]["issue"];
381
- pathRelativeToFile: (string | number)[];
382
- }[]
383
- >();
384
- for (const { issue, targetPath, pathRelativeToFile } of issuesWithTargetPath) {
385
- const pathKey = targetPath;
386
- if (!byPath.has(pathKey)) byPath.set(pathKey, []);
387
- byPath.get(pathKey)!.push({ issue, pathRelativeToFile });
388
- }
389
- for (const [targetPathKey, groupItems] of byPath) {
390
- await printEntityHeader(entityType, key, targetPathKey);
391
- for (const { issue, pathRelativeToFile } of groupItems) {
392
- console.log(CLI_FORMAT_RED, ` => Error: ${issue.message}`);
393
- console.log(" Path:", pathRelativeToFile.join("."));
394
- if (typeof issue.value !== "undefined" && issue.value !== "undefined") {
395
- console.log(" Value:", issue.value);
396
- }
397
- console.log("");
398
- }
399
- }
400
- } else {
401
- await printEntityHeader(entityType, key, fullPath);
402
- printZodError(error);
403
- }
404
- }
405
- }
406
-
407
- const keyPattern = options.keyPattern ? new RegExp(options.keyPattern) : null;
408
-
409
- if (keyPattern) {
410
- log("");
411
- log(`Linting only keys matching pattern: ${keyPattern}`);
412
- log("");
413
- }
414
-
415
- // List schemas and load parsed schemas for attribute and feature validation.
416
- const schemas = await datasource.listSchemas();
417
- const schemasByKey: Record<string, Schema> = {};
418
- for (const key of schemas) {
419
- try {
420
- schemasByKey[key] = await datasource.readSchema(key);
421
- } catch {
422
- // Schema file may be invalid; skip for cross-entity resolution and let schema linting report it.
423
- }
424
- }
425
-
426
- // lint attributes
427
- const attributes = await datasource.listAttributes();
428
- const attributeZodSchema = getAttributeZodSchema();
429
- const attributesByKey: Record<string, Attribute> = {};
430
-
431
- for (const key of attributes) {
432
- try {
433
- attributesByKey[key] = await datasource.readAttribute(key);
434
- } catch {
435
- // Attribute read failures are reported during attribute linting below.
436
- }
437
- }
438
-
439
- if (!options.entityType || options.entityType === "attribute") {
440
- await lintReservedNamespaceCharacterInEntityPaths("attribute");
441
-
442
- const filteredKeys = !keyPattern
443
- ? attributes
444
- : attributes.filter((key) => keyPattern.test(key));
445
-
446
- if (filteredKeys.length > 0) {
447
- log(`Linting ${filteredKeys.length} attributes...\n`);
448
- }
449
-
450
- for (const key of filteredKeys) {
451
- const fullPath = getFullPathFromKey("attribute", key);
452
-
453
- if (!isValidEntityKey(key, projectConfig.namespaceCharacter)) {
454
- await reportSimpleError({
455
- entityType: "attribute",
456
- key,
457
- fullPath,
458
- message: `Invalid name: "${key}"`,
459
- detail: ATTRIBUTE_NAME_REGEX_ERROR,
460
- code: "invalid_name",
461
- });
462
- }
463
-
464
- try {
465
- const parsed = attributesByKey[key] ?? (await datasource.readAttribute(key));
466
- const result = attributeZodSchema.safeParse(parsed);
467
-
468
- if (!result.success && "error" in result) {
469
- await reportZodValidationError("attribute", key, fullPath, result.error);
470
- }
471
- } catch (error) {
472
- await reportThrownError("attribute", key, fullPath, error);
473
- }
474
- }
475
- }
476
-
477
- const explicitAttributePaths = new Set<string>();
478
- for (const key of attributes) {
479
- const parsed = attributesByKey[key];
480
- if (!parsed) {
481
- continue;
482
- }
483
-
484
- collectExplicitAttributePaths(key, parsed, explicitAttributePaths);
485
- }
486
- const flattenedAttributes = Array.from(explicitAttributePaths);
487
-
488
- // lint segments
489
- const segments = await datasource.listSegments();
490
- const conditionsZodSchema = getConditionsZodSchema(projectConfig, attributesByKey, schemasByKey);
491
- const segmentZodSchema = getSegmentZodSchema(projectConfig, conditionsZodSchema);
492
-
493
- if (!options.entityType || options.entityType === "segment") {
494
- await lintReservedNamespaceCharacterInEntityPaths("segment");
495
-
496
- const filteredKeys = !keyPattern ? segments : segments.filter((key) => keyPattern.test(key));
497
-
498
- if (filteredKeys.length > 0) {
499
- log(`Linting ${filteredKeys.length} segments...\n`);
500
- }
501
-
502
- for (const key of filteredKeys) {
503
- const fullPath = getFullPathFromKey("segment", key);
504
-
505
- if (!isValidEntityKey(key, projectConfig.namespaceCharacter)) {
506
- await reportSimpleError({
507
- entityType: "segment",
508
- key,
509
- fullPath,
510
- message: `Invalid name: "${key}"`,
511
- detail: ENTITY_NAME_REGEX_ERROR,
512
- code: "invalid_name",
513
- });
514
- }
515
-
516
- try {
517
- const parsed = await datasource.readSegment(key);
518
- const result = segmentZodSchema.safeParse(parsed);
519
-
520
- if (!result.success && "error" in result) {
521
- await reportZodValidationError("segment", key, fullPath, result.error);
522
- }
523
- } catch (error) {
524
- await reportThrownError("segment", key, fullPath, error);
525
- }
526
- }
527
- }
528
-
529
- // lint features
530
- const features = await datasource.listFeatures();
531
- const featureZodSchema = getFeatureZodSchema(
532
- projectConfig,
533
- conditionsZodSchema,
534
- flattenedAttributes as [string, ...string[]],
535
- segments as [string, ...string[]],
536
- features as [string, ...string[]],
537
- schemas,
538
- schemasByKey,
539
- );
540
-
541
- if (!options.entityType || options.entityType === "feature") {
542
- await lintReservedNamespaceCharacterInEntityPaths("feature");
543
-
544
- const filteredKeys = !keyPattern ? features : features.filter((key) => keyPattern.test(key));
545
-
546
- if (filteredKeys.length > 0) {
547
- log(`Linting ${filteredKeys.length} features...\n`);
548
- }
549
-
550
- for (const key of filteredKeys) {
551
- const fullPath = getFullPathFromKey("feature", key);
552
-
553
- if (!isValidEntityKey(key, projectConfig.namespaceCharacter)) {
554
- await reportSimpleError({
555
- entityType: "feature",
556
- key,
557
- fullPath,
558
- message: `Invalid name: "${key}"`,
559
- detail: ENTITY_NAME_REGEX_ERROR,
560
- code: "invalid_name",
561
- });
562
- }
563
-
564
- let parsed;
565
-
566
- try {
567
- parsed = await datasource.readFeature(key);
568
-
569
- const result = featureZodSchema.safeParse(parsed);
570
-
571
- if (!result.success && "error" in result) {
572
- await reportZodValidationError("feature", key, fullPath, result.error);
573
- }
574
- } catch (error) {
575
- await reportThrownError("feature", key, fullPath, error);
576
- }
577
-
578
- if (parsed && parsed.required) {
579
- try {
580
- await checkForCircularDependencyInRequired(datasource, key, parsed.required);
581
- } catch (error) {
582
- await reportSimpleError({
583
- entityType: "feature",
584
- key,
585
- fullPath,
586
- message: error instanceof Error ? error.message : String(error),
587
- code: error instanceof Error ? error.name : "error",
588
- });
589
- }
590
- }
591
- }
592
- }
593
-
594
- // lint groups
595
- const groups = await datasource.listGroups();
596
- const groupZodSchema = getGroupZodSchema(projectConfig, datasource, features);
597
-
598
- if (!options.entityType || options.entityType === "group") {
599
- await lintReservedNamespaceCharacterInEntityPaths("group");
600
-
601
- const filteredKeys = !keyPattern ? groups : groups.filter((key) => keyPattern.test(key));
602
-
603
- if (filteredKeys.length > 0) {
604
- log(`Linting ${filteredKeys.length} groups...\n`);
605
- }
606
-
607
- for (const key of filteredKeys) {
608
- const fullPath = getFullPathFromKey("group", key);
609
-
610
- if (!isValidEntityKey(key, projectConfig.namespaceCharacter)) {
611
- await reportSimpleError({
612
- entityType: "group",
613
- key,
614
- fullPath,
615
- message: `Invalid name: "${key}"`,
616
- detail: ENTITY_NAME_REGEX_ERROR,
617
- code: "invalid_name",
618
- });
619
- }
620
-
621
- let parsed;
622
-
623
- try {
624
- parsed = await datasource.readGroup(key);
625
-
626
- const result = groupZodSchema.safeParse(parsed);
627
-
628
- if (!result.success && "error" in result) {
629
- await reportZodValidationError("group", key, fullPath, result.error);
630
- }
631
- } catch (error) {
632
- await reportThrownError("group", key, fullPath, error);
633
- }
634
-
635
- if (parsed) {
636
- try {
637
- await checkForFeatureExceedingGroupSlotPercentage(datasource, parsed, features);
638
- } catch (error) {
639
- await reportSimpleError({
640
- entityType: "group",
641
- key,
642
- fullPath,
643
- message: error instanceof Error ? error.message : String(error),
644
- code: error instanceof Error ? error.name : "error",
645
- });
646
- }
647
- }
648
- }
649
- }
650
-
651
- // @TODO: feature cannot exist in multiple groups
652
-
653
- // lint schemas (schemas and schemasByKey already loaded above for feature linting)
654
- const schemaZodSchema = getSchemaZodSchema(schemas);
655
-
656
- if (!options.entityType || options.entityType === "schema") {
657
- await lintReservedNamespaceCharacterInEntityPaths("schema");
658
-
659
- const filteredKeys = !keyPattern ? schemas : schemas.filter((key) => keyPattern.test(key));
660
-
661
- if (filteredKeys.length > 0) {
662
- log(`Linting ${filteredKeys.length} schemas...\n`);
663
- }
664
-
665
- for (const key of filteredKeys) {
666
- const fullPath = getFullPathFromKey("schema", key);
667
-
668
- if (!isValidEntityKey(key, projectConfig.namespaceCharacter)) {
669
- await reportSimpleError({
670
- entityType: "schema",
671
- key,
672
- fullPath,
673
- message: `Invalid name: "${key}"`,
674
- detail: ENTITY_NAME_REGEX_ERROR,
675
- code: "invalid_name",
676
- });
677
- }
678
-
679
- try {
680
- const parsed = await datasource.readSchema(key);
681
-
682
- const result = schemaZodSchema.safeParse(parsed);
683
-
684
- if (!result.success && "error" in result) {
685
- await reportZodValidationError("schema", key, fullPath, result.error);
686
- }
687
- } catch (error) {
688
- await reportThrownError("schema", key, fullPath, error);
689
- }
690
- }
691
- }
692
-
693
- // lint targets
694
- const targets = await datasource.listTargets();
695
- const targetZodSchema = getTargetZodSchema(projectConfig);
696
-
697
- if (!options.entityType || options.entityType === "target") {
698
- await lintReservedNamespaceCharacterInEntityPaths("target");
699
-
700
- const filteredKeys = !keyPattern ? targets : targets.filter((key) => keyPattern.test(key));
701
-
702
- if (filteredKeys.length > 0) {
703
- log(`Linting ${filteredKeys.length} targets...\n`);
704
- }
705
-
706
- for (const key of filteredKeys) {
707
- const fullPath = getFullPathFromKey("target", key);
708
-
709
- if (!isValidEntityKey(key, projectConfig.namespaceCharacter)) {
710
- await reportSimpleError({
711
- entityType: "target",
712
- key,
713
- fullPath,
714
- message: `Invalid name: "${key}"`,
715
- detail: ENTITY_NAME_REGEX_ERROR,
716
- code: "invalid_name",
717
- });
718
- }
719
-
720
- try {
721
- const parsed = await datasource.readTarget(key);
722
-
723
- const result = targetZodSchema.safeParse(parsed);
724
-
725
- if (!result.success && "error" in result) {
726
- await reportZodValidationError("target", key, fullPath, result.error);
727
- }
728
- } catch (error) {
729
- await reportThrownError("target", key, fullPath, error);
730
- }
731
- }
732
- }
733
-
734
- // lint tests
735
- const tests = await datasource.listTests();
736
-
737
- const testsZodSchema = getTestsZodSchema(
738
- projectConfig,
739
- features as [string, ...string[]],
740
- segments as [string, ...string[]],
741
- targets as [string, ...string[]],
742
- );
743
-
744
- if (!options.entityType || options.entityType === "test") {
745
- await lintReservedNamespaceCharacterInEntityPaths("test");
746
-
747
- const filteredKeys = !keyPattern ? tests : tests.filter((key) => keyPattern.test(key));
748
-
749
- if (filteredKeys.length > 0) {
750
- log(`Linting ${filteredKeys.length} tests...\n`);
751
- }
752
-
753
- for (const key of filteredKeys) {
754
- const fullPath = getFullPathFromKey("test", key);
755
-
756
- if (!isValidEntityKey(key, projectConfig.namespaceCharacter)) {
757
- await reportSimpleError({
758
- entityType: "test",
759
- key,
760
- fullPath,
761
- message: `Invalid name: "${key}"`,
762
- detail: ENTITY_NAME_REGEX_ERROR,
763
- code: "invalid_name",
764
- });
765
- }
766
-
767
- try {
768
- const parsed = await datasource.readTest(key);
769
-
770
- const result = testsZodSchema.safeParse(parsed);
771
-
772
- if (!result.success && "error" in result) {
773
- await reportZodValidationError("test", key, fullPath, result.error);
774
- }
775
- } catch (error) {
776
- await reportThrownError("test", key, fullPath, error);
777
- }
778
- }
779
- }
780
-
781
- return {
782
- hasError: errors.length > 0,
783
- errors,
784
- };
785
- }
786
-
787
- export const lintPlugin: Plugin = {
788
- command: "lint",
789
- handler: async function (options) {
790
- const { rootDirectoryPath, projectConfig, datasource, parsed } = options;
791
-
792
- assertProjectSetJsonSelection(projectConfig, parsed.set, parsed.json);
793
-
794
- const executions = await getProjectSetExecutions(projectConfig, datasource, parsed.set);
795
- let hasError = false;
796
-
797
- for (const execution of executions) {
798
- printSetHeader(projectConfig, execution.set, parsed.json);
799
-
800
- const result = await lintProject(
801
- {
802
- rootDirectoryPath,
803
- projectConfig: execution.projectConfig,
804
- datasource: execution.datasource,
805
- options: parsed,
806
- },
807
- {
808
- keyPattern: parsed.keyPattern,
809
- entityType: parsed.entityType,
810
- authors: parsed.authors,
811
- json: parsed.json,
812
- pretty: parsed.pretty,
813
- },
814
- );
815
-
816
- if (parsed.json) {
817
- const payload = { errors: result.errors };
818
- console.log(parsed.pretty ? JSON.stringify(payload, null, 2) : JSON.stringify(payload));
819
- }
820
-
821
- if (result.hasError) {
822
- hasError = true;
823
- }
824
- }
825
-
826
- if (hasError) {
827
- return false;
828
- }
829
-
830
- if (!parsed.json) {
831
- console.log("");
832
- console.log(CLI_FORMAT_GREEN, "✔ No lint errors found");
833
- }
834
- },
835
- examples: [
836
- {
837
- command: "lint",
838
- description: "lint all entities",
839
- },
840
- {
841
- command: "lint --entityType=feature",
842
- description: "lint only features",
843
- },
844
- {
845
- command: "lint --entityType=segment",
846
- description: "lint only segments",
847
- },
848
- {
849
- command: "lint --entityType=group",
850
- description: "lint only groups",
851
- },
852
- {
853
- command: "lint --entityType=schema",
854
- description: "lint only schemas",
855
- },
856
- {
857
- command: "lint --entityType=test",
858
- description: "lint only tests",
859
- },
860
- {
861
- command: 'lint --keyPattern="abc"',
862
- description: `lint only entities with keys containing "abc"`,
863
- },
864
- {
865
- command: "lint --json",
866
- description: "print lint errors as JSON",
867
- },
868
- {
869
- command: "lint --json --pretty",
870
- description: "print lint errors as pretty JSON",
871
- },
872
- ],
873
- };