Skip to content

Commit 54d5f7b

Browse files
getExpression is not an async function
While here, I cleaned up all the jsdoc comments. We don't need to specify types in the comments since we're using TypeScript.
1 parent 0ff9f32 commit 54d5f7b

2 files changed

Lines changed: 52 additions & 48 deletions

File tree

src/types/global.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ declare namespace browser.experiments.nimbus {
155155

156156
function setCollection(collectionId: string): Promise<void>;
157157

158-
function evaluateJEXL(expression: string, context: object): Promise<object>;
158+
function evaluateJEXL(expression: string, context: object): Promise<unknown>;
159159

160160
function getClientContext(): Promise<object>;
161161

src/ui/jexlParser.ts

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ const grammar = {
2121

2222
/**
2323
* Evaluates a JEXL expression within a given context.
24-
* @param {string} expression - The JEXL expression to evaluate.
25-
* @param {object} context - The context in which to evaluate the expression.
26-
* @returns {Promise<string>} - The result of the evaluation as a string.
24+
*
25+
* @param expression The JEXL expression to evaluate.
26+
* @param context The context in which to evaluate the expression.
27+
*
28+
* @returns The result of the evaluation as a string.
2729
*/
2830
export async function evaluateJexl(
2931
expression: string,
@@ -51,14 +53,16 @@ export async function evaluateJexl(
5153

5254
/**
5355
* Evaluates a JEXL expression using the browser's Nimbus API.
54-
* @param {string} expression - The JEXL expression to evaluate.
55-
* @param {object} context - The context in which to evaluate the expression.
56-
* @returns {Promise<object|boolean>} - The result of the evaluation.
56+
*
57+
* @param expression - The JEXL expression to evaluate.
58+
* @param context - The context in which to evaluate the expression.
59+
*
60+
* @returns The result of the evaluation.
5761
*/
5862
async function evaluateExpression(
5963
expression: string,
6064
context: object,
61-
): Promise<object | boolean> {
65+
): Promise<unknown> {
6266
try {
6367
return await browser.experiments.nimbus.evaluateJEXL(expression, context);
6468
} catch (error) {
@@ -69,10 +73,12 @@ async function evaluateExpression(
6973

7074
/**
7175
* Traverses the AST to evaluate sub-expressions and collect parts that evaluate to false.
72-
* @param {ASTNode} ast - The AST node to traverse.
73-
* @param {object} context - The context in which to evaluate the expressions.
74-
* @param {string[]} falseParts - An array to collect expressions that evaluate to false.
75-
* @returns {Promise<unknown>} - The result of the traversal.
76+
*
77+
* @param ast The AST node to traverse.
78+
* @param context The context in which to evaluate the expressions.
79+
* @param falseParts An array to collect expressions that evaluate to false.
80+
*
81+
* @returns The result of the traversal.
7682
*/
7783
async function traverseAst(
7884
ast: ASTNode,
@@ -83,16 +89,16 @@ async function traverseAst(
8389
return true;
8490
}
8591

86-
const subExpr = await getExpression(ast);
92+
const subExpr = getExpression(ast);
8793
const result = await evaluateExpression(subExpr, context);
8894

8995
if (ast.type === "BinaryExpression" || ast.type === "LogicalExpression") {
9096
const leftResult = await evaluateExpression(
91-
await getExpression(ast.left),
97+
getExpression(ast.left),
9298
context,
9399
);
94100
const rightResult = await evaluateExpression(
95-
await getExpression(ast.right),
101+
getExpression(ast.right),
96102
context,
97103
);
98104

@@ -164,8 +170,10 @@ async function traverseAst(
164170

165171
/**
166172
* Retrieves the full identifier string from an AST node.
167-
* @param {Identifier} ast - The AST node representing an identifier.
168-
* @returns {string} - The full identifier string.
173+
*
174+
* @param ast The AST node representing an identifier.
175+
*
176+
* @returns The full identifier string.
169177
*/
170178
function getFullIdentifier(ast: Identifier): string {
171179
if (!ast.from) {
@@ -176,17 +184,19 @@ function getFullIdentifier(ast: Identifier): string {
176184

177185
/**
178186
* Converts an AST node to its corresponding JEXL expression string.
179-
* @param {ASTNode} ast - The AST node to convert.
180-
* @returns {Promise<string>} - The JEXL expression string.
187+
*
188+
* @param ast The AST node to convert.
189+
*
190+
* @returns The JEXL expression string.
181191
*/
182-
async function getExpression(ast: ASTNode): Promise<string> {
192+
function getExpression(ast: ASTNode): string {
183193
if (!ast) {
184194
return "";
185195
}
186196

187197
if (ast.type === "BinaryExpression" || ast.type === "LogicalExpression") {
188-
const leftExpr = await getExpression(ast.left);
189-
const rightExpr = await getExpression(ast.right);
198+
const leftExpr = getExpression(ast.left);
199+
const rightExpr = getExpression(ast.right);
190200
const leftWrapped =
191201
ast.left.type === "Literal" ||
192202
ast.left.type === "Identifier" ||
@@ -201,47 +211,41 @@ async function getExpression(ast: ASTNode): Promise<string> {
201211
: `(${rightExpr})`;
202212
return `${leftWrapped} ${ast.operator} ${rightWrapped}`;
203213
} else if (ast.type === "UnaryExpression") {
204-
const rightExpr = await getExpression(ast.right);
214+
const rightExpr = getExpression(ast.right);
205215
return `${ast.operator}(${rightExpr})`;
206216
} else if (ast.type === "Transform") {
207-
const subjectExpr = await getExpression(ast.subject);
208-
const argsExpr = ast.args
209-
? (await Promise.all(ast.args.map(getExpression))).join(", ")
210-
: "";
217+
const subjectExpr = getExpression(ast.subject);
218+
const argsExpr = ast.args ? ast.args.map(getExpression).join(", ") : "";
211219
return argsExpr.length === 0
212220
? `${subjectExpr}|${ast.name}`
213221
: `${subjectExpr}|${ast.name}(${argsExpr})`;
214222
} else if (ast.type === "FilterExpression") {
215-
const subjectExpr = await getExpression(ast.subject);
216-
const filterExpr = await getExpression(ast.expr);
223+
const subjectExpr = getExpression(ast.subject);
224+
const filterExpr = getExpression(ast.expr);
217225
return `${subjectExpr}[${filterExpr}]`;
218226
} else if (ast.type === "Literal") {
219227
return typeof ast.value === "string" ? `'${ast.value}'` : `${ast.value}`;
220228
} else if (ast.type === "Identifier") {
221229
return getFullIdentifier(ast);
222230
} else if (ast.type === "ArrayLiteral") {
223-
const elementsExpr = (await Promise.all(ast.value.map(getExpression))).join(
224-
", ",
225-
);
231+
const elementsExpr = ast.value.map(getExpression).join(", ");
226232
return `[${elementsExpr}]`;
227233
} else if (ast.type === "ObjectLiteral") {
228234
const entries = Object.entries(ast.value);
229-
const objectExpr = await Promise.all(
230-
entries.map(async ([key, value]) => {
231-
const valueExpr = await getExpression(value);
232-
if (value.type === "ObjectLiteral") {
233-
const nestedEntries = Object.entries(value.value);
234-
const nestedObjectExpr = await Promise.all(
235-
nestedEntries.map(async ([nestedKey, nestedValue]) => {
236-
const nestedValueExpr = await getExpression(nestedValue);
237-
return `${nestedKey}: ${nestedValueExpr}`;
238-
}),
239-
);
240-
return `${key}: { ${nestedObjectExpr.join(", ")} }`;
241-
}
242-
return `${key}: ${valueExpr}`;
243-
}),
244-
);
235+
const objectExpr = entries.map(async ([key, value]) => {
236+
const valueExpr = getExpression(value);
237+
if (value.type === "ObjectLiteral") {
238+
const nestedEntries = Object.entries(value.value);
239+
const nestedObjectExpr = nestedEntries.map(
240+
([nestedKey, nestedValue]) => {
241+
const nestedValueExpr = getExpression(nestedValue);
242+
return `${nestedKey}: ${nestedValueExpr}`;
243+
},
244+
);
245+
return `${key}: { ${nestedObjectExpr.join(", ")} }`;
246+
}
247+
return `${key}: ${valueExpr}`;
248+
});
245249
return `{ ${objectExpr.join(", ")} }`;
246250
}
247251
return "";

0 commit comments

Comments
 (0)