@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
|
-
|
|
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
|
-
//
|
|
3849
|
-
// `Property.value` of type `AssignmentPattern
|
|
3850
|
-
//
|
|
3851
|
-
//
|
|
3852
|
-
//
|
|
3853
|
-
//
|
|
3854
|
-
//
|
|
3855
|
-
//
|
|
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
|
-
|
|
3860
|
-
//
|
|
3861
|
-
//
|
|
3862
|
-
//
|
|
3863
|
-
//
|
|
3864
|
-
//
|
|
3865
|
-
|
|
3866
|
-
|
|
3867
|
-
|
|
3868
|
-
|
|
3869
|
-
|
|
3870
|
-
|
|
3871
|
-
|
|
3872
|
-
|
|
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
|
-
|
|
3875
|
-
//
|
|
3876
|
-
//
|
|
3877
|
-
|
|
3878
|
-
|
|
3879
|
-
|
|
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
|
-
|
|
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;
|