From f092b9053a5c556ee3cde615ceaf3d5a69f9d444 Mon Sep 17 00:00:00 2001 From: Elon Volo Date: Wed, 17 Feb 2021 09:35:58 -0500 Subject: [PATCH] Fix for errors caused by rest paramters in functions. --- .../function-rest-parameter.input.ts | 3 ++ .../function-rest-parameter.output.ts | 12 +++++ __tests__/transform-test.js | 1 + temptest/package-lock.json | 46 ------------------- temptest/package.json | 14 ------ utils.js | 23 +++++++++- 6 files changed, 38 insertions(+), 61 deletions(-) create mode 100644 __testfixtures__/function-rest-parameter.input.ts create mode 100644 __testfixtures__/function-rest-parameter.output.ts delete mode 100644 temptest/package-lock.json delete mode 100644 temptest/package.json diff --git a/__testfixtures__/function-rest-parameter.input.ts b/__testfixtures__/function-rest-parameter.input.ts new file mode 100644 index 0000000..25fb15e --- /dev/null +++ b/__testfixtures__/function-rest-parameter.input.ts @@ -0,0 +1,3 @@ +function doStuff(...myArray) { + let stuff = 'things'; +} diff --git a/__testfixtures__/function-rest-parameter.output.ts b/__testfixtures__/function-rest-parameter.output.ts new file mode 100644 index 0000000..c2efcca --- /dev/null +++ b/__testfixtures__/function-rest-parameter.output.ts @@ -0,0 +1,12 @@ +function doStuff(...myArray) { + console.log( + "[logitall] __testfixtures__/function-rest-parameter.input.ts:1:doStuff(...myArray)" + ); + + console.log( + `[logitall] __testfixtures__/function-rest-parameter.input.ts:1:doStuff:param myArray value: + ${JSON.stringify(myArray)}` + ); + + let stuff = 'things'; +} diff --git a/__tests__/transform-test.js b/__tests__/transform-test.js index de000ce..8511490 100644 --- a/__tests__/transform-test.js +++ b/__tests__/transform-test.js @@ -8,6 +8,7 @@ defineTest(__dirname + '../', 'transform', {'relpath': '__testfixtures__', 'para defineTest(__dirname + '../', 'transform', {'relpath': '__testfixtures__'}, 'function-declaration', { 'parser': 'ts' }); defineTest(__dirname + '../', 'transform', {'relpath': '__testfixtures__'}, 'function-declaration-export-default-function', { 'parser': 'ts' }); defineTest(__dirname + '../', 'transform', {'relpath': '__testfixtures__', 'params' : true }, 'function-declaration-logparams', { 'parser': 'ts' }); +defineTest(__dirname + '../', 'transform', {'relpath': '__testfixtures__', 'params' : true }, 'function-rest-parameter', { 'parser': 'ts' }); defineTest(__dirname + '../', 'transform', {'relpath': '__testfixtures__'}, 'function-return', { 'parser': 'ts' }); defineTest(__dirname + '../', 'transform', {'relpath': '__testfixtures__'}, 'method-constructor', { 'parser': 'ts' }); defineTest(__dirname + '../', 'transform', {'relpath': '__testfixtures__', 'params': true }, 'method-constructor-logparams', { 'parser': 'ts' }); diff --git a/temptest/package-lock.json b/temptest/package-lock.json deleted file mode 100644 index 2d5ae3e..0000000 --- a/temptest/package-lock.json +++ /dev/null @@ -1,46 +0,0 @@ -{ - "name": "temptest", - "version": "1.0.0", - "lockfileVersion": 2, - "requires": true, - "packages": { - "": { - "version": "1.0.0", - "license": "ISC", - "dependencies": { - "rxjs": "^6.6.3" - } - }, - "node_modules/rxjs": { - "version": "6.6.3", - "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.6.3.tgz", - "integrity": "sha512-trsQc+xYYXZ3urjOiJOuCOa5N3jAZ3eiSpQB5hIT8zGlL2QfnHLJ2r7GMkBGuIausdJN1OneaI6gQlsqNHHmZQ==", - "dependencies": { - "tslib": "^1.9.0" - }, - "engines": { - "npm": ">=2.0.0" - } - }, - "node_modules/tslib": { - "version": "1.14.1", - "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz", - "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==" - } - }, - "dependencies": { - "rxjs": { - "version": "6.6.3", - "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.6.3.tgz", - "integrity": "sha512-trsQc+xYYXZ3urjOiJOuCOa5N3jAZ3eiSpQB5hIT8zGlL2QfnHLJ2r7GMkBGuIausdJN1OneaI6gQlsqNHHmZQ==", - "requires": { - "tslib": "^1.9.0" - } - }, - "tslib": { - "version": "1.14.1", - "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz", - "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==" - } - } -} diff --git a/temptest/package.json b/temptest/package.json deleted file mode 100644 index ea53ba6..0000000 --- a/temptest/package.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "name": "temptest", - "version": "1.0.0", - "description": "", - "main": "index.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "author": "", - "license": "ISC", - "dependencies": { - "rxjs": "^6.6.3" - } -} diff --git a/utils.js b/utils.js index 62c2dde..35805c6 100644 --- a/utils.js +++ b/utils.js @@ -35,7 +35,18 @@ const buildAnonymousParamsList = (paramNodes) => { let paramString = '('; for (let index = 0; index < paramNodes.length; index++) { - paramString = paramString + paramNodes[index].name; + let currentNode = paramNodes[index]; + let currentNodeName; + + if (!currentNode.name && currentNode.type === 'RestElement') { + // The solution is to descend into the RestElement and get the child identifier + currentNodeName = '...' + currentNode.argument.name; + } else { + currentNodeName = currentNode.name; + } + + // Deal with situations where there's a rest element + paramString = paramString + currentNodeName; if (index !== (paramNodes.length - 1)) { paramString = paramString + ', '; @@ -72,6 +83,16 @@ const buildParamLoggingList = (paramNodes, relPathToFile, linenum, functionName) currentNodeName = currentNode.parameter.name; } + // If we have a rest parameter (i.e. a ...myParamName in the following: + // function doStuff(...myParamName) { + // + // } + // Then Babel will end up putting a RestElement in the params[] array, + if (!currentNodeName && currentNode.type === 'RestElement') { + // The solution is to descend into the RestElement and get the child identifier + currentNodeName = currentNode.argument.name; + } + let announcement = `${LIA_PREFIX}\t${relPathToFile}:${linenum}${functionName}:param ${currentNodeName} value:${LIA_SUFFIX} \n\t\t\t\t\t\t\t\t\t\t`; let quasis = [