fix(editor): Use bracket notation for all invalid identifiers in expressions (#8933)

This commit is contained in:
Elias Meire 2024-03-22 13:53:06 +01:00 committed by GitHub
parent 76041b8587
commit 0e4216d7af
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 10 deletions

View file

@ -216,19 +216,19 @@ describe('Mapping Utils', () => {
expect(result).toBe("{{ $('nodeName').item.json.sample[0].path }}"); expect(result).toBe("{{ $('nodeName').item.json.sample[0].path }}");
}); });
it('should generate a mapped expression with special characters in array path', () => { it('should generate a mapped expression with invalid identifier names in array path', () => {
const input = { const input = {
nodeName: 'nodeName', nodeName: 'nodeName',
distanceFromActive: 2, distanceFromActive: 2,
path: ['sample', 'path with-space', 'path-with-hyphen'], path: ['sample', 'path with-space', 'path-with-hyphen', '2iStartWithANumber'],
}; };
const result = getMappedExpression(input); const result = getMappedExpression(input);
expect(result).toBe( expect(result).toBe(
"{{ $('nodeName').item.json.sample['path with-space']['path-with-hyphen'] }}", "{{ $('nodeName').item.json.sample['path with-space']['path-with-hyphen']['2iStartWithANumber'] }}",
); );
}); });
it('should handle paths with special characters', () => { it('should handle paths with invalid identifier names', () => {
const input = { const input = {
nodeName: 'nodeName', nodeName: 'nodeName',
distanceFromActive: 2, distanceFromActive: 2,
@ -243,11 +243,12 @@ describe('Mapping Utils', () => {
'test,', 'test,',
'test:', 'test:',
'path.', 'path.',
'2iStartWithANumber',
], ],
}; };
const result = getMappedExpression(input); const result = getMappedExpression(input);
expect(result).toBe( expect(result).toBe(
"{{ $('nodeName').item.json.sample['\"Execute\"']['`Execute`']['\\'Execute\\'']['[Execute]']['{Execute}']['execute?']['test,']['test:']['path.'] }}", "{{ $('nodeName').item.json.sample['\"Execute\"']['`Execute`']['\\'Execute\\'']['[Execute]']['{Execute}']['execute?']['test,']['test:']['path.']['2iStartWithANumber'] }}",
); );
}); });

View file

@ -1,17 +1,20 @@
import type { INodeProperties, NodeParameterValueType } from 'n8n-workflow'; import type { INodeProperties, NodeParameterValueType } from 'n8n-workflow';
import { isResourceLocatorValue } from 'n8n-workflow'; import { isResourceLocatorValue } from 'n8n-workflow';
const validJsIdNameRegex = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
function isValidJsIdentifierName(name: string | number): boolean {
return validJsIdNameRegex.test(name.toString());
}
export function generatePath(root: string, path: Array<string | number>): string { export function generatePath(root: string, path: Array<string | number>): string {
return path.reduce((accu: string, part: string | number) => { return path.reduce((accu: string, part: string | number) => {
if (typeof part === 'number') { if (typeof part === 'number') {
return `${accu}[${part}]`; return `${accu}[${part}]`;
} }
const special = ['-', ' ', '.', "'", '"', '`', '[', ']', '{', '}', '(', ')', ':', ',', '?']; if (!isValidJsIdentifierName(part)) {
const hasSpecial = !!special.find((s) => part.includes(s)); return `${accu}['${escapeMappingString(part)}']`;
if (hasSpecial) {
const escaped = part.replaceAll("'", "\\'");
return `${accu}['${escaped}']`;
} }
return `${accu}.${part}`; return `${accu}.${part}`;