From 06f84fc21c216ec9fd50ef5198dc64feec8a0881 Mon Sep 17 00:00:00 2001 From: colinw Date: Fri, 17 Feb 2023 08:41:39 +0000 Subject: [PATCH] fix: handle function toString node10 change In node 10 the space preceding with () is removed. This causes the error: TypeError: Cannot read properties of null (reading '1') --- lib/containerHelpers/dependencyParser.js | 2 +- .../containerHelpers/dependencyParserUnitTests.js | 12 ++++++++++++ tests/unit/lib/containerHelpers/storeUnitTests.js | 2 +- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/containerHelpers/dependencyParser.js b/lib/containerHelpers/dependencyParser.js index 00b1171..471bca0 100644 --- a/lib/containerHelpers/dependencyParser.js +++ b/lib/containerHelpers/dependencyParser.js @@ -48,7 +48,7 @@ module.exports = function() { var fnString = fn.toString(); var parameters = fnString .replace( /\n/g, ' ' ) - .match( /function\s+\w*\s*\((.*?)\)/ )[ 1 ].split( /\s*,\s*/ ) + .match( /function\s?\w*\s*\((.*?)\)/ )[ 1 ].split( /\s*,\s*/ ) .map( function( parameterName ) { return parameterName.trim(); } ) .filter( function( parameterName ) { return parameterName.length > 0; } ) .map( function( parameterName ) { diff --git a/tests/unit/lib/containerHelpers/dependencyParserUnitTests.js b/tests/unit/lib/containerHelpers/dependencyParserUnitTests.js index 4430ff0..c33bd8b 100644 --- a/tests/unit/lib/containerHelpers/dependencyParserUnitTests.js +++ b/tests/unit/lib/containerHelpers/dependencyParserUnitTests.js @@ -23,6 +23,18 @@ describe( 'lib/dependencyParser', function() { } ); } ); describe( 'getDependencies', function() { + describe( 'old node compatibility', function() { + it( 'function.toString() is different in node 10', function() { + // https://github.com/nodejs/node/issues/20355 + var node9andBelowfunc = function () {}; + node9andBelowfunc.toString = function() { return "function () {}"}; + dependencyParser.getDependencies(node9andBelowfunc); + + var node10andAbovefunc = function (){}; + node10andAbovefunc.toString = function() { return "function() {}"}; + dependencyParser.getDependencies(node10andAbovefunc); + } ); + } ); describe( 'parameters', function() { it( 'Should have the correct parameters', function() { var dependencies = dependencyParser.getDependencies( function( callback, parentName, pub, setup, p1, p2 ) { diff --git a/tests/unit/lib/containerHelpers/storeUnitTests.js b/tests/unit/lib/containerHelpers/storeUnitTests.js index 09ac289..f6bd5cc 100644 --- a/tests/unit/lib/containerHelpers/storeUnitTests.js +++ b/tests/unit/lib/containerHelpers/storeUnitTests.js @@ -118,7 +118,7 @@ describe( 'lib/containerHelpers/store', function() { describe( 'getAllResolvingProblems()', function() { it( 'Should return all resolving problems', function() { var problems = store.getAllResolvingProblems().sort( function( problem1, problem2 ) { - return problem1 < problem2 ? 1 : -1; + return problem1 < problem2 ? -1 : 1; } ); assert.deepEqual( problems, [ { name: 'injectable1', errors: [