diff --git a/src/modules/helpers/eval.ts b/src/modules/helpers/eval.ts index 4a861ec0700..5c34fbe6b35 100644 --- a/src/modules/helpers/eval.ts +++ b/src/modules/helpers/eval.ts @@ -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); } @@ -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 + entrypoints: ReadonlyArray, + expression: string ): [continueIndex: number, mapped: unknown[]] { const [index, params] = findParams(input); const nextChar = input[index + 1]; @@ -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; + } ), ]; } diff --git a/test/modules/helpers-eval.spec.ts b/test/modules/helpers-eval.spec.ts index cb8d0eb6a9d..7643fe94204 100644 --- a/test/modules/helpers-eval.spec.ts +++ b/test/modules/helpers-eval.spec.ts @@ -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'`)