Lua tests and fix for excessive binary_expression calculations #16
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
General
Test cases for Lua are mostly inspired by JavaScript. My idea is that it would be easier to work with if we kept some consistency with test names and what we are testing.
binary_expression
errorI guess the original idea for the
binary_expression
rule was to test the logical expressions and their nesting? If not, please let me know 🙂Almost everything in Lua is a
binary_expression
(overstatement, but still...). Let us take some quick examples of how many places they actually occur and see the calculations in the MAIN codebase. This is to show why the code fix was done.Simple.lua
Notice that each little multiplication, equality check and string concatenation is a
binary_expression
. If we see the debug prints for the Simple.lua file, it looks like this:We can easily verify that this is from the
binary_expression
operations with the output from tree-sitter:The calculation is correct here, but that is due to no real nesting of
binary_expression
s.If we try write a
quad
function instead ofsquare
, we see the issue quite clearly:As you can see, we now get a score of 1 for the first multiplication. From my understanding of cognitive complexity, only logical expressions should increase the score. I see nothing in any documentation (e.g, the Sonar paper) that arithmetic should increase it.
Recursion.lua
To really drive the point home, let us look at the recursion example.
Here we see something similar. (one of the
+1
s is correct due to recursive call!). Because the multiplicationn * factorial(n - 1)
is abinary_expression
that has abinary_expression
(i.e,n - 1
) in a child of child nodes, it will get a score. That is not (at least to my knowledge) something that should happen. If it should happen, then it is wrong for ALL other languages...We could probably continue down this road, and check each test. I don't think that is necessary. Feel free to do so if you want 🙂
binary_expression
fixif it was not clear, this PR fixes the above consistencies. This is by changing the
binary_expression
checks to only give any score for logical operators that has a logical operator operation as a child. If not, the calculations above will happen resulting in the situation we see in the recursion example.