Skip to content

Commit

Permalink
Add getObjectIdentifiers to expressionParser
Browse files Browse the repository at this point in the history
  • Loading branch information
edi9999 committed Feb 16, 2024
1 parent 753b53f commit 5fa030e
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 8 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
### 3.44.1
### 3.45.1

Add getObjectIdentifiers to expressionParser, which can be used like this :

```js
const expressionParser = require("docxtemplater/expressions.js");
expressionParser("a.b.c").getObjectIdentifiers();
// returns { a: { b: { c: {} } } }
```

### 3.45.0

Bugfix for proofstate module : Following error was thrown when using this module :

Expand Down
39 changes: 39 additions & 0 deletions es6/expressions-ie11.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,42 @@ function uniq(arr) {
return result;
}

function getObjectIdentifiers(x, scope = {}) {
if (x.expression) {
getObjectIdentifiers(x.expression, scope);
return scope;
}
if (x.body) {
x.body.forEach((y) => getObjectIdentifiers(y, scope));
return scope;
}
if (x.type === "CallExpression") {
getObjectIdentifiers(x.callee, scope);
if (x.arguments) {
x.arguments.forEach((y) => getObjectIdentifiers(y, scope));
}
}
if (x.ast) {
return getObjectIdentifiers(x.ast);
}

if (x.left) {
getObjectIdentifiers(x.left, scope);
getObjectIdentifiers(x.right, scope);
}

if (x.type === "Identifier") {
const subscope = scope[x.name] || {};
scope[x.name] = subscope;
return subscope;
}
if (x.type === "MemberExpression") {
const subscope = getObjectIdentifiers(x.object, scope);
return getObjectIdentifiers(x.property, subscope);
}
return scope;
}

function getIdentifiers(x) {
if (x.expression) {
return getIdentifiers(x.expression);
Expand Down Expand Up @@ -98,6 +134,9 @@ function configuredParser(config = {}) {
getIdentifiers() {
return uniq(getIdentifiers(expr));
},
getObjectIdentifiers() {
return getObjectIdentifiers(expr);
},
get(scope, context) {
let obj = {};
const scopeList = context.scopeList;
Expand Down
39 changes: 39 additions & 0 deletions es6/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,42 @@ function uniq(arr) {
return result;
}

function getObjectIdentifiers(x, scope = {}) {
if (x.expression) {
getObjectIdentifiers(x.expression, scope);
return scope;
}
if (x.body) {
x.body.forEach((y) => getObjectIdentifiers(y, scope));
return scope;
}
if (x.type === "CallExpression") {
getObjectIdentifiers(x.callee, scope);
if (x.arguments) {
x.arguments.forEach((y) => getObjectIdentifiers(y, scope));
}
}
if (x.ast) {
return getObjectIdentifiers(x.ast);
}

if (x.left) {
getObjectIdentifiers(x.left, scope);
getObjectIdentifiers(x.right, scope);
}

if (x.type === "Identifier") {
const subscope = scope[x.name] || {};
scope[x.name] = subscope;
return subscope;
}
if (x.type === "MemberExpression") {
const subscope = getObjectIdentifiers(x.object, scope);
return getObjectIdentifiers(x.property, subscope);
}
return scope;
}

function getIdentifiers(x) {
if (x.expression) {
return getIdentifiers(x.expression);
Expand Down Expand Up @@ -99,6 +135,9 @@ function configuredParser(config = {}) {
getIdentifiers() {
return uniq(getIdentifiers(expr));
},
getObjectIdentifiers() {
return getObjectIdentifiers(expr);
},
get(scope, context) {
const scopeList = context.scopeList;
if (tag.trim() === "this") {
Expand Down
52 changes: 45 additions & 7 deletions es6/tests/unit/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,28 @@ const { expect } = require("../utils.js");

describe("Angular parser", function () {
it("should work", function () {
const result = angularParser("x+x", {
tag: {
value: "x+x",
},
scopePath: [],
}).get({ x: 1 }, { scopePathItem: [] });
expect(result).to.equal(2);
expect(
angularParser("x+x", {
tag: {
value: "x+x",
},
scopePath: [],
}).get({ x: 1 }, { scopePathItem: [] })
).to.equal(2);

expect(
angularParser("x(y)", {
scopePath: [],
}).get(
{
x(y) {
return y * 2;
},
y: 3,
},
{ scopePathItem: [] }
)
).to.equal(6);
});

it("should work with ie 11", function () {
Expand All @@ -23,8 +38,31 @@ describe("Angular parser", function () {
expect(result).to.equal(2);
});

it("should be able to get object identifiers", function () {
expect(
angularParser("(x.y.z + x.m) / a").getObjectIdentifiers()
).to.deep.equal({ a: {}, x: { m: {}, y: { z: {} } } });

expect(angularParser("x(a.b.c)").getObjectIdentifiers()).to.deep.equal({
x: {},
a: { b: { c: {} } },
});
});

it("should be able to get object identifiers ie11", function () {
expect(
angularParserIE11("(x.y.z + x.m) / a").getObjectIdentifiers()
).to.deep.equal({ a: {}, x: { m: {}, y: { z: {} } } });

expect(angularParserIE11("x(a.b.c)").getObjectIdentifiers()).to.deep.equal({
x: {},
a: { b: { c: {} } },
});
});

it("should be able to getIdentifiers", function () {
angularParser.filters.getimg = () => 0;

expect(
angularParser("x+x", {
scopePath: [],
Expand Down

0 comments on commit 5fa030e

Please sign in to comment.