@elastic/esql 4.0.0 → 4.1.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.
package/lib/index.mjs CHANGED
@@ -156,10 +156,13 @@ function* childrenOfPromqlNode(node) {
156
156
  return;
157
157
  }
158
158
  case "function": {
159
- if (node.grouping) {
159
+ if (node.grouping && node.groupingPosition !== "after") {
160
160
  yield node.grouping;
161
161
  }
162
162
  yield* node.args;
163
+ if (node.grouping && node.groupingPosition === "after") {
164
+ yield node.grouping;
165
+ }
163
166
  return;
164
167
  }
165
168
  case "selector": {
@@ -172,6 +175,49 @@ function* childrenOfPromqlNode(node) {
172
175
  if (value) yield value;
173
176
  return;
174
177
  }
178
+ case "binary-expression": {
179
+ yield node.left;
180
+ if (node.modifier) yield node.modifier;
181
+ yield node.right;
182
+ return;
183
+ }
184
+ case "unary-expression": {
185
+ yield node.arg;
186
+ return;
187
+ }
188
+ case "subquery": {
189
+ yield node.expr;
190
+ yield node.range;
191
+ if (node.resolution) yield node.resolution;
192
+ if (node.evaluation) yield node.evaluation;
193
+ return;
194
+ }
195
+ case "parens": {
196
+ if (node.child) yield node.child;
197
+ return;
198
+ }
199
+ case "evaluation": {
200
+ if (node.offset) yield node.offset;
201
+ if (node.at) yield node.at;
202
+ return;
203
+ }
204
+ case "offset": {
205
+ if (node.duration) yield node.duration;
206
+ return;
207
+ }
208
+ case "at": {
209
+ if (typeof node.value !== "string") yield node.value;
210
+ return;
211
+ }
212
+ case "modifier": {
213
+ yield* node.labels;
214
+ if (node.groupModifier) yield node.groupModifier;
215
+ return;
216
+ }
217
+ case "group-modifier": {
218
+ yield* node.labels;
219
+ return;
220
+ }
175
221
  }
176
222
  if ("args" in node && Array.isArray(node.args)) {
177
223
  yield* node.args;
@@ -910,9 +956,18 @@ var Walker = class _Walker {
910
956
  _Walker.walk(tree, { ...options, visitAny });
911
957
  };
912
958
  aborted = false;
959
+ skippedChildren = false;
913
960
  abort() {
914
961
  this.aborted = true;
915
962
  }
963
+ skipChildren() {
964
+ this.skippedChildren = true;
965
+ }
966
+ readAndResetSkippedChildren() {
967
+ const skipped = this.skippedChildren;
968
+ this.skippedChildren = false;
969
+ return skipped;
970
+ }
916
971
  walk(tree, parent = void 0) {
917
972
  if (this.aborted) return;
918
973
  if (!tree) return;
@@ -950,17 +1005,20 @@ var Walker = class _Walker {
950
1005
  if (this.aborted) return;
951
1006
  const { options } = this;
952
1007
  (options.visitCommand ?? options.visitAny)?.(node, parent, this);
1008
+ if (this.readAndResetSkippedChildren()) return;
953
1009
  this.walkList(node.args, node);
954
1010
  }
955
1011
  walkHeaderCommand(node, parent) {
956
1012
  if (this.aborted) return;
957
1013
  const { options } = this;
958
1014
  (options.visitHeaderCommand ?? options.visitAny)?.(node, parent, this);
1015
+ if (this.readAndResetSkippedChildren()) return;
959
1016
  this.walkList(node.args, node);
960
1017
  }
961
1018
  walkOption(node, parent) {
962
1019
  const { options } = this;
963
1020
  (options.visitCommandOption ?? options.visitAny)?.(node, parent, this);
1021
+ if (this.readAndResetSkippedChildren()) return;
964
1022
  this.walkList(node.args, node);
965
1023
  }
966
1024
  walkExpression(node, parent = void 0) {
@@ -975,11 +1033,13 @@ var Walker = class _Walker {
975
1033
  walkListLiteral(node, parent) {
976
1034
  const { options } = this;
977
1035
  (options.visitListLiteral ?? options.visitAny)?.(node, parent, this);
1036
+ if (this.readAndResetSkippedChildren()) return;
978
1037
  this.walkList(node.values, node);
979
1038
  }
980
1039
  walkSource(node, parent) {
981
1040
  const { options } = this;
982
1041
  (options.visitSource ?? options.visitAny)?.(node, parent, this);
1042
+ if (this.readAndResetSkippedChildren()) return;
983
1043
  const children2 = [];
984
1044
  if (node.prefix) {
985
1045
  children2.push(node.prefix);
@@ -996,6 +1056,7 @@ var Walker = class _Walker {
996
1056
  const { options } = this;
997
1057
  const { args } = node;
998
1058
  (options.visitColumn ?? options.visitAny)?.(node, parent, this);
1059
+ if (this.readAndResetSkippedChildren()) return;
999
1060
  if (args) {
1000
1061
  this.walkList(args, node);
1001
1062
  }
@@ -1003,27 +1064,32 @@ var Walker = class _Walker {
1003
1064
  walkOrder(node, parent) {
1004
1065
  const { options } = this;
1005
1066
  (options.visitOrder ?? options.visitAny)?.(node, parent, this);
1067
+ if (this.readAndResetSkippedChildren()) return;
1006
1068
  this.walkList(node.args, node);
1007
1069
  }
1008
1070
  walkInlineCast(node, parent) {
1009
1071
  const { options } = this;
1010
1072
  (options.visitInlineCast ?? options.visitAny)?.(node, parent, this);
1073
+ if (this.readAndResetSkippedChildren()) return;
1011
1074
  this.walkExpression(node.value, node);
1012
1075
  }
1013
1076
  walkFunction(node, parent = void 0) {
1014
1077
  const { options } = this;
1015
1078
  (options.visitFunction ?? options.visitAny)?.(node, parent, this);
1079
+ if (this.readAndResetSkippedChildren()) return;
1016
1080
  if (node.operator) this.walkSingleAstItem(node.operator, node);
1017
1081
  this.walkList(node.args, node);
1018
1082
  }
1019
1083
  walkMap(node, parent) {
1020
1084
  const { options } = this;
1021
1085
  (options.visitMap ?? options.visitAny)?.(node, parent, this);
1086
+ if (this.readAndResetSkippedChildren()) return;
1022
1087
  this.walkList(node.entries, node);
1023
1088
  }
1024
1089
  walkMapEntry(node, parent) {
1025
1090
  const { options } = this;
1026
1091
  (options.visitMapEntry ?? options.visitAny)?.(node, parent, this);
1092
+ if (this.readAndResetSkippedChildren()) return;
1027
1093
  if (options.order === "backward") {
1028
1094
  this.walkSingleAstItem(resolveItem(node.value), node);
1029
1095
  this.walkSingleAstItem(resolveItem(node.key), node);
@@ -1035,6 +1101,7 @@ var Walker = class _Walker {
1035
1101
  walkParens(node, parent) {
1036
1102
  const { options } = this;
1037
1103
  (options.visitParens ?? options.visitAny)?.(node, parent, this);
1104
+ if (this.readAndResetSkippedChildren()) return;
1038
1105
  if (node.child) {
1039
1106
  this.walkSingleAstItem(node.child, node);
1040
1107
  }
@@ -1042,6 +1109,7 @@ var Walker = class _Walker {
1042
1109
  walkQuery(node, parent) {
1043
1110
  const { options } = this;
1044
1111
  (options.visitQuery ?? options.visitAny)?.(node, parent, this);
1112
+ if (this.readAndResetSkippedChildren()) return;
1045
1113
  if (node.header && !options.skipHeader) {
1046
1114
  this.walkList(node.header, node);
1047
1115
  }
@@ -1110,6 +1178,7 @@ var Walker = class _Walker {
1110
1178
  }
1111
1179
  case "literal": {
1112
1180
  (options.visitLiteral ?? options.visitAny)?.(node, parent, this);
1181
+ this.readAndResetSkippedChildren();
1113
1182
  break;
1114
1183
  }
1115
1184
  case "list": {
@@ -1126,10 +1195,12 @@ var Walker = class _Walker {
1126
1195
  }
1127
1196
  case "identifier": {
1128
1197
  (options.visitIdentifier ?? options.visitAny)?.(node, parent, this);
1198
+ this.readAndResetSkippedChildren();
1129
1199
  break;
1130
1200
  }
1131
1201
  case "unknown": {
1132
1202
  (options.visitUnknown ?? options.visitAny)?.(node, parent, this);
1203
+ this.readAndResetSkippedChildren();
1133
1204
  break;
1134
1205
  }
1135
1206
  }
@@ -61011,6 +61082,140 @@ var PromQLCstToAstConverter = class {
61011
61082
  }
61012
61083
  };
61013
61084
 
61085
+ // src/embedded_languages/promql/ast/find_node.ts
61086
+ var sortedLocatedChildren = (node) => {
61087
+ const children2 = [];
61088
+ for (const child of childrenOfPromqlNode(node)) {
61089
+ if (child.location) children2.push(child);
61090
+ }
61091
+ children2.sort((a, b) => a.location.min - b.location.min);
61092
+ return children2;
61093
+ };
61094
+ var findForward = (node, pos) => {
61095
+ for (const child of sortedLocatedChildren(node)) {
61096
+ const { min, max } = child.location;
61097
+ if (min <= pos && max >= pos) {
61098
+ return findForward(child, pos) ?? child;
61099
+ }
61100
+ if (min > pos) {
61101
+ return child;
61102
+ }
61103
+ }
61104
+ return null;
61105
+ };
61106
+ var findBackward = (node, pos) => {
61107
+ const children2 = sortedLocatedChildren(node);
61108
+ for (let i = children2.length - 1; i >= 0; i--) {
61109
+ const child = children2[i];
61110
+ const { min, max } = child.location;
61111
+ if (min <= pos && max >= pos) {
61112
+ return findBackward(child, pos) ?? child;
61113
+ }
61114
+ if (max < pos) {
61115
+ return child;
61116
+ }
61117
+ }
61118
+ return null;
61119
+ };
61120
+ var findNodeAtOrAfter = (ast, pos) => findForward(ast, pos);
61121
+ var findNodeAtOrBefore = (ast, pos) => findBackward(ast, pos);
61122
+
61123
+ // src/embedded_languages/promql/parser/decorations.ts
61124
+ var HIDDEN_CHANNEL2 = 1;
61125
+ var punctuationChars2 = /* @__PURE__ */ new Set([".", ",", ";", ":", "(", ")", "[", "]", "{", "}"]);
61126
+ var isLikelyPunctuation2 = (text2) => text2.length === 1 && punctuationChars2.has(text2);
61127
+ var cleanCommentText = (text2) => {
61128
+ let end = text2.length;
61129
+ if (text2[end - 1] === "\n") {
61130
+ end--;
61131
+ }
61132
+ if (end > 0 && text2[end - 1] === "\r") {
61133
+ end--;
61134
+ }
61135
+ return text2.slice(1, end);
61136
+ };
61137
+ var collectPromQLDecorations = (tokens, offset = 0) => {
61138
+ const result = [];
61139
+ const list12 = tokens.tokens;
61140
+ const length = list12.length;
61141
+ let pos = 0;
61142
+ let hasContentToLeft = false;
61143
+ for (let i = 0; i < length - 1; i++) {
61144
+ const token = list12[i];
61145
+ const { channel, text: text2 } = token;
61146
+ const min = pos;
61147
+ const max = min + text2.length - 1;
61148
+ pos = max + 1;
61149
+ if (channel !== HIDDEN_CHANNEL2) {
61150
+ if (!isLikelyPunctuation2(text2)) {
61151
+ hasContentToLeft = true;
61152
+ }
61153
+ continue;
61154
+ }
61155
+ if (text2[0] !== "#") {
61156
+ if (text2.indexOf("\n") !== -1) {
61157
+ hasContentToLeft = false;
61158
+ }
61159
+ continue;
61160
+ }
61161
+ const cleanText = cleanCommentText(text2);
61162
+ const endsWithNewline = text2[text2.length - 1] === "\n";
61163
+ const maxOffset = endsWithNewline ? text2[text2.length - 2] === "\r" ? 2 : 1 : 0;
61164
+ const node = Builder.comment("single-line", cleanText, {
61165
+ min: min + offset,
61166
+ max: max - maxOffset + offset
61167
+ });
61168
+ result.push({
61169
+ type: "comment",
61170
+ hasContentToLeft,
61171
+ node
61172
+ });
61173
+ if (endsWithNewline) {
61174
+ hasContentToLeft = false;
61175
+ }
61176
+ }
61177
+ return result;
61178
+ };
61179
+ var ensureFormatting = (node) => {
61180
+ if (!node.formatting) {
61181
+ node.formatting = {};
61182
+ }
61183
+ return node.formatting;
61184
+ };
61185
+ var attachTopComment2 = (node, comment) => {
61186
+ const formatting = ensureFormatting(node);
61187
+ if (!formatting.top) formatting.top = [];
61188
+ formatting.top.push(comment);
61189
+ };
61190
+ var attachBottomComment2 = (node, comment) => {
61191
+ const formatting = ensureFormatting(node);
61192
+ if (!formatting.bottom) formatting.bottom = [];
61193
+ formatting.bottom.push(comment);
61194
+ };
61195
+ var attachTrailingComment = (node, comment) => {
61196
+ const formatting = ensureFormatting(node);
61197
+ formatting.rightSingleLine = comment;
61198
+ };
61199
+ var attachPromQLDecorations = (ast, comments) => {
61200
+ for (const comment of comments) {
61201
+ const { location } = comment.node;
61202
+ if (!location) continue;
61203
+ if (!comment.hasContentToLeft) {
61204
+ const target2 = findNodeAtOrAfter(ast, location.max);
61205
+ if (target2) {
61206
+ attachTopComment2(target2, comment.node);
61207
+ } else {
61208
+ attachBottomComment2(ast, comment.node);
61209
+ }
61210
+ continue;
61211
+ }
61212
+ const target = findNodeAtOrBefore(ast, location.min);
61213
+ if (target) {
61214
+ attachTrailingComment(target, comment.node);
61215
+ }
61216
+ }
61217
+ };
61218
+
61014
61219
  // src/embedded_languages/promql/parser/parser.ts
61015
61220
  var PromQLParser = class _PromQLParser {
61016
61221
  constructor(src2, options = {}) {
@@ -61076,6 +61281,11 @@ var PromQLParser = class _PromQLParser {
61076
61281
  errors: this.errors.getErrors()
61077
61282
  };
61078
61283
  }
61284
+ if (this.options.withFormatting) {
61285
+ this.tokens.fill();
61286
+ const comments = collectPromQLDecorations(this.tokens, this.options.offset ?? 0);
61287
+ attachPromQLDecorations(root, comments);
61288
+ }
61079
61289
  return {
61080
61290
  root,
61081
61291
  errors: this.errors.getErrors()