@zvenigora/ng-eval-core 0.4.0 → 0.5.0

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.
@@ -3825,63 +3825,115 @@ const evaluateMemberExpression = (pattern) => {
3825
3825
  }
3826
3826
  return {};
3827
3827
  };
3828
+ /**
3829
+ * The keys an object pattern has already consumed, in source-key terms.
3830
+ *
3831
+ * A rest element binds *the remainder*, so it needs the names the sibling
3832
+ * properties took - and it needs them as they appear on the **source**, which
3833
+ * is `Property.key`, not the name each one bound. For `{ a: x, ...r }` the key
3834
+ * removed from `r` is `a`; `x` was never a source key and removing it would
3835
+ * take the wrong property when the source happens to carry that name too.
3836
+ */
3837
+ const restOf = (source, taken) => {
3838
+ const object = {};
3839
+ if (!source || typeof source !== 'object') {
3840
+ return object;
3841
+ }
3842
+ const consumed = new Set(taken.map(key => String(key)));
3843
+ for (const key of Object.keys(source)) {
3844
+ if (!consumed.has(key)) {
3845
+ safeSetProperty(object, key, source[key]);
3846
+ }
3847
+ }
3848
+ return object;
3849
+ };
3828
3850
  const evaluateObjectPattern = (pattern, st, callback, arg) => {
3829
3851
  let context = {};
3852
+ const taken = [];
3830
3853
  pattern.properties.map((pattern) => {
3831
3854
  switch (pattern.type) {
3832
3855
  case 'Property':
3833
3856
  {
3834
3857
  let key;
3835
- if (pattern.key.type === 'Identifier') {
3858
+ // `computed` is tested first, and that is the whole of a third
3859
+ // defect. `{ [keyName]: q }` parses as `key` an **Identifier** named
3860
+ // `keyName`, so an `Identifier`-first chain took the name it is
3861
+ // spelled with and read `src.keyName` instead of evaluating it and
3862
+ // reading `src[keyName]`. The literal computed form `{ ["a"]: q }`
3863
+ // hid it the way shorthand hid A11 - a `Literal`'s value *is* its
3864
+ // key, so taking the wrong branch landed on the right answer. See
3865
+ // `docs/backlog.md` A14.
3866
+ if (!pattern.computed && pattern.key.type === 'Identifier') {
3836
3867
  key = pattern.key.name;
3837
3868
  }
3838
- else if (pattern.key.type === 'Literal') {
3869
+ else if (!pattern.computed && pattern.key.type === 'Literal') {
3839
3870
  key = pattern.key.value;
3840
3871
  }
3841
3872
  else {
3873
+ // Walked **before** the source property is read, and deliberately:
3874
+ // a computed key is an expression of the *enclosing* scope, not of
3875
+ // the object being destructured. `let { [keyName]: v } = src`
3876
+ // resolves `keyName` where the declaration is written, exactly as
3877
+ // JavaScript does. This is the only `callback` left in the module,
3878
+ // and nothing pushes the source as a scope any more, so there is no
3879
+ // arrangement in which it could resolve against the source.
3842
3880
  callback(pattern.key, st);
3843
3881
  key = popVisitorResult(pattern, st);
3844
3882
  if (typeof key !== 'string' && typeof key !== 'number')
3845
3883
  throw new Error(`Unsupported property key type: ${pattern.key.type}`);
3846
3884
  }
3847
- // **Where `let { a = 1 } = o` actually lands**, which is not where
3848
- // it was expected to. A default in an object pattern is parsed as
3849
- // `Property.value` of type `AssignmentPattern` - so it is handed to
3850
- // `callback` below and walked as an *expression*, and reaches neither
3851
- // of this module's two `switch` defaults. Rejecting it there would
3852
- // have left the object form of the very example that names the
3853
- // divergence still binding silently; the array form
3854
- // (`let [a = 1] = arr`) and the parameter form (`(a = 1) => a`) do go
3855
- // through `evaluatePatterns`, and are covered there.
3885
+ // **Where `let { a = 1 } = o` actually lands**, which is not where it
3886
+ // was expected to. A default in an object pattern is parsed as
3887
+ // `Property.value` of type `AssignmentPattern`, and this branch is
3888
+ // the only thing that sees it: the recursion below would hand it to
3889
+ // `evaluatePattern`, whose `AssignmentPattern` case is commented out,
3890
+ // so it would reach that function's `default` and be rejected with
3891
+ // the same message one frame further down. Kept here so the rejection
3892
+ // is attributable to the object form, and because it predates the
3893
+ // recursion - before it, nothing reached either `switch` at all and
3894
+ // the form bound silently. The array form (`let [a = 1] = arr`) and
3895
+ // the parameter form (`(a = 1) => a`) go through `evaluatePatterns`
3896
+ // and are covered there.
3856
3897
  if (pattern.value.type === 'AssignmentPattern') {
3857
3898
  unsupportedBindingTarget(pattern.value.type);
3858
3899
  }
3859
- const ctx = arg;
3860
- // `finally`, for the reason given at the other push site in
3861
- // arrow-function-expression.ts: the scope stack is on EvalContext,
3862
- // which outlives this walk, so a scope left behind here shadows a
3863
- // source key on every later evaluation against the same context.
3864
- // The `try` opens after the push, never around it.
3865
- st.context?.push(ctx);
3866
- let value;
3867
- try {
3868
- callback(pattern.value, st);
3869
- value = popVisitorResult(pattern, st);
3870
- }
3871
- finally {
3872
- st.context?.pop();
3900
+ taken.push(key);
3901
+ // Guarded on the way *in*, which is where the blocklist has to sit
3902
+ // now that the value is a binding target rather than an expression.
3903
+ // `let { __proto__: p } = o` and `let { constructor: { x } } = o`
3904
+ // both stop here, and the nested form is why the read is guarded and
3905
+ // not only the binding name below: a plain read would hand `Function`
3906
+ // to the recursion, and `evaluateIdentifier`'s guard would then be
3907
+ // inspecting the *inner* pattern's names, by which point the escape
3908
+ // has happened.
3909
+ //
3910
+ // Tested explicitly rather than left to `safeGetProperty`, which
3911
+ // returns early for a non-object target **before** it checks the key
3912
+ // - so `(({ valueOf: v }) => v)(o)` with `o` unbound would read
3913
+ // `undefined` and bind quietly. The blocklist applies to the source
3914
+ // key whatever the source turns out to be, which is the net the
3915
+ // declaration and arrow-parameter binders already cast.
3916
+ if (isDangerousProperty(key)) {
3917
+ throw new Error(`Access to dangerous property "${String(key)}" is blocked for security reasons`);
3873
3918
  }
3874
- // Guarded for the same reason `evaluateIdentifier` is: the key comes
3875
- // from the *pattern*, so `let { __proto__: p } = o` names `__proto__`
3876
- // here and a raw assignment would set this record's prototype.
3877
- const pair = {};
3878
- safeSetProperty(pair, key, value);
3879
- context = { ...context, ...pair };
3919
+ const value = safeGetProperty(arg, key);
3920
+ // The binding target, which may be a name, another object pattern,
3921
+ // or an array pattern. Recursion is the whole repair: `Property.value`
3922
+ // is a *pattern* and is bound against the source property, where it
3923
+ // used to be walked as an **expression** against the source object -
3924
+ // which resolved the wrong name and bound the wrong one. See
3925
+ // `docs/backlog.md` A11.
3926
+ const bound = evaluatePattern(pattern.value, st, callback, value);
3927
+ context = { ...context, ...bound };
3880
3928
  }
3881
3929
  break;
3882
3930
  case 'RestElement':
3883
3931
  {
3884
- const object = evaluateRestElement(pattern, st, callback, arg);
3932
+ // The remainder, not the whole source. Acorn requires the rest
3933
+ // element last in an object pattern, so `taken` is complete here.
3934
+ // Binding the whole argument left an already-destructured key on the
3935
+ // rest record - `docs/backlog.md` A13.
3936
+ const object = evaluateRestElement(pattern, st, callback, restOf(arg, taken));
3885
3937
  context = { ...context, ...object };
3886
3938
  }
3887
3939
  break;