Skip to content

Commit

Permalink
fix(Code Node): Do not validate code within comments (#12938)
Browse files Browse the repository at this point in the history
  • Loading branch information
RicardoE105 authored Jan 30, 2025
1 parent 9590e5d commit cdfa225
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/nodes-base/nodes/Code/JsCodeValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ export function validateNoDisallowedMethodsInRunForEach(code: string, itemIndex:

const lineNumber =
code.split('\n').findIndex((line) => {
return line.includes(disallowedMethod) && !line.startsWith('//') && !line.startsWith('*');
line = line.trimStart();
return (
line.includes(disallowedMethod) &&
!line.startsWith('//') &&
!line.startsWith('/*') &&
!line.startsWith('*')
);
}) + 1;

const disallowedMethodFound = lineNumber !== 0;
Expand Down
51 changes: 51 additions & 0 deletions packages/nodes-base/nodes/Code/test/JsCodeValidator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { validateNoDisallowedMethodsInRunForEach } from '../JsCodeValidator';

describe('JsCodeValidator', () => {
describe('validateNoDisallowedMethodsInRunForEach', () => {
it('should not throw error if disallow method is used within single line comments', () => {
const code = [
"// Add a new field called 'myNewField' to the JSON of the item",
'$input.item.json.myNewField = 1;',
' // const xxx = $input.all()',
'return $input.item;',
].join('\n');

expect(() => validateNoDisallowedMethodsInRunForEach(code, 0)).not.toThrow();
});

it('should not throw error if disallow method is used in single multi line comments', () => {
const code = [
"// Add a new field called 'myNewField' to the JSON of the item",
'$input.item.json.myNewField = 1;',
'/** const xxx = $input.all()*/',
'return $input.item;',
].join('\n');

expect(() => validateNoDisallowedMethodsInRunForEach(code, 0)).not.toThrow();
});

it('should not throw error if disallow method is used within multi line comments', () => {
const code = [
"// Add a new field called 'myNewField' to the JSON of the item",
'$input.item.json.myNewField = 1;',
'/**',
'*const xxx = $input.all()',
'*/',
'return $input.item;',
].join('\n');

expect(() => validateNoDisallowedMethodsInRunForEach(code, 0)).not.toThrow();
});

it('should throw error if disallow method is used', () => {
const code = [
"// Add a new field called 'myNewField' to the JSON of the item",
'$input.item.json.myNewField = 1;',
'const xxx = $input.all()',
'return $input.item;',
].join('\n');

expect(() => validateNoDisallowedMethodsInRunForEach(code, 0)).toThrow();
});
});
});

0 comments on commit cdfa225

Please sign in to comment.