@dronedeploy/rocos-js-sdk 3.0.10 → 3.0.12

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 (123) hide show
  1. package/cjs/api/StreamRegister.d.ts +3 -3
  2. package/cjs/api/StreamRegister.js +6 -6
  3. package/cjs/api/streams/telemetry/TelemetryStreamAbstract.js +8 -7
  4. package/cjs/helpers/kscript/Context.d.ts +14 -8
  5. package/cjs/helpers/kscript/Context.js +59 -22
  6. package/cjs/helpers/kscript/kscript.d.ts +7 -2
  7. package/cjs/helpers/kscript/kscript.js +24 -14
  8. package/cjs/helpers/kscript/nodes/Identifier.d.ts +1 -0
  9. package/cjs/helpers/kscript/nodes/Identifier.js +2 -1
  10. package/cjs/helpers/kscript/nodes/Literal.d.ts +1 -0
  11. package/cjs/helpers/kscript/nodes/Literal.js +4 -1
  12. package/cjs/helpers/kscript/nodes/Node.d.ts +6 -1
  13. package/cjs/helpers/kscript/nodes/Node.js +0 -1
  14. package/cjs/helpers/kscript/nodes/Program.d.ts +1 -0
  15. package/cjs/helpers/kscript/nodes/Program.js +13 -4
  16. package/cjs/helpers/kscript/nodes/TemplateLiteral.d.ts +1 -0
  17. package/cjs/helpers/kscript/nodes/TemplateLiteral.js +8 -0
  18. package/cjs/helpers/kscript/nodes/VariableDeclaration.d.ts +8 -0
  19. package/cjs/helpers/kscript/nodes/VariableDeclaration.js +43 -0
  20. package/cjs/helpers/kscript/nodes/expressions/ArrayExpression.d.ts +1 -0
  21. package/cjs/helpers/kscript/nodes/expressions/ArrayExpression.js +11 -0
  22. package/cjs/helpers/kscript/nodes/expressions/ArrowFunctionExpression.d.ts +1 -0
  23. package/cjs/helpers/kscript/nodes/expressions/ArrowFunctionExpression.js +19 -5
  24. package/cjs/helpers/kscript/nodes/expressions/AssignmentExpression.d.ts +8 -0
  25. package/cjs/helpers/kscript/nodes/expressions/AssignmentExpression.js +33 -0
  26. package/cjs/helpers/kscript/nodes/expressions/BinaryExpression.d.ts +8 -0
  27. package/cjs/helpers/kscript/nodes/expressions/BinaryExpression.js +55 -22
  28. package/cjs/helpers/kscript/nodes/expressions/CallExpression.d.ts +1 -0
  29. package/cjs/helpers/kscript/nodes/expressions/CallExpression.js +11 -1
  30. package/cjs/helpers/kscript/nodes/expressions/ChainExpression.d.ts +1 -0
  31. package/cjs/helpers/kscript/nodes/expressions/ChainExpression.js +1 -0
  32. package/cjs/helpers/kscript/nodes/expressions/ConditionalExpression.d.ts +1 -0
  33. package/cjs/helpers/kscript/nodes/expressions/ConditionalExpression.js +10 -0
  34. package/cjs/helpers/kscript/nodes/expressions/LogicalExpression.d.ts +2 -0
  35. package/cjs/helpers/kscript/nodes/expressions/LogicalExpression.js +11 -0
  36. package/cjs/helpers/kscript/nodes/expressions/MemberExpression.d.ts +1 -0
  37. package/cjs/helpers/kscript/nodes/expressions/MemberExpression.js +7 -0
  38. package/cjs/helpers/kscript/nodes/expressions/NewExpression.d.ts +9 -0
  39. package/cjs/helpers/kscript/nodes/expressions/NewExpression.js +44 -0
  40. package/cjs/helpers/kscript/nodes/expressions/ObjectExpression.d.ts +1 -0
  41. package/cjs/helpers/kscript/nodes/expressions/ObjectExpression.js +29 -0
  42. package/cjs/helpers/kscript/nodes/expressions/UnaryExpression.d.ts +2 -0
  43. package/cjs/helpers/kscript/nodes/expressions/UnaryExpression.js +9 -0
  44. package/cjs/helpers/kscript/nodes/expressions/index.d.ts +2 -1
  45. package/cjs/helpers/kscript/nodes/expressions/index.js +5 -3
  46. package/cjs/helpers/kscript/nodes/nodeTypes.d.ts +9 -1
  47. package/cjs/helpers/kscript/nodes/nodeTypes.js +9 -1
  48. package/cjs/helpers/kscript/nodes/statements/BlockStatement.d.ts +8 -0
  49. package/cjs/helpers/kscript/nodes/statements/BlockStatement.js +29 -0
  50. package/cjs/helpers/kscript/nodes/{expressions → statements}/ExpressionStatement.d.ts +1 -0
  51. package/cjs/helpers/kscript/nodes/{expressions → statements}/ExpressionStatement.js +8 -1
  52. package/cjs/helpers/kscript/nodes/statements/ReturnStatement.d.ts +12 -0
  53. package/cjs/helpers/kscript/nodes/statements/ReturnStatement.js +33 -0
  54. package/cjs/helpers/kscript/nodes/statements/SwitchStatement.d.ts +9 -0
  55. package/cjs/helpers/kscript/nodes/statements/SwitchStatement.js +52 -0
  56. package/cjs/helpers/kscript/nodes/statements/index.d.ts +4 -0
  57. package/cjs/helpers/kscript/nodes/statements/index.js +14 -0
  58. package/cjs/models/asset-storage/SyncIntegrations.d.ts +3 -3
  59. package/cjs/services/AssetStorageService.d.ts +3 -3
  60. package/cjs/services/BaseStreamService.js +1 -1
  61. package/cjs/services/WebRTCSignallingService.js +1 -2
  62. package/esm/api/StreamRegister.d.ts +3 -3
  63. package/esm/api/StreamRegister.js +6 -6
  64. package/esm/api/streams/telemetry/TelemetryStreamAbstract.js +8 -7
  65. package/esm/helpers/kscript/Context.d.ts +14 -8
  66. package/esm/helpers/kscript/Context.js +59 -22
  67. package/esm/helpers/kscript/kscript.d.ts +7 -2
  68. package/esm/helpers/kscript/kscript.js +22 -13
  69. package/esm/helpers/kscript/nodes/Identifier.d.ts +1 -0
  70. package/esm/helpers/kscript/nodes/Identifier.js +2 -1
  71. package/esm/helpers/kscript/nodes/Literal.d.ts +1 -0
  72. package/esm/helpers/kscript/nodes/Literal.js +4 -1
  73. package/esm/helpers/kscript/nodes/Node.d.ts +6 -1
  74. package/esm/helpers/kscript/nodes/Node.js +0 -1
  75. package/esm/helpers/kscript/nodes/Program.d.ts +1 -0
  76. package/esm/helpers/kscript/nodes/Program.js +13 -4
  77. package/esm/helpers/kscript/nodes/TemplateLiteral.d.ts +1 -0
  78. package/esm/helpers/kscript/nodes/TemplateLiteral.js +8 -0
  79. package/esm/helpers/kscript/nodes/VariableDeclaration.d.ts +8 -0
  80. package/esm/helpers/kscript/nodes/VariableDeclaration.js +37 -0
  81. package/esm/helpers/kscript/nodes/expressions/ArrayExpression.d.ts +1 -0
  82. package/esm/helpers/kscript/nodes/expressions/ArrayExpression.js +11 -0
  83. package/esm/helpers/kscript/nodes/expressions/ArrowFunctionExpression.d.ts +1 -0
  84. package/esm/helpers/kscript/nodes/expressions/ArrowFunctionExpression.js +19 -5
  85. package/esm/helpers/kscript/nodes/expressions/AssignmentExpression.d.ts +8 -0
  86. package/esm/helpers/kscript/nodes/expressions/AssignmentExpression.js +27 -0
  87. package/esm/helpers/kscript/nodes/expressions/BinaryExpression.d.ts +8 -0
  88. package/esm/helpers/kscript/nodes/expressions/BinaryExpression.js +57 -23
  89. package/esm/helpers/kscript/nodes/expressions/CallExpression.d.ts +1 -0
  90. package/esm/helpers/kscript/nodes/expressions/CallExpression.js +11 -1
  91. package/esm/helpers/kscript/nodes/expressions/ChainExpression.d.ts +1 -0
  92. package/esm/helpers/kscript/nodes/expressions/ChainExpression.js +1 -0
  93. package/esm/helpers/kscript/nodes/expressions/ConditionalExpression.d.ts +1 -0
  94. package/esm/helpers/kscript/nodes/expressions/ConditionalExpression.js +10 -0
  95. package/esm/helpers/kscript/nodes/expressions/LogicalExpression.d.ts +2 -0
  96. package/esm/helpers/kscript/nodes/expressions/LogicalExpression.js +13 -1
  97. package/esm/helpers/kscript/nodes/expressions/MemberExpression.d.ts +1 -0
  98. package/esm/helpers/kscript/nodes/expressions/MemberExpression.js +7 -0
  99. package/esm/helpers/kscript/nodes/expressions/NewExpression.d.ts +9 -0
  100. package/esm/helpers/kscript/nodes/expressions/NewExpression.js +38 -0
  101. package/esm/helpers/kscript/nodes/expressions/ObjectExpression.d.ts +1 -0
  102. package/esm/helpers/kscript/nodes/expressions/ObjectExpression.js +29 -0
  103. package/esm/helpers/kscript/nodes/expressions/UnaryExpression.d.ts +2 -0
  104. package/esm/helpers/kscript/nodes/expressions/UnaryExpression.js +11 -1
  105. package/esm/helpers/kscript/nodes/expressions/index.d.ts +2 -1
  106. package/esm/helpers/kscript/nodes/expressions/index.js +2 -1
  107. package/esm/helpers/kscript/nodes/nodeTypes.d.ts +9 -1
  108. package/esm/helpers/kscript/nodes/nodeTypes.js +9 -1
  109. package/esm/helpers/kscript/nodes/statements/BlockStatement.d.ts +8 -0
  110. package/esm/helpers/kscript/nodes/statements/BlockStatement.js +23 -0
  111. package/esm/helpers/kscript/nodes/{expressions → statements}/ExpressionStatement.d.ts +1 -0
  112. package/esm/helpers/kscript/nodes/{expressions → statements}/ExpressionStatement.js +8 -1
  113. package/esm/helpers/kscript/nodes/statements/ReturnStatement.d.ts +12 -0
  114. package/esm/helpers/kscript/nodes/statements/ReturnStatement.js +25 -0
  115. package/esm/helpers/kscript/nodes/statements/SwitchStatement.d.ts +9 -0
  116. package/esm/helpers/kscript/nodes/statements/SwitchStatement.js +46 -0
  117. package/esm/helpers/kscript/nodes/statements/index.d.ts +4 -0
  118. package/esm/helpers/kscript/nodes/statements/index.js +4 -0
  119. package/esm/models/asset-storage/SyncIntegrations.d.ts +3 -3
  120. package/esm/services/AssetStorageService.d.ts +3 -3
  121. package/esm/services/BaseStreamService.js +1 -1
  122. package/esm/services/WebRTCSignallingService.js +1 -2
  123. package/package.json +1 -1
@@ -0,0 +1,27 @@
1
+ import Node from '../Node';
2
+ import { construct } from '../../utils';
3
+ export default class AssignmentExpressionNode extends Node {
4
+ constructor(node, context) {
5
+ super(node, context, 'AssignmentExpression');
6
+ }
7
+ validate(validateChildren = false) {
8
+ if (this.node.operator !== '=') {
9
+ throw new Error('Only = operator is supported');
10
+ }
11
+ if (this.node.left.type !== 'Identifier') {
12
+ throw new Error('Left side of assignment must be an identifier');
13
+ }
14
+ if (!validateChildren) {
15
+ return;
16
+ }
17
+ construct(this.node.left, this.context).validate(true);
18
+ construct(this.node.right, this.context).validate(true);
19
+ }
20
+ run() {
21
+ this.validate();
22
+ const identifier = this.node.left.name;
23
+ const right = construct(this.node.right, this.context).run();
24
+ this.context.set(identifier, right);
25
+ return right;
26
+ }
27
+ }
@@ -6,9 +6,17 @@ import Node from '../Node';
6
6
  * e.g. `1 + 2`, `a === b`, `c < d`
7
7
  */
8
8
  export default class BinaryExpressionNode extends Node<BinaryExpression> {
9
+ private static readonly EQUALITY_OPERATORS;
10
+ private static readonly COMPARISON_OPERATORS;
11
+ private static readonly ARITHMETIC_OPERATORS;
12
+ private static readonly BITWISE_OPERATORS;
13
+ private static readonly IN_OPERATOR;
14
+ private static readonly SUPPORTED_OPERATORS;
9
15
  constructor(node: AnyNode, context: Context);
16
+ validate(validateChildren?: boolean): void;
10
17
  run(): unknown;
11
18
  private operate;
19
+ private bitwise;
12
20
  private arithmetic;
13
21
  private comparison;
14
22
  private equality;
@@ -4,38 +4,52 @@ import { construct } from '../../utils';
4
4
  *
5
5
  * e.g. `1 + 2`, `a === b`, `c < d`
6
6
  */
7
- export default class BinaryExpressionNode extends Node {
7
+ class BinaryExpressionNode extends Node {
8
8
  constructor(node, context) {
9
9
  super(node, context, 'BinaryExpression');
10
10
  }
11
+ validate(validateChildren = false) {
12
+ if (!BinaryExpressionNode.SUPPORTED_OPERATORS.has(this.node.operator)) {
13
+ throw new Error(`Unsupported operator: ${this.node.operator}`);
14
+ }
15
+ if (!validateChildren) {
16
+ return;
17
+ }
18
+ construct(this.node.left, this.context).validate(true);
19
+ construct(this.node.right, this.context).validate(true);
20
+ }
11
21
  run() {
12
22
  const left = construct(this.node.left, this.context).run();
13
23
  const right = construct(this.node.right, this.context).run();
14
24
  return this.operate(left, right);
15
25
  }
16
26
  operate(left, right) {
17
- switch (this.node.operator) {
18
- case '==':
19
- case '!=':
20
- case '===':
21
- case '!==':
22
- return this.equality(this.node.operator, left, right);
23
- case '<':
24
- case '<=':
25
- case '>':
26
- case '>=':
27
- return this.comparison(this.node.operator, left, right);
28
- case 'in':
29
- return this.checkIn(left, right);
30
- case '**':
31
- case '+':
32
- case '-':
33
- case '*':
34
- case '/':
35
- case '%':
36
- return this.arithmetic(this.node.operator, left, right);
37
- default:
38
- throw new Error(`Unsupported operator: ${this.node.operator}`);
27
+ if (BinaryExpressionNode.EQUALITY_OPERATORS.has(this.node.operator)) {
28
+ return this.equality(this.node.operator, left, right);
29
+ }
30
+ if (BinaryExpressionNode.COMPARISON_OPERATORS.has(this.node.operator)) {
31
+ return this.comparison(this.node.operator, left, right);
32
+ }
33
+ if (BinaryExpressionNode.ARITHMETIC_OPERATORS.has(this.node.operator)) {
34
+ return this.arithmetic(this.node.operator, left, right);
35
+ }
36
+ if (BinaryExpressionNode.BITWISE_OPERATORS.has(this.node.operator)) {
37
+ return this.bitwise(this.node.operator, left, right);
38
+ }
39
+ if (this.node.operator === BinaryExpressionNode.IN_OPERATOR) {
40
+ return this.checkIn(left, right);
41
+ }
42
+ throw new Error(`Unsupported operator: ${this.node.operator}`);
43
+ }
44
+ bitwise(operator, left, right) {
45
+ if (typeof left !== 'number' || typeof right !== 'number') {
46
+ throw new Error('Bitwise operators are only supported for numbers');
47
+ }
48
+ switch (operator) {
49
+ case '<<':
50
+ return left << right;
51
+ case '>>':
52
+ return left >> right;
39
53
  }
40
54
  }
41
55
  arithmetic(operator, left, right) {
@@ -96,3 +110,23 @@ export default class BinaryExpressionNode extends Node {
96
110
  return right.includes(left);
97
111
  }
98
112
  }
113
+ BinaryExpressionNode.EQUALITY_OPERATORS = new Set(['==', '!=', '===', '!==']);
114
+ BinaryExpressionNode.COMPARISON_OPERATORS = new Set(['<', '<=', '>', '>=']);
115
+ BinaryExpressionNode.ARITHMETIC_OPERATORS = new Set([
116
+ '+',
117
+ '-',
118
+ '*',
119
+ '/',
120
+ '%',
121
+ '**',
122
+ ]);
123
+ BinaryExpressionNode.BITWISE_OPERATORS = new Set(['<<', '>>']);
124
+ BinaryExpressionNode.IN_OPERATOR = 'in';
125
+ BinaryExpressionNode.SUPPORTED_OPERATORS = new Set([
126
+ ...BinaryExpressionNode.EQUALITY_OPERATORS,
127
+ ...BinaryExpressionNode.COMPARISON_OPERATORS,
128
+ ...BinaryExpressionNode.ARITHMETIC_OPERATORS,
129
+ ...BinaryExpressionNode.BITWISE_OPERATORS,
130
+ BinaryExpressionNode.IN_OPERATOR,
131
+ ]);
132
+ export default BinaryExpressionNode;
@@ -3,6 +3,7 @@ import Context from '../../Context';
3
3
  import Node from '../Node';
4
4
  export default class CallExpressionNode extends Node<CallExpression> {
5
5
  constructor(node: AnyNode, context: Context);
6
+ validate(validateChildren?: boolean): void;
6
7
  run(): unknown;
7
8
  private spreadArgs;
8
9
  }
@@ -4,9 +4,19 @@ export default class CallExpressionNode extends Node {
4
4
  constructor(node, context) {
5
5
  super(node, context, 'CallExpression');
6
6
  }
7
- run() {
7
+ validate(validateChildren = false) {
8
8
  if (!this.context.isFunctionCallAllowed)
9
9
  throw new Error('Function calls are not allowed');
10
+ if (!validateChildren) {
11
+ return;
12
+ }
13
+ construct(this.node.callee, this.context).validate(true);
14
+ for (const arg of this.node.arguments) {
15
+ construct(arg, this.context).validate(true);
16
+ }
17
+ }
18
+ run() {
19
+ this.validate();
10
20
  const callee = construct(this.node.callee, this.context).run();
11
21
  let thisArg = {};
12
22
  if (this.node.callee.type === 'MemberExpression') {
@@ -12,5 +12,6 @@ import Node from '../Node';
12
12
  */
13
13
  export default class ExpressionStatementNode extends Node<ChainExpression> {
14
14
  constructor(node: AnyNode, context: Context);
15
+ validate(): void;
15
16
  run(): unknown;
16
17
  }
@@ -13,6 +13,7 @@ export default class ExpressionStatementNode extends Node {
13
13
  constructor(node, context) {
14
14
  super(node, context, 'ChainExpression');
15
15
  }
16
+ validate() { }
16
17
  run() {
17
18
  return construct(this.node.expression, this.context).run();
18
19
  }
@@ -10,5 +10,6 @@ import Node from '../Node';
10
10
  */
11
11
  export default class ConditionalExpressionNode extends Node<IfStatement | ConditionalExpression> {
12
12
  constructor(node: AnyNode, context: Context);
13
+ validate(validateChildren: boolean): void;
13
14
  run(): unknown;
14
15
  }
@@ -11,6 +11,16 @@ export default class ConditionalExpressionNode extends Node {
11
11
  constructor(node, context) {
12
12
  super(node, context, ['ConditionalExpression', 'IfStatement']);
13
13
  }
14
+ validate(validateChildren) {
15
+ if (!validateChildren) {
16
+ return;
17
+ }
18
+ construct(this.node.test, this.context).validate(true);
19
+ construct(this.node.consequent, this.context).validate(true);
20
+ if (this.node.alternate !== null && this.node.alternate !== undefined) {
21
+ construct(this.node.alternate, this.context).validate(true);
22
+ }
23
+ }
14
24
  run() {
15
25
  const test = construct(this.node.test, this.context).run();
16
26
  if (test) {
@@ -2,7 +2,9 @@ import { AnyNode, LogicalExpression } from 'acorn';
2
2
  import Context from '../../Context';
3
3
  import Node from '../Node';
4
4
  export default class BinaryExpressionNode extends Node<LogicalExpression> {
5
+ private static readonly SUPPORTED_OPERATORS;
5
6
  constructor(node: AnyNode, context: Context);
7
+ validate(validateChildren: boolean): void;
6
8
  run(): unknown;
7
9
  /** Try and skip evaluating the right operand */
8
10
  private shortCircuit;
@@ -1,9 +1,19 @@
1
1
  import Node from '../Node';
2
2
  import { construct } from '../../utils';
3
- export default class BinaryExpressionNode extends Node {
3
+ class BinaryExpressionNode extends Node {
4
4
  constructor(node, context) {
5
5
  super(node, context, 'LogicalExpression');
6
6
  }
7
+ validate(validateChildren) {
8
+ if (!BinaryExpressionNode.SUPPORTED_OPERATORS.has(this.node.operator)) {
9
+ throw new Error(`Unsupported operator: ${this.node.operator}`);
10
+ }
11
+ if (!validateChildren) {
12
+ return;
13
+ }
14
+ construct(this.node.left, this.context).validate(true);
15
+ construct(this.node.right, this.context).validate(true);
16
+ }
7
17
  run() {
8
18
  const left = construct(this.node.left, this.context).run();
9
19
  const shortCircuit = this.shortCircuit(left);
@@ -39,3 +49,5 @@ export default class BinaryExpressionNode extends Node {
39
49
  }
40
50
  }
41
51
  }
52
+ BinaryExpressionNode.SUPPORTED_OPERATORS = new Set(['||', '&&', '??']);
53
+ export default BinaryExpressionNode;
@@ -22,6 +22,7 @@ import Node from '../Node';
22
22
  */
23
23
  export default class MemberExpressionNode extends Node<MemberExpression> {
24
24
  constructor(node: AnyNode, context: Context);
25
+ validate(validateChildren?: boolean): void;
25
26
  run(): unknown;
26
27
  /** Returns the name of the property being accessed.
27
28
  *
@@ -23,6 +23,13 @@ export default class MemberExpressionNode extends Node {
23
23
  constructor(node, context) {
24
24
  super(node, context, 'MemberExpression');
25
25
  }
26
+ validate(validateChildren = false) {
27
+ if (!validateChildren) {
28
+ return;
29
+ }
30
+ construct(this.node.object, this.context).validate(true);
31
+ construct(this.node.property, this.context).validate(true);
32
+ }
26
33
  run() {
27
34
  const object = construct(this.node.object, this.context).run();
28
35
  if (object === null || object === undefined) {
@@ -0,0 +1,9 @@
1
+ import { AnyNode, NewExpression } from 'acorn';
2
+ import Context from '../../Context';
3
+ import Node from '../Node';
4
+ export default class NewExpressionNode extends Node<NewExpression> {
5
+ constructor(node: AnyNode, context: Context);
6
+ validate(validateChildren?: boolean): void;
7
+ run(): unknown;
8
+ private spreadArgs;
9
+ }
@@ -0,0 +1,38 @@
1
+ import Node from '../Node';
2
+ import { construct } from '../../utils';
3
+ export default class NewExpressionNode extends Node {
4
+ constructor(node, context) {
5
+ super(node, context, 'NewExpression');
6
+ }
7
+ validate(validateChildren = false) {
8
+ if (!this.context.isFunctionCallAllowed)
9
+ throw new Error('Constructors are not allowed');
10
+ if (!validateChildren) {
11
+ return;
12
+ }
13
+ construct(this.node.callee, this.context).validate(true);
14
+ for (const arg of this.node.arguments) {
15
+ construct(arg, this.context).validate(true);
16
+ }
17
+ }
18
+ run() {
19
+ this.validate();
20
+ const callee = construct(this.node.callee, this.context).run();
21
+ if (typeof callee !== 'function') {
22
+ throw new Error('callee is not a function');
23
+ }
24
+ const args = this.spreadArgs(this.node.arguments);
25
+ return new (Function.prototype.bind.apply(callee, [null, ...args]))();
26
+ }
27
+ spreadArgs(args) {
28
+ return args.reduce((acc, arg) => {
29
+ if (arg.type === 'SpreadElement') {
30
+ acc.push(...construct(arg.argument, this.context).run());
31
+ }
32
+ else {
33
+ acc.push(construct(arg, this.context).run());
34
+ }
35
+ return acc;
36
+ }, []);
37
+ }
38
+ }
@@ -3,6 +3,7 @@ import type Context from '../../Context';
3
3
  import Node from '../Node';
4
4
  export default class ObjectExpressionNode extends Node<ObjectExpression> {
5
5
  constructor(node: AnyNode, context: Context);
6
+ validate(validateChildren: boolean): void;
6
7
  run(): unknown;
7
8
  private addProperty;
8
9
  private spreadProperty;
@@ -4,6 +4,35 @@ export default class ObjectExpressionNode extends Node {
4
4
  constructor(node, context) {
5
5
  super(node, context, 'ObjectExpression');
6
6
  }
7
+ validate(validateChildren) {
8
+ for (const property of this.node.properties) {
9
+ // SpreadElements do not need to be validated - they only have a single argument
10
+ if (property.type === 'SpreadElement') {
11
+ continue;
12
+ }
13
+ if (property.kind !== 'init') {
14
+ // do not support getters or setters
15
+ throw new Error('Only init properties are supported');
16
+ }
17
+ if (!property.computed) {
18
+ // ensure that the key is valid
19
+ this.getStaticKey(property.key);
20
+ }
21
+ }
22
+ if (!validateChildren) {
23
+ return;
24
+ }
25
+ for (const property of this.node.properties) {
26
+ if (property.type === 'SpreadElement') {
27
+ construct(property.argument, this.context).validate(true);
28
+ continue;
29
+ }
30
+ if (property.computed) {
31
+ construct(property.key, this.context).validate(true);
32
+ }
33
+ construct(property.value, this.context).validate(true);
34
+ }
35
+ }
7
36
  run() {
8
37
  const object = {};
9
38
  for (const property of this.node.properties) {
@@ -6,7 +6,9 @@ import Node from '../Node';
6
6
  * e.g. `+1`, `-2`, `!true`, `typeof x`
7
7
  */
8
8
  export default class UnaryExpressionNode extends Node<UnaryExpression> {
9
+ private static readonly SUPPORTED_OPERATORS;
9
10
  constructor(node: AnyNode, context: Context);
11
+ validate(validateChildren: boolean): void;
10
12
  run(): unknown;
11
13
  private arithmetic;
12
14
  }
@@ -4,10 +4,18 @@ import { construct } from '../../utils';
4
4
  *
5
5
  * e.g. `+1`, `-2`, `!true`, `typeof x`
6
6
  */
7
- export default class UnaryExpressionNode extends Node {
7
+ class UnaryExpressionNode extends Node {
8
8
  constructor(node, context) {
9
9
  super(node, context, 'UnaryExpression');
10
10
  }
11
+ validate(validateChildren) {
12
+ if (!UnaryExpressionNode.SUPPORTED_OPERATORS.has(this.node.operator)) {
13
+ throw new Error(`Unsupported operator: ${this.node.operator}`);
14
+ }
15
+ if (validateChildren) {
16
+ construct(this.node.argument, this.context).validate(true);
17
+ }
18
+ }
11
19
  run() {
12
20
  const argument = construct(this.node.argument, this.context).run();
13
21
  switch (this.node.operator) {
@@ -36,3 +44,5 @@ export default class UnaryExpressionNode extends Node {
36
44
  }
37
45
  }
38
46
  }
47
+ UnaryExpressionNode.SUPPORTED_OPERATORS = new Set(['+', '-', '!', 'typeof']);
48
+ export default UnaryExpressionNode;
@@ -1,4 +1,3 @@
1
- export { default as ExpressionStatement } from './ExpressionStatement';
2
1
  export { default as ArrayExpression } from './ArrayExpression';
3
2
  export { default as ObjectExpression } from './ObjectExpression';
4
3
  export { default as BinaryExpression } from './BinaryExpression';
@@ -9,3 +8,5 @@ export { default as CallExpression } from './CallExpression';
9
8
  export { default as ArrowFunctionExpression } from './ArrowFunctionExpression';
10
9
  export { default as MemberExpression } from './MemberExpression';
11
10
  export { default as ChainExpression } from './ChainExpression';
11
+ export { default as NewExpression } from './NewExpression';
12
+ export { default as AssignmentExpression } from './AssignmentExpression';
@@ -1,4 +1,3 @@
1
- export { default as ExpressionStatement } from './ExpressionStatement';
2
1
  export { default as ArrayExpression } from './ArrayExpression';
3
2
  export { default as ObjectExpression } from './ObjectExpression';
4
3
  export { default as BinaryExpression } from './BinaryExpression';
@@ -9,3 +8,5 @@ export { default as CallExpression } from './CallExpression';
9
8
  export { default as ArrowFunctionExpression } from './ArrowFunctionExpression';
10
9
  export { default as MemberExpression } from './MemberExpression';
11
10
  export { default as ChainExpression } from './ChainExpression';
11
+ export { default as NewExpression } from './NewExpression';
12
+ export { default as AssignmentExpression } from './AssignmentExpression';
@@ -1,4 +1,5 @@
1
- import { ArrayExpression, ArrowFunctionExpression, BinaryExpression, CallExpression, ChainExpression, ConditionalExpression, ExpressionStatement, LogicalExpression, MemberExpression, ObjectExpression, UnaryExpression } from './expressions';
1
+ import { ArrayExpression, ArrowFunctionExpression, AssignmentExpression, BinaryExpression, CallExpression, ChainExpression, ConditionalExpression, LogicalExpression, MemberExpression, NewExpression, ObjectExpression, UnaryExpression } from './expressions';
2
+ import { BlockStatement, ExpressionStatement, ReturnStatement, SwitchStatement } from './statements';
2
3
  import { AnyNode } from 'acorn';
3
4
  import type Context from '../Context';
4
5
  import Identifier from './Identifier';
@@ -6,6 +7,7 @@ import Literal from './Literal';
6
7
  import Node from './Node';
7
8
  import Program from './Program';
8
9
  import TemplateLiteral from './TemplateLiteral';
10
+ import VariableDeclaration from './VariableDeclaration';
9
11
  type NodeConstructor = {
10
12
  new (node: AnyNode, scope: Context): Node<AnyNode>;
11
13
  };
@@ -27,6 +29,12 @@ declare const nodes: {
27
29
  readonly CallExpression: typeof CallExpression;
28
30
  readonly MemberExpression: typeof MemberExpression;
29
31
  readonly ChainExpression: typeof ChainExpression;
32
+ readonly NewExpression: typeof NewExpression;
33
+ readonly BlockStatement: typeof BlockStatement;
34
+ readonly ReturnStatement: typeof ReturnStatement;
35
+ readonly VariableDeclaration: typeof VariableDeclaration;
36
+ readonly AssignmentExpression: typeof AssignmentExpression;
37
+ readonly SwitchStatement: typeof SwitchStatement;
30
38
  };
31
39
  export type NodeType = keyof typeof nodes;
32
40
  declare const _default: NodeMap;
@@ -1,8 +1,10 @@
1
- import { ArrayExpression, ArrowFunctionExpression, BinaryExpression, CallExpression, ChainExpression, ConditionalExpression, ExpressionStatement, LogicalExpression, MemberExpression, ObjectExpression, UnaryExpression, } from './expressions';
1
+ import { ArrayExpression, ArrowFunctionExpression, AssignmentExpression, BinaryExpression, CallExpression, ChainExpression, ConditionalExpression, LogicalExpression, MemberExpression, NewExpression, ObjectExpression, UnaryExpression, } from './expressions';
2
+ import { BlockStatement, ExpressionStatement, ReturnStatement, SwitchStatement } from './statements';
2
3
  import Identifier from './Identifier';
3
4
  import Literal from './Literal';
4
5
  import Program from './Program';
5
6
  import TemplateLiteral from './TemplateLiteral';
7
+ import VariableDeclaration from './VariableDeclaration';
6
8
  const nodes = {
7
9
  Program,
8
10
  Literal,
@@ -20,5 +22,11 @@ const nodes = {
20
22
  CallExpression,
21
23
  MemberExpression,
22
24
  ChainExpression,
25
+ NewExpression,
26
+ BlockStatement,
27
+ ReturnStatement,
28
+ VariableDeclaration,
29
+ AssignmentExpression,
30
+ SwitchStatement,
23
31
  };
24
32
  export default new Map(Object.entries(nodes));
@@ -0,0 +1,8 @@
1
+ import { AnyNode, BlockStatement } from 'acorn';
2
+ import Context from '../../Context';
3
+ import Node from '../Node';
4
+ export default class BlockStatementNode extends Node<BlockStatement> {
5
+ constructor(node: AnyNode, context: Context);
6
+ validate(validateChildren?: boolean): void;
7
+ run(): unknown;
8
+ }
@@ -0,0 +1,23 @@
1
+ import Node from '../Node';
2
+ import { construct } from '../../utils';
3
+ export default class BlockStatementNode extends Node {
4
+ constructor(node, context) {
5
+ super(node, context.newChild(), 'BlockStatement');
6
+ }
7
+ validate(validateChildren = false) {
8
+ if (!validateChildren) {
9
+ return;
10
+ }
11
+ for (const statement of this.node.body) {
12
+ construct(statement, this.context).validate(true);
13
+ }
14
+ }
15
+ run() {
16
+ this.validate();
17
+ let result;
18
+ for (const statement of this.node.body) {
19
+ result = construct(statement, this.context).run();
20
+ }
21
+ return result;
22
+ }
23
+ }
@@ -3,5 +3,6 @@ import Context from '../../Context';
3
3
  import Node from '../Node';
4
4
  export default class ExpressionStatementNode extends Node<ExpressionStatement> {
5
5
  constructor(node: AnyNode, context: Context);
6
+ validate(validateChildren?: boolean): void;
6
7
  run(): unknown;
7
8
  }
@@ -4,9 +4,16 @@ export default class ExpressionStatementNode extends Node {
4
4
  constructor(node, context) {
5
5
  super(node, context, 'ExpressionStatement');
6
6
  }
7
- run() {
7
+ validate(validateChildren = false) {
8
8
  if (this.node.directive)
9
9
  throw new Error('Directives are not supported');
10
+ if (!validateChildren) {
11
+ return;
12
+ }
13
+ construct(this.node.expression, this.context).validate(true);
14
+ }
15
+ run() {
16
+ this.validate();
10
17
  return construct(this.node.expression, this.context).run();
11
18
  }
12
19
  }
@@ -0,0 +1,12 @@
1
+ import { AnyNode, ReturnStatement } from 'acorn';
2
+ import type Context from '../../Context';
3
+ import Node from '../Node';
4
+ export declare class ReturnStatementError extends Error {
5
+ readonly value: unknown;
6
+ constructor(value: unknown);
7
+ }
8
+ export default class ReturnStatementNode extends Node<ReturnStatement> {
9
+ constructor(node: AnyNode, context: Context);
10
+ validate(validateChildren?: boolean): void;
11
+ run(): unknown;
12
+ }
@@ -0,0 +1,25 @@
1
+ import Node from '../Node';
2
+ import { construct } from '../../utils';
3
+ export class ReturnStatementError extends Error {
4
+ constructor(value) {
5
+ super('Illegal return statement');
6
+ this.value = value;
7
+ }
8
+ }
9
+ export default class ReturnStatementNode extends Node {
10
+ constructor(node, context) {
11
+ super(node, context, 'ReturnStatement');
12
+ }
13
+ validate(validateChildren = false) {
14
+ const arg = this.node.argument;
15
+ if (!validateChildren || arg === undefined || arg === null) {
16
+ return;
17
+ }
18
+ construct(arg, this.context).validate(true);
19
+ }
20
+ run() {
21
+ this.validate();
22
+ const result = this.node.argument ? construct(this.node.argument, this.context).run() : undefined;
23
+ throw new ReturnStatementError(result);
24
+ }
25
+ }
@@ -0,0 +1,9 @@
1
+ import { AnyNode, SwitchStatement } from 'acorn';
2
+ import Context from '../../Context';
3
+ import Node from '../Node';
4
+ export default class SwitchStatementNode extends Node<SwitchStatement> {
5
+ constructor(node: AnyNode, context: Context);
6
+ private test_match;
7
+ validate(validateChildren?: boolean): void;
8
+ run(): unknown;
9
+ }
@@ -0,0 +1,46 @@
1
+ import Node from '../Node';
2
+ import { construct } from '../../utils';
3
+ export default class SwitchStatementNode extends Node {
4
+ constructor(node, context) {
5
+ super(node, context, 'SwitchStatement');
6
+ }
7
+ test_match(test, discriminant) {
8
+ if (!test) {
9
+ return true;
10
+ }
11
+ const testValue = construct(test, this.context).run();
12
+ return testValue === discriminant;
13
+ }
14
+ validate(validateChildren = false) {
15
+ if (!validateChildren) {
16
+ return;
17
+ }
18
+ construct(this.node.discriminant, this.context).validate(true);
19
+ for (const c of this.node.cases) {
20
+ if (c.test) {
21
+ construct(c.test, this.context).validate(true);
22
+ }
23
+ for (const s of c.consequent) {
24
+ construct(s, this.context).validate(true);
25
+ }
26
+ }
27
+ }
28
+ run() {
29
+ this.validate();
30
+ const discriminant = construct(this.node.discriminant, this.context).run();
31
+ let result;
32
+ let matched = false;
33
+ for (const c of this.node.cases) {
34
+ if (!matched && this.test_match(c.test, discriminant)) {
35
+ matched = true;
36
+ }
37
+ if (!matched) {
38
+ continue;
39
+ }
40
+ for (const s of c.consequent) {
41
+ result = construct(s, this.context).run();
42
+ }
43
+ }
44
+ return result;
45
+ }
46
+ }