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