diff --git a/packages/nodes-base/nodes/Code/JsCodeValidator.ts b/packages/nodes-base/nodes/Code/JsCodeValidator.ts index f56de67d85..422ef1d537 100644 --- a/packages/nodes-base/nodes/Code/JsCodeValidator.ts +++ b/packages/nodes-base/nodes/Code/JsCodeValidator.ts @@ -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; diff --git a/packages/nodes-base/nodes/Code/test/JsCodeValidator.test.ts b/packages/nodes-base/nodes/Code/test/JsCodeValidator.test.ts new file mode 100644 index 0000000000..f319fd9b9d --- /dev/null +++ b/packages/nodes-base/nodes/Code/test/JsCodeValidator.test.ts @@ -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(); + }); + }); +});