@witchcraft/expressit 0.1.0 → 0.1.2

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 (59) hide show
  1. package/README.md +2 -4
  2. package/dist/Lexer.js +2 -2
  3. package/dist/Parser.d.ts +94 -23
  4. package/dist/Parser.d.ts.map +1 -1
  5. package/dist/Parser.js +831 -23
  6. package/dist/ast/builders/token.js +1 -1
  7. package/dist/helpers/errors.js +3 -3
  8. package/dist/helpers/parser/checkParserOpts.js +2 -2
  9. package/dist/package.json.js +8 -6
  10. package/dist/utils/extractTokens.js +1 -1
  11. package/dist/utils/getCursorInfo.js +2 -2
  12. package/dist/utils/getOppositeDelimiter.js +1 -1
  13. package/dist/utils/prettyAst.js +6 -6
  14. package/package.json +6 -6
  15. package/src/Lexer.ts +2 -2
  16. package/src/Parser.ts +1102 -60
  17. package/src/ast/builders/token.ts +1 -1
  18. package/src/helpers/errors.ts +3 -3
  19. package/src/helpers/parser/checkParserOpts.ts +2 -2
  20. package/src/utils/extractTokens.ts +1 -1
  21. package/src/utils/getCursorInfo.ts +2 -2
  22. package/src/utils/getOppositeDelimiter.ts +1 -1
  23. package/src/utils/prettyAst.ts +3 -3
  24. package/dist/methods/autocomplete.d.ts +0 -18
  25. package/dist/methods/autocomplete.d.ts.map +0 -1
  26. package/dist/methods/autocomplete.js +0 -109
  27. package/dist/methods/autoreplace.d.ts +0 -13
  28. package/dist/methods/autoreplace.d.ts.map +0 -1
  29. package/dist/methods/autoreplace.js +0 -36
  30. package/dist/methods/autosuggest.d.ts +0 -28
  31. package/dist/methods/autosuggest.d.ts.map +0 -1
  32. package/dist/methods/autosuggest.js +0 -371
  33. package/dist/methods/evaluate.d.ts +0 -11
  34. package/dist/methods/evaluate.d.ts.map +0 -1
  35. package/dist/methods/evaluate.js +0 -32
  36. package/dist/methods/getBestIndex.d.ts +0 -19
  37. package/dist/methods/getBestIndex.d.ts.map +0 -1
  38. package/dist/methods/getBestIndex.js +0 -53
  39. package/dist/methods/getIndexes.d.ts +0 -17
  40. package/dist/methods/getIndexes.d.ts.map +0 -1
  41. package/dist/methods/getIndexes.js +0 -98
  42. package/dist/methods/index.d.ts +0 -9
  43. package/dist/methods/index.d.ts.map +0 -1
  44. package/dist/methods/index.js +0 -18
  45. package/dist/methods/normalize.d.ts +0 -10
  46. package/dist/methods/normalize.d.ts.map +0 -1
  47. package/dist/methods/normalize.js +0 -98
  48. package/dist/methods/validate.d.ts +0 -11
  49. package/dist/methods/validate.d.ts.map +0 -1
  50. package/dist/methods/validate.js +0 -113
  51. package/src/methods/autocomplete.ts +0 -128
  52. package/src/methods/autoreplace.ts +0 -46
  53. package/src/methods/autosuggest.ts +0 -543
  54. package/src/methods/evaluate.ts +0 -39
  55. package/src/methods/getBestIndex.ts +0 -53
  56. package/src/methods/getIndexes.ts +0 -100
  57. package/src/methods/index.ts +0 -10
  58. package/src/methods/normalize.ts +0 -137
  59. package/src/methods/validate.ts +0 -143
@@ -1,11 +0,0 @@
1
- import { Condition } from "../ast/classes/Condition.js";
2
- import { Expression } from "../ast/classes/Expression.js";
3
- export declare class EvaluateMixin<T extends {}> {
4
- /**
5
- * Evaluates a {@link Parser.normalize normalized} ast.
6
- *
7
- * How the ast is evaluated for different operators can be controlled by the {@link ParserOptions.valueComparer valueComparer} option.
8
- */
9
- evaluate(ast: Expression<any, any> | Condition<any, any>, context: Record<string, any>): boolean;
10
- }
11
- //# sourceMappingURL=evaluate.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"evaluate.d.ts","sourceRoot":"","sources":["../../src/methods/evaluate.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAA;AACvD,OAAO,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAA;AAKzD,qBAAa,aAAa,CAAC,CAAC,SAAS,EAAE;IACtC;;;;OAIG;IACH,QAAQ,CAAC,GAAG,EAAE,UAAU,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG,SAAS,CAAC,GAAG,EAAE,GAAG,CAAC,EAAE,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,OAAO;CAsBhG"}
@@ -1,32 +0,0 @@
1
- import { get } from "@alanscodelog/utils/get";
2
- import "@alanscodelog/utils/types";
3
- import { unreachable } from "@alanscodelog/utils/unreachable";
4
- import { Condition } from "../ast/classes/Condition.js";
5
- import { Expression } from "../ast/classes/Expression.js";
6
- import { TOKEN_TYPE } from "../types/ast.js";
7
- class EvaluateMixin {
8
- /**
9
- * Evaluates a {@link Parser.normalize normalized} ast.
10
- *
11
- * How the ast is evaluated for different operators can be controlled by the {@link ParserOptions.valueComparer valueComparer} option.
12
- */
13
- evaluate(ast, context) {
14
- this._checkEvaluationOptions();
15
- const opts = this.options;
16
- const self_ = this;
17
- if (ast instanceof Condition) {
18
- const contextValue = get(context, ast.property);
19
- const res = opts.valueComparer({ property: ast.property, value: ast.value, operator: ast.operator }, contextValue, context);
20
- return ast.negate ? !res : res;
21
- }
22
- if (ast instanceof Expression) {
23
- const left = self_.evaluate(ast.left, context);
24
- const right = self_.evaluate(ast.right, context);
25
- return ast.operator === TOKEN_TYPE.AND ? left && right : left || right;
26
- }
27
- return unreachable();
28
- }
29
- }
30
- export {
31
- EvaluateMixin
32
- };
@@ -1,19 +0,0 @@
1
- export declare class GetBestIndexesMixin {
2
- /**
3
- * Given the set of indexes returned by {@link getBestIndex}, the set of existing indexes in a database, and the index to sort by\*, will return a list of the best/shortest sets of indexes.
4
- *
5
- * For example, given the query `a && b && c`, `getBestIndex` will return `[Set(a), Set(b)]`.
6
- *
7
- * Suppose we have indexes on all the variables and that the user wants to sort by `c`, this function will return [`Set(c)`].
8
- *
9
- * Suppose instead we have indexes only on `a` and `b` and that the user wants to sort by `c`, this function will return [`Set(a), Set(b)`]. Either can be picked by some other criteria (e.g. size of the indexes). Sort should then be done in memory.
10
- *
11
- * And then finally, if we have no existing indexes on any of the variables, the function will return `[]`.
12
- *
13
- * Note: This is a simple algorithm and is not designed to take into account instances where entries are indexed by two or more properties as their keys (i.e. multicolumn indexes).
14
- *
15
- * \* If the sort index is not in the list of existing indexes it is not taken into account.
16
- */
17
- getBestIndexes(indexes: Set<string>[], existing: Set<string> | Map<string, number>, sortIndex?: string): Set<string>[];
18
- }
19
- //# sourceMappingURL=getBestIndex.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"getBestIndex.d.ts","sourceRoot":"","sources":["../../src/methods/getBestIndex.ts"],"names":[],"mappings":"AAAA,qBAAa,mBAAmB;IAC/B;;;;;;;;;;;;;;OAcG;IACH,cAAc,CAAC,OAAO,EAAE,GAAG,CAAC,MAAM,CAAC,EAAE,EAAE,QAAQ,EAAE,GAAG,CAAC,MAAM,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,SAAS,GAAE,MAAW,GAAG,GAAG,CAAC,MAAM,CAAC,EAAE;CAmC1H"}
@@ -1,53 +0,0 @@
1
- class GetBestIndexesMixin {
2
- /**
3
- * Given the set of indexes returned by {@link getBestIndex}, the set of existing indexes in a database, and the index to sort by\*, will return a list of the best/shortest sets of indexes.
4
- *
5
- * For example, given the query `a && b && c`, `getBestIndex` will return `[Set(a), Set(b)]`.
6
- *
7
- * Suppose we have indexes on all the variables and that the user wants to sort by `c`, this function will return [`Set(c)`].
8
- *
9
- * Suppose instead we have indexes only on `a` and `b` and that the user wants to sort by `c`, this function will return [`Set(a), Set(b)`]. Either can be picked by some other criteria (e.g. size of the indexes). Sort should then be done in memory.
10
- *
11
- * And then finally, if we have no existing indexes on any of the variables, the function will return `[]`.
12
- *
13
- * Note: This is a simple algorithm and is not designed to take into account instances where entries are indexed by two or more properties as their keys (i.e. multicolumn indexes).
14
- *
15
- * \* If the sort index is not in the list of existing indexes it is not taken into account.
16
- */
17
- getBestIndexes(indexes, existing, sortIndex = "") {
18
- indexes = indexes.filter((set) => {
19
- for (const key of set) {
20
- if (!existing.has(key))
21
- return false;
22
- }
23
- return true;
24
- });
25
- let finalIndexes = indexes;
26
- if (existing.has(sortIndex)) {
27
- const indexesWithSortIndex = indexes.filter((set) => set.has(sortIndex));
28
- if (indexesWithSortIndex.length > 0)
29
- finalIndexes = indexesWithSortIndex;
30
- }
31
- let smallest = Infinity;
32
- if (existing instanceof Map) {
33
- const scores = /* @__PURE__ */ new Map();
34
- for (const set of finalIndexes) {
35
- let score = 0;
36
- for (const key of set) {
37
- score += existing.get(key) ?? 0;
38
- }
39
- scores.set(set, score);
40
- smallest = score < smallest ? score : smallest;
41
- }
42
- return indexes.filter((set) => smallest === Infinity || scores.get(set) === smallest);
43
- } else {
44
- for (const set of finalIndexes) {
45
- smallest = set.size < smallest ? set.size : smallest;
46
- }
47
- return indexes.filter((set) => smallest === Infinity || set.size === smallest);
48
- }
49
- }
50
- }
51
- export {
52
- GetBestIndexesMixin
53
- };
@@ -1,17 +0,0 @@
1
- import { Condition } from "../ast/classes/Condition.js";
2
- import { Expression } from "../ast/classes/Expression.js";
3
- export declare class GetIndexMixin<T> {
4
- /**
5
- * Returns a list of the different sets of keys that need to be indexed to run a normalized query on a database and hit an existing index.
6
- *
7
- * For example, the expression `a || b` requires both `a` AND `b` be indexed to use an index. The function would return `[Set(a, b)]`.
8
- *
9
- * On the otherhand, the expression `a && b` only requires `a` OR `b` to be indexed (`[Set(a), Set(b)]`) If at least one is indexed, the rest of the filtering can be done in memory. There is no need to in memory filter the entire database.
10
- *
11
- * Now take a more complicated query like `(a && b) || (a && c)`. This only requires `a` be indexed, or both `b` AND `c`. (`[Set(a)], [Set(b), Set(c)]`).
12
- *
13
- * Queries like `(a || b) && (a || c)` would require all the variables to be indexed `[Set(a), Set(b), Set(c)]`.
14
- */
15
- getIndexes(ast: Condition | Expression): Set<string>[];
16
- }
17
- //# sourceMappingURL=getIndexes.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"getIndexes.d.ts","sourceRoot":"","sources":["../../src/methods/getIndexes.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAA;AACvD,OAAO,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAA;AAIzD,qBAAa,aAAa,CAAC,CAAC;IAC3B;;;;;;;;;;OAUG;IACH,UAAU,CAAC,GAAG,EAAE,SAAS,GAAG,UAAU,GAAG,GAAG,CAAC,MAAM,CAAC,EAAE;CAqEtD"}
@@ -1,98 +0,0 @@
1
- import "@alanscodelog/utils/types";
2
- import { unreachable } from "@alanscodelog/utils/unreachable";
3
- import { Condition } from "../ast/classes/Condition.js";
4
- import { Expression } from "../ast/classes/Expression.js";
5
- import { TOKEN_TYPE } from "../types/ast.js";
6
- class GetIndexMixin {
7
- /**
8
- * Returns a list of the different sets of keys that need to be indexed to run a normalized query on a database and hit an existing index.
9
- *
10
- * For example, the expression `a || b` requires both `a` AND `b` be indexed to use an index. The function would return `[Set(a, b)]`.
11
- *
12
- * On the otherhand, the expression `a && b` only requires `a` OR `b` to be indexed (`[Set(a), Set(b)]`) If at least one is indexed, the rest of the filtering can be done in memory. There is no need to in memory filter the entire database.
13
- *
14
- * Now take a more complicated query like `(a && b) || (a && c)`. This only requires `a` be indexed, or both `b` AND `c`. (`[Set(a)], [Set(b), Set(c)]`).
15
- *
16
- * Queries like `(a || b) && (a || c)` would require all the variables to be indexed `[Set(a), Set(b), Set(c)]`.
17
- */
18
- getIndexes(ast) {
19
- const self_ = this;
20
- if (ast instanceof Condition) {
21
- return [new Set(ast.property.join("."))];
22
- }
23
- if (ast instanceof Expression) {
24
- const left = self_.getIndexes(ast.left);
25
- const right = self_.getIndexes(ast.right);
26
- if (ast.operator === TOKEN_TYPE.AND) {
27
- const sets = [];
28
- const allKeys = /* @__PURE__ */ new Set();
29
- for (const leftSet of left) {
30
- const exists = sets.find((set) => isEqualSet(set, leftSet));
31
- if (exists)
32
- continue;
33
- sets.push(leftSet);
34
- for (const key of leftSet) {
35
- allKeys.add(key);
36
- }
37
- }
38
- for (const rightSet of right) {
39
- const exists = sets.find((set) => isEqualSet(set, rightSet));
40
- if (exists)
41
- continue;
42
- sets.push(rightSet);
43
- for (const key of rightSet) {
44
- allKeys.add(key);
45
- }
46
- }
47
- const commonKeys = /* @__PURE__ */ new Set();
48
- outerCheck:
49
- for (const key of allKeys) {
50
- for (const set of sets) {
51
- if (!set.has(key))
52
- continue outerCheck;
53
- }
54
- commonKeys.add(key);
55
- }
56
- if (commonKeys.size > 0) {
57
- return [commonKeys, allKeys];
58
- } else {
59
- return sets;
60
- }
61
- }
62
- if (ast.operator === TOKEN_TYPE.OR) {
63
- for (const rightSet of right) {
64
- for (const leftSet of left) {
65
- if (isEqualSet(leftSet, rightSet)) {
66
- return [rightSet];
67
- }
68
- }
69
- }
70
- const res = /* @__PURE__ */ new Set();
71
- for (const leftSet of left) {
72
- for (const key of leftSet) {
73
- res.add(key);
74
- }
75
- }
76
- for (const rightSet of right) {
77
- for (const key of rightSet) {
78
- res.add(key);
79
- }
80
- }
81
- return [res];
82
- }
83
- }
84
- return unreachable();
85
- }
86
- }
87
- function isEqualSet(setA, setB) {
88
- if (setA.size !== setB.size)
89
- return false;
90
- for (const key of setA) {
91
- if (!setB.has(key))
92
- return false;
93
- }
94
- return true;
95
- }
96
- export {
97
- GetIndexMixin
98
- };
@@ -1,9 +0,0 @@
1
- export { AutocompleteMixin } from "./autocomplete.js";
2
- export { AutoreplaceMixin } from "./autoreplace.js";
3
- export { Autosuggest } from "./autosuggest.js";
4
- export { EvaluateMixin } from "./evaluate.js";
5
- export { GetBestIndexesMixin } from "./getBestIndex.js";
6
- export { GetIndexMixin } from "./getIndexes.js";
7
- export { NormalizeMixin } from "./normalize.js";
8
- export { ValidateMixin } from "./validate.js";
9
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/methods/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA;AACrD,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAA;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAC9C,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAA;AAC7C,OAAO,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAA;AACvD,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAC/C,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAC/C,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAA"}
@@ -1,18 +0,0 @@
1
- import { AutocompleteMixin } from "./autocomplete.js";
2
- import { AutoreplaceMixin } from "./autoreplace.js";
3
- import { Autosuggest } from "./autosuggest.js";
4
- import { EvaluateMixin } from "./evaluate.js";
5
- import { GetBestIndexesMixin } from "./getBestIndex.js";
6
- import { GetIndexMixin } from "./getIndexes.js";
7
- import { NormalizeMixin } from "./normalize.js";
8
- import { ValidateMixin } from "./validate.js";
9
- export {
10
- AutocompleteMixin,
11
- AutoreplaceMixin,
12
- Autosuggest,
13
- EvaluateMixin,
14
- GetBestIndexesMixin,
15
- GetIndexMixin,
16
- NormalizeMixin,
17
- ValidateMixin
18
- };
@@ -1,10 +0,0 @@
1
- import { Condition } from "../ast/classes/Condition.js";
2
- import { Expression } from "../ast/classes/Expression.js";
3
- import { type ParserResults } from "../types/ast.js";
4
- export declare class NormalizeMixin<T extends {}> {
5
- /**
6
- * Normalizes the ast by applying {@link GroupNode GroupNodes} and converting {@link ConditionNode ConditionNodes} to {@link NormalizedConditionNode NormalizedConditionNodes}.
7
- */
8
- normalize<TType extends string, TValue>(ast: ParserResults): Condition<TType, TValue> | Expression<TType, TValue>;
9
- }
10
- //# sourceMappingURL=normalize.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"normalize.d.ts","sourceRoot":"","sources":["../../src/methods/normalize.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAA;AAGvD,OAAO,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAA;AAQzD,OAAO,EAAE,KAAK,aAAa,EAAsC,MAAM,iBAAiB,CAAA;AASxF,qBAAa,cAAc,CAAC,CAAC,SAAS,EAAE;IACvC;;OAEG;IACH,SAAS,CAAC,KAAK,SAAS,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,aAAa,GAAG,SAAS,CAAC,KAAK,EAAE,MAAM,CAAC,GAAG,UAAU,CAAC,KAAK,EAAE,MAAM,CAAC;CA4GjH"}
@@ -1,98 +0,0 @@
1
- import "@alanscodelog/utils/types";
2
- import { unreachable } from "@alanscodelog/utils/unreachable";
3
- import { ArrayNode } from "../ast/classes/ArrayNode.js";
4
- import { Condition } from "../ast/classes/Condition.js";
5
- import { ConditionNode } from "../ast/classes/ConditionNode.js";
6
- import { ErrorToken } from "../ast/classes/ErrorToken.js";
7
- import { Expression } from "../ast/classes/Expression.js";
8
- import { ExpressionNode } from "../ast/classes/ExpressionNode.js";
9
- import { GroupNode } from "../ast/classes/GroupNode.js";
10
- import { ValidToken } from "../ast/classes/ValidToken.js";
11
- import { VariableNode } from "../ast/classes/VariableNode.js";
12
- import { applyBoolean } from "../helpers/general/applyBoolean.js";
13
- import { applyPrefix } from "../helpers/general/applyPrefix.js";
14
- import { TOKEN_TYPE } from "../types/ast.js";
15
- const OPPOSITE = {
16
- [TOKEN_TYPE.AND]: TOKEN_TYPE.OR,
17
- [TOKEN_TYPE.OR]: TOKEN_TYPE.AND
18
- };
19
- class NormalizeMixin {
20
- /**
21
- * Normalizes the ast by applying {@link GroupNode GroupNodes} and converting {@link ConditionNode ConditionNodes} to {@link NormalizedConditionNode NormalizedConditionNodes}.
22
- */
23
- normalize(ast) {
24
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
25
- this._checkEvaluationOptions();
26
- const opts = this.options;
27
- if (ast instanceof ErrorToken || !ast.valid) {
28
- throw new Error("AST node must be valid.");
29
- }
30
- const prefix = arguments[1];
31
- const groupValue = arguments[2];
32
- let operator = arguments[3];
33
- const self_ = this;
34
- if (ast instanceof ConditionNode) {
35
- if (!(ast.value instanceof GroupNode)) {
36
- const isValue = ast.value instanceof ArrayNode || ((_b = (_a = ast.value) == null ? void 0 : _a.quote) == null ? void 0 : _b.left.type) === TOKEN_TYPE.REGEX;
37
- let name = ast.property ? unescape(ast.property.value.value) : isValue ? void 0 : unescape((_c = ast.value) == null ? void 0 : _c.value.value);
38
- const isNested = operator !== void 0;
39
- if (prefix !== void 0 && !isNested) {
40
- name = name ? applyPrefix(prefix, name, opts.prefixApplier) : prefix;
41
- }
42
- let value;
43
- if (isNested) {
44
- value = name ?? true;
45
- name = prefix;
46
- } else {
47
- value = ast.value instanceof ArrayNode ? ast.value.values.map((val) => unescape(val.value.value)) : ((_e = (_d = ast.value) == null ? void 0 : _d.quote) == null ? void 0 : _e.left.type) === TOKEN_TYPE.REGEX ? ast.value.value.value : ast.property && ast.value instanceof VariableNode ? unescape(ast.value.value.value) : true;
48
- }
49
- const propertyKeys = name ? opts.keyParser(name) : [];
50
- const boolValue = applyBoolean(groupValue, ast.operator === void 0);
51
- const valuePrefix = ast.value instanceof VariableNode && ast.value.prefix ? unescape(ast.value.prefix.value) : void 0;
52
- operator ?? (operator = (_f = ast.propertyOperator) == null ? void 0 : _f.value);
53
- const isRegex = ((_h = (_g = ast.value) == null ? void 0 : _g.quote) == null ? void 0 : _h.left.type) === TOKEN_TYPE.REGEX;
54
- const isQuoted = ((_i = ast.value) == null ? void 0 : _i.quote) !== void 0;
55
- const isExpanded = ast.sep !== void 0;
56
- const regexFlags = (_l = (_k = (_j = ast.value) == null ? void 0 : _j.quote) == null ? void 0 : _k.flags) == null ? void 0 : _l.value;
57
- const query = {
58
- value,
59
- operator,
60
- prefix: valuePrefix,
61
- regexFlags,
62
- property: propertyKeys,
63
- isRegex,
64
- isQuoted,
65
- isExpanded,
66
- isNegated: !boolValue,
67
- condition: ast
68
- };
69
- const res = opts.conditionNormalizer(query);
70
- return new Condition({ property: propertyKeys, ...res });
71
- } else {
72
- let name = unescape(ast.property.value.value);
73
- if (prefix !== void 0) {
74
- name = applyPrefix(prefix, name, opts.prefixApplier);
75
- }
76
- const boolValue = applyBoolean(groupValue, ast.operator === void 0);
77
- const operator2 = (_m = ast.propertyOperator) == null ? void 0 : _m.value;
78
- return self_.normalize(ast.value, name, boolValue, operator2);
79
- }
80
- }
81
- if (ast instanceof GroupNode) {
82
- const _prefix = ast.prefix instanceof ConditionNode && ast.prefix.value instanceof VariableNode ? unescape(ast.prefix.value.value.value) : void 0;
83
- const _groupValue = ast.prefix instanceof ConditionNode ? ast.prefix.operator === void 0 : !(ast.prefix instanceof ValidToken);
84
- const applied = applyPrefix(prefix, _prefix ?? "", opts.prefixApplier);
85
- return self_.normalize(ast.expression, applied, applyBoolean(groupValue, _groupValue), operator);
86
- }
87
- if (ast instanceof ExpressionNode) {
88
- const left = self_.normalize(ast.left, prefix, groupValue, operator);
89
- const right = self_.normalize(ast.right, prefix, groupValue, operator);
90
- const type = groupValue === false ? OPPOSITE[ast.operator.type] : ast.operator.type;
91
- return new Expression({ operator: type, left, right });
92
- }
93
- return unreachable();
94
- }
95
- }
96
- export {
97
- NormalizeMixin
98
- };
@@ -1,11 +0,0 @@
1
- import { type ParserResults, type Position } from "../types/ast.js";
2
- export declare class ValidateMixin<T extends {}> {
3
- /**
4
- * Allows pre-validating ASTs for syntax highlighting purposes.
5
- * Works similar to evaluate. Internally it will use the prefixApplier, keyParser, and valueValidator (instead of comparer).
6
- *
7
- * The context does not need to be passed. If it's not passed, the function will not attempt to get the values (so it will not error) and the contextValue param of the valueValidator will be undefined.
8
- */
9
- validate(ast: ParserResults, context?: Record<string, any>): (Position & T)[];
10
- }
11
- //# sourceMappingURL=validate.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"validate.d.ts","sourceRoot":"","sources":["../../src/methods/validate.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,KAAK,aAAa,EAAE,KAAK,QAAQ,EAAc,MAAM,iBAAiB,CAAA;AAI/E,qBAAa,aAAa,CAAC,CAAC,SAAS,EAAE;IACtC;;;;;OAKG;IACH,QAAQ,CAAC,GAAG,EAAE,aAAa,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,CAAC,QAAQ,GAAG,CAAC,CAAC,EAAE;CAqH7E"}
@@ -1,113 +0,0 @@
1
- import { get } from "@alanscodelog/utils/get";
2
- import { isArray } from "@alanscodelog/utils/isArray";
3
- import "@alanscodelog/utils/types";
4
- import { ArrayNode } from "../ast/classes/ArrayNode.js";
5
- import { ConditionNode } from "../ast/classes/ConditionNode.js";
6
- import { ErrorToken } from "../ast/classes/ErrorToken.js";
7
- import { ExpressionNode } from "../ast/classes/ExpressionNode.js";
8
- import { GroupNode } from "../ast/classes/GroupNode.js";
9
- import { ValidToken } from "../ast/classes/ValidToken.js";
10
- import { VariableNode } from "../ast/classes/VariableNode.js";
11
- import { applyBoolean } from "../helpers/general/applyBoolean.js";
12
- import { applyPrefix } from "../helpers/general/applyPrefix.js";
13
- import { TOKEN_TYPE } from "../types/ast.js";
14
- class ValidateMixin {
15
- /**
16
- * Allows pre-validating ASTs for syntax highlighting purposes.
17
- * Works similar to evaluate. Internally it will use the prefixApplier, keyParser, and valueValidator (instead of comparer).
18
- *
19
- * The context does not need to be passed. If it's not passed, the function will not attempt to get the values (so it will not error) and the contextValue param of the valueValidator will be undefined.
20
- */
21
- validate(ast, context) {
22
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
23
- const self = this;
24
- self._checkValidationOptions();
25
- const opts = self.options;
26
- if (ast instanceof ErrorToken || !ast.valid) {
27
- throw new Error("AST node must be valid.");
28
- }
29
- const prefix = arguments[2];
30
- const groupValue = arguments[3];
31
- const results = arguments[4] ?? [];
32
- const prefixes = arguments[5] ?? [];
33
- let operator = arguments[6];
34
- const self_ = this;
35
- if (ast instanceof ConditionNode) {
36
- if (!(ast.value instanceof GroupNode)) {
37
- const isValue = ast.value instanceof ArrayNode || ((_b = (_a = ast.value) == null ? void 0 : _a.quote) == null ? void 0 : _b.left.type) === TOKEN_TYPE.REGEX;
38
- const nameNode = ast.property ? ast.property : isValue ? void 0 : ast.value;
39
- let name = nameNode ? unescape(nameNode.value.value) : void 0;
40
- const isNested = operator !== void 0;
41
- if (prefix !== void 0 && !isNested) {
42
- name = name ? applyPrefix(prefix, name, opts.prefixApplier) : prefix;
43
- }
44
- let value;
45
- let propertyNodes = [];
46
- if (isNested) {
47
- value = name;
48
- name = prefix;
49
- propertyNodes = [...prefixes];
50
- } else {
51
- propertyNodes = [...prefixes, ...nameNode ? [nameNode] : []];
52
- value = ast.value instanceof ArrayNode ? ast.value.values : ((_d = (_c = ast.value) == null ? void 0 : _c.quote) == null ? void 0 : _d.left.type) === TOKEN_TYPE.REGEX ? ast.value : ast.property && ast.value instanceof VariableNode ? ast.value : true;
53
- }
54
- const propertyKeys = name ? opts.keyParser(name) : [];
55
- const contextValue = context !== void 0 ? get(context, propertyKeys) : void 0;
56
- const boolValue = applyBoolean(groupValue, ast.operator === void 0);
57
- const valuePrefix = ast.value instanceof VariableNode && ast.value.prefix ? ast.value.prefix : void 0;
58
- operator ?? (operator = ast.propertyOperator);
59
- const isRegex = ((_f = (_e = ast.value) == null ? void 0 : _e.quote) == null ? void 0 : _f.left.type) === TOKEN_TYPE.REGEX;
60
- const isQuoted = ((_g = ast.value) == null ? void 0 : _g.quote) !== void 0;
61
- const isExpanded = ast.sep !== void 0;
62
- const regexFlags = (_i = (_h = ast.value) == null ? void 0 : _h.quote) == null ? void 0 : _i.flags;
63
- const query = {
64
- value,
65
- operator,
66
- prefix: valuePrefix,
67
- prefixes,
68
- property: propertyNodes,
69
- propertyKeys,
70
- propertyName: name,
71
- regexFlags,
72
- isRegex,
73
- isNegated: !boolValue,
74
- isQuoted,
75
- isExpanded,
76
- condition: ast
77
- };
78
- const res = opts.valueValidator(contextValue, query, context);
79
- if (res && !isArray(res))
80
- throw new Error("The valueValidator must return an array or nothing/undefined");
81
- if (res) {
82
- for (const entry of res)
83
- results.push(entry);
84
- }
85
- } else {
86
- let name = unescape(ast.property.value.value);
87
- if (prefix !== void 0) {
88
- name = applyPrefix(prefix, name, opts.prefixApplier);
89
- }
90
- const boolValue = applyBoolean(groupValue, ast.operator === void 0);
91
- if (ast.property)
92
- prefixes.push(ast.property);
93
- const operator2 = ast.propertyOperator;
94
- self_.validate(ast.value, context, name, boolValue, results, prefixes, operator2);
95
- }
96
- }
97
- if (ast instanceof GroupNode) {
98
- const _prefix = ast.prefix instanceof ConditionNode && ast.prefix.value instanceof VariableNode ? ast.prefix.value : void 0;
99
- if (_prefix)
100
- prefixes.push(_prefix);
101
- const _groupValue = ast.prefix instanceof ConditionNode ? ast.prefix.operator === void 0 : !(ast.prefix instanceof ValidToken);
102
- self_.validate(ast.expression, context, applyPrefix(prefix, (_prefix == null ? void 0 : _prefix.value.value) ?? "", opts.prefixApplier), applyBoolean(groupValue, _groupValue), results, prefixes, operator);
103
- }
104
- if (ast instanceof ExpressionNode) {
105
- self_.validate(ast.left, context, prefix, groupValue, results, [...prefixes], operator);
106
- self_.validate(ast.right, context, prefix, groupValue, results, [...prefixes], operator);
107
- }
108
- return results;
109
- }
110
- }
111
- export {
112
- ValidateMixin
113
- };
@@ -1,128 +0,0 @@
1
- import { unreachable } from "@alanscodelog/utils/unreachable"
2
-
3
- import { ConditionNode } from "../ast/classes/ConditionNode.js"
4
- import { VariableNode } from "../ast/classes/VariableNode.js"
5
- import type { Parser } from "../Parser.js"
6
- import { type Completion, type Suggestion, SUGGESTION_TYPE } from "../types/autocomplete.js"
7
- import type { FullParserOptions } from "../types/parser.js"
8
-
9
-
10
- export class AutocompleteMixin<T extends {}> {
11
- /**
12
- * Given a list of @see Suggestion entries, the parser options, and a list of variables, prefixes, operators, etc, and the preferred quote type, returns a list of @see Completion entries.
13
- *
14
- * It takes care of suggesting the correct delimiters for fixes, quoting variables/prefixes if it would not be possible to parse them unquoted, and separating symbol from non-symbol (word) operators.
15
- *
16
- * Does not add whitespace or group requirements. The suggestion information is still in the completion if you wish to show these. But they should not be added to the completion value if using @see autoreplace which will take care of it.
17
- *
18
- * Is not aware of existing values. You will have to use @see getCursorInfo to understand the context in which the suggestion was made, so that, for example, you could filter out used regex flags.
19
- */
20
- autocomplete(
21
- suggestions: Suggestion[],
22
- {
23
- values = [], arrayValues = [], variables = [], prefixes = [], properties = [], expandedPropertyOperators = [], customPropertyOperators = (this as any as Parser<T>).options.customPropertyOperators ?? [], keywords = (this as any as Parser<T>).options.keywords, regexFlags = ["i", "m", "u"], quote = "\"",
24
- }: Partial<Record<
25
- "variables" |
26
- "values" |
27
- "arrayValues" |
28
- "prefixes" |
29
- "properties" |
30
- "regexFlags" |
31
- "expandedPropertyOperators" |
32
- "customPropertyOperators", string[]>> & {
33
- quote?: string
34
- keywords?: FullParserOptions<T>["keywords"]
35
- } = {},
36
- ): Completion[] {
37
- const self = (this as any as Parser<T>)
38
- return suggestions.map(suggestion => {
39
- const type = suggestion.type
40
- switch (type) {
41
- case SUGGESTION_TYPE.BACKTICK: return [{ suggestion, value: "`" }]
42
- case SUGGESTION_TYPE.DOUBLEQUOTE: return [{ suggestion, value: "\"" }]
43
- case SUGGESTION_TYPE.SINGLEQUOTE: return [{ suggestion, value: "'" }]
44
- case SUGGESTION_TYPE.PARENL: return [{ suggestion, value: "(" }]
45
- case SUGGESTION_TYPE.PARENR: return [{ suggestion, value: ")" }]
46
- case SUGGESTION_TYPE.BRAKCETR: return [{ suggestion, value: "]" }] // L not needed
47
- case SUGGESTION_TYPE.REGEX: return [{ suggestion, value: "/" }]
48
- case SUGGESTION_TYPE.REGEX_FLAGS:
49
- return regexFlags
50
- .map(value => ({ suggestion, value }))
51
- // remove existing flags from suggestions
52
- .filter(completion => {
53
- // eslint-disable-next-line @typescript-eslint/no-shadow
54
- const { suggestion, value } = completion
55
- if (suggestion.type !== SUGGESTION_TYPE.REGEX_FLAGS) {return true}
56
-
57
- const token = suggestion.cursorInfo
58
- const flags = token.at && (token.at.parent as VariableNode)?.quote?.flags === suggestion.cursorInfo.at
59
- ? token.at
60
- : token.next && (token.next.parent as VariableNode)?.quote?.flags === suggestion.cursorInfo.next
61
- ? token.next
62
- : token.prev && (token.prev.parent as VariableNode)?.quote?.flags === suggestion.cursorInfo.prev
63
- ? token.prev
64
- : undefined
65
-
66
- if (flags?.value?.includes(value)) {return false}
67
- return true
68
- })
69
- case SUGGESTION_TYPE.PROPERTY: {
70
- return properties.map(value => ({ suggestion, value }))
71
- }
72
- case SUGGESTION_TYPE.PROPERTY_SEP: {
73
- return [{ suggestion, value: self.options.expandedPropertySeparator! }]
74
- }
75
- case SUGGESTION_TYPE.EXPANDED_PROPERTY_OPERATOR: {
76
- return expandedPropertyOperators.map(value => ({ suggestion, value }))
77
- }
78
- case SUGGESTION_TYPE.CUSTOM_PROPERTY_OPERATOR: {
79
- return customPropertyOperators.map(value => ({ suggestion, value }))
80
- }
81
- case SUGGESTION_TYPE.BOOLEAN_SYMBOL_OP: {
82
- const keywordsList = [...keywords.and, ...keywords.or]
83
- const symOpts = keywordsList.filter(_ => _.isSymbol)
84
- return symOpts.map(({ value }) => ({ suggestion, value }))
85
- }
86
- case SUGGESTION_TYPE.BOOLEAN_WORD_OP: {
87
- const keywordsList = [...keywords.and, ...keywords.or]
88
- const wordOpts = keywordsList.filter(_ => !_.isSymbol)
89
- return wordOpts.map(({ value }) => ({ suggestion, value }))
90
- }
91
- case SUGGESTION_TYPE.VALUE:
92
- case SUGGESTION_TYPE.ARRAY_VALUE:
93
- case SUGGESTION_TYPE.VARIABLE: {
94
- const arr = type === SUGGESTION_TYPE.VARIABLE
95
- ? variables
96
- : type === SUGGESTION_TYPE.ARRAY_VALUE
97
- ? arrayValues
98
- : type === SUGGESTION_TYPE.VALUE
99
- ? values
100
- : unreachable()
101
- return arr.map(variable => {
102
- // we don't need to alter options since we can just check there are no quotes (also tells us no prefixes are used) and no operators are defined
103
- const res = self.parse(variable)
104
- if (res instanceof ConditionNode &&
105
- res.operator === undefined &&
106
- res.value instanceof VariableNode &&
107
- res.value.quote === undefined) {
108
- return { suggestion, value: res.value.value.value }
109
- } else {
110
- return { suggestion, value: quote + variable.replace(new RegExp(quote, "g"), `\\${quote}`) + quote }
111
- }
112
- })
113
- }
114
- case SUGGESTION_TYPE.PREFIX: return prefixes.map(prefix => {
115
- const res = self.parse(prefix)
116
- if (res instanceof ConditionNode &&
117
- res.operator === undefined &&
118
- res.value instanceof VariableNode &&
119
- res.value.quote === undefined) {
120
- return { suggestion, value: res.value.value.value }
121
- } else {
122
- return { suggestion, value: quote + prefix.replace(new RegExp(quote, "g"), `\\${quote}`) + quote }
123
- }
124
- })
125
- }
126
- }).flat()
127
- }
128
- }