Skip to content

Commit

Permalink
fix: Ensure all variables are accessible in their local contexts. Closes
Browse files Browse the repository at this point in the history
  • Loading branch information
kofrasa committed Jan 12, 2023
1 parent 1aa0556 commit d2842bc
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"parser": "@typescript-eslint/parser",
"parserOptions": {
"sourceType": "module",
"project": "./tsconfig.eslint.json"
"project": "./tsconfig.lint.json"
},
"extends": [
"eslint:recommended",
Expand Down
3 changes: 2 additions & 1 deletion src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export class ComputeOptions implements Options {
? options.update(
// value can be '0' or 'false'
isNil(options.root) ? root : options.root,
options.local || local
Object.assign({}, options.local, local)
)
: new ComputeOptions(options || initOptions(), root, local);
}
Expand Down Expand Up @@ -482,6 +482,7 @@ export function computeValue(
copts.local?.variables // local vars
);
const prefix = arr[0].slice(2);
console.log(context, copts);
assert(
has(context as RawObject, prefix),
`Use of undefined variable: ${prefix}`
Expand Down
133 changes: 132 additions & 1 deletion test/operators/expression/variable.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "../../../src/init/system";

import { aggregate } from "../../../src";
import { aggregate, find } from "../../../src";

describe("operators/expression/variable", () => {
it("can apply $let operator", () => {
Expand Down Expand Up @@ -31,4 +31,135 @@ describe("operators/expression/variable", () => {
expect(result[0].finalTotal).toEqual(9.450000000000001);
expect(result[1].finalTotal).toEqual(10.25);
});

// See: https://github.com/kofrasa/mingo/issues/302
it("can access all variables within defined scope", () => {
const docs = [
{
_id: "document1",
pattern: [
"string1",
[
"descriptor",
{
format: "longDate",
id: "1",
},
],
"string2",
[
"descriptor",
{
format: "longDate",
id: "2",
},
],
"string3",
[
"descriptor",
{
format: "longDate",
id: "3",
},
],
],
},
{
_id: "document2",
pattern: [
"string1",
[
"descriptor",
{
format: "longDate",
id: "4",
},
],
"string2",
[
"descriptor",
{
format: "longDate",
id: "5",
},
],
"string3",
[
"descriptor",
{
format: "longDate",
id: "6",
},
],
],
},
];

const results = find(docs, {
$expr: {
$gt: [
{
$size: {
$filter: {
input: "$pattern",
as: "p",
cond: {
$and: [
{
$isArray: ["$$p"],
},
{
$let: {
vars: {
e: {
$arrayElemAt: ["$$p", 1],
},
},
in: {
$eq: ["$$e.id", "1"],
},
},
},
],
},
},
},
},
0,
],
},
}).all();

expect(results).toEqual([
{
_id: "document1",
pattern: [
"string1",
[
"descriptor",
{
format: "longDate",
id: "1",
},
],
"string2",
[
"descriptor",
{
format: "longDate",
id: "2",
},
],
"string3",
[
"descriptor",
{
format: "longDate",
id: "3",
},
],
],
},
]);
});
});

0 comments on commit d2842bc

Please sign in to comment.