@featurevisor/core 4.4.0 → 4.6.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 (108) hide show
  1. package/lib/cli/options.js +10 -2
  2. package/lib/cli/options.js.map +1 -1
  3. package/lib/config/projectConfig.d.ts +10 -0
  4. package/lib/config/projectConfig.js +8 -0
  5. package/lib/config/projectConfig.js.map +1 -1
  6. package/lib/convert/check.js +58 -24
  7. package/lib/convert/check.js.map +1 -1
  8. package/lib/convert/documents.d.ts +6 -21
  9. package/lib/convert/documents.js +8 -49
  10. package/lib/convert/documents.js.map +1 -1
  11. package/lib/convert/index.d.ts +1 -1
  12. package/lib/convert/index.js +14 -2
  13. package/lib/convert/index.js.map +1 -1
  14. package/lib/convert/plan.d.ts +15 -3
  15. package/lib/convert/plan.js +109 -2
  16. package/lib/convert/plan.js.map +1 -1
  17. package/lib/convert/run.js +24 -7
  18. package/lib/convert/run.js.map +1 -1
  19. package/lib/datasource/conditionLocations.d.ts +9 -0
  20. package/lib/datasource/conditionLocations.js +70 -0
  21. package/lib/datasource/conditionLocations.js.map +1 -0
  22. package/lib/datasource/datasource.d.ts +9 -0
  23. package/lib/datasource/datasource.js +18 -6
  24. package/lib/datasource/datasource.js.map +1 -1
  25. package/lib/datasource/expressions.d.ts +36 -0
  26. package/lib/datasource/expressions.js +118 -0
  27. package/lib/datasource/expressions.js.map +1 -0
  28. package/lib/datasource/filesystemAdapter.js +10 -20
  29. package/lib/datasource/filesystemAdapter.js.map +1 -1
  30. package/lib/datasource/testKeys.d.ts +46 -0
  31. package/lib/datasource/testKeys.js +161 -0
  32. package/lib/datasource/testKeys.js.map +1 -0
  33. package/lib/diff/conditions.d.ts +23 -0
  34. package/lib/diff/conditions.js +86 -0
  35. package/lib/diff/conditions.js.map +1 -0
  36. package/lib/diff/details.d.ts +7 -1
  37. package/lib/diff/details.js +54 -25
  38. package/lib/diff/details.js.map +1 -1
  39. package/lib/diff/index.d.ts +5 -0
  40. package/lib/diff/index.js +17 -2
  41. package/lib/diff/index.js.map +1 -1
  42. package/lib/evaluate/explain.d.ts +4 -1
  43. package/lib/evaluate/explain.js +35 -14
  44. package/lib/evaluate/explain.js.map +1 -1
  45. package/lib/evaluate/index.js +9 -5
  46. package/lib/evaluate/index.js.map +1 -1
  47. package/lib/formatter/expressions.d.ts +21 -0
  48. package/lib/formatter/expressions.js +79 -0
  49. package/lib/formatter/expressions.js.map +1 -0
  50. package/lib/formatter/formatDefinition.d.ts +18 -4
  51. package/lib/formatter/formatDefinition.js +54 -14
  52. package/lib/formatter/formatDefinition.js.map +1 -1
  53. package/lib/formatter/formatProject.d.ts +2 -0
  54. package/lib/formatter/formatProject.js +5 -3
  55. package/lib/formatter/formatProject.js.map +1 -1
  56. package/lib/formatter/index.js +4 -0
  57. package/lib/formatter/index.js.map +1 -1
  58. package/lib/formatter/printFormatResult.d.ts +2 -1
  59. package/lib/formatter/printFormatResult.js +4 -2
  60. package/lib/formatter/printFormatResult.js.map +1 -1
  61. package/lib/formatter/run.js +6 -1
  62. package/lib/formatter/run.js.map +1 -1
  63. package/lib/formatter/simplify.d.ts +14 -0
  64. package/lib/formatter/simplify.js +21 -0
  65. package/lib/formatter/simplify.js.map +1 -0
  66. package/lib/formatter/style.d.ts +11 -0
  67. package/lib/formatter/style.js +11 -0
  68. package/lib/formatter/style.js.map +1 -1
  69. package/lib/linter/conditionKinds.js +2 -54
  70. package/lib/linter/conditionKinds.js.map +1 -1
  71. package/lib/linter/conditionSchema.js +42 -13
  72. package/lib/linter/conditionSchema.js.map +1 -1
  73. package/lib/linter/diagnostics.d.ts +2 -2
  74. package/lib/linter/expressionDiagnostics.d.ts +28 -0
  75. package/lib/linter/expressionDiagnostics.js +127 -0
  76. package/lib/linter/expressionDiagnostics.js.map +1 -0
  77. package/lib/linter/featureSchema.d.ts +2 -2
  78. package/lib/linter/lintProject.d.ts +7 -0
  79. package/lib/linter/lintProject.js +345 -54
  80. package/lib/linter/lintProject.js.map +1 -1
  81. package/lib/linter/listTypes.d.ts +7 -0
  82. package/lib/linter/listTypes.js +19 -0
  83. package/lib/linter/listTypes.js.map +1 -0
  84. package/lib/linter/printError.d.ts +2 -0
  85. package/lib/linter/printError.js +1 -0
  86. package/lib/linter/printError.js.map +1 -1
  87. package/lib/linter/requiredVariablesSchema.js +129 -13
  88. package/lib/linter/requiredVariablesSchema.js.map +1 -1
  89. package/lib/migrate/migrateProject.d.ts +4 -0
  90. package/lib/migrate/migrateProject.js +42 -1
  91. package/lib/migrate/migrateProject.js.map +1 -1
  92. package/lib/migrate/project.d.ts +2 -0
  93. package/lib/migrate/project.js +1 -1
  94. package/lib/migrate/project.js.map +1 -1
  95. package/lib/migrate/run.js +42 -1
  96. package/lib/migrate/run.js.map +1 -1
  97. package/lib/promoter/index.js +47 -2
  98. package/lib/promoter/index.js.map +1 -1
  99. package/lib/rewrite/conditions.d.ts +73 -0
  100. package/lib/rewrite/conditions.js +485 -0
  101. package/lib/rewrite/conditions.js.map +1 -0
  102. package/lib/rewrite/documents.d.ts +25 -0
  103. package/lib/rewrite/documents.js +54 -0
  104. package/lib/rewrite/documents.js.map +1 -0
  105. package/lib/rewrite/lines.d.ts +1 -1
  106. package/lib/rewrite/lines.js +3 -2
  107. package/lib/rewrite/lines.js.map +1 -1
  108. package/package.json +5 -4
@@ -64,17 +64,17 @@ const contextWarnings_1 = require("./contextWarnings");
64
64
  const compositionWarnings_1 = require("./compositionWarnings");
65
65
  const diagnostics_1 = require("./diagnostics");
66
66
  const parseError_1 = require("../datasource/parseError");
67
+ const testKeys_1 = require("../datasource/testKeys");
68
+ const expressions_1 = require("@featurevisor/expressions");
69
+ const expressions_2 = require("../datasource/expressions");
70
+ const expressionDiagnostics_1 = require("./expressionDiagnostics");
67
71
  const attributeNamespaces_1 = require("../datasource/attributeNamespaces");
68
- const ENTITY_NAME_REGEX = /^[a-zA-Z0-9_\-./]+$/;
69
72
  const ENTITY_NAME_REGEX_ERROR = "Names must be alphanumeric and can contain _, -, /, ., and the configured namespace character";
70
73
  const ATTRIBUTE_NAME_REGEX = /^[a-zA-Z0-9_\-]+$/;
71
74
  const ATTRIBUTE_NAME_REGEX_ERROR = "Attribute names must be alphanumeric and can contain _ and -. Dots, directory separators and the configured namespace character are not allowed.";
72
- function escapeRegExp(value) {
73
- return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
74
- }
75
+ /** One rule for key shapes, shared with expressions, which read a string of this shape as a segment. */
75
76
  function isValidEntityKey(key, namespaceCharacter) {
76
- const keyWithoutNamespaceCharacter = key.replace(new RegExp(escapeRegExp(namespaceCharacter), "g"), "");
77
- return ENTITY_NAME_REGEX.test(keyWithoutNamespaceCharacter);
77
+ return (0, expressions_1.isEntityKey)(key, { namespaceCharacter });
78
78
  }
79
79
  function isReservedKey(key, reservedKeys) {
80
80
  return reservedKeys.includes(key);
@@ -229,16 +229,228 @@ async function lintProject(deps, options = {}) {
229
229
  console.log(` Authors: ${authors.join(", ")}\n`);
230
230
  }
231
231
  }
232
+ // Expressions: problems inside them point to the exact file, line and column.
233
+ const locateExpression = (0, expressionDiagnostics_1.createExpressionLocator)(datasource.getExtension() === "yml");
234
+ /** The expressions of the definition being linted, so its problems can be located. */
235
+ let expressionContext;
236
+ /** Code frames to print, kept out of JSON output. */
237
+ const expressionFrames = new WeakMap();
238
+ /** Aliased YAML repeats expressions, so one location is reported once. */
239
+ const reportedExpressionLocations = new Set();
240
+ /** Definitions for hints inside expressions, filled once they are loaded. */
241
+ const expressionLookup = { attributes: {}, features: {}, variables: {} };
242
+ function locateExpressionItem(item, fullPath, stringPath, range) {
243
+ const location = locateExpression(fullPath, stringPath, range.start, range.end);
244
+ if (!location) {
245
+ return {
246
+ ...item,
247
+ message: `${item.message} (line ${range.start.line}, column ${range.start.column} of the expression)`,
248
+ };
249
+ }
250
+ const located = {
251
+ ...item,
252
+ line: location.line,
253
+ column: location.column,
254
+ endLine: location.endLine,
255
+ endColumn: location.endColumn,
256
+ };
257
+ expressionFrames.set(located, location.frame);
258
+ return located;
259
+ }
260
+ /** Points a problem on a leaf compiled from an expression at its place in the expression. */
261
+ function withExpressionLocation(item) {
262
+ if (item.line !== undefined)
263
+ return item;
264
+ const range = expressionContext
265
+ ? (0, expressionDiagnostics_1.findExpressionRange)(expressionContext.records, item.path)
266
+ : undefined;
267
+ if (!expressionContext || !range) {
268
+ // In a project writing expressions, advise them for plain strings too.
269
+ return projectConfig.conditionExpressions
270
+ ? { ...item, message: rewordForExpressions(item.message) }
271
+ : item;
272
+ }
273
+ return locateExpressionItem({ ...item, message: rewordForExpressions(item.message) }, expressionContext.fullPath, range.record.path, range);
274
+ }
275
+ /** Tree advice in an expression's own terms. */
276
+ function rewordForExpressions(message) {
277
+ const state = /^Unknown feature variable "([^"]+)"\. To require feature "([^"]+)" to be enabled, leave out "variable"\.$/.exec(message);
278
+ if (state) {
279
+ return `Unknown feature variable "${state[1]}". feature('${state[2]}') alone requires the feature to be enabled, and feature('${state[2]}') == false requires it to be off.`;
280
+ }
281
+ const unknownSegment = /^Unknown segment "([^"]+)"\./.exec(message);
282
+ if (!unknownSegment)
283
+ return message;
284
+ const name = unknownSegment[1];
285
+ const prefix = `Unknown segment "${name}".`;
286
+ if (Object.prototype.hasOwnProperty.call(expressionLookup.attributes, name)) {
287
+ const type = expressionLookup.attributes[name].type;
288
+ const example = type === "boolean"
289
+ ? `${name} == true`
290
+ : type === "string"
291
+ ? `${name} == '...'`
292
+ : type === "integer" || type === "number"
293
+ ? `${name} >= 1`
294
+ : `${name} exists`;
295
+ return `${prefix} "${name}" is an attribute: compare it, such as ${example}.`;
296
+ }
297
+ if (Object.prototype.hasOwnProperty.call(expressionLookup.features, name)) {
298
+ return `${prefix} "${name}" is a feature: write feature('${name}').`;
299
+ }
300
+ if (Object.prototype.hasOwnProperty.call(expressionLookup.variables, name)) {
301
+ return `${prefix} "${name}" is a global variable: compare it, such as variable('${name}') == true.`;
302
+ }
303
+ return message;
304
+ }
305
+ function isRepeatedExpressionLocation(item) {
306
+ if (item.line === undefined && !String(item.code).startsWith("expression_"))
307
+ return false;
308
+ const id = JSON.stringify([
309
+ item.filePath,
310
+ item.line ?? item.path.join("."),
311
+ item.column,
312
+ item.code,
313
+ item.message,
314
+ ]);
315
+ if (reportedExpressionLocations.has(id))
316
+ return true;
317
+ reportedExpressionLocations.add(id);
318
+ return false;
319
+ }
320
+ function printExpressionDetails(item, print = console.log) {
321
+ const frame = expressionFrames.get(item);
322
+ if (item.line !== undefined && frame) {
323
+ print(` ${item.filePath}:${item.line}:${item.column}`);
324
+ for (const line of frame)
325
+ print(` ${line}`);
326
+ }
327
+ if (item.hint)
328
+ print(` Hint: ${item.hint}`);
329
+ }
330
+ /** Records an error, located inside an expression where it applies. Undefined when already reported. */
232
331
  function recordError(error) {
233
- errors.push(error);
332
+ const item = withExpressionLocation(error);
333
+ if (isRepeatedExpressionLocation(item))
334
+ return undefined;
335
+ errors.push(item);
336
+ return item;
234
337
  }
235
338
  function reportWarning(warning) {
236
- warnings.push(warning);
237
- if (!isJsonMode)
238
- console.log(` => Warning: ${warning.message} (${warning.entityType} ${warning.key}: ${warning.path.join(".")})`);
339
+ const item = withExpressionLocation(warning);
340
+ if (isRepeatedExpressionLocation(item))
341
+ return;
342
+ warnings.push(item);
343
+ if (!isJsonMode) {
344
+ console.log(` => Warning: ${item.message} (${item.entityType} ${item.key}: ${item.path.join(".")})`);
345
+ printExpressionDetails(item);
346
+ }
347
+ }
348
+ /**
349
+ * Reports what expansion found while reading a definition: expressions that do not compile, their
350
+ * warnings, expressions in a project that has not allowed them, and checks that need the
351
+ * project's attributes.
352
+ */
353
+ async function reportConditionExpressions(entityType, key, fullPath, records, definition) {
354
+ const filePath = path.relative(process.cwd(), fullPath);
355
+ for (const record of records) {
356
+ if (!projectConfig.conditionExpressions) {
357
+ const lines = record.source.split("\n");
358
+ const item = locateExpressionItem({
359
+ filePath,
360
+ entityType,
361
+ key,
362
+ path: record.path,
363
+ code: "expression_not_allowed",
364
+ message: "Condition expressions are not allowed in this project.",
365
+ hint: "Set conditionExpressions: true in featurevisor.config.js to write conditions as expressions, or write this condition as a YAML tree.",
366
+ }, fullPath, record.path, {
367
+ start: { offset: 0, line: 1, column: 1 },
368
+ end: {
369
+ offset: record.source.length,
370
+ line: lines.length,
371
+ column: lines[lines.length - 1].length + 1,
372
+ },
373
+ });
374
+ await printExpressionError(item, fullPath);
375
+ }
376
+ for (const error of record.errors) {
377
+ const item = locateExpressionItem({
378
+ filePath,
379
+ entityType,
380
+ key,
381
+ path: record.path,
382
+ code: error.code,
383
+ message: error.message,
384
+ ...(error.hint ? { hint: error.hint } : {}),
385
+ }, fullPath, record.path, error);
386
+ await printExpressionError(item, fullPath);
387
+ }
388
+ for (const warning of record.warnings) {
389
+ const item = locateExpressionItem({
390
+ filePath,
391
+ entityType,
392
+ key,
393
+ path: record.path,
394
+ code: warning.code,
395
+ message: warning.message,
396
+ ...(warning.hint ? { hint: warning.hint } : {}),
397
+ }, fullPath, record.path, warning);
398
+ reportWarning(item);
399
+ }
400
+ }
401
+ if (definition)
402
+ reportExpressionSemverText(entityType, key, fullPath, records, definition);
403
+ }
404
+ /** `appVersion == '5.5.0'` compares text, which is rarely meant for a version attribute. */
405
+ function reportExpressionSemverText(entityType, key, fullPath, records, definition) {
406
+ for (const record of records) {
407
+ for (const span of record.spans) {
408
+ let leaf = definition;
409
+ for (const part of span.path) {
410
+ leaf =
411
+ leaf && typeof leaf === "object" ? leaf[part] : undefined;
412
+ }
413
+ if (!leaf || typeof leaf !== "object")
414
+ continue;
415
+ const condition = leaf;
416
+ if (typeof condition.attribute !== "string")
417
+ continue;
418
+ if (condition.operator !== "equals" && condition.operator !== "notEquals")
419
+ continue;
420
+ const attribute = expressionLookup.attributes[condition.attribute];
421
+ if (!attribute || attribute.format !== "semver")
422
+ continue;
423
+ if (typeof condition.value !== "string")
424
+ continue;
425
+ const symbol = condition.operator === "equals" ? "==" : "!=";
426
+ reportWarning(locateExpressionItem({
427
+ filePath: path.relative(process.cwd(), fullPath),
428
+ entityType,
429
+ key,
430
+ path: span.path,
431
+ code: "expression_semver_text",
432
+ message: `"${condition.attribute}" holds versions, so ${symbol} compares them as text.`,
433
+ hint: `Write ${condition.attribute} ${symbol} semver('${condition.value}') to compare versions, so 5.5 matches 5.5.0.`,
434
+ }, fullPath, record.path, span.operator ?? span));
435
+ }
436
+ }
437
+ }
438
+ async function printExpressionError(item, fullPath) {
439
+ if (isRepeatedExpressionLocation(item))
440
+ return;
441
+ errors.push(item);
442
+ if (!isJsonMode) {
443
+ await printEntityHeader(item.entityType, item.key, fullPath);
444
+ console.log(cliFormat_1.CLI_FORMAT_RED, ` => Error: ${item.message}`);
445
+ printExpressionDetails(item);
446
+ // Inside an expression the code frame shows the place. The tree path would name keys the file lacks.
447
+ if (item.line === undefined || !expressionFrames.has(item))
448
+ console.log(" Path:", item.path.join(".") || "(root)");
449
+ console.log("");
450
+ }
239
451
  }
240
452
  async function reportSimpleError({ entityType, key, fullPath, message, detail, pathParts = [], code, value, }) {
241
- recordError({
453
+ const item = recordError({
242
454
  filePath: path.relative(process.cwd(), fullPath),
243
455
  entityType,
244
456
  key,
@@ -247,12 +459,13 @@ async function lintProject(deps, options = {}) {
247
459
  code,
248
460
  value,
249
461
  });
250
- if (!isJsonMode) {
462
+ if (!isJsonMode && item) {
251
463
  await printEntityHeader(entityType, key, fullPath);
252
- console.log(cliFormat_1.CLI_FORMAT_RED, ` => Error: ${message}`);
464
+ console.log(cliFormat_1.CLI_FORMAT_RED, ` => Error: ${item.message}`);
253
465
  if (detail) {
254
466
  console.log(cliFormat_1.CLI_FORMAT_RED, ` ${detail}`);
255
467
  }
468
+ printExpressionDetails(item);
256
469
  console.log("");
257
470
  }
258
471
  }
@@ -263,6 +476,11 @@ async function lintProject(deps, options = {}) {
263
476
  typeof error.featurevisorFilePath === "string"
264
477
  ? error.featurevisorFilePath
265
478
  : undefined;
479
+ if (error instanceof expressions_2.ConditionExpressionError) {
480
+ // Reported against the definition holding the expression, whoever read it.
481
+ await reportConditionExpressions(error.entityType, error.entityKey, getFullPathFromKey(error.entityType, error.entityKey), error.expressions, error.definition);
482
+ return;
483
+ }
266
484
  const targetPath = pathFromError || fullPath;
267
485
  const message = error instanceof Error ? error.message : String(error);
268
486
  if (error instanceof parseError_1.EntityParseError) {
@@ -310,49 +528,52 @@ async function lintProject(deps, options = {}) {
310
528
  }
311
529
  }
312
530
  async function reportZodValidationError(entityType, key, fullPath, error, { ignoreUnknownReferences = false } = {}) {
313
- const issues = (0, printError_1.getLintIssuesFromZodError)(error).filter((issue) => !(ignoreUnknownReferences && issue.unknownReference));
314
- if (issues.length === 0)
531
+ const issues = (0, printError_1.getLintIssuesFromZodError)(error).filter((issue) => !(ignoreUnknownReferences && issue.unknownReference) &&
532
+ // An expression that did not compile is reported once, as itself.
533
+ !(expressionContext && (0, expressionDiagnostics_1.isInsideFailedExpression)(expressionContext.records, issue.path)));
534
+ // checks stricter than earlier versions had warn until the next major version
535
+ for (const issue of issues.filter((issue) => issue.warning)) {
536
+ reportWarning({
537
+ filePath: path.relative(process.cwd(), fullPath),
538
+ entityType,
539
+ key,
540
+ message: issue.message,
541
+ path: issue.path,
542
+ code: issue.code,
543
+ });
544
+ }
545
+ const errors = issues.filter((issue) => !issue.warning);
546
+ if (errors.length === 0)
315
547
  return;
316
- const issuesWithTargetPath = [];
317
- for (const issue of issues) {
318
- const targetPath = fullPath;
319
- const pathRelativeToFile = issue.path;
320
- issuesWithTargetPath.push({ issue, targetPath, pathRelativeToFile });
321
- recordError({
322
- filePath: path.relative(process.cwd(), targetPath),
548
+ const items = [];
549
+ for (const issue of errors) {
550
+ const item = recordError({
551
+ filePath: path.relative(process.cwd(), fullPath),
323
552
  entityType,
324
553
  key,
325
554
  message: issue.message,
326
- path: pathRelativeToFile,
555
+ path: issue.path,
327
556
  code: issue.code,
328
557
  value: issue.value,
329
558
  });
559
+ if (item)
560
+ items.push(item);
330
561
  }
331
- if (!isJsonMode) {
332
- if (entityType === "feature" && issuesWithTargetPath.length > 0) {
333
- const byPath = new Map();
334
- for (const { issue, targetPath, pathRelativeToFile } of issuesWithTargetPath) {
335
- const pathKey = targetPath;
336
- if (!byPath.has(pathKey))
337
- byPath.set(pathKey, []);
338
- byPath.get(pathKey).push({ issue, pathRelativeToFile });
339
- }
340
- for (const [targetPathKey, groupItems] of byPath) {
341
- await printEntityHeader(entityType, key, targetPathKey);
342
- for (const { issue, pathRelativeToFile } of groupItems) {
343
- console.log(cliFormat_1.CLI_FORMAT_RED, ` => Error: ${issue.message}`);
344
- console.log(" Path:", pathRelativeToFile.join(".") || "(root)");
345
- if (typeof issue.value !== "undefined" && issue.value !== "undefined") {
346
- console.log(" Value:", issue.value);
347
- }
348
- console.log("");
349
- }
350
- }
351
- }
352
- else {
353
- await printEntityHeader(entityType, key, fullPath);
354
- (0, printError_1.printLintIssues)(issues);
562
+ if (isJsonMode || items.length === 0)
563
+ return;
564
+ await printEntityHeader(entityType, key, fullPath);
565
+ // Features have always printed to stdout and other definitions to stderr.
566
+ const print = entityType === "feature" ? console.log : console.error;
567
+ for (const item of items) {
568
+ print(cliFormat_1.CLI_FORMAT_RED, ` => Error: ${item.message}`);
569
+ printExpressionDetails(item, print);
570
+ // Inside an expression the code frame shows the place. The tree path would name keys the file lacks.
571
+ if (item.line === undefined || !expressionFrames.has(item))
572
+ print(" Path:", item.path.join(".") || "(root)");
573
+ if (typeof item.value !== "undefined" && item.value !== "undefined") {
574
+ print(" Value:", item.value);
355
575
  }
576
+ print("");
356
577
  }
357
578
  }
358
579
  const keyPattern = options.keyPattern
@@ -471,7 +692,11 @@ async function lintProject(deps, options = {}) {
471
692
  try {
472
693
  variablesByKey[key] = await datasource.readVariable(key);
473
694
  }
474
- catch {
695
+ catch (error) {
696
+ // An invalid expression is reported below. Its valid parts still serve as a reference.
697
+ if (error instanceof expressions_2.ConditionExpressionError) {
698
+ variablesByKey[key] = error.definition;
699
+ }
475
700
  // Variable read failures are reported during variable linting below.
476
701
  }
477
702
  }
@@ -482,7 +707,10 @@ async function lintProject(deps, options = {}) {
482
707
  try {
483
708
  featuresByKey[key] = await datasource.readFeature(key);
484
709
  }
485
- catch {
710
+ catch (error) {
711
+ if (error instanceof expressions_2.ConditionExpressionError) {
712
+ featuresByKey[key] = error.definition;
713
+ }
486
714
  // Feature read failures are reported during feature linting below.
487
715
  }
488
716
  }
@@ -493,10 +721,16 @@ async function lintProject(deps, options = {}) {
493
721
  try {
494
722
  segmentsByKey[key] = await datasource.readSegment(key);
495
723
  }
496
- catch {
724
+ catch (error) {
725
+ if (error instanceof expressions_2.ConditionExpressionError) {
726
+ segmentsByKey[key] = error.definition;
727
+ }
497
728
  // Read errors are reported alongside the segment's schema diagnostics.
498
729
  }
499
730
  }
731
+ expressionLookup.attributes = attributesByKey;
732
+ expressionLookup.features = featuresByKey;
733
+ expressionLookup.variables = variablesByKey;
500
734
  const referenceIndex = {
501
735
  attributes: attributesByKey,
502
736
  segments: segmentsByKey,
@@ -567,7 +801,9 @@ async function lintProject(deps, options = {}) {
567
801
  });
568
802
  }
569
803
  try {
570
- const parsed = await datasource.readSegment(key);
804
+ const parsed = segmentsByKey[key] ?? (await datasource.readSegment(key));
805
+ expressionContext = { fullPath, records: (0, expressions_2.getConditionExpressions)(parsed) };
806
+ await reportConditionExpressions("segment", key, fullPath, expressionContext.records, parsed);
571
807
  const archived = isArchived(parsed);
572
808
  const result = (archived ? isolatedSegmentZodSchema : segmentZodSchema).safeParse(parsed, diagnostics_1.diagnosticParseOptions);
573
809
  if (!result.success && "error" in result) {
@@ -582,6 +818,9 @@ async function lintProject(deps, options = {}) {
582
818
  catch (error) {
583
819
  await reportThrownError("segment", key, fullPath, error);
584
820
  }
821
+ finally {
822
+ expressionContext = undefined;
823
+ }
585
824
  }
586
825
  }
587
826
  const featureZodSchema = (0, featureSchema_1.getFeatureZodSchema)(projectConfig, conditionsZodSchema, flattenedAttributes, segments, features, schemas, schemasByKey, featuresByKey, variablesByKey);
@@ -629,6 +868,8 @@ async function lintProject(deps, options = {}) {
629
868
  let parsed;
630
869
  try {
631
870
  parsed = featuresByKey[key] ?? (await datasource.readFeature(key));
871
+ expressionContext = { fullPath, records: (0, expressions_2.getConditionExpressions)(parsed) };
872
+ await reportConditionExpressions("feature", key, fullPath, expressionContext.records, parsed);
632
873
  const archived = isArchived(parsed);
633
874
  const result = (archived ? isolatedFeatureZodSchema : featureZodSchema).safeParse(parsed, diagnostics_1.diagnosticParseOptions);
634
875
  if (!result.success && "error" in result) {
@@ -655,6 +896,9 @@ async function lintProject(deps, options = {}) {
655
896
  catch (error) {
656
897
  await reportThrownError("feature", key, fullPath, error);
657
898
  }
899
+ finally {
900
+ expressionContext = undefined;
901
+ }
658
902
  }
659
903
  }
660
904
  // lint global variables
@@ -702,6 +946,8 @@ async function lintProject(deps, options = {}) {
702
946
  }
703
947
  try {
704
948
  const parsed = variablesByKey[key] ?? (await datasource.readVariable(key));
949
+ expressionContext = { fullPath, records: (0, expressions_2.getConditionExpressions)(parsed) };
950
+ await reportConditionExpressions("variable", key, fullPath, expressionContext.records, parsed);
705
951
  currentVariableKey = key;
706
952
  const archived = isArchived(parsed);
707
953
  const result = (archived ? isolatedVariableZodSchema : variableZodSchema).safeParse(parsed, diagnostics_1.diagnosticParseOptions);
@@ -727,6 +973,9 @@ async function lintProject(deps, options = {}) {
727
973
  catch (error) {
728
974
  await reportThrownError("variable", key, fullPath, error);
729
975
  }
976
+ finally {
977
+ expressionContext = undefined;
978
+ }
730
979
  }
731
980
  }
732
981
  // lint schemas (schemas and schemasByKey already loaded above for feature linting)
@@ -839,8 +1088,50 @@ async function lintProject(deps, options = {}) {
839
1088
  });
840
1089
  }
841
1090
  }
1091
+ // Spec files that share a test key stop tests and scenarios from being listed, so lint
1092
+ // reports every collision as an issue of its own and checks the specs once they are fixed
1093
+ let testKeyCollisionsReported = false;
1094
+ async function reportTestKeyCollisions(collisions) {
1095
+ for (const collision of collisions) {
1096
+ if (keyPattern && !keyPattern.test(collision.key))
1097
+ continue;
1098
+ const [kept, ...others] = collision.filePaths;
1099
+ for (const filePath of others) {
1100
+ const rename = collision.renames.find((candidate) => candidate.from === filePath);
1101
+ await reportSimpleError({
1102
+ entityType: "test",
1103
+ key: collision.key,
1104
+ fullPath: filePath,
1105
+ message: `Test key "${collision.key}" is also used by ${path.relative(process.cwd(), kept)}.`,
1106
+ detail: `A test key drops a final ".spec", so both files are one test. Rename this one${rename ? `, for example to ${path.basename(rename.to)}` : ""}.`,
1107
+ code: "test_key_collision",
1108
+ });
1109
+ }
1110
+ }
1111
+ if (!isJsonMode) {
1112
+ console.log(cliFormat_1.CLI_FORMAT_YELLOW, "Test specs are linted once their keys are unique.");
1113
+ console.log("");
1114
+ }
1115
+ }
1116
+ async function listSpecs(list) {
1117
+ try {
1118
+ return await list();
1119
+ }
1120
+ catch (error) {
1121
+ if (!(error instanceof testKeys_1.TestKeyCollisionError))
1122
+ throw error;
1123
+ if (!testKeyCollisionsReported) {
1124
+ testKeyCollisionsReported = true;
1125
+ if (!options.entityType ||
1126
+ options.entityType === "test" ||
1127
+ options.entityType === "scenario")
1128
+ await reportTestKeyCollisions(error.collisions);
1129
+ }
1130
+ return [];
1131
+ }
1132
+ }
842
1133
  // lint tests
843
- const tests = await datasource.listTests();
1134
+ const tests = await listSpecs(() => datasource.listTests());
844
1135
  const testsZodSchema = (0, testSchema_1.getTestsZodSchema)(projectConfig, features, segments, targets, variables, featuresByKey, segmentsByKey);
845
1136
  if (!options.entityType || options.entityType === "test") {
846
1137
  await lintReservedNamespaceCharacterInEntityPaths("test");
@@ -886,7 +1177,7 @@ async function lintProject(deps, options = {}) {
886
1177
  }
887
1178
  }
888
1179
  // lint scenarios
889
- const scenarios = await datasource.listScenarios();
1180
+ const scenarios = await listSpecs(() => datasource.listScenarios());
890
1181
  const scenariosZodSchema = (0, testSchema_1.getScenariosZodSchema)(projectConfig, features, segments, targets, variables, featuresByKey);
891
1182
  if (!options.entityType || options.entityType === "scenario") {
892
1183
  if (options.entityType === "scenario") {
@@ -959,7 +1250,7 @@ async function lintProject(deps, options = {}) {
959
1250
  ? variables[0] || ""
960
1251
  : features[0] || "";
961
1252
  const message = error instanceof Error ? error.message : String(error);
962
- if (error instanceof parseError_1.EntityParseError) {
1253
+ if (error instanceof parseError_1.EntityParseError || error instanceof expressions_2.ConditionExpressionError) {
963
1254
  await reportThrownError(entityType, key, getFullPathFromKey(entityType, key), error);
964
1255
  }
965
1256
  else if (!message.startsWith("Legacy feature `required` was removed") ||