@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.
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.1";
95
+ var version = "2.1.2";
96
96
 
97
97
  function getCodeMarker(sourceCodeInfo) {
98
98
  if (!sourceCodeInfo.position || !sourceCodeInfo.code)
@@ -190,12 +190,12 @@ var specialExpressionTypes = {
190
190
  '||': 2,
191
191
  'array': 3,
192
192
  'cond': 4,
193
- 'def': 5,
193
+ '0_def': 5,
194
194
  'defined?': 6,
195
- 'defn': 7,
195
+ '0_defn': 7,
196
196
  'do': 8,
197
197
  'doseq': 9,
198
- 'fn': 10,
198
+ '0_fn': 10,
199
199
  'for': 11,
200
200
  'function': 12,
201
201
  'if': 13,
@@ -309,25 +309,9 @@ function assertUserDefinedSymbolNode(node, sourceCodeInfo) {
309
309
  function isNormalBuiltinSymbolNode(node) {
310
310
  return NodeTypes.NormalBuiltinSymbol === node[0];
311
311
  }
312
- function isSpecialBuiltinSymbolNode(node, name) {
313
- if (NodeTypes.SpecialBuiltinSymbol !== node[0]) {
314
- return false;
315
- }
316
- {
317
- return true;
318
- }
312
+ function isSpecialBuiltinSymbolNode(node) {
313
+ return NodeTypes.SpecialBuiltinSymbol === node[0];
319
314
  }
320
- // export function isNumberNode(node: Node): node is NumberNode {
321
- // return node[0] === NodeTypes.Number
322
- // }
323
- // export function asNumberNode(node: Node, sourceCodeInfo?: SourceCodeInfo): NumberNode {
324
- // assertNumberNode(node, sourceCodeInfo)
325
- // return node
326
- // }
327
- // export function assertNumberNode(node: Node, sourceCodeInfo?: SourceCodeInfo): asserts node is NumberNode {
328
- // if (!isNumberNode(node))
329
- // throw getAssertionError('NumberNode', node, sourceCodeInfo)
330
- // }
331
315
  function isNormalExpressionNode(node) {
332
316
  return node[0] === NodeTypes.NormalExpression;
333
317
  }
@@ -380,6 +364,7 @@ function findUnresolvedSymbolsInNode(node, contextStack, builtin, evaluateNode)
380
364
  case NodeTypes.String:
381
365
  case NodeTypes.Number:
382
366
  case NodeTypes.ReservedSymbol:
367
+ case NodeTypes.Binding:
383
368
  return null;
384
369
  case NodeTypes.NormalExpression: {
385
370
  var normalExpressionNode = node;
@@ -424,10 +409,7 @@ function findUnresolvedSymbolsInNode(node, contextStack, builtin, evaluateNode)
424
409
  }
425
410
  case NodeTypes.Spread:
426
411
  return findUnresolvedSymbolsInNode(node[1], contextStack, builtin, evaluateNode);
427
- case NodeTypes.Binding: {
428
- var bindingNode = node;
429
- return findUnresolvedSymbolsInNode(bindingNode[1][1], contextStack, builtin, evaluateNode);
430
- }
412
+ /* v8 ignore next 2 */
431
413
  default:
432
414
  throw new Error("Unhandled node type: ".concat(nodeType));
433
415
  }
@@ -711,9 +693,6 @@ function assertLitsFunction(value, sourceCodeInfo) {
711
693
  if (!isLitsFunction(value))
712
694
  throw getAssertionError('LitsFunction', value, sourceCodeInfo);
713
695
  }
714
- function isBuiltinFunction(value) {
715
- return isUnknownRecord(value) && value.functionType === 'Builtin';
716
- }
717
696
 
718
697
  function isAny(value) {
719
698
  // TODO weak test
@@ -3903,6 +3882,29 @@ var bindingTargetTypes = {
3903
3882
  array: 14,
3904
3883
  };
3905
3884
 
3885
+ function walkDefaults(bindingTarget, onDefault) {
3886
+ var _a;
3887
+ if (bindingTarget[0] === bindingTargetTypes.object) {
3888
+ Object.values(bindingTarget[1][0]).forEach(function (element) {
3889
+ if (element[1][1]) {
3890
+ onDefault(element[1][1]);
3891
+ }
3892
+ walkDefaults(element, onDefault);
3893
+ });
3894
+ }
3895
+ else if (bindingTarget[0] === bindingTargetTypes.array) {
3896
+ for (var index = 0; index < bindingTarget[1][0].length; index += 1) {
3897
+ var element = (_a = bindingTarget[1][0][index]) !== null && _a !== void 0 ? _a : null;
3898
+ if (element === null) {
3899
+ continue;
3900
+ }
3901
+ if (element[1][1]) {
3902
+ onDefault(element[1][1]);
3903
+ }
3904
+ walkDefaults(element, onDefault);
3905
+ }
3906
+ }
3907
+ }
3906
3908
  function evalueateBindingNodeValues(target, value, evaluate) {
3907
3909
  var sourceCodeInfo = target[2];
3908
3910
  var record = {};
@@ -4042,6 +4044,9 @@ var defSpecialExpression = {
4042
4044
  var target = bindingNode[1][0];
4043
4045
  var value = bindingNode[1][1];
4044
4046
  var bindingResult = getUndefinedSymbols([value], contextStack, builtin, evaluateNode);
4047
+ walkDefaults(target, function (defaultNode) {
4048
+ addToSet(bindingResult, getUndefinedSymbols([defaultNode], contextStack, builtin, evaluateNode));
4049
+ });
4045
4050
  contextStack.addValues(getAllBindingTargetNames(target));
4046
4051
  return bindingResult;
4047
4052
  },
@@ -4169,7 +4174,7 @@ var functionSpecialExpression = {
4169
4174
  var functionName = node[1][1][1];
4170
4175
  contextStack.addValues((_b = {}, _b[functionName] = true, _b));
4171
4176
  var newContext = (_c = {}, _c[functionName] = { value: true }, _c);
4172
- return addFunctionUnresolvedSymbols(node[1][2], contextStack, getUndefinedSymbols, builtin, evaluateNode, newContext);
4177
+ return getFunctionUnresolvedSymbols(node[1][2], contextStack, getUndefinedSymbols, builtin, evaluateNode, newContext);
4173
4178
  },
4174
4179
  };
4175
4180
  var defnSpecialExpression = {
@@ -4198,7 +4203,7 @@ var defnSpecialExpression = {
4198
4203
  var fn = node[1][2];
4199
4204
  contextStack.exportValues((_b = {}, _b[functionName] = true, _b));
4200
4205
  var newContext = (_c = {}, _c[functionName] = { value: true }, _c);
4201
- return addFunctionUnresolvedSymbols(fn, contextStack, getUndefinedSymbols, builtin, evaluateNode, newContext);
4206
+ return getFunctionUnresolvedSymbols(fn, contextStack, getUndefinedSymbols, builtin, evaluateNode, newContext);
4202
4207
  },
4203
4208
  };
4204
4209
  var fnSpecialExpression = {
@@ -4220,7 +4225,7 @@ var fnSpecialExpression = {
4220
4225
  getUndefinedSymbols: function (node, contextStack, _a) {
4221
4226
  var getUndefinedSymbols = _a.getUndefinedSymbols, builtin = _a.builtin, evaluateNode = _a.evaluateNode;
4222
4227
  var fn = node[1][1];
4223
- return addFunctionUnresolvedSymbols(fn, contextStack, getUndefinedSymbols, builtin, evaluateNode);
4228
+ return getFunctionUnresolvedSymbols(fn, contextStack, getUndefinedSymbols, builtin, evaluateNode);
4224
4229
  },
4225
4230
  };
4226
4231
  function evaluateFunction(fn, contextStack, builtin, getUndefinedSymbols, evaluateNode) {
@@ -4245,12 +4250,15 @@ function evaluateFunction(fn, contextStack, builtin, getUndefinedSymbols, evalua
4245
4250
  ];
4246
4251
  return evaluatedFunction;
4247
4252
  }
4248
- function addFunctionUnresolvedSymbols(fn, contextStack, getUndefinedSymbols, builtin, evaluateNode, functionNameContext) {
4253
+ function getFunctionUnresolvedSymbols(fn, contextStack, getUndefinedSymbols, builtin, evaluateNode, functionNameContext) {
4249
4254
  var result = new Set();
4250
4255
  var contextStackWithFunctionName = functionNameContext ? contextStack.create(functionNameContext) : contextStack;
4251
4256
  var newContext = {};
4252
4257
  fn[0].forEach(function (arg) {
4253
4258
  Object.assign(newContext, getAllBindingTargetNames(arg));
4259
+ walkDefaults(arg, function (defaultNode) {
4260
+ addToSet(result, getUndefinedSymbols([defaultNode], contextStack, builtin, evaluateNode));
4261
+ });
4254
4262
  });
4255
4263
  var newContextStack = contextStackWithFunctionName.create(newContext);
4256
4264
  var overloadResult = getUndefinedSymbols(fn[1], newContextStack, builtin, evaluateNode);
@@ -4314,6 +4322,9 @@ var letSpecialExpression = {
4314
4322
  var target = bindingNode[1][0];
4315
4323
  var value = bindingNode[1][1];
4316
4324
  var bindingResult = getUndefinedSymbols([value], contextStack, builtin, evaluateNode);
4325
+ walkDefaults(target, function (defaultNode) {
4326
+ addToSet(bindingResult, getUndefinedSymbols([defaultNode], contextStack, builtin, evaluateNode));
4327
+ });
4317
4328
  contextStack.addValues(getAllBindingTargetNames(target));
4318
4329
  return bindingResult;
4319
4330
  },
@@ -5265,8 +5276,6 @@ var ContextStackImpl = /** @class */ (function () {
5265
5276
  var lookUpResult = this.lookUp(node);
5266
5277
  if (isContextEntry(lookUpResult))
5267
5278
  return lookUpResult.value;
5268
- else if (isBuiltinFunction(lookUpResult))
5269
- return lookUpResult;
5270
5279
  throw new UndefinedSymbolError(node[1], node[2]);
5271
5280
  };
5272
5281
  return ContextStackImpl;
@@ -5350,10 +5359,10 @@ var nonFunctionOperators = [
5350
5359
  'cond',
5351
5360
  'def',
5352
5361
  'defined?',
5353
- 'defn',
5362
+ // 'defn',
5354
5363
  'do',
5355
5364
  'doseq',
5356
- 'fn',
5365
+ // 'fn',
5357
5366
  'if',
5358
5367
  'let',
5359
5368
  'loop',
@@ -6198,10 +6207,7 @@ var Parser = /** @class */ (function () {
6198
6207
  if (specialExpressionTypes[operatorName] !== undefined) {
6199
6208
  return withSourceCodeInfo([NodeTypes.SpecialBuiltinSymbol, specialExpressionTypes[operatorName]], token[2]);
6200
6209
  }
6201
- else if (normalExpressionTypes[operatorName] !== undefined) {
6202
- return withSourceCodeInfo([NodeTypes.NormalBuiltinSymbol, normalExpressionTypes[operatorName]], token[2]);
6203
- }
6204
- return withSourceCodeInfo([NodeTypes.UserDefinedSymbol, operatorName], token[2]);
6210
+ return withSourceCodeInfo([NodeTypes.NormalBuiltinSymbol, normalExpressionTypes[operatorName]], token[2]);
6205
6211
  }
6206
6212
  if (operatorName === '->') {
6207
6213
  return this.parseShorthandLamdaFunction();
@@ -6361,9 +6367,9 @@ var Parser = /** @class */ (function () {
6361
6367
  var _b = __read(params, 1), param = _b[0];
6362
6368
  return withSourceCodeInfo([NodeTypes.SpecialExpression, [type, param]], symbol[2]);
6363
6369
  }
6364
- case specialExpressionTypes.fn:
6365
- case specialExpressionTypes.def:
6366
- case specialExpressionTypes.defn:
6370
+ case specialExpressionTypes['0_fn']:
6371
+ case specialExpressionTypes['0_def']:
6372
+ case specialExpressionTypes['0_defn']:
6367
6373
  throw new LitsError("".concat(type, " is not allowed"), symbol[2]);
6368
6374
  /* v8 ignore next 2 */
6369
6375
  default:
@@ -6391,7 +6397,7 @@ var Parser = /** @class */ (function () {
6391
6397
  }
6392
6398
  this.advance();
6393
6399
  var body = this.parseExpression();
6394
- return withSourceCodeInfo([NodeTypes.SpecialExpression, [specialExpressionTypes.fn, [
6400
+ return withSourceCodeInfo([NodeTypes.SpecialExpression, [specialExpressionTypes['0_fn'], [
6395
6401
  functionArguments,
6396
6402
  [body],
6397
6403
  ]]], firstToken[2]);
@@ -6474,7 +6480,7 @@ var Parser = /** @class */ (function () {
6474
6480
  functionArguments.push(withSourceCodeInfo([bindingTargetTypes.symbol, [[NodeTypes.UserDefinedSymbol, "$".concat(i)], undefined]], firstToken[2]));
6475
6481
  }
6476
6482
  }
6477
- var node = withSourceCodeInfo([NodeTypes.SpecialExpression, [specialExpressionTypes.fn, [
6483
+ var node = withSourceCodeInfo([NodeTypes.SpecialExpression, [specialExpressionTypes['0_fn'], [
6478
6484
  functionArguments,
6479
6485
  [exprNode],
6480
6486
  ]]], firstToken[2]);
@@ -6993,7 +6999,7 @@ var Parser = /** @class */ (function () {
6993
6999
  this.advance();
6994
7000
  if (isSymbolToken(this.peek(), 'let')) {
6995
7001
  var letNode = this.parseLet(asSymbolToken(this.peek()));
6996
- return withSourceCodeInfo([NodeTypes.SpecialExpression, [specialExpressionTypes.def, letNode[1][1]]], token[2]);
7002
+ return withSourceCodeInfo([NodeTypes.SpecialExpression, [specialExpressionTypes['0_def'], letNode[1][1]]], token[2]);
6997
7003
  }
6998
7004
  else if (isReservedSymbolToken(this.peek(), 'function')) {
6999
7005
  this.advance();
@@ -7011,7 +7017,7 @@ var Parser = /** @class */ (function () {
7011
7017
  }
7012
7018
  assertReservedSymbolToken(this.peek(), 'end');
7013
7019
  this.advance();
7014
- return withSourceCodeInfo([NodeTypes.SpecialExpression, [specialExpressionTypes.defn, symbol, [
7020
+ return withSourceCodeInfo([NodeTypes.SpecialExpression, [specialExpressionTypes['0_defn'], symbol, [
7015
7021
  functionArguments,
7016
7022
  body,
7017
7023
  ]]], token[2]);
@@ -7021,7 +7027,7 @@ var Parser = /** @class */ (function () {
7021
7027
  }
7022
7028
  };
7023
7029
  Parser.prototype.stringToSymbolNode = function (value, sourceCodeInfo) {
7024
- if (specialExpressionTypes[value] !== undefined) {
7030
+ if (specialExpressionTypes[value] !== undefined && value !== 'fn' && value !== 'def' && value !== 'defn') {
7025
7031
  return withSourceCodeInfo([NodeTypes.SpecialBuiltinSymbol, specialExpressionTypes[value]], sourceCodeInfo);
7026
7032
  }
7027
7033
  if (normalExpressionTypes[value] !== undefined) {
@@ -7636,28 +7642,6 @@ var api = {
7636
7642
  'replace',
7637
7643
  'replace-all',
7638
7644
  ],
7639
- specialExpressions: [
7640
- '&&',
7641
- '||',
7642
- 'def',
7643
- 'let',
7644
- 'fn',
7645
- 'defn',
7646
- 'function',
7647
- 'try',
7648
- 'throw',
7649
- 'if',
7650
- 'unless',
7651
- 'cond',
7652
- 'switch',
7653
- 'do',
7654
- 'recur',
7655
- 'loop',
7656
- 'doseq',
7657
- 'for',
7658
- 'defined?',
7659
- '??',
7660
- ],
7661
7645
  string: [
7662
7646
  'string-repeat',
7663
7647
  'str',
@@ -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;