@mojir/lits 2.1.1 → 2.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.
@@ -9,7 +9,6 @@ export declare const api: {
9
9
  readonly object: readonly ["dissoc", "keys", "vals", "entries", "find", "merge", "merge-with", "zipmap", "select-keys"];
10
10
  readonly predicate: readonly ["boolean?", "null?", "number?", "string?", "function?", "integer?", "array?", "object?", "coll?", "seq?", "regexp?", "zero?", "pos?", "neg?", "even?", "odd?", "finite?", "nan?", "negative-infinity?", "positive-infinity?", "false?", "true?", "empty?", "not-empty?"];
11
11
  readonly regularExpression: readonly ["regexp", "match", "replace", "replace-all"];
12
- readonly specialExpressions: readonly ["&&", "||", "def", "let", "fn", "defn", "function", "try", "throw", "if", "unless", "cond", "switch", "do", "recur", "loop", "doseq", "for", "defined?", "??"];
13
12
  readonly string: readonly ["string-repeat", "str", "number", "lower-case", "upper-case", "trim", "trim-left", "trim-right", "pad-left", "pad-right", "split", "split-lines", "template", "to-char-code", "from-char-code", "encode-base64", "decode-base64", "encode-uri-component", "decode-uri-component", "join", "capitalize", "blank?"];
14
13
  readonly bitwise: readonly ["<<", ">>", ">>>", "~", "&", "bit-and-not", "|", "^", "bit-flip", "bit-clear", "bit-set", "bit-test"];
15
14
  readonly assert: readonly ["assert", "assert=", "assert!=", "assert-gt", "assert-lt", "assert-gte", "assert-lte", "assert-true", "assert-false", "assert-truthy", "assert-falsy", "assert-null", "assert-throws", "assert-throws-error", "assert-not-throws"];
@@ -1,4 +1,5 @@
1
1
  import type { Any } from '../interface';
2
2
  import { type BindingTarget, type Node } from '../parser/types';
3
+ export declare function walkDefaults(bindingTarget: BindingTarget, onDefault: (Node: Node) => void): void;
3
4
  export declare function evalueateBindingNodeValues(target: BindingTarget, value: Any, evaluate: (Node: Node) => Any): Record<string, Any>;
4
5
  export declare function getAllBindingTargetNames(bindingTarget: BindingTarget): Record<string, true>;
@@ -1,16 +1,15 @@
1
- import type { SpecialExpressionName, SpecialExpressionType } from '.';
2
1
  export declare const specialExpressionTypes: {
3
2
  readonly '??': 0;
4
3
  readonly '&&': 1;
5
4
  readonly '||': 2;
6
5
  readonly array: 3;
7
6
  readonly cond: 4;
8
- readonly def: 5;
7
+ readonly '0_def': 5;
9
8
  readonly 'defined?': 6;
10
- readonly defn: 7;
9
+ readonly '0_defn': 7;
11
10
  readonly do: 8;
12
11
  readonly doseq: 9;
13
- readonly fn: 10;
12
+ readonly '0_fn': 10;
14
13
  readonly for: 11;
15
14
  readonly function: 12;
16
15
  readonly if: 13;
@@ -24,4 +23,3 @@ export declare const specialExpressionTypes: {
24
23
  readonly unless: 21;
25
24
  };
26
25
  export type SpecialExpressionTypes = typeof specialExpressionTypes;
27
- export declare function getNameFromSpecialExpressionType(type: SpecialExpressionType): SpecialExpressionName;
@@ -1,5 +1,5 @@
1
1
  import type { BindingNode, SpecialExpressionNode } from '../../parser/types';
2
2
  import type { BuiltinSpecialExpression } from '../interface';
3
3
  import type { specialExpressionTypes } from '../specialExpressionTypes';
4
- export type DefNode = SpecialExpressionNode<[typeof specialExpressionTypes['def'], BindingNode]>;
4
+ export type DefNode = SpecialExpressionNode<[typeof specialExpressionTypes['0_def'], BindingNode]>;
5
5
  export declare const defSpecialExpression: BuiltinSpecialExpression<null, DefNode>;
@@ -2,9 +2,9 @@ import type { LitsFunction, SpecialExpressionNode, SymbolNode } from '../../pars
2
2
  import type { BuiltinSpecialExpression } from '../interface';
3
3
  import type { Function } from '../utils';
4
4
  import type { specialExpressionTypes } from '../specialExpressionTypes';
5
- export type DefnNode = SpecialExpressionNode<[typeof specialExpressionTypes['defn'], SymbolNode, Function]>;
5
+ export type DefnNode = SpecialExpressionNode<[typeof specialExpressionTypes['0_defn'], SymbolNode, Function]>;
6
6
  export type FunctionNode = SpecialExpressionNode<[typeof specialExpressionTypes['function'], SymbolNode, Function]>;
7
- export type FnNode = SpecialExpressionNode<[typeof specialExpressionTypes['fn'], Function]>;
7
+ export type FnNode = SpecialExpressionNode<[typeof specialExpressionTypes['0_fn'], Function]>;
8
8
  export declare const functionSpecialExpression: BuiltinSpecialExpression<null, FunctionNode>;
9
9
  export declare const defnSpecialExpression: BuiltinSpecialExpression<null, DefnNode>;
10
10
  export declare const fnSpecialExpression: BuiltinSpecialExpression<LitsFunction, FnNode>;
@@ -1,4 +1,3 @@
1
- import type { SpecialExpressionName } from '../builtin';
2
1
  import type { ExpressionNode, Node, NormalBuiltinSymbolNode, NormalExpressionNode, NormalExpressionNodeWithName, SpecialBuiltinSymbolNode, SpreadNode, SymbolNode, UserDefinedSymbolNode } from '../parser/types';
3
2
  import type { SourceCodeInfo } from '../tokenizer/token';
4
3
  export declare function isSymbolNode(node: Node): node is SymbolNode;
@@ -8,11 +7,7 @@ export declare function isUserDefinedSymbolNode(node: Node): node is UserDefined
8
7
  export declare function asUserDefinedSymbolNode(node: Node, sourceCodeInfo?: SourceCodeInfo): UserDefinedSymbolNode;
9
8
  export declare function assertUserDefinedSymbolNode(node: Node, sourceCodeInfo?: SourceCodeInfo): asserts node is UserDefinedSymbolNode;
10
9
  export declare function isNormalBuiltinSymbolNode(node: Node): node is NormalBuiltinSymbolNode;
11
- export declare function asNormalBuiltinSymbolNode(node: Node, sourceCodeInfo?: SourceCodeInfo): NormalBuiltinSymbolNode;
12
- export declare function assertNormalBuiltinSymbolNode(node: Node, sourceCodeInfo?: SourceCodeInfo): asserts node is NormalBuiltinSymbolNode;
13
- export declare function isSpecialBuiltinSymbolNode(node: Node, name?: SpecialExpressionName): node is SpecialBuiltinSymbolNode;
14
- export declare function asSpecialBuiltinSymbolNode(node: Node, sourceCodeInfo?: SourceCodeInfo): SpecialBuiltinSymbolNode;
15
- export declare function assertSpecialBuiltinSymbolNode(node: Node, sourceCodeInfo?: SourceCodeInfo): asserts node is SpecialBuiltinSymbolNode;
10
+ export declare function isSpecialBuiltinSymbolNode(node: Node): node is SpecialBuiltinSymbolNode;
16
11
  export declare function isNormalExpressionNode(node: Node): node is NormalExpressionNode;
17
12
  export declare function asNormalExpressionNode(node: Node, sourceCodeInfo?: SourceCodeInfo): NormalExpressionNode;
18
13
  export declare function assertNormalExpressionNode(node: Node, sourceCodeInfo?: SourceCodeInfo): asserts node is NormalExpressionNode;
@@ -183,12 +183,12 @@ var specialExpressionTypes = {
183
183
  '||': 2,
184
184
  'array': 3,
185
185
  'cond': 4,
186
- 'def': 5,
186
+ '0_def': 5,
187
187
  'defined?': 6,
188
- 'defn': 7,
188
+ '0_defn': 7,
189
189
  'do': 8,
190
190
  'doseq': 9,
191
- 'fn': 10,
191
+ '0_fn': 10,
192
192
  'for': 11,
193
193
  'function': 12,
194
194
  'if': 13,
@@ -302,25 +302,9 @@ function assertUserDefinedSymbolNode(node, sourceCodeInfo) {
302
302
  function isNormalBuiltinSymbolNode(node) {
303
303
  return NodeTypes.NormalBuiltinSymbol === node[0];
304
304
  }
305
- function isSpecialBuiltinSymbolNode(node, name) {
306
- if (NodeTypes.SpecialBuiltinSymbol !== node[0]) {
307
- return false;
308
- }
309
- {
310
- return true;
311
- }
305
+ function isSpecialBuiltinSymbolNode(node) {
306
+ return NodeTypes.SpecialBuiltinSymbol === node[0];
312
307
  }
313
- // export function isNumberNode(node: Node): node is NumberNode {
314
- // return node[0] === NodeTypes.Number
315
- // }
316
- // export function asNumberNode(node: Node, sourceCodeInfo?: SourceCodeInfo): NumberNode {
317
- // assertNumberNode(node, sourceCodeInfo)
318
- // return node
319
- // }
320
- // export function assertNumberNode(node: Node, sourceCodeInfo?: SourceCodeInfo): asserts node is NumberNode {
321
- // if (!isNumberNode(node))
322
- // throw getAssertionError('NumberNode', node, sourceCodeInfo)
323
- // }
324
308
  function isNormalExpressionNode(node) {
325
309
  return node[0] === NodeTypes.NormalExpression;
326
310
  }
@@ -373,6 +357,7 @@ function findUnresolvedSymbolsInNode(node, contextStack, builtin, evaluateNode)
373
357
  case NodeTypes.String:
374
358
  case NodeTypes.Number:
375
359
  case NodeTypes.ReservedSymbol:
360
+ case NodeTypes.Binding:
376
361
  return null;
377
362
  case NodeTypes.NormalExpression: {
378
363
  var normalExpressionNode = node;
@@ -417,10 +402,7 @@ function findUnresolvedSymbolsInNode(node, contextStack, builtin, evaluateNode)
417
402
  }
418
403
  case NodeTypes.Spread:
419
404
  return findUnresolvedSymbolsInNode(node[1], contextStack, builtin, evaluateNode);
420
- case NodeTypes.Binding: {
421
- var bindingNode = node;
422
- return findUnresolvedSymbolsInNode(bindingNode[1][1], contextStack, builtin, evaluateNode);
423
- }
405
+ /* v8 ignore next 2 */
424
406
  default:
425
407
  throw new Error("Unhandled node type: ".concat(nodeType));
426
408
  }
@@ -689,9 +671,6 @@ function assertLitsFunction(value, sourceCodeInfo) {
689
671
  if (!isLitsFunction(value))
690
672
  throw getAssertionError('LitsFunction', value, sourceCodeInfo);
691
673
  }
692
- function isBuiltinFunction(value) {
693
- return isUnknownRecord(value) && value.functionType === 'Builtin';
694
- }
695
674
 
696
675
  function isAny(value) {
697
676
  // TODO weak test
@@ -3881,6 +3860,29 @@ var bindingTargetTypes = {
3881
3860
  array: 14,
3882
3861
  };
3883
3862
 
3863
+ function walkDefaults(bindingTarget, onDefault) {
3864
+ var _a;
3865
+ if (bindingTarget[0] === bindingTargetTypes.object) {
3866
+ Object.values(bindingTarget[1][0]).forEach(function (element) {
3867
+ if (element[1][1]) {
3868
+ onDefault(element[1][1]);
3869
+ }
3870
+ walkDefaults(element, onDefault);
3871
+ });
3872
+ }
3873
+ else if (bindingTarget[0] === bindingTargetTypes.array) {
3874
+ for (var index = 0; index < bindingTarget[1][0].length; index += 1) {
3875
+ var element = (_a = bindingTarget[1][0][index]) !== null && _a !== void 0 ? _a : null;
3876
+ if (element === null) {
3877
+ continue;
3878
+ }
3879
+ if (element[1][1]) {
3880
+ onDefault(element[1][1]);
3881
+ }
3882
+ walkDefaults(element, onDefault);
3883
+ }
3884
+ }
3885
+ }
3884
3886
  function evalueateBindingNodeValues(target, value, evaluate) {
3885
3887
  var sourceCodeInfo = target[2];
3886
3888
  var record = {};
@@ -4020,6 +4022,9 @@ var defSpecialExpression = {
4020
4022
  var target = bindingNode[1][0];
4021
4023
  var value = bindingNode[1][1];
4022
4024
  var bindingResult = getUndefinedSymbols([value], contextStack, builtin, evaluateNode);
4025
+ walkDefaults(target, function (defaultNode) {
4026
+ addToSet(bindingResult, getUndefinedSymbols([defaultNode], contextStack, builtin, evaluateNode));
4027
+ });
4023
4028
  contextStack.addValues(getAllBindingTargetNames(target));
4024
4029
  return bindingResult;
4025
4030
  },
@@ -4147,7 +4152,7 @@ var functionSpecialExpression = {
4147
4152
  var functionName = node[1][1][1];
4148
4153
  contextStack.addValues((_b = {}, _b[functionName] = true, _b));
4149
4154
  var newContext = (_c = {}, _c[functionName] = { value: true }, _c);
4150
- return addFunctionUnresolvedSymbols(node[1][2], contextStack, getUndefinedSymbols, builtin, evaluateNode, newContext);
4155
+ return getFunctionUnresolvedSymbols(node[1][2], contextStack, getUndefinedSymbols, builtin, evaluateNode, newContext);
4151
4156
  },
4152
4157
  };
4153
4158
  var defnSpecialExpression = {
@@ -4176,7 +4181,7 @@ var defnSpecialExpression = {
4176
4181
  var fn = node[1][2];
4177
4182
  contextStack.exportValues((_b = {}, _b[functionName] = true, _b));
4178
4183
  var newContext = (_c = {}, _c[functionName] = { value: true }, _c);
4179
- return addFunctionUnresolvedSymbols(fn, contextStack, getUndefinedSymbols, builtin, evaluateNode, newContext);
4184
+ return getFunctionUnresolvedSymbols(fn, contextStack, getUndefinedSymbols, builtin, evaluateNode, newContext);
4180
4185
  },
4181
4186
  };
4182
4187
  var fnSpecialExpression = {
@@ -4198,7 +4203,7 @@ var fnSpecialExpression = {
4198
4203
  getUndefinedSymbols: function (node, contextStack, _a) {
4199
4204
  var getUndefinedSymbols = _a.getUndefinedSymbols, builtin = _a.builtin, evaluateNode = _a.evaluateNode;
4200
4205
  var fn = node[1][1];
4201
- return addFunctionUnresolvedSymbols(fn, contextStack, getUndefinedSymbols, builtin, evaluateNode);
4206
+ return getFunctionUnresolvedSymbols(fn, contextStack, getUndefinedSymbols, builtin, evaluateNode);
4202
4207
  },
4203
4208
  };
4204
4209
  function evaluateFunction(fn, contextStack, builtin, getUndefinedSymbols, evaluateNode) {
@@ -4223,12 +4228,15 @@ function evaluateFunction(fn, contextStack, builtin, getUndefinedSymbols, evalua
4223
4228
  ];
4224
4229
  return evaluatedFunction;
4225
4230
  }
4226
- function addFunctionUnresolvedSymbols(fn, contextStack, getUndefinedSymbols, builtin, evaluateNode, functionNameContext) {
4231
+ function getFunctionUnresolvedSymbols(fn, contextStack, getUndefinedSymbols, builtin, evaluateNode, functionNameContext) {
4227
4232
  var result = new Set();
4228
4233
  var contextStackWithFunctionName = functionNameContext ? contextStack.create(functionNameContext) : contextStack;
4229
4234
  var newContext = {};
4230
4235
  fn[0].forEach(function (arg) {
4231
4236
  Object.assign(newContext, getAllBindingTargetNames(arg));
4237
+ walkDefaults(arg, function (defaultNode) {
4238
+ addToSet(result, getUndefinedSymbols([defaultNode], contextStack, builtin, evaluateNode));
4239
+ });
4232
4240
  });
4233
4241
  var newContextStack = contextStackWithFunctionName.create(newContext);
4234
4242
  var overloadResult = getUndefinedSymbols(fn[1], newContextStack, builtin, evaluateNode);
@@ -4292,6 +4300,9 @@ var letSpecialExpression = {
4292
4300
  var target = bindingNode[1][0];
4293
4301
  var value = bindingNode[1][1];
4294
4302
  var bindingResult = getUndefinedSymbols([value], contextStack, builtin, evaluateNode);
4303
+ walkDefaults(target, function (defaultNode) {
4304
+ addToSet(bindingResult, getUndefinedSymbols([defaultNode], contextStack, builtin, evaluateNode));
4305
+ });
4295
4306
  contextStack.addValues(getAllBindingTargetNames(target));
4296
4307
  return bindingResult;
4297
4308
  },
@@ -5243,8 +5254,6 @@ var ContextStackImpl = /** @class */ (function () {
5243
5254
  var lookUpResult = this.lookUp(node);
5244
5255
  if (isContextEntry(lookUpResult))
5245
5256
  return lookUpResult.value;
5246
- else if (isBuiltinFunction(lookUpResult))
5247
- return lookUpResult;
5248
5257
  throw new UndefinedSymbolError(node[1], node[2]);
5249
5258
  };
5250
5259
  return ContextStackImpl;
@@ -5328,10 +5337,10 @@ var nonFunctionOperators = [
5328
5337
  'cond',
5329
5338
  'def',
5330
5339
  'defined?',
5331
- 'defn',
5340
+ // 'defn',
5332
5341
  'do',
5333
5342
  'doseq',
5334
- 'fn',
5343
+ // 'fn',
5335
5344
  'if',
5336
5345
  'let',
5337
5346
  'loop',
@@ -6176,10 +6185,7 @@ var Parser = /** @class */ (function () {
6176
6185
  if (specialExpressionTypes[operatorName] !== undefined) {
6177
6186
  return withSourceCodeInfo([NodeTypes.SpecialBuiltinSymbol, specialExpressionTypes[operatorName]], token[2]);
6178
6187
  }
6179
- else if (normalExpressionTypes[operatorName] !== undefined) {
6180
- return withSourceCodeInfo([NodeTypes.NormalBuiltinSymbol, normalExpressionTypes[operatorName]], token[2]);
6181
- }
6182
- return withSourceCodeInfo([NodeTypes.UserDefinedSymbol, operatorName], token[2]);
6188
+ return withSourceCodeInfo([NodeTypes.NormalBuiltinSymbol, normalExpressionTypes[operatorName]], token[2]);
6183
6189
  }
6184
6190
  if (operatorName === '->') {
6185
6191
  return this.parseShorthandLamdaFunction();
@@ -6339,9 +6345,9 @@ var Parser = /** @class */ (function () {
6339
6345
  var _b = __read(params, 1), param = _b[0];
6340
6346
  return withSourceCodeInfo([NodeTypes.SpecialExpression, [type, param]], symbol[2]);
6341
6347
  }
6342
- case specialExpressionTypes.fn:
6343
- case specialExpressionTypes.def:
6344
- case specialExpressionTypes.defn:
6348
+ case specialExpressionTypes['0_fn']:
6349
+ case specialExpressionTypes['0_def']:
6350
+ case specialExpressionTypes['0_defn']:
6345
6351
  throw new LitsError("".concat(type, " is not allowed"), symbol[2]);
6346
6352
  /* v8 ignore next 2 */
6347
6353
  default:
@@ -6369,7 +6375,7 @@ var Parser = /** @class */ (function () {
6369
6375
  }
6370
6376
  this.advance();
6371
6377
  var body = this.parseExpression();
6372
- return withSourceCodeInfo([NodeTypes.SpecialExpression, [specialExpressionTypes.fn, [
6378
+ return withSourceCodeInfo([NodeTypes.SpecialExpression, [specialExpressionTypes['0_fn'], [
6373
6379
  functionArguments,
6374
6380
  [body],
6375
6381
  ]]], firstToken[2]);
@@ -6452,7 +6458,7 @@ var Parser = /** @class */ (function () {
6452
6458
  functionArguments.push(withSourceCodeInfo([bindingTargetTypes.symbol, [[NodeTypes.UserDefinedSymbol, "$".concat(i)], undefined]], firstToken[2]));
6453
6459
  }
6454
6460
  }
6455
- var node = withSourceCodeInfo([NodeTypes.SpecialExpression, [specialExpressionTypes.fn, [
6461
+ var node = withSourceCodeInfo([NodeTypes.SpecialExpression, [specialExpressionTypes['0_fn'], [
6456
6462
  functionArguments,
6457
6463
  [exprNode],
6458
6464
  ]]], firstToken[2]);
@@ -6971,7 +6977,7 @@ var Parser = /** @class */ (function () {
6971
6977
  this.advance();
6972
6978
  if (isSymbolToken(this.peek(), 'let')) {
6973
6979
  var letNode = this.parseLet(asSymbolToken(this.peek()));
6974
- return withSourceCodeInfo([NodeTypes.SpecialExpression, [specialExpressionTypes.def, letNode[1][1]]], token[2]);
6980
+ return withSourceCodeInfo([NodeTypes.SpecialExpression, [specialExpressionTypes['0_def'], letNode[1][1]]], token[2]);
6975
6981
  }
6976
6982
  else if (isReservedSymbolToken(this.peek(), 'function')) {
6977
6983
  this.advance();
@@ -6989,7 +6995,7 @@ var Parser = /** @class */ (function () {
6989
6995
  }
6990
6996
  assertReservedSymbolToken(this.peek(), 'end');
6991
6997
  this.advance();
6992
- return withSourceCodeInfo([NodeTypes.SpecialExpression, [specialExpressionTypes.defn, symbol, [
6998
+ return withSourceCodeInfo([NodeTypes.SpecialExpression, [specialExpressionTypes['0_defn'], symbol, [
6993
6999
  functionArguments,
6994
7000
  body,
6995
7001
  ]]], token[2]);
@@ -6999,7 +7005,7 @@ var Parser = /** @class */ (function () {
6999
7005
  }
7000
7006
  };
7001
7007
  Parser.prototype.stringToSymbolNode = function (value, sourceCodeInfo) {
7002
- if (specialExpressionTypes[value] !== undefined) {
7008
+ if (specialExpressionTypes[value] !== undefined && value !== 'fn' && value !== 'def' && value !== 'defn') {
7003
7009
  return withSourceCodeInfo([NodeTypes.SpecialBuiltinSymbol, specialExpressionTypes[value]], sourceCodeInfo);
7004
7010
  }
7005
7011
  if (normalExpressionTypes[value] !== undefined) {