Skip to content

Commit

Permalink
fix(editor): Use bracket notation for all invalid identifiers in expr…
Browse files Browse the repository at this point in the history
…essions (#8933)
  • Loading branch information
elsmr authored and krynble committed Mar 25, 2024
1 parent becc804 commit 1316f2d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
11 changes: 6 additions & 5 deletions packages/editor-ui/src/utils/__tests__/mappingUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,19 +216,19 @@ describe('Mapping Utils', () => {
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 = {
nodeName: 'nodeName',
distanceFromActive: 2,
path: ['sample', 'path with-space', 'path-with-hyphen'],
path: ['sample', 'path with-space', 'path-with-hyphen', '2iStartWithANumber'],
};
const result = getMappedExpression(input);
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 = {
nodeName: 'nodeName',
distanceFromActive: 2,
Expand All @@ -243,11 +243,12 @@ describe('Mapping Utils', () => {
'test,',
'test:',
'path.',
'2iStartWithANumber',
],
};
const result = getMappedExpression(input);
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'] }}",
);
});

Expand Down
13 changes: 8 additions & 5 deletions packages/editor-ui/src/utils/mappingUtils.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import type { INodeProperties, NodeParameterValueType } 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 {
return path.reduce((accu: string, part: string | number) => {
if (typeof part === 'number') {
return `${accu}[${part}]`;
}

const special = ['-', ' ', '.', "'", '"', '`', '[', ']', '{', '}', '(', ')', ':', ',', '?'];
const hasSpecial = !!special.find((s) => part.includes(s));
if (hasSpecial) {
const escaped = part.replaceAll("'", "\\'");
return `${accu}['${escaped}']`;
if (!isValidJsIdentifierName(part)) {
return `${accu}['${escapeMappingString(part)}']`;
}

return `${accu}.${part}`;
Expand Down

0 comments on commit 1316f2d

Please sign in to comment.