fix(Code Node): Do not validate code within comments (#12938)

This commit is contained in:
Ricardo Espinoza 2025-01-30 09:44:35 -05:00 committed by GitHub
parent 9590e5d58b
commit cdfa22593b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 58 additions and 1 deletions

View file

@ -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;

View file

@ -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();
});
});
});