fix(core): Fix expression extension misdetection (#5219)

* 🐛 Narrow down check

* fix: converted expression extension check loop into regex

* fix: remove greedy string check

* fix: re-add string spliting regex

Co-authored-by: Valya Bullions <valya@n8n.io>
This commit is contained in:
Iván Ovejero 2023-01-23 11:08:27 +01:00 committed by GitHub
parent c5245dd387
commit 0b123ce05e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 5 deletions

View file

@ -314,11 +314,15 @@ export class Expression {
}
extendSyntax(bracketedExpression: string): string {
if (!hasExpressionExtension(bracketedExpression) || hasNativeMethod(bracketedExpression))
return bracketedExpression;
const chunks = splitExpression(bracketedExpression);
const codeChunks = chunks
.filter((c) => c.type === 'code')
.map((c) => c.text.replace(/("|').*?("|')/, '').trim());
if (!codeChunks.some(hasExpressionExtension) || hasNativeMethod(bracketedExpression))
return bracketedExpression;
const extendedChunks = chunks.map((chunk): ExpressionChunk => {
if (chunk.type === 'code') {
const output = extendTransform(chunk.text);

View file

@ -41,14 +41,17 @@ const EXPRESSION_EXTENSION_METHODS = Array.from(
...Object.keys(arrayExtensions.functions),
...Object.keys(objectExtensions.functions),
...Object.keys(genericExtensions),
'$if',
]),
);
const EXPRESSION_EXTENSION_REGEX = new RegExp(
`(\\$if|\\.(${EXPRESSION_EXTENSION_METHODS.join('|')}))\\s*\\(`,
);
const isExpressionExtension = (str: string) => EXPRESSION_EXTENSION_METHODS.some((m) => m === str);
export const hasExpressionExtension = (str: string): boolean =>
EXPRESSION_EXTENSION_METHODS.some((m) => str.includes(m));
EXPRESSION_EXTENSION_REGEX.test(str);
export const hasNativeMethod = (method: string): boolean => {
if (hasExpressionExtension(method)) {