@aehrc/ecl-core 1.0.4 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -52271,7 +52271,7 @@ function isValidDescriptionId(sctid) {
52271
52271
  }
52272
52272
  const DEFAULT_USER_AGENT = "ecl-lsp/1.0.0";
52273
52273
  const IS_BROWSER = typeof globalThis !== "undefined" && "document" in globalThis;
52274
- class FhirTerminologyService {
52274
+ const _FhirTerminologyService = class _FhirTerminologyService {
52275
52275
  constructor(options = {}) {
52276
52276
  __publicField(this, "cache", /* @__PURE__ */ new Map());
52277
52277
  __publicField(this, "searchCache", /* @__PURE__ */ new Map());
@@ -52427,6 +52427,46 @@ class FhirTerminologyService {
52427
52427
  return null;
52428
52428
  }
52429
52429
  }
52430
+ async getHistoricalAssociations(conceptId) {
52431
+ const entries = Object.entries(_FhirTerminologyService.ASSOCIATION_REFSETS);
52432
+ const promises = entries.map(async ([refsetId, type2]) => {
52433
+ const targets = await this.translateAssociation(conceptId, refsetId);
52434
+ if (targets.length === 0) return null;
52435
+ return { type: type2, refsetId, targets };
52436
+ });
52437
+ const settled = await Promise.all(promises);
52438
+ const typeOrder = ["same-as", "replaced-by", "possibly-equivalent-to", "alternative"];
52439
+ return typeOrder.map((type2) => settled.find((a) => (a == null ? void 0 : a.type) === type2)).filter((a) => a !== null && a !== void 0);
52440
+ }
52441
+ /** Query a single implicit ConceptMap for historical association targets. */
52442
+ async translateAssociation(conceptId, refsetId) {
52443
+ try {
52444
+ const cmUrl = `${this.snomedSystemUrl}?fhir_cm=${refsetId}`;
52445
+ const targetUrl = "http://snomed.info/sct?fhir_vs";
52446
+ const url = `${this.baseUrl}/ConceptMap/$translate?code=${encodeURIComponent(conceptId)}&system=${encodeURIComponent("http://snomed.info/sct")}&target=${encodeURIComponent(targetUrl)}&url=${encodeURIComponent(cmUrl)}`;
52447
+ const response = await this.fetchWithTimeout(url, this.timeout, {
52448
+ headers: { Accept: "application/fhir+json" }
52449
+ });
52450
+ if (!response.ok) return [];
52451
+ const data = await response.json();
52452
+ const params = data.parameter ?? [];
52453
+ const resultParam = params.find((p) => p.name === "result");
52454
+ if ((resultParam == null ? void 0 : resultParam.valueBoolean) !== true) return [];
52455
+ const targets = [];
52456
+ for (const param of params) {
52457
+ if (param.name !== "match") continue;
52458
+ const parts = param.part ?? [];
52459
+ const conceptPart = parts.find((p) => p.name === "concept");
52460
+ const coding = conceptPart == null ? void 0 : conceptPart.valueCoding;
52461
+ if (coding == null ? void 0 : coding.code) {
52462
+ targets.push({ code: coding.code, display: coding.display ?? "" });
52463
+ }
52464
+ }
52465
+ return targets;
52466
+ } catch {
52467
+ return [];
52468
+ }
52469
+ }
52430
52470
  async validateConcepts(conceptIds) {
52431
52471
  const results = /* @__PURE__ */ new Map();
52432
52472
  if (conceptIds.length === 0) {
@@ -52676,7 +52716,15 @@ Note: Some ECL 2.2 filter syntax (e.g. {{ D id = ... }}) may not be supported by
52676
52716
  }
52677
52717
  return editions;
52678
52718
  }
52679
- }
52719
+ };
52720
+ /** Map of SNOMED CT historical association reference set IDs to association types. */
52721
+ __publicField(_FhirTerminologyService, "ASSOCIATION_REFSETS", {
52722
+ "900000000000527005": "same-as",
52723
+ "900000000000526001": "replaced-by",
52724
+ "900000000000523009": "possibly-equivalent-to",
52725
+ "900000000000530003": "alternative"
52726
+ });
52727
+ let FhirTerminologyService = _FhirTerminologyService;
52680
52728
  function getSymbolText(sym) {
52681
52729
  if (typeof sym === "string") return sym;
52682
52730
  if (sym && typeof sym === "object" && "text" in sym) {
@@ -53144,26 +53192,26 @@ function printAst(ast, sourceText, options) {
53144
53192
  if (!ast) {
53145
53193
  return "";
53146
53194
  }
53147
- return printExpression(ast, 0, 0, options, sourceText);
53195
+ return printExpression$1(ast, 0, 0, options, sourceText);
53148
53196
  }
53149
- function printExpression(node, depth, column, opts, src) {
53197
+ function printExpression$1(node, depth, column, opts, src) {
53150
53198
  const inner = node.expression;
53151
53199
  switch (inner.type) {
53152
53200
  case NodeType.SubExpressionConstraint:
53153
- return printSubExpression(inner, depth, column, opts, src);
53201
+ return printSubExpression$1(inner, depth, column, opts, src);
53154
53202
  case NodeType.CompoundExpression:
53155
- return printCompoundExpression(inner, depth, column, opts, src);
53203
+ return printCompoundExpression$1(inner, depth, column, opts, src);
53156
53204
  case NodeType.RefinedExpression:
53157
- return printRefinedExpression(inner, depth, column, opts, src);
53205
+ return printRefinedExpression$1(inner, depth, column, opts, src);
53158
53206
  case NodeType.DottedExpression:
53159
- return printDottedExpression(inner, depth, column, opts, src);
53207
+ return printDottedExpression$1(inner, depth, column, opts, src);
53160
53208
  default: {
53161
53209
  const _exhaustive = inner;
53162
53210
  return String(_exhaustive);
53163
53211
  }
53164
53212
  }
53165
53213
  }
53166
- function printSubExpression(node, depth, column, opts, src) {
53214
+ function printSubExpression$1(node, depth, column, opts, src) {
53167
53215
  let result = "";
53168
53216
  if (node.operator) {
53169
53217
  result += node.operator.operator + " ";
@@ -53173,22 +53221,29 @@ function printSubExpression(node, depth, column, opts, src) {
53173
53221
  }
53174
53222
  switch (node.focus.type) {
53175
53223
  case NodeType.ConceptReference:
53176
- result += printConceptReference(node.focus);
53224
+ result += printConceptReference$1(node.focus);
53177
53225
  break;
53178
53226
  case NodeType.Wildcard:
53179
53227
  result += "*";
53180
53228
  break;
53181
53229
  case NodeType.ExpressionConstraint: {
53182
53230
  const innerExpr = node.focus;
53231
+ if (opts.removeRedundantParentheses && !node.operator && !node.memberOf) {
53232
+ const inner = innerExpr.expression;
53233
+ if (inner.type !== NodeType.CompoundExpression) {
53234
+ result += printExpression$1(innerExpr, depth, column + result.length, opts, src);
53235
+ break;
53236
+ }
53237
+ }
53183
53238
  const innerIsRefined = innerExpr.expression.type === NodeType.RefinedExpression;
53184
53239
  const innerDepth = innerIsRefined ? depth : depth + 1;
53185
53240
  const openCol = column + result.length;
53186
- const innerInline = printExpression(innerExpr, innerDepth, openCol + 1, opts, src);
53241
+ const innerInline = printExpression$1(innerExpr, innerDepth, openCol + 1, opts, src);
53187
53242
  if (!innerInline.includes("\n") && (opts.maxLineLength <= 0 || openCol + 1 + innerInline.length + 1 <= opts.maxLineLength)) {
53188
53243
  result += "(" + innerInline + ")";
53189
53244
  } else {
53190
53245
  const innerInd = getIndentString(innerDepth, opts);
53191
- const innerMulti = printExpression(innerExpr, innerDepth, innerInd.length, opts, src);
53246
+ const innerMulti = printExpression$1(innerExpr, innerDepth, innerInd.length, opts, src);
53192
53247
  result += "(" + innerMulti + ")";
53193
53248
  }
53194
53249
  break;
@@ -53206,10 +53261,14 @@ function printSubExpression(node, depth, column, opts, src) {
53206
53261
  }
53207
53262
  return result;
53208
53263
  }
53209
- function printCompoundExpression(node, depth, column, opts, src) {
53264
+ function printCompoundExpression$1(node, depth, column, opts, src) {
53210
53265
  const op = node.operator.operator;
53211
53266
  const opSep = opts.spaceAroundOperators ? ` ${op} ` : op;
53212
- const printed = node.operands.map((operand) => printSubExpression(operand, depth, 0, opts, src));
53267
+ let operands = node.operands;
53268
+ if (opts.removeRedundantParentheses && (op === "AND" || op === "OR")) {
53269
+ operands = flattenSameOperatorOperands(operands, op);
53270
+ }
53271
+ const printed = operands.map((operand) => printSubExpression$1(operand, depth, 0, opts, src));
53213
53272
  const inline = printed.join(opSep);
53214
53273
  const fits = opts.maxLineLength <= 0 || column + inline.length <= opts.maxLineLength;
53215
53274
  if (fits && !opts.breakOnOperators) {
@@ -53228,9 +53287,23 @@ function printCompoundExpression(node, depth, column, opts, src) {
53228
53287
  }
53229
53288
  return lines.join("\n");
53230
53289
  }
53231
- function printDottedExpression(node, depth, column, opts, src) {
53232
- const source = printSubExpression(node.source, depth, column, opts, src);
53233
- const dotAttrs = node.attributes.map((a) => printSubExpression(a.attributeName, depth, 0, opts, src));
53290
+ function flattenSameOperatorOperands(operands, operator) {
53291
+ const result = [];
53292
+ for (const operand of operands) {
53293
+ if (operand.focus.type === NodeType.ExpressionConstraint && !operand.operator && !operand.memberOf && !operand.filters && !operand.historySupplement) {
53294
+ const inner = operand.focus.expression;
53295
+ if (inner.type === NodeType.CompoundExpression && inner.operator.operator === operator) {
53296
+ result.push(...flattenSameOperatorOperands(inner.operands, operator));
53297
+ continue;
53298
+ }
53299
+ }
53300
+ result.push(operand);
53301
+ }
53302
+ return result;
53303
+ }
53304
+ function printDottedExpression$1(node, depth, column, opts, src) {
53305
+ const source = printSubExpression$1(node.source, depth, column, opts, src);
53306
+ const dotAttrs = node.attributes.map((a) => printSubExpression$1(a.attributeName, depth, 0, opts, src));
53234
53307
  const inline = source + dotAttrs.map((a) => " . " + a).join("");
53235
53308
  if (opts.maxLineLength <= 0 || column + inline.length <= opts.maxLineLength) {
53236
53309
  return inline;
@@ -53242,9 +53315,9 @@ function printDottedExpression(node, depth, column, opts, src) {
53242
53315
  }
53243
53316
  return lines.join("\n");
53244
53317
  }
53245
- function printRefinedExpression(node, depth, column, opts, src) {
53246
- const focus = printSubExpression(node.expression, depth, column, opts, src);
53247
- const refinement = printRefinement(node.refinement, depth + 1, opts, src);
53318
+ function printRefinedExpression$1(node, depth, column, opts, src) {
53319
+ const focus = printSubExpression$1(node.expression, depth, column, opts, src);
53320
+ const refinement = printRefinement$1(node.refinement, depth + 1, opts, src);
53248
53321
  const inline = focus + ": " + refinement;
53249
53322
  const inlineEndCol = focus.includes("\n") ? lastLineLength(inline) : column + inline.length;
53250
53323
  const fits = opts.maxLineLength <= 0 || inlineEndCol <= opts.maxLineLength;
@@ -53258,42 +53331,42 @@ function printRefinedExpression(node, depth, column, opts, src) {
53258
53331
  const wholeBraces = refSrc.startsWith("{");
53259
53332
  if (wholeBraces) {
53260
53333
  const innerInd = getIndentString(attrDepth + 1, opts);
53261
- const attrs = node.refinement.attributes.map((a) => printAttribute(a, attrDepth + 1, innerInd.length, opts, src));
53334
+ const attrs = node.refinement.attributes.map((a) => printAttribute$1(a, attrDepth + 1, innerInd.length, opts, src));
53262
53335
  return focus + ":\n" + ind + "{\n" + attrs.map((a) => innerInd + a).join(",\n") + "\n" + ind + "}";
53263
53336
  }
53264
53337
  const attrParts = [];
53265
53338
  let prevEnd = node.refinement.range.start.offset;
53266
53339
  for (const attr of node.refinement.attributes) {
53267
- const printed = printAttribute(attr, attrDepth, ind.length, opts, src);
53268
- const inBraces = isAttrInBraceGroup(attr, prevEnd, src);
53340
+ const printed = printAttribute$1(attr, attrDepth, ind.length, opts, src);
53341
+ const inBraces = isAttrInBraceGroup$1(attr, prevEnd, src);
53269
53342
  attrParts.push(ind + (inBraces ? "{ " + printed + " }" : printed));
53270
53343
  prevEnd = attr.range.end.offset;
53271
53344
  }
53272
53345
  return focus + ":\n" + attrParts.join(",\n");
53273
53346
  }
53274
- function isAttrInBraceGroup(attr, prevEnd, src) {
53347
+ function isAttrInBraceGroup$1(attr, prevEnd, src) {
53275
53348
  const gap = src.slice(prevEnd, attr.range.start.offset);
53276
53349
  return gap.includes("{");
53277
53350
  }
53278
- function printRefinement(node, depth, opts, src) {
53351
+ function printRefinement$1(node, depth, opts, src) {
53279
53352
  const parts = [];
53280
53353
  let prevEnd = node.range.start.offset;
53281
53354
  for (const attr of node.attributes) {
53282
- const printed = printAttribute(attr, depth, 0, opts, src);
53283
- const inBraces = isAttrInBraceGroup(attr, prevEnd, src);
53355
+ const printed = printAttribute$1(attr, depth, 0, opts, src);
53356
+ const inBraces = isAttrInBraceGroup$1(attr, prevEnd, src);
53284
53357
  parts.push(inBraces ? "{ " + printed + " }" : printed);
53285
53358
  prevEnd = attr.range.end.offset;
53286
53359
  }
53287
53360
  return parts.join(", ");
53288
53361
  }
53289
- function printAttribute(node, depth, column, opts, src) {
53362
+ function printAttribute$1(node, depth, column, opts, src) {
53290
53363
  const preNameSrc = src.slice(node.range.start.offset, node.name.range.start.offset).trim();
53291
53364
  const cardinalityPrefix = preNameSrc.length > 0 ? preNameSrc + " " : "";
53292
53365
  let name;
53293
53366
  if (node.name.conceptId) {
53294
53367
  name = node.name.term ? node.name.conceptId + normalizeTerm(node.name.term) : node.name.conceptId;
53295
53368
  } else if (node.name.expression) {
53296
- name = printSubExpression(node.name.expression, depth, column, opts, src);
53369
+ name = printSubExpression$1(node.name.expression, depth, column, opts, src);
53297
53370
  } else {
53298
53371
  name = src.slice(node.name.range.start.offset, node.name.range.end.offset);
53299
53372
  }
@@ -53303,7 +53376,7 @@ function printAttribute(node, depth, column, opts, src) {
53303
53376
  const hasHashPrefix = between.includes("#");
53304
53377
  let value;
53305
53378
  if (node.value.expression) {
53306
- value = printSubExpression(node.value.expression, depth, column + name.length + 3, opts, src);
53379
+ value = printSubExpression$1(node.value.expression, depth, column + name.length + 3, opts, src);
53307
53380
  } else if (node.value.rawValue) {
53308
53381
  value = hasHashPrefix ? "#" + node.value.rawValue : node.value.rawValue;
53309
53382
  } else {
@@ -53312,7 +53385,7 @@ function printAttribute(node, depth, column, opts, src) {
53312
53385
  const reversed = node.reversed ? "R " : "";
53313
53386
  return cardinalityPrefix + reversed + name + " " + compOp + " " + value;
53314
53387
  }
53315
- function printConceptReference(node) {
53388
+ function printConceptReference$1(node) {
53316
53389
  if (node.term) {
53317
53390
  return node.conceptId + normalizeTerm(node.term);
53318
53391
  }
@@ -53573,7 +53646,8 @@ const defaultFormattingOptions = {
53573
53646
  wrapComments: false,
53574
53647
  breakOnOperators: false,
53575
53648
  breakOnRefinementComma: false,
53576
- breakAfterColon: false
53649
+ breakAfterColon: false,
53650
+ removeRedundantParentheses: false
53577
53651
  };
53578
53652
  function expandToExpressionBoundaries(text, range2) {
53579
53653
  const startOffset = offsetAt(text, range2.start);
@@ -54722,6 +54796,31 @@ function getRefactoringActions(documentUri, documentText, range2) {
54722
54796
  return actions;
54723
54797
  }
54724
54798
  const REFACTORING_RESOLVE_KIND = "ecl.refactoring.resolve";
54799
+ function buildReplacementText(association) {
54800
+ const targets = association.targets;
54801
+ const typeLabel = association.type.replace(/-/g, " ");
54802
+ if (targets.length === 0) {
54803
+ return { replacement: "", label: `No targets for ${typeLabel}` };
54804
+ }
54805
+ if (association.type === "same-as" || association.type === "replaced-by") {
54806
+ const target = targets[0];
54807
+ const display = target.display ? ` |${target.display}|` : "";
54808
+ const replacement2 = target.code + display;
54809
+ return { replacement: replacement2, label: `Replace with ${typeLabel}: ${replacement2}` };
54810
+ }
54811
+ if (targets.length === 1) {
54812
+ const target = targets[0];
54813
+ const display = target.display ? ` |${target.display}|` : "";
54814
+ const replacement2 = target.code + display;
54815
+ return { replacement: replacement2, label: `Replace with ${typeLabel}: ${replacement2}` };
54816
+ }
54817
+ const parts = targets.map((t) => {
54818
+ const display = t.display ? ` |${t.display}|` : "";
54819
+ return t.code + display;
54820
+ });
54821
+ const replacement = "(" + parts.join(" OR ") + ")";
54822
+ return { replacement, label: `Replace with ${typeLabel}: ${replacement}` };
54823
+ }
54725
54824
  const eclSnippetCompletions = [
54726
54825
  {
54727
54826
  label: "Descendant of",
@@ -56016,6 +56115,297 @@ function isInsideFilterBlock(ranges, matchStart, matchEnd) {
56016
56115
  }
56017
56116
  return false;
56018
56117
  }
56118
+ function printCanonical(ast, sourceText, outerOperator) {
56119
+ if (ast.type === NodeType.SubExpressionConstraint) {
56120
+ return printSubExpression(ast, sourceText, outerOperator);
56121
+ }
56122
+ return printExpression(ast, sourceText, outerOperator);
56123
+ }
56124
+ function printExpression(node, src, outerOperator) {
56125
+ const inner = node.expression;
56126
+ switch (inner.type) {
56127
+ case NodeType.SubExpressionConstraint:
56128
+ return printSubExpression(inner, src, outerOperator);
56129
+ case NodeType.CompoundExpression:
56130
+ return printCompoundExpression(inner, src);
56131
+ case NodeType.RefinedExpression:
56132
+ return printRefinedExpression(inner, src);
56133
+ case NodeType.DottedExpression:
56134
+ return printDottedExpression(inner, src);
56135
+ default: {
56136
+ const _exhaustive = inner;
56137
+ return String(_exhaustive);
56138
+ }
56139
+ }
56140
+ }
56141
+ function printSubExpression(node, src, outerOperator) {
56142
+ let result = "";
56143
+ if (node.operator) {
56144
+ result += node.operator.operator;
56145
+ }
56146
+ if (node.memberOf) {
56147
+ result += "^";
56148
+ }
56149
+ switch (node.focus.type) {
56150
+ case NodeType.ConceptReference:
56151
+ result += printConceptReference(node.focus);
56152
+ break;
56153
+ case NodeType.Wildcard:
56154
+ result += "*";
56155
+ break;
56156
+ case NodeType.ExpressionConstraint: {
56157
+ const innerExpr = node.focus.expression;
56158
+ if (innerExpr.type !== NodeType.CompoundExpression) {
56159
+ result += printExpression(node.focus, src, outerOperator);
56160
+ } else if (outerOperator && innerExpr.operator.operator === outerOperator) {
56161
+ result += printExpression(node.focus, src, outerOperator);
56162
+ } else {
56163
+ result += "(" + printExpression(node.focus, src) + ")";
56164
+ }
56165
+ break;
56166
+ }
56167
+ }
56168
+ if (node.filters) {
56169
+ for (const filter of node.filters) {
56170
+ const filterText = src.slice(filter.range.start.offset, filter.range.end.offset);
56171
+ result += " " + filterText.replaceAll(/\s+/g, " ").trim();
56172
+ }
56173
+ }
56174
+ if (node.historySupplement) {
56175
+ const histText = src.slice(node.historySupplement.range.start.offset, node.historySupplement.range.end.offset);
56176
+ result += " " + histText.replaceAll(/\s+/g, " ").trim();
56177
+ }
56178
+ return result;
56179
+ }
56180
+ function printCompoundExpression(node, src) {
56181
+ const op = node.operator.operator;
56182
+ return node.operands.map((operand) => printSubExpression(operand, src, op)).join(` ${op} `);
56183
+ }
56184
+ function printRefinedExpression(node, src) {
56185
+ const focus = printSubExpression(node.expression, src);
56186
+ const refinement = printRefinement(node.refinement, src);
56187
+ return focus + ":" + refinement;
56188
+ }
56189
+ function printDottedExpression(node, src) {
56190
+ const source = printSubExpression(node.source, src);
56191
+ const dots = node.attributes.map((a) => printSubExpression(a.attributeName, src));
56192
+ return source + dots.map((d) => "." + d).join("");
56193
+ }
56194
+ function printRefinement(node, src) {
56195
+ const parts = [];
56196
+ let prevEnd = node.range.start.offset;
56197
+ for (const attr of node.attributes) {
56198
+ const printed = printAttribute(attr, src);
56199
+ const inBraces = isAttrInBraceGroup(attr, prevEnd, src);
56200
+ parts.push(inBraces ? "{" + printed + "}" : printed);
56201
+ prevEnd = attr.range.end.offset;
56202
+ }
56203
+ return parts.join(",");
56204
+ }
56205
+ function isAttrInBraceGroup(attr, prevEnd, src) {
56206
+ const gap = src.slice(prevEnd, attr.range.start.offset);
56207
+ return gap.includes("{");
56208
+ }
56209
+ function printAttribute(node, src) {
56210
+ const preNameSrc = src.slice(node.range.start.offset, node.name.range.start.offset).trim();
56211
+ const cardinalityRaw = preNameSrc.replace(/^\{?\s*/, "").trim();
56212
+ const cardinalityPrefix = cardinalityRaw.length > 0 ? cardinalityRaw + " " : "";
56213
+ let name;
56214
+ if (node.name.conceptId) {
56215
+ name = node.name.conceptId;
56216
+ } else if (node.name.expression) {
56217
+ name = printSubExpression(node.name.expression, src);
56218
+ } else {
56219
+ name = src.slice(node.name.range.start.offset, node.name.range.end.offset).trim();
56220
+ }
56221
+ const between = src.slice(node.name.range.end.offset, node.value.range.start.offset);
56222
+ const opMatch = /(!?=|[<>]=)/.exec(between);
56223
+ const compOp = opMatch ? opMatch[1] : "=";
56224
+ const hasHashPrefix = between.includes("#");
56225
+ let value;
56226
+ if (node.value.expression) {
56227
+ value = printSubExpression(node.value.expression, src);
56228
+ } else if (node.value.rawValue) {
56229
+ value = hasHashPrefix ? "#" + node.value.rawValue : node.value.rawValue;
56230
+ } else {
56231
+ value = src.slice(node.value.range.start.offset, node.value.range.end.offset).trim();
56232
+ }
56233
+ const reversed = node.reversed ? "R " : "";
56234
+ return cardinalityPrefix + reversed + name + " " + compOp + " " + value;
56235
+ }
56236
+ function printConceptReference(node) {
56237
+ return node.conceptId;
56238
+ }
56239
+ function normaliseAst(ast, sourceText) {
56240
+ return normaliseExpression(ast, sourceText);
56241
+ }
56242
+ function normaliseExpression(node, src) {
56243
+ const inner = node.expression;
56244
+ let normalised;
56245
+ switch (inner.type) {
56246
+ case NodeType.SubExpressionConstraint:
56247
+ normalised = normaliseSubExpression(inner, src);
56248
+ break;
56249
+ case NodeType.CompoundExpression:
56250
+ normalised = normaliseCompoundExpression(inner, src);
56251
+ break;
56252
+ case NodeType.RefinedExpression:
56253
+ normalised = normaliseRefinedExpression(inner, src);
56254
+ break;
56255
+ case NodeType.DottedExpression:
56256
+ normalised = normaliseDottedExpression(inner, src);
56257
+ break;
56258
+ default: {
56259
+ const _exhaustive = inner;
56260
+ normalised = _exhaustive;
56261
+ }
56262
+ }
56263
+ return { ...node, expression: normalised };
56264
+ }
56265
+ function normaliseSubExpression(node, src) {
56266
+ let focus;
56267
+ switch (node.focus.type) {
56268
+ case NodeType.ConceptReference:
56269
+ focus = stripTerm(node.focus);
56270
+ break;
56271
+ case NodeType.Wildcard:
56272
+ focus = { ...node.focus };
56273
+ break;
56274
+ case NodeType.ExpressionConstraint:
56275
+ focus = normaliseExpression(node.focus, src);
56276
+ break;
56277
+ }
56278
+ return {
56279
+ ...node,
56280
+ operator: node.operator ? { ...node.operator } : void 0,
56281
+ focus,
56282
+ filters: node.filters ? node.filters.map(copyFilter) : void 0,
56283
+ historySupplement: node.historySupplement ? { ...node.historySupplement } : void 0
56284
+ };
56285
+ }
56286
+ function normaliseCompoundExpression(node, src) {
56287
+ const op = node.operator.operator;
56288
+ let normOperands = node.operands.map((o) => normaliseSubExpression(o, src));
56289
+ if (op === "AND" || op === "OR") {
56290
+ normOperands = flattenSameOperator(normOperands, op, src);
56291
+ }
56292
+ if (op === "AND" || op === "OR") {
56293
+ normOperands.sort((a, b) => {
56294
+ const ka = printCanonical(a, src);
56295
+ const kb = printCanonical(b, src);
56296
+ return ka.localeCompare(kb);
56297
+ });
56298
+ }
56299
+ return {
56300
+ ...node,
56301
+ operator: { ...node.operator },
56302
+ operands: normOperands
56303
+ };
56304
+ }
56305
+ function flattenSameOperator(operands, operator, src) {
56306
+ const result = [];
56307
+ for (const operand of operands) {
56308
+ if (operand.focus.type === NodeType.ExpressionConstraint && !operand.operator && !operand.memberOf && !operand.filters && !operand.historySupplement) {
56309
+ const inner = operand.focus.expression;
56310
+ if (inner.type === NodeType.CompoundExpression && inner.operator.operator === operator) {
56311
+ const innerNorm = normaliseCompoundExpression(inner, src);
56312
+ result.push(...innerNorm.operands);
56313
+ continue;
56314
+ }
56315
+ }
56316
+ result.push(operand);
56317
+ }
56318
+ return result;
56319
+ }
56320
+ function normaliseRefinedExpression(node, src) {
56321
+ return {
56322
+ ...node,
56323
+ expression: normaliseSubExpression(node.expression, src),
56324
+ refinement: normaliseRefinement(node.refinement, src)
56325
+ };
56326
+ }
56327
+ function normaliseRefinement(node, src) {
56328
+ const normAttrs = node.attributes.map((a) => normaliseAttribute(a, src));
56329
+ normAttrs.sort((a, b) => compareAttributes(a, b, src));
56330
+ return { ...node, attributes: normAttrs };
56331
+ }
56332
+ function compareAttributes(a, b, src) {
56333
+ const aId = a.name.conceptId;
56334
+ const bId = b.name.conceptId;
56335
+ if (aId && bId) {
56336
+ const aBig = BigInt(aId);
56337
+ const bBig = BigInt(bId);
56338
+ if (aBig < bBig) return -1;
56339
+ if (aBig > bBig) return 1;
56340
+ return 0;
56341
+ }
56342
+ if (aId) return -1;
56343
+ if (bId) return 1;
56344
+ const aKey = a.name.expression ? printCanonical(a.name.expression, src) : "";
56345
+ const bKey = b.name.expression ? printCanonical(b.name.expression, src) : "";
56346
+ return aKey.localeCompare(bKey);
56347
+ }
56348
+ function normaliseAttribute(node, src) {
56349
+ return {
56350
+ ...node,
56351
+ name: normaliseAttributeName(node.name, src),
56352
+ value: normaliseAttributeValue(node.value, src)
56353
+ };
56354
+ }
56355
+ function normaliseAttributeName(node, src) {
56356
+ return {
56357
+ ...node,
56358
+ term: void 0,
56359
+ // §5.1: strip display terms
56360
+ expression: node.expression ? normaliseSubExpression(node.expression, src) : void 0
56361
+ };
56362
+ }
56363
+ function normaliseAttributeValue(node, src) {
56364
+ return {
56365
+ ...node,
56366
+ expression: node.expression ? normaliseSubExpression(node.expression, src) : void 0
56367
+ };
56368
+ }
56369
+ function normaliseDottedExpression(node, src) {
56370
+ return {
56371
+ ...node,
56372
+ source: normaliseSubExpression(node.source, src),
56373
+ attributes: node.attributes.map(
56374
+ (a) => ({
56375
+ ...a,
56376
+ attributeName: normaliseSubExpression(a.attributeName, src)
56377
+ })
56378
+ )
56379
+ };
56380
+ }
56381
+ function stripTerm(node) {
56382
+ return { ...node, term: void 0 };
56383
+ }
56384
+ function copyFilter(node) {
56385
+ return { ...node };
56386
+ }
56387
+ class CanonicaliseError extends Error {
56388
+ constructor(message, parseErrors) {
56389
+ super(message);
56390
+ this.parseErrors = parseErrors;
56391
+ this.name = "CanonicaliseError";
56392
+ }
56393
+ }
56394
+ function canonicalise(expression) {
56395
+ var _a;
56396
+ const { ast, errors: errors2 } = parseECL(expression);
56397
+ if (!ast || errors2.length > 0) {
56398
+ throw new CanonicaliseError(`Cannot canonicalise invalid ECL: ${((_a = errors2[0]) == null ? void 0 : _a.message) ?? "unknown error"}`, errors2);
56399
+ }
56400
+ const normalised = normaliseAst(ast, expression);
56401
+ return printCanonical(normalised, expression);
56402
+ }
56403
+ function compareExpressions(a, b) {
56404
+ if (a.trim() === b.trim()) return "identical";
56405
+ const ca = canonicalise(a);
56406
+ const cb = canonicalise(b);
56407
+ return ca === cb ? "structurally_equivalent" : "different";
56408
+ }
56019
56409
  const operatorArticles = [
56020
56410
  {
56021
56411
  id: "op:selfOf",
@@ -57036,12 +57426,16 @@ function getGuide(uri2) {
57036
57426
  return knowledgeGuides.find((g) => g.uri === uri2);
57037
57427
  }
57038
57428
  export {
57429
+ CanonicaliseError,
57039
57430
  FhirTerminologyService,
57040
57431
  REFACTORING_RESOLVE_KIND,
57041
57432
  allArticles,
57042
57433
  analyzeExpression,
57434
+ buildReplacementText,
57435
+ canonicalise,
57043
57436
  checkMixedRefinementOperators,
57044
57437
  clearFilterCache,
57438
+ compareExpressions,
57045
57439
  computeSemanticTokens,
57046
57440
  coreInsert,
57047
57441
  coreReplace,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aehrc/ecl-core",
3
- "version": "1.0.4",
3
+ "version": "1.1.1",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },