From 396701d866f59b1e272d46976d925f4633fd5db1 Mon Sep 17 00:00:00 2001 From: Marie Katrine Ekeberg Date: Tue, 16 Apr 2024 21:05:37 +0200 Subject: [PATCH 1/5] Basic tests for Lua and commenting out of rule not working properly --- Eask | 1 + codemetrics-rules.el | 3 ++- test/lua-test.el | 45 ++++++++++++++++++++++++++++++++++++++++++ test/lua/Recursion.lua | 7 +++++++ test/lua/Simple.lua | 10 ++++++++++ 5 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 test/lua-test.el create mode 100644 test/lua/Recursion.lua create mode 100644 test/lua/Simple.lua diff --git a/Eask b/Eask index 4f21d1b..7aa9e6d 100644 --- a/Eask +++ b/Eask @@ -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")) diff --git a/codemetrics-rules.el b/codemetrics-rules.el index 4db1844..a03c0e5 100644 --- a/codemetrics-rules.el +++ b/codemetrics-rules.el @@ -209,7 +209,8 @@ (while_statement . (1 t)) (for_statement . (1 t)) (repeat_statement . (1 t)) - (binary_expression . codemetrics-rules--lua-binary-expressions) + ;; TODO: this gets wrong... Even simple n * factorial(n - 1) gets high complexity... + ;;(binary_expression . codemetrics-rules--lua-binary-expressions) (goto_statement . (1 nil)) (function_call . codemetrics-rules--recursion))) diff --git a/test/lua-test.el b/test/lua-test.el new file mode 100644 index 0000000..1bd3c37 --- /dev/null +++ b/test/lua-test.el @@ -0,0 +1,45 @@ +;;; lua-test.el --- Lua language tests for codemetrics.el -*- lexical-binding: t; -*- + +;; Copyright (C) 2024 Marie Katrine Ekeberg + +;; Author: Marie Katrine Ekeberg + +;; 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 . + +;;; Commentary: +;; + +;;; Code: +(require 'codemetrics) +(require 'lua-mode) + +(codemetrics-test lua-simple + "test/lua/Simple.lua" + lua-mode + '(3 + (function_declaration . 0) + (function_call . 0) + (function_call . 0) + (for_statement . 1) + (if_statement . 2))) + +(codemetrics-test lua-recursion + "test/lua/Recursion.lua" + lua-mode + '(2 + (function_declaration . 0) + (if_statement . 1) + (function_call . 1))) + +;;; lua-test.el ends here diff --git a/test/lua/Recursion.lua b/test/lua/Recursion.lua new file mode 100644 index 0000000..913af5c --- /dev/null +++ b/test/lua/Recursion.lua @@ -0,0 +1,7 @@ +function factorial(n) + if 1 <= n then + return 1 + end + + return n * factorial(n - 1) +end diff --git a/test/lua/Simple.lua b/test/lua/Simple.lua new file mode 100644 index 0000000..c6d5cdb --- /dev/null +++ b/test/lua/Simple.lua @@ -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 From 915c2f43d4c788f824482bec98aabb3b6554bfdb Mon Sep 17 00:00:00 2001 From: Marie Katrine Ekeberg Date: Thu, 18 Apr 2024 17:46:42 +0200 Subject: [PATCH 2/5] Verify implementation of nesting logic for Lua --- test/lua-test.el | 9 +++++++++ test/lua/Nesting.lua | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 test/lua/Nesting.lua diff --git a/test/lua-test.el b/test/lua-test.el index 1bd3c37..04ae7af 100644 --- a/test/lua-test.el +++ b/test/lua-test.el @@ -42,4 +42,13 @@ (if_statement . 1) (function_call . 1))) +(codemetrics-test lua-nesting + "test/lua/Nesting.lua" + lua-mode + '(7 + (while_statement . 1) + (if_statement . 2) + (repeat_statement . 2) + (if_statement . 1) + (goto_statement . 1))) ;;; lua-test.el ends here diff --git a/test/lua/Nesting.lua b/test/lua/Nesting.lua new file mode 100644 index 0000000..4f8e172 --- /dev/null +++ b/test/lua/Nesting.lua @@ -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 From 067136dfec7506aaef68204aa1f2afb71dcfab26 Mon Sep 17 00:00:00 2001 From: Marie Katrine Ekeberg Date: Thu, 18 Apr 2024 18:52:40 +0200 Subject: [PATCH 3/5] Reintroduce binary_expression due to regular logical operations calc not working --- codemetrics-rules.el | 3 +-- test/lua-test.el | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/codemetrics-rules.el b/codemetrics-rules.el index a03c0e5..4db1844 100644 --- a/codemetrics-rules.el +++ b/codemetrics-rules.el @@ -209,8 +209,7 @@ (while_statement . (1 t)) (for_statement . (1 t)) (repeat_statement . (1 t)) - ;; TODO: this gets wrong... Even simple n * factorial(n - 1) gets high complexity... - ;;(binary_expression . codemetrics-rules--lua-binary-expressions) + (binary_expression . codemetrics-rules--lua-binary-expressions) (goto_statement . (1 nil)) (function_call . codemetrics-rules--recursion))) diff --git a/test/lua-test.el b/test/lua-test.el index 04ae7af..ac52cfa 100644 --- a/test/lua-test.el +++ b/test/lua-test.el @@ -29,10 +29,13 @@ lua-mode '(3 (function_declaration . 0) + (binary_expression . 0) (function_call . 0) + (binary_expression . 0) (function_call . 0) (for_statement . 1) - (if_statement . 2))) + (if_statement . 2) + (binary_expression . 0))) (codemetrics-test lua-recursion "test/lua/Recursion.lua" @@ -40,15 +43,25 @@ '(2 (function_declaration . 0) (if_statement . 1) - (function_call . 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))) ;;; lua-test.el ends here From c80845edc26fabf5b0ee7bb20ed6d4907fdb1370 Mon Sep 17 00:00:00 2001 From: Marie Katrine Ekeberg Date: Thu, 18 Apr 2024 18:53:11 +0200 Subject: [PATCH 4/5] Add simple Lua test for logical operators --- test/lua-test.el | 24 ++++++++++++++++++++++++ test/lua/LogicalOperators.lua | 5 +++++ 2 files changed, 29 insertions(+) create mode 100644 test/lua/LogicalOperators.lua diff --git a/test/lua-test.el b/test/lua-test.el index ac52cfa..2347ad1 100644 --- a/test/lua-test.el +++ b/test/lua-test.el @@ -64,4 +64,28 @@ (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 diff --git a/test/lua/LogicalOperators.lua b/test/lua/LogicalOperators.lua new file mode 100644 index 0000000..5dae2b2 --- /dev/null +++ b/test/lua/LogicalOperators.lua @@ -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 From b621a94d985b7202d22434c9915b98af11a7c60d Mon Sep 17 00:00:00 2001 From: Marie Katrine Ekeberg Date: Thu, 18 Apr 2024 18:54:17 +0200 Subject: [PATCH 5/5] Rewrite Lua binary expression code to make tests pass --- codemetrics.el | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/codemetrics.el b/codemetrics.el index b2fd724..c107267 100644 --- a/codemetrics.el +++ b/codemetrics.el @@ -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 _)