@mojir/lits 2.1.21 → 2.1.22

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/cli/cli.js CHANGED
@@ -92,7 +92,7 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
92
92
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
93
93
  };
94
94
 
95
- var version = "2.1.21";
95
+ var version = "2.1.22";
96
96
 
97
97
  function getCodeMarker(sourceCodeInfo) {
98
98
  if (!sourceCodeInfo.position || !sourceCodeInfo.code)
@@ -918,6 +918,91 @@ function approxEqual(a, b, epsilon) {
918
918
  function approxZero(value) {
919
919
  return Math.abs(value) < EPSILON;
920
920
  }
921
+ function paramCountAccepts(paramsCount, nbrOfParams) {
922
+ if (typeof paramsCount === 'number') {
923
+ return paramsCount === nbrOfParams;
924
+ }
925
+ var min = paramsCount.min, max = paramsCount.max, even = paramsCount.even, odd = paramsCount.odd;
926
+ if (even && nbrOfParams % 2 !== 0) {
927
+ return false;
928
+ }
929
+ if (odd && nbrOfParams % 2 !== 1) {
930
+ return false;
931
+ }
932
+ if (typeof min === 'number' && nbrOfParams < min) {
933
+ return false;
934
+ }
935
+ if (typeof max === 'number' && nbrOfParams > max) {
936
+ return false;
937
+ }
938
+ return true;
939
+ }
940
+ function paramCountAcceptsMin(paramsCount, nbrOfParams) {
941
+ if (typeof paramsCount === 'number') {
942
+ return nbrOfParams >= paramsCount;
943
+ }
944
+ var min = paramsCount.min;
945
+ if (typeof min === 'number' && nbrOfParams < min) {
946
+ return false;
947
+ }
948
+ return true;
949
+ }
950
+ function getCommonParamCount(params) {
951
+ return params.reduce(function (acc, param) {
952
+ if (acc === null) {
953
+ return null;
954
+ }
955
+ var paramCount = (typeof param === 'number' || isColl(param)) ? 1 : param.paramCount;
956
+ if (typeof acc === 'number' && typeof paramCount === 'number') {
957
+ return acc === paramCount ? acc : null;
958
+ }
959
+ if (typeof paramCount === 'number') {
960
+ if (paramCountAccepts(acc, paramCount)) {
961
+ return paramCount;
962
+ }
963
+ return null;
964
+ }
965
+ if (typeof acc === 'number') {
966
+ if (paramCountAccepts(paramCount, acc)) {
967
+ return acc;
968
+ }
969
+ return null;
970
+ }
971
+ var aMin = paramCount.min, aMax = paramCount.max, aEven = paramCount.even, aOdd = paramCount.odd;
972
+ var bMin = acc.min, bMax = acc.max, bEven = acc.even, bOdd = acc.odd;
973
+ var min = typeof aMin === 'number' && typeof bMin === 'number'
974
+ ? Math.max(aMin, bMin)
975
+ : typeof aMin === 'number' ? aMin : typeof bMin === 'number' ? bMin : undefined;
976
+ var max = typeof aMax === 'number' && typeof bMax === 'number'
977
+ ? Math.min(aMax, bMax)
978
+ : typeof aMax === 'number' ? aMax : typeof bMax === 'number' ? bMax : undefined;
979
+ var even = aEven !== null && aEven !== void 0 ? aEven : bEven;
980
+ var odd = aOdd !== null && aOdd !== void 0 ? aOdd : bOdd;
981
+ if (min !== undefined && max !== undefined && min > max) {
982
+ return null;
983
+ }
984
+ if (even && odd) {
985
+ return null;
986
+ }
987
+ if (odd && min !== undefined && min < 1) {
988
+ return null;
989
+ }
990
+ return { min: min, max: max, even: even, odd: odd };
991
+ }, {});
992
+ }
993
+ function getParamCount(param) {
994
+ return (typeof param === 'number' || isColl(param)) ? 1 : param.paramCount;
995
+ }
996
+ function paramCountMinus(paramCount, count) {
997
+ if (typeof paramCount === 'number') {
998
+ return paramCount - count;
999
+ }
1000
+ var min = paramCount.min === undefined ? undefined : paramCount.min - count;
1001
+ var max = paramCount.max === undefined ? undefined : paramCount.max - count;
1002
+ var even = paramCount.even === undefined ? undefined : count % 2 === 0 ? true : undefined;
1003
+ var odd = paramCount.odd === undefined ? undefined : count % 2 === 0 ? true : undefined;
1004
+ return { min: min, max: max, even: even, odd: odd };
1005
+ }
921
1006
 
922
1007
  // isArray not needed, use Array.isArary
923
1008
  function asArray(value, sourceCodeInfo) {
@@ -4581,11 +4666,13 @@ var functionalNormalExpression = {
4581
4666
  'comp': {
4582
4667
  evaluate: function (params, sourceCodeInfo) {
4583
4668
  var _a;
4669
+ params.forEach(function (param) { return assertFunctionLike(param, sourceCodeInfo); });
4584
4670
  return _a = {},
4585
4671
  _a[FUNCTION_SYMBOL] = true,
4586
4672
  _a.sourceCodeInfo = sourceCodeInfo,
4587
4673
  _a.functionType = 'Comp',
4588
4674
  _a.params = params,
4675
+ _a.paramCount = params.length > 0 ? getParamCount(params.at(-1)) : 1,
4589
4676
  _a;
4590
4677
  },
4591
4678
  paramCount: {},
@@ -4599,6 +4686,7 @@ var functionalNormalExpression = {
4599
4686
  _b.sourceCodeInfo = sourceCodeInfo,
4600
4687
  _b.functionType = 'Constantly',
4601
4688
  _b.value = toAny(value),
4689
+ _b.paramCount = {},
4602
4690
  _b;
4603
4691
  },
4604
4692
  paramCount: 1,
@@ -4606,11 +4694,17 @@ var functionalNormalExpression = {
4606
4694
  'juxt': {
4607
4695
  evaluate: function (params, sourceCodeInfo) {
4608
4696
  var _a;
4697
+ params.forEach(function (param) { return assertFunctionLike(param, sourceCodeInfo); });
4698
+ var paramCount = getCommonParamCount(params);
4699
+ if (paramCount === null) {
4700
+ throw new LitsError('All functions must accept the same number of arguments', sourceCodeInfo);
4701
+ }
4609
4702
  return _a = {},
4610
4703
  _a[FUNCTION_SYMBOL] = true,
4611
4704
  _a.sourceCodeInfo = sourceCodeInfo,
4612
4705
  _a.functionType = 'Juxt',
4613
4706
  _a.params = params,
4707
+ _a.paramCount = paramCount,
4614
4708
  _a;
4615
4709
  },
4616
4710
  paramCount: { min: 1 },
@@ -4619,11 +4713,13 @@ var functionalNormalExpression = {
4619
4713
  evaluate: function (_a, sourceCodeInfo) {
4620
4714
  var _b;
4621
4715
  var _c = __read(_a, 1), fn = _c[0];
4716
+ var fun = asFunctionLike(fn, sourceCodeInfo);
4622
4717
  return _b = {},
4623
4718
  _b[FUNCTION_SYMBOL] = true,
4624
4719
  _b.sourceCodeInfo = sourceCodeInfo,
4625
4720
  _b.functionType = 'Complement',
4626
- _b.function = asFunctionLike(fn, sourceCodeInfo),
4721
+ _b.function = fun,
4722
+ _b.paramCount = getParamCount(fun),
4627
4723
  _b;
4628
4724
  },
4629
4725
  paramCount: 1,
@@ -4636,6 +4732,7 @@ var functionalNormalExpression = {
4636
4732
  _a.sourceCodeInfo = sourceCodeInfo,
4637
4733
  _a.functionType = 'EveryPred',
4638
4734
  _a.params = params,
4735
+ _a.paramCount = 1,
4639
4736
  _a;
4640
4737
  },
4641
4738
  paramCount: { min: 1 },
@@ -4648,6 +4745,7 @@ var functionalNormalExpression = {
4648
4745
  _a.sourceCodeInfo = sourceCodeInfo,
4649
4746
  _a.functionType = 'SomePred',
4650
4747
  _a.params = params,
4748
+ _a.paramCount = 1,
4651
4749
  _a;
4652
4750
  },
4653
4751
  paramCount: { min: 1 },
@@ -4656,12 +4754,14 @@ var functionalNormalExpression = {
4656
4754
  evaluate: function (_a, sourceCodeInfo) {
4657
4755
  var _b;
4658
4756
  var _c = __read(_a), fn = _c[0], params = _c.slice(1);
4757
+ var fun = asFunctionLike(fn, sourceCodeInfo);
4659
4758
  return _b = {},
4660
4759
  _b[FUNCTION_SYMBOL] = true,
4661
4760
  _b.sourceCodeInfo = sourceCodeInfo,
4662
4761
  _b.functionType = 'Fnull',
4663
- _b.function = asFunctionLike(fn, sourceCodeInfo),
4762
+ _b.function = fun,
4664
4763
  _b.params = params,
4764
+ _b.paramCount = getParamCount(fun),
4665
4765
  _b;
4666
4766
  },
4667
4767
  paramCount: { min: 2 },
@@ -11736,12 +11836,16 @@ var functionSpecialExpression = {
11736
11836
  assertUserDefinedSymbolNode(functionSymbol, node[2]);
11737
11837
  assertNameNotDefined(functionSymbol[1], contextStack, builtin, node[2]);
11738
11838
  var evaluatedFunction = evaluateFunction(fn, contextStack, builtin, getUndefinedSymbols, evaluateNode);
11839
+ var min = evaluatedFunction[0].filter(function (arg) { return arg[0] !== bindingTargetTypes.rest && arg[1][1] === undefined; }).length;
11840
+ var max = evaluatedFunction[0].some(function (arg) { return arg[0] === bindingTargetTypes.rest; }) ? undefined : evaluatedFunction[0].length;
11841
+ var paramCount = min === max ? min : { min: min, max: max };
11739
11842
  var litsFunction = (_b = {},
11740
11843
  _b[FUNCTION_SYMBOL] = true,
11741
11844
  _b.sourceCodeInfo = node[2],
11742
11845
  _b.functionType = 'UserDefined',
11743
11846
  _b.name = functionSymbol[1],
11744
11847
  _b.evaluatedfunction = evaluatedFunction,
11848
+ _b.paramCount = paramCount,
11745
11849
  _b);
11746
11850
  contextStack.addValues((_c = {}, _c[functionSymbol[1]] = litsFunction, _c), functionSymbol[2]);
11747
11851
  return litsFunction;
@@ -11763,13 +11867,17 @@ var defnSpecialExpression = {
11763
11867
  var _d = __read(node[1], 3), functionSymbol = _d[1], fn = _d[2];
11764
11868
  assertUserDefinedSymbolNode(functionSymbol, node[2]);
11765
11869
  assertNameNotDefined(functionSymbol[1], contextStack, builtin, node[2]);
11766
- var evaluatedFunctionOverloades = evaluateFunction(fn, contextStack, builtin, getUndefinedSymbols, evaluateNode);
11870
+ var evaluatedFunction = evaluateFunction(fn, contextStack, builtin, getUndefinedSymbols, evaluateNode);
11871
+ var min = evaluatedFunction[0].filter(function (arg) { return arg[0] !== bindingTargetTypes.rest && arg[1][1] === undefined; }).length;
11872
+ var max = evaluatedFunction[0].some(function (arg) { return arg[0] === bindingTargetTypes.rest; }) ? undefined : evaluatedFunction[0].length;
11873
+ var paramCount = min === max ? min : { min: min, max: max };
11767
11874
  var litsFunction = (_b = {},
11768
11875
  _b[FUNCTION_SYMBOL] = true,
11769
11876
  _b.sourceCodeInfo = node[2],
11770
11877
  _b.functionType = 'UserDefined',
11771
11878
  _b.name = functionSymbol[1],
11772
- _b.evaluatedfunction = evaluatedFunctionOverloades,
11879
+ _b.evaluatedfunction = evaluatedFunction,
11880
+ _b.paramCount = paramCount,
11773
11881
  _b);
11774
11882
  contextStack.exportValues((_c = {}, _c[functionSymbol[1]] = litsFunction, _c), functionSymbol[2]);
11775
11883
  return litsFunction;
@@ -11791,12 +11899,16 @@ var fnSpecialExpression = {
11791
11899
  var builtin = _a.builtin, getUndefinedSymbols = _a.getUndefinedSymbols, evaluateNode = _a.evaluateNode;
11792
11900
  var fn = node[1][1];
11793
11901
  var evaluatedFunction = evaluateFunction(fn, contextStack, builtin, getUndefinedSymbols, evaluateNode);
11902
+ var min = evaluatedFunction[0].filter(function (arg) { return arg[0] !== bindingTargetTypes.rest && arg[1][1] === undefined; }).length;
11903
+ var max = evaluatedFunction[0].some(function (arg) { return arg[0] === bindingTargetTypes.rest; }) ? undefined : evaluatedFunction[0].length;
11904
+ var paramCount = min === max ? min : { min: min, max: max };
11794
11905
  var litsFunction = (_b = {},
11795
11906
  _b[FUNCTION_SYMBOL] = true,
11796
11907
  _b.sourceCodeInfo = node[2],
11797
11908
  _b.functionType = 'UserDefined',
11798
11909
  _b.name = undefined,
11799
11910
  _b.evaluatedfunction = evaluatedFunction,
11911
+ _b.paramCount = paramCount,
11800
11912
  _b);
11801
11913
  return litsFunction;
11802
11914
  },
@@ -12412,12 +12524,6 @@ new Set(specialExpressionKeys);
12412
12524
  // TODO, remove
12413
12525
  // console.log('builtin', [...specialExpressionKeys, ...normalExpressionKeys].length)
12414
12526
 
12415
- function checkParams(evaluatedFunction, nbrOfParams, sourceCodeInfo) {
12416
- var minArity = evaluatedFunction[0].filter(function (arg) { return arg[0] !== bindingTargetTypes.rest && arg[1][1] === undefined; }).length;
12417
- if (nbrOfParams < minArity) {
12418
- throw new LitsError("Unexpected number of arguments. Expected at least ".concat(minArity, ", got ").concat(nbrOfParams, "."), sourceCodeInfo);
12419
- }
12420
- }
12421
12527
  var functionExecutors = {
12422
12528
  NativeJsFunction: function (fn, params, sourceCodeInfo) {
12423
12529
  var _a;
@@ -12437,7 +12543,10 @@ var functionExecutors = {
12437
12543
  var evaluateNode = _a.evaluateNode;
12438
12544
  var _loop_1 = function () {
12439
12545
  var e_1, _b;
12440
- checkParams(fn.evaluatedfunction, params.length, sourceCodeInfo);
12546
+ if (!paramCountAcceptsMin(fn.paramCount, params.length)) {
12547
+ throw new LitsError("Expected ".concat(fn.paramCount, " arguments, got ").concat(params.length, "."), sourceCodeInfo);
12548
+ }
12549
+ // checkParams(fn.evaluatedfunction, params.length, sourceCodeInfo)
12441
12550
  var evaluatedFunction = fn.evaluatedfunction;
12442
12551
  var args = evaluatedFunction[0];
12443
12552
  var nbrOfNonRestArgs = args.filter(function (arg) { return arg[0] !== bindingTargetTypes.rest; }).length;
@@ -12734,6 +12843,7 @@ function evaluateNormalExpression(node, contextStack) {
12734
12843
  _a.params = params,
12735
12844
  _a.placeholders = placeholders,
12736
12845
  _a.sourceCodeInfo = sourceCodeInfo,
12846
+ _a.paramCount = paramCountMinus(getParamCount(fn), params.length),
12737
12847
  _a);
12738
12848
  return partialFunction;
12739
12849
  }
@@ -12756,11 +12866,12 @@ function evaluateNormalExpression(node, contextStack) {
12756
12866
  if (placeholders.length > 0) {
12757
12867
  var partialFunction = (_b = {},
12758
12868
  _b[FUNCTION_SYMBOL] = true,
12759
- _b.function = asFunctionLike(fn, sourceCodeInfo),
12869
+ _b.function = fn,
12760
12870
  _b.functionType = 'Partial',
12761
12871
  _b.params = params,
12762
12872
  _b.placeholders = placeholders,
12763
12873
  _b.sourceCodeInfo = sourceCodeInfo,
12874
+ _b.paramCount = paramCountMinus(getParamCount(fn), params.length),
12764
12875
  _b);
12765
12876
  return partialFunction;
12766
12877
  }
@@ -12982,11 +13093,13 @@ var ContextStackImpl = /** @class */ (function () {
12982
13093
  }
12983
13094
  if (isNormalBuiltinSymbolNode(node)) {
12984
13095
  var type = node[1];
13096
+ var normalExpression = allNormalExpressions[type];
12985
13097
  return _b = {},
12986
13098
  _b[FUNCTION_SYMBOL] = true,
12987
13099
  _b.functionType = 'Builtin',
12988
13100
  _b.normalBuitinSymbolType = type,
12989
13101
  _b.sourceCodeInfo = node[2],
13102
+ _b.paramCount = normalExpression.paramCount,
12990
13103
  _b;
12991
13104
  }
12992
13105
  var lookUpResult = this.lookUp(node);
@@ -13023,6 +13136,7 @@ function createContextStack(params) {
13023
13136
  name: name
13024
13137
  },
13025
13138
  _b[FUNCTION_SYMBOL] = true,
13139
+ _b.paramCount = {},
13026
13140
  _b);
13027
13141
  return acc;
13028
13142
  }, {}),
@@ -5,7 +5,7 @@ import type { Any, Arr } from '../interface';
5
5
  import type { SpecialExpressionNode } from '../parser/types';
6
6
  import type { SourceCodeInfo } from '../tokenizer/token';
7
7
  import type { SpecialExpressions } from '.';
8
- export type Count = number | {
8
+ export type ParamCount = number | {
9
9
  min?: number;
10
10
  max?: number;
11
11
  even?: true;
@@ -16,7 +16,7 @@ export type NormalExpressionEvaluator<T> = (params: Arr, sourceCodeInfo: SourceC
16
16
  }) => T;
17
17
  export interface BuiltinNormalExpression<T> {
18
18
  evaluate: NormalExpressionEvaluator<T>;
19
- paramCount: Count;
19
+ paramCount: ParamCount;
20
20
  aliases?: string[];
21
21
  }
22
22
  export type BuiltinNormalExpressions = Record<string, BuiltinNormalExpression<Any>>;
@@ -29,7 +29,7 @@ export interface EvaluateHelpers {
29
29
  export interface BuiltinSpecialExpression<T, N extends SpecialExpressionNode> {
30
30
  evaluate: (node: N, contextStack: ContextStack, helpers: EvaluateHelpers) => T;
31
31
  evaluateAsNormalExpression?: NormalExpressionEvaluator<T>;
32
- paramCount: Count;
32
+ paramCount: ParamCount;
33
33
  getUndefinedSymbols: (node: N, contextStack: ContextStack, params: {
34
34
  getUndefinedSymbols: GetUndefinedSymbols;
35
35
  builtin: Builtin;
@@ -1,4 +1,4 @@
1
- import type { LitsFunction, SpecialExpressionNode, SymbolNode } from '../../parser/types';
1
+ import { type LitsFunction, type SpecialExpressionNode, type SymbolNode } from '../../parser/types';
2
2
  import type { BuiltinSpecialExpression } from '../interface';
3
3
  import type { Function } from '../utils';
4
4
  import type { specialExpressionTypes } from '../specialExpressionTypes';
@@ -1,5 +1,6 @@
1
1
  import type { JsFunction } from '../Lits/Lits';
2
2
  import type { SpecialExpressionType } from '../builtin';
3
+ import type { ParamCount } from '../builtin/interface';
3
4
  import type { specialExpressionTypes } from '../builtin/specialExpressionTypes';
4
5
  import type { FunctionType, NodeType, NodeTypes } from '../constants/constants';
5
6
  import type { Context } from '../evaluator/interface';
@@ -15,6 +16,7 @@ interface GenericLitsFunction {
15
16
  [FUNCTION_SYMBOL]: true;
16
17
  sourceCodeInfo?: SourceCodeInfo;
17
18
  functionType: FunctionType;
19
+ paramCount: ParamCount;
18
20
  }
19
21
  export interface RegularExpression {
20
22
  [REGEXP_SYMBOL]: true;
@@ -1,12 +1,12 @@
1
- import type { Count } from '../builtin/interface';
1
+ import type { ParamCount } from '../builtin/interface';
2
2
  import type { UnknownRecord } from '../interface';
3
3
  import type { NormalExpressionNodeWithName } from '../parser/types';
4
4
  import type { SourceCodeInfo } from '../tokenizer/token';
5
- export declare function assertNumberOfParams(count: Count, node: NormalExpressionNodeWithName): void;
5
+ export declare function assertNumberOfParams(count: ParamCount, node: NormalExpressionNodeWithName): void;
6
6
  export declare function isNonUndefined<T>(value: T | undefined): value is T;
7
7
  export declare function asNonUndefined<T>(value: T | undefined, sourceCodeInfo?: SourceCodeInfo): T;
8
8
  export declare function assertNonUndefined<T>(value: T | undefined, sourceCodeInfo?: SourceCodeInfo): asserts value is T;
9
9
  export declare function isUnknownRecord(value: unknown): value is Record<string, unknown>;
10
10
  export declare function assertUnknownRecord(value: unknown, sourceCodeInfo?: SourceCodeInfo): asserts value is UnknownRecord;
11
11
  export declare function asUnknownRecord(value: unknown, sourceCodeInfo?: SourceCodeInfo): UnknownRecord;
12
- export declare function canBeOperator(count: Count): boolean;
12
+ export declare function canBeOperator(count: ParamCount): boolean;
@@ -1,6 +1,7 @@
1
1
  import type { Any, Coll } from '../interface';
2
- import type { NativeJsFunction } from '../parser/types';
2
+ import type { FunctionLike, NativeJsFunction, NormalExpressionNodeWithName } from '../parser/types';
3
3
  import type { SourceCodeInfo } from '../tokenizer/token';
4
+ import type { ParamCount } from '../builtin/interface';
4
5
  export declare function collHasKey(coll: unknown, key: string | number): boolean;
5
6
  export declare function compare<T extends string | number>(a: T, b: T, sourceCodeInfo: SourceCodeInfo | undefined): number;
6
7
  export declare function deepEqual(a: unknown, b: unknown, sourceCodeInfo?: SourceCodeInfo): boolean;
@@ -13,3 +14,9 @@ export declare function addToSet<T>(target: Set<T>, source: Set<T>): void;
13
14
  export declare const EPSILON = 1e-10;
14
15
  export declare function approxEqual(a: number, b: number, epsilon?: number): boolean;
15
16
  export declare function approxZero(value: number): boolean;
17
+ export declare function mergeParamCounts(count: ParamCount, node: NormalExpressionNodeWithName): void;
18
+ export declare function assertNumberOfParams(count: ParamCount, node: NormalExpressionNodeWithName): void;
19
+ export declare function paramCountAcceptsMin(paramsCount: ParamCount, nbrOfParams: number): boolean;
20
+ export declare function getCommonParamCount(params: FunctionLike[]): ParamCount | null;
21
+ export declare function getParamCount(param: FunctionLike): ParamCount;
22
+ export declare function paramCountMinus(paramCount: ParamCount, count: number): ParamCount;