Skip to content

Commit

Permalink
Fix for errors caused by rest paramters in functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
ElonVolo committed Feb 24, 2021
1 parent fa8baf6 commit f092b90
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 61 deletions.
3 changes: 3 additions & 0 deletions __testfixtures__/function-rest-parameter.input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function doStuff(...myArray) {
let stuff = 'things';
}
12 changes: 12 additions & 0 deletions __testfixtures__/function-rest-parameter.output.ts
Original file line number Diff line number Diff line change
@@ -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';
}
1 change: 1 addition & 0 deletions __tests__/transform-test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 0 additions & 46 deletions temptest/package-lock.json

This file was deleted.

14 changes: 0 additions & 14 deletions temptest/package.json

This file was deleted.

23 changes: 22 additions & 1 deletion utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 + ', ';
Expand Down Expand Up @@ -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 = [
Expand Down

0 comments on commit f092b90

Please sign in to comment.