-
Notifications
You must be signed in to change notification settings - Fork 198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed bug - passing parameter values #189
base: master
Are you sure you want to change the base?
Fixed bug - passing parameter values #189
Conversation
Hey Thanks for the PR. I am looking into it. I am very unfamiliar with this code, so I may take some time. :| |
Sure @ketkarameya ! |
Hey @Srihari123456 , I was looking at the goal of the PR. I have one concern here - isn't the literal For Case 1 - should'nt the refactoring be
Note I have removed the literal |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have done my first pass over the PR.
javascript/src/refactor.js
Outdated
nodeArgumentIsFlag = nodeArgument.value === engine.flagname; | ||
break; | ||
} | ||
if (nodeArgumentIsFlag) { | ||
const flagType = methodHashMap.get(node.callee.name).flagType; | ||
const flagType = methodHashMap.get(node.callee.name).flagType; // control || treated |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dangling comments?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll remove this in the updated commit
javascript/src/refactor.js
Outdated
engine.changed = true; | ||
|
||
if ( | ||
(flagType === 'treated' && engine.behaviour) || | ||
(flagType === 'treated' && engine.behaviour) || // engine.behaviour = true => treated |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dangling comments?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll remove this in the updated commit
javascript/src/refactor.js
Outdated
@@ -462,7 +460,7 @@ class RefactorEngine { | |||
engine.isPiranhaLiteral(declaration.init) && | |||
typeof declaration.init.value === 'boolean' | |||
) { | |||
assignments[declaration.id.name] = declaration.init.value; | |||
assignments[declaration.id.name] = declaration.init.value; // Assigning a -> true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dangling comments?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll remove this in the updated commit
engine.changed = true; | ||
return engine.trueLiteral(); | ||
} else if (assignments[node.name] === false) { | ||
engine.changed = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should engine.changed
be false here? if not, can we pull out the two assignments outside the if statement
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
engine.changed
must be true here. I'll refine the code such that it isnt redundant
javascript/src/refactor.js
Outdated
@@ -498,6 +524,64 @@ class RefactorEngine { | |||
return this.remove(); | |||
} | |||
} else if (node.type === 'Identifier') { | |||
// this is the part where the function arguments are substituted values if they are a part of the assignments | |||
if (node.name in assignments) { | |||
if (assignments[node.name] === true) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simplify if-else-if
to if-else
or ternary
.
return assignments[node.name] === true ? engine.trueLiteral() : engine.falseLiteral()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ketkarameya the value of assignments[node.name] can be true or false or undefined. Thats why I didnt code it in if-else. Anyways I'll incorporate this suggestion and refine the code
@@ -478,11 +476,39 @@ class RefactorEngine { | |||
return assignments; | |||
} | |||
|
|||
adjustFunctionParams(parameterList) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we have some comments here - what is adjustFunctionParams
doing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes added necessary comments
return parameterList; | ||
} | ||
|
||
pruneVarReferencesinFunc(nn, assignments) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we have some comments on what this function is doing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added necessary comments
if (node.name === parent.params[i].name) { | ||
for (let j = 0; j < parameterList.length; j++) { | ||
if ( | ||
parameterList[j].functionName === parent.id.name && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move this check outside of this for loop ? From what i understand, you are checking if the parent
function declaration name is same as the function name of parameter_list
right? Maybe this could be like the first line inside enter
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also do we also need to check that parameterList.length == parent.params.length
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ketkarameya parameterList
contains the details of the parameters to be removed across all the functions in the file. On the other hand, parent.params
denotes only the arguments of a specific function declaration. Hence parameterList.length
need not be same as parent.params.length
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then how do we know that parent
is the correct method declaration? Will this handle overriden methods?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move this check outside of this for loop ? From what i understand, you are checking if the
parent
function declaration name is same as the function name ofparameter_list
right? Maybe this could be like the first line insideenter
?
parameterList
is a list of objects, each of which has functionName, parameterIndex, value
. So even if its brought under enter
, we need to iterate over the list of objects available in parameterList
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it.
javascript/src/refactor.js
Outdated
@@ -536,7 +620,7 @@ class RefactorEngine { | |||
estraverse.traverse(this.ast, { | |||
enter: function (node, parent) { | |||
if (node.type === 'FunctionDeclaration') { | |||
current = node.id.name; | |||
current = node.id.name; // name of the function |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dangling comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll remove this in the updated commit
javascript/src/refactor.js
Outdated
var functionsWithSingleReturn = this.getFunctionsWithSingleReturn(); | ||
var redundantFunctions = this.getRedundantFunctions(functionsWithSingleReturn); | ||
this.pruneFuncReferences(redundantFunctions); | ||
|
||
iterations++; | ||
} | ||
|
||
console.log(this.changed); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stale code? could be removed!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll remove this in the updated commit
@Srihari123456 I just wanted to make sure that you didn't miss this comment. |
@ketkarameya Yes I have figured the way to eliminate redundant literals and actually wanted to implement it this way. So I thought of raising a separate ticket for the same. Anyways I'll include this in my updated commit as well |
@ketkarameya I've commited the necessary changes. Please check that when you can |
Hey @Srihari123456 i am out on vacation until end of the week. I ll take a look at it as soon as I am back. |
@Srihari123456 can you please look into why these test cases are failing? Also I was wondering if you could add end to end test cases for this new feature you have implemented (The scenarios similar to the ones you have added in the PR description) |
Will check this |
Addressed Issue #183
Case 1:
Before Refactoring:
After Refactoring (Previously):
After Refactoring (Now):
Case 2:
Before Refactoring:
After Refactoring (Previously):
After Refactoring (Now):
Case 3:
Before Refactoring:
After Refactoring (Previously):
After Refactoring (Now):