Skip to content
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

Lua tests and fix for excessive binary_expression calculations #16

Merged
merged 5 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Eask
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
(development
(depends-on "kotlin-mode")
(depends-on "go-mode")
(depends-on "lua-mode")
(depends-on "rust-mode")
(depends-on "typescript-mode")
(depends-on "tree-sitter-langs"))
19 changes: 13 additions & 6 deletions codemetrics.el
Original file line number Diff line number Diff line change
Expand Up @@ -491,15 +491,22 @@ For argument NODE, see function `codemetrics-analyze' for more information."
'(0 nil)))

(defun codemetrics-rules--lua-binary-expressions (node &rest _)
"Define rule for Lua binary expressions.
"Define rule for Lua binary expressions, which includes logical operators.

For argument NODE, see function `codemetrics-analyze' for more information."
(codemetrics-with-complexity
(let ((matches (codemetrics--tsc-find-children-traverse node "binary_expression"))
(sequence nil))
(when (<= 2 (length matches))
(setq sequence t))
(list (if sequence 1 0) nil))
(let* ((node-is-logical-operator (lambda (node)
(-contains? '("and" "or")
;; binary_expressions contain 3 elements; two expressions and one middle string
(tsc-node-text (tsc-get-nth-child node 1)))))
(matches (codemetrics--tsc-find-children node "binary_expression"))
(has-child-logical-operator (-any (lambda (x) (funcall node-is-logical-operator x))
matches))
(self-is-logical-operator (funcall node-is-logical-operator node)))
(list (if (and self-is-logical-operator has-child-logical-operator)
1
0)
nil))
'(1 nil)))

(defun codemetrics-rules--ruby-binary (node &rest _)
Expand Down
91 changes: 91 additions & 0 deletions test/lua-test.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
;;; lua-test.el --- Lua language tests for codemetrics.el -*- lexical-binding: t; -*-

;; Copyright (C) 2024 Marie Katrine Ekeberg

;; Author: Marie Katrine Ekeberg <mke@themkat.net>

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:
;;

;;; Code:
(require 'codemetrics)
(require 'lua-mode)

(codemetrics-test lua-simple
"test/lua/Simple.lua"
lua-mode
'(3
(function_declaration . 0)
(binary_expression . 0)
(function_call . 0)
(binary_expression . 0)
(function_call . 0)
(for_statement . 1)
(if_statement . 2)
(binary_expression . 0)))

(codemetrics-test lua-recursion
"test/lua/Recursion.lua"
lua-mode
'(2
(function_declaration . 0)
(if_statement . 1)
(binary_expression . 0)
(binary_expression . 0)
(function_call . 1)
(binary_expression . 0)))

(codemetrics-test lua-nesting
"test/lua/Nesting.lua"
lua-mode
'(7
(while_statement . 1)
(binary_expression . 0)
(if_statement . 2)
(binary_expression . 0)
(binary_expression . 0)
(binary_expression . 0)
(repeat_statement . 2)
(binary_expression . 0)
(binary_expression . 0)
(if_statement . 1)
(binary_expression . 0)
(goto_statement . 1)))

(codemetrics-test lua-logical-operators
"test/lua/LogicalOperators.lua"
lua-mode
'(2
(binary_expression . 0)
(binary_expression . 0)
(binary_expression . 0)
(binary_expression . 0)
(binary_expression . 0)
(binary_expression . 0)
(binary_expression . 1)
(binary_expression . 0)
(binary_expression . 0)
(binary_expression . 0)
(binary_expression . 0)
(binary_expression . 1)
(binary_expression . 0)
(binary_expression . 0)
(binary_expression . 0)
(binary_expression . 0)))



;;; lua-test.el ends here
5 changes: 5 additions & 0 deletions test/lua/LogicalOperators.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
local simple_and = 1 == 1 and 1 == 2
local simple_or = 1 == 1 or 1 == 2

local mix_one = 2 == 2 and 1 == 1 or 2 == 3
local mix_two = 1 == 2 or 2 == 3 and 3 == 3
23 changes: 23 additions & 0 deletions test/lua/Nesting.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-- Testing various statements and nesting of those
local my_arr = { 1, 3, 5, 10, 23 }
local i = 1
local some_value = 0

::beginning_flow::
while i < 6 do
if my_arr[i] % 2 == 0 then
some_value = some_value + 1
break
end

local j = 0
repeat
j = j + 1
until j == 10
end



if 10 < some_value then
goto beginning_flow
end
7 changes: 7 additions & 0 deletions test/lua/Recursion.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function factorial(n)
if 1 <= n then
return 1
end

return n * factorial(n - 1)
end
10 changes: 10 additions & 0 deletions test/lua/Simple.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function square(n)
return n * n
end

print("Square of 3: " .. square(3))

for i=1,10 do
if 1 == i then
end
end
Loading