Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(helpers): deprecate invocations on non-functions for fakeEval #2913

Merged
merged 10 commits into from
Jun 14, 2024
23 changes: 19 additions & 4 deletions src/modules/helpers/eval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export function fakeEval(
do {
let index: number;
if (remaining.startsWith('(')) {
[index, current] = evalProcessFunction(remaining, current);
[index, current] = evalProcessFunction(remaining, current, expression);
} else {
[index, current] = evalProcessExpression(remaining, current);
}
Expand Down Expand Up @@ -109,10 +109,12 @@ export function fakeEval(
*
* @param input The input string to parse.
* @param entrypoints The entrypoints to attempt the call on.
* @param expression The full expression to use in errors.
*/
function evalProcessFunction(
input: string,
entrypoints: ReadonlyArray<unknown>
entrypoints: ReadonlyArray<unknown>,
expression: string
): [continueIndex: number, mapped: unknown[]] {
const [index, params] = findParams(input);
const nextChar = input[index + 1];
Expand All @@ -133,9 +135,22 @@ function evalProcessFunction(
return [
index + (nextChar === '.' ? 2 : 1), // one for the closing bracket, one for the dot
entrypoints.map((entrypoint): unknown =>
// TODO @ST-DDT 2023-12-11: Replace in v9
// TODO @ST-DDT 2023-12-11: Replace in v10
// typeof entrypoint === 'function' ? entrypoint(...params) : undefined
typeof entrypoint === 'function' ? entrypoint(...params) : entrypoint
{
if (typeof entrypoint === 'function') {
return entrypoint(...params);
}

console.warn(
`[@faker-js/faker]: Invoking expressions which are not functions is deprecated since v9.0 and will be removed in v10.0.
Please remove the parentheses or replace the expression with an actual function.
${expression}
${' '.repeat(expression.length - input.length)}^`
);

return entrypoint;
}
),
];
}
Expand Down
2 changes: 1 addition & 1 deletion test/modules/helpers-eval.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ describe('fakeEval()', () => {
});

it('requires a function for parameters', () => {
// TODO @ST-DDT 2023-12-11: Replace in v9
// TODO @ST-DDT 2023-12-11: Replace in v10
// expect(faker.definitions.person.first_name).toBeDefined();
//expect(() => fakeEval('person.first_name()', faker)).toThrow(
// new FakerError(`Cannot resolve expression 'person.first_name'`)
Expand Down