diff --git a/Eask b/Eask index 2063795..7e3e276 100644 --- a/Eask +++ b/Eask @@ -2,7 +2,7 @@ "1.0.0" "Plugin shows complexity information") -(website-url "https://github.com/abougouffa/cognitive-complexity") +(website-url "https://github.com/emacs-vs/cognitive-complexity") (keywords "convenience" "complexity") (package-file "cognitive-complexity.el") @@ -18,5 +18,11 @@ (development (depends-on "kotlin-mode") + (depends-on "go-mode") (depends-on "lua-mode") + (depends-on "rust-mode") + (depends-on "typescript-mode") + (depends-on "treesit-langs" + :repo "emacs-tree-sitter/treesit-langs" + :fetcher 'github) ; pre-built binary (depends-on "treesit-auto")) diff --git a/cognitive-complexity.el b/cognitive-complexity.el index cb6cbd8..3bc20a2 100644 --- a/cognitive-complexity.el +++ b/cognitive-complexity.el @@ -7,7 +7,7 @@ ;; Author: Abdelhak BOUGOUFFA ;; Maintainer: Shen, Jen-Chieh ;; Maintainer: Abdelhak BOUGOUFFA -;; URL: https://github.com/abougouffa/cognitive-complexity +;; URL: https://github.com/emacs-vs/cognitive-complexity ;; Version: 1.0.0 ;; Package-Requires: ((emacs "29.1")) ;; Keywords: convenience tools c languages diff --git a/test/bash-test.el b/test/bash-test.el index 2d5f608..44ed61d 100644 --- a/test/bash-test.el +++ b/test/bash-test.el @@ -21,35 +21,36 @@ ;; ;;; Code: + (require 'cognitive-complexity) (cognitive-complexity-test bash-simple "test/bash/Simple.sh" - bash-ts-mode + sh-mode '(3 - (function_definition . 0) - (command . 0) - (function_definition . 0) - (for_statement . 1) - (command . 0) - (command . 0) - (function_definition . 0) - (while_statement . 1) - (command . 0) - (if_statement . 1) - (command . 0) - (command . 0) - (command . 0))) + ("function_definition" . 0) + ("command" . 0) + ("function_definition" . 0) + ("for_statement" . 1) + ("command" . 0) + ("command" . 0) + ("function_definition" . 0) + ("while_statement" . 1) + ("command" . 0) + ("if_statement" . 1) + ("command" . 0) + ("command" . 0) + ("command" . 0))) (cognitive-complexity-test bash-recursion "test/bash/Recursion.sh" - bash-ts-mode + sh-mode '(2 - (function_definition . 0) - (if_statement . 1) - (command . 0) - (command . 1) - (command . 0) - (command . 0))) + ("function_definition" . 0) + ("if_statement" . 1) + ("command" . 0) + ("command" . 1) + ("command" . 0) + ("command" . 0))) ;;; bash-test.el ends here diff --git a/test/c-test.el b/test/c-test.el index 1ff2936..4a6d2f6 100644 --- a/test/c-test.el +++ b/test/c-test.el @@ -21,69 +21,70 @@ ;; ;;; Code: + (require 'cognitive-complexity) (cognitive-complexity-test c-simple "test/c/Simple.c" - c-ts-mode + c-mode '(4 - (preproc_ifdef . 1) - (function_definition . 0) - (for_statement . 1) - (while_statement . 1) - (if_statement . 1) - (call_expression . 0))) + ("preproc_ifdef" . 1) + ("function_definition" . 0) + ("for_statement" . 1) + ("while_statement" . 1) + ("if_statement" . 1) + ("call_expression" . 0))) (cognitive-complexity-test c-recursion "test/c/Recursion.c" - c-ts-mode + c-mode '(2 - (function_definition . 0) - (if_statement . 1) - (call_expression . 1))) + ("function_definition" . 0) + ("if_statement" . 1) + ("call_expression" . 1))) (cognitive-complexity-test c-control-flow "test/c/ControlFlow.c" - c-ts-mode + c-mode '(5 - (function_definition . 0) - (switch_statement . 1) - (call_expression . 0) - (function_definition . 0) - (do_statement . 1) - (call_expression . 0) - (function_definition . 0) - (call_expression . 0) - (if_statement . 1) - (goto_statement . 2))) + ("function_definition" . 0) + ("switch_statement" . 1) + ("call_expression" . 0) + ("function_definition" . 0) + ("do_statement" . 1) + ("call_expression" . 0) + ("function_definition" . 0) + ("call_expression" . 0) + ("if_statement" . 1) + ("goto_statement" . 2))) (cognitive-complexity-test c-logical-operators "test/c/LogicalOperators.c" - c-ts-mode + c-mode '(6 - (function_definition . 0) - (if_statement . 1) - ("&&" . 0) - (if_statement . 1) - ("||" . 0) - (if_statement . 1) - ("&&" . 0) - ("||" . 1) - (if_statement . 1) - ("||" . 1) - ("&&" . 0))) + ("function_definition" . 0) + ("if_statement" . 1) + ("&&" . 0) + ("if_statement" . 1) + ("||" . 0) + ("if_statement" . 1) + ("&&" . 0) + ("||" . 1) + ("if_statement" . 1) + ("||" . 1) + ("&&" . 0))) ;; Loosely inspired by go nested prints issue ;; (for verifying issue with multiple nested ifs inside a for-loop) (cognitive-complexity-test c-nested-prints "test/c/NestedPrints.c" - c-ts-mode + c-mode '(5 (function_definition . 0) - (for_statement . 1) - (if_statement . 2) - (call_expression . 0) - (if_statement . 2) - (call_expression . 0))) + (for_statement . 1) + (if_statement . 2) + (call_expression . 0) + (if_statement . 2) + (call_expression . 0))) ;;; c-test.el ends here diff --git a/test/go-test.el b/test/go-test.el index 017b52e..3160cc1 100644 --- a/test/go-test.el +++ b/test/go-test.el @@ -26,33 +26,33 @@ (cognitive-complexity-test simple-go-code "test/go/Simple.go" - go-ts-mode + go-mode '(2 ("function_declaration" . 0) - ("if_statement" . 1) - ("call_expression" . 0) - ("for_statement" . 1))) + ("if_statement" . 1) + ("call_expression" . 0) + ("for_statement" . 1))) (cognitive-complexity-test recursion-go-code "test/go/Recursion.go" - go-ts-mode + go-mode '(2 ("function_declaration" . 0) - ("if_statement" . 1) - ("call_expression" . 1))) + ("if_statement" . 1) + ("call_expression" . 1))) ;; Test of issue: ;; https://github.com/emacs-vs/codemetrics/issues/7 (cognitive-complexity-test go-nested-print-calls "test/go/NestedPrints.go" - go-ts-mode + go-mode '(6 ("function_declaration" . 0) - ("for_statement" . 1) - ("if_statement" . 2) - ("call_expression" . 0) - ("if_statement" . 2) - ("call_expression" . 0) - ("for_statement" . 1))) + ("for_statement" . 1) + ("if_statement" . 2) + ("call_expression" . 0) + ("if_statement" . 2) + ("call_expression" . 0) + ("for_statement" . 1))) ;;; go-test.el ends here diff --git a/test/java-test.el b/test/java-test.el index 36bc689..ae8d220 100644 --- a/test/java-test.el +++ b/test/java-test.el @@ -21,15 +21,16 @@ ;; ;;; Code: + (require 'cognitive-complexity) (cognitive-complexity-test java-recursion "test/java/Recursion.java" java-mode '(2 - (class_declaration . 0) - (method_declaration . 0) - (if_statement . 1) - (method_invocation . 1))) + ("class_declaration" . 0) + ("method_declaration" . 0) + ("if_statement" . 1) + ("method_invocation" . 1))) ;;; java-test.el ends here diff --git a/test/javascript-test.el b/test/javascript-test.el index a786145..e0ff636 100644 --- a/test/javascript-test.el +++ b/test/javascript-test.el @@ -21,43 +21,44 @@ ;; ;;; Code: + (require 'cognitive-complexity) (cognitive-complexity-test javascript-simple "test/javascript/Simple.js" - js-ts-mode + js-mode '(3 - (function_declaration . 0) - (call_expression . 0) - (call_expression . 0) - (for_statement . 1) - (if_statement . 2))) + ("function_declaration" . 0) + ("call_expression" . 0) + ("call_expression" . 0) + ("for_statement" . 1) + ("if_statement" . 2))) (cognitive-complexity-test javascript-recursion "test/javascript/Recursion.js" - js-ts-mode + js-mode '(2 - (function_declaration . 0) - (if_statement . 1) - (call_expression . 1))) + ("function_declaration" . 0) + ("if_statement" . 1) + ("call_expression" . 1))) (cognitive-complexity-test javascript-nesting "test/javascript/Nesting.js" - js-ts-mode + js-mode '(4 - (call_expression . 0) - (arrow_function . 0) - (call_expression . 0) - (call_expression . 0) - (function_expression . 0) - (if_statement . 2) - (arrow_function . 0) - (if_statement . 2) - (call_expression . 0))) + ("call_expression" . 0) + ("arrow_function" . 0) + ("call_expression" . 0) + ("call_expression" . 0) + ("function_expression" . 0) + ("if_statement" . 2) + ("arrow_function" . 0) + ("if_statement" . 2) + ("call_expression" . 0))) (cognitive-complexity-test javascript-logical-operators "test/javascript/LogicalOperators.js" - js-ts-mode + js-mode '(2 ("&&" . 0) ("||" . 0) diff --git a/test/kotlin-test.el b/test/kotlin-test.el index 9aeb956..16e2cd9 100644 --- a/test/kotlin-test.el +++ b/test/kotlin-test.el @@ -21,6 +21,7 @@ ;; ;;; Code: + (require 'cognitive-complexity) (require 'kotlin-mode) @@ -28,30 +29,30 @@ "test/kotlin/Simple.kt" kotlin-mode '(2 - (function_declaration . 0) - (if_expression . 1) - (for_statement . 1) - (call_expression . 0))) + ("function_declaration" . 0) + ("if_expression" . 1) + ("for_statement" . 1) + ("call_expression" . 0))) (cognitive-complexity-test kotlin-recursion "test/kotlin/Recursion.kt" kotlin-mode '(2 - (function_declaration . 0) - (if_expression . 1) - (call_expression . 1))) + ("function_declaration" . 0) + ("if_expression" . 1) + ("call_expression" . 1))) (cognitive-complexity-test kotlin-break-continue "test/kotlin/BreakContinue.kt" kotlin-mode '(6 - (function_declaration . 0) - (call_expression . 0) - (for_statement . 1) - (if_expression . 2) - ("break" . 0) - (for_statement . 1) - (if_expression . 2) - ("continue" . 0))) + ("function_declaration" . 0) + ("call_expression" . 0) + ("for_statement" . 1) + ("if_expression" . 2) + ("break" . 0) + ("for_statement" . 1) + ("if_expression" . 2) + ("continue" . 0))) ;;; kotlin-test.el ends here diff --git a/test/lua-test.el b/test/lua-test.el index 4990fac..888561d 100644 --- a/test/lua-test.el +++ b/test/lua-test.el @@ -21,6 +21,7 @@ ;; ;;; Code: + (require 'cognitive-complexity) (require 'lua-mode) @@ -28,64 +29,62 @@ "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))) + ("function_declaration" . 0) + ("binary_expression" . 0) + ("function_call" . 0) + ("binary_expression" . 0) + ("function_call" . 0) + ("for_statement" . 1) + ("if_statement" . 2) + ("binary_expression" . 0))) (cognitive-complexity-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))) + ("function_declaration" . 0) + ("if_statement" . 1) + ("binary_expression" . 0) + ("binary_expression" . 0) + ("function_call" . 1) + ("binary_expression" . 0))) (cognitive-complexity-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))) + ("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))) (cognitive-complexity-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))) - - + ("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/rust-test.el b/test/rust-test.el index 297028f..763e0ed 100644 --- a/test/rust-test.el +++ b/test/rust-test.el @@ -21,74 +21,75 @@ ;; ;;; Code: + (require 'cognitive-complexity) (cognitive-complexity-test rust-simple "test/rust/Simple.rs" - rust-ts-mode + rust-mode '(2 - (function_item . 0) - (function_item . 0) - (call_expression . 0) - (macro_invocation . 0) - (if_expression . 1) - (for_expression . 1))) + ("function_item" . 0) + ("function_item" . 0) + ("call_expression" . 0) + ("macro_invocation" . 0) + ("if_expression" . 1) + ("for_expression" . 1))) (cognitive-complexity-test rust-recursion "test/rust/Recursion.rs" - rust-ts-mode + rust-mode '(2 - (function_item . 0) - (if_expression . 1) - (call_expression . 1))) + ("function_item" . 0) + ("if_expression" . 1) + ("call_expression" . 1))) (cognitive-complexity-test rust-nesting "test/rust/Nesting.rs" - rust-ts-mode + rust-mode '(5 - (function_item . 0) - (macro_invocation . 0) - (call_expression . 0) - (call_expression . 0) - (call_expression . 0) - (closure_expression . 0) - (closure_expression . 0) - (match_expression . 2) - (loop_expression . 1) - (if_expression . 2))) + ("function_item" . 0) + ("macro_invocation" . 0) + ("call_expression" . 0) + ("call_expression" . 0) + ("call_expression" . 0) + ("closure_expression" . 0) + ("closure_expression" . 0) + ("match_expression" . 2) + ("loop_expression" . 1) + ("if_expression" . 2))) (cognitive-complexity-test rust-logical-operators "test/rust/LogicalOperators.rs" - rust-ts-mode + rust-mode '(6 - (function_item . 0) - (if_expression . 1) - ("&&" . 0) - (if_expression . 1) - ("||" . 0) - (if_expression . 1) - ("&&" . 0) - ("||" . 1) - (if_expression . 1) - ("||" . 1) - ("&&" . 0))) + ("function_item" . 0) + ("if_expression" . 1) + ("&&" . 0) + ("if_expression" . 1) + ("||" . 0) + ("if_expression" . 1) + ("&&" . 0) + ("||" . 1) + ("if_expression" . 1) + ("||" . 1) + ("&&" . 0))) (cognitive-complexity-test rust-control-flow "test/rust/ControlFlow.rs" - rust-ts-mode + rust-mode '(17 - (function_item . 0) - (loop_expression . 1) - (loop_expression . 2) - (if_expression . 3) - (break_expression . 1) - (if_expression . 3) - (continue_expression . 0) - (macro_invocation . 0) - (for_expression . 1) - (loop_expression . 2) - (if_expression . 3) - (continue_expression . 1) - (break_expression . 0))) + ("function_item" . 0) + ("loop_expression" . 1) + ("loop_expression" . 2) + ("if_expression" . 3) + ("break_expression" . 1) + ("if_expression" . 3) + ("continue_expression" . 0) + ("macro_invocation" . 0) + ("for_expression" . 1) + ("loop_expression" . 2) + ("if_expression" . 3) + ("continue_expression" . 1) + ("break_expression" . 0))) ;;; rust-test.el ends here diff --git a/test/test-helper.el b/test/test-helper.el index 16ca84c..dbc434c 100644 --- a/test/test-helper.el +++ b/test/test-helper.el @@ -21,19 +21,23 @@ ;; ;;; Code: + +(require 'treesit-langs) ; Install pre-built binaries + (defmacro cognitive-complexity-test (test-name file major-mode expected-result) "Create a cognitive-complexity test named TEST-NAME using FILE in MAJOR-MODE. Expecting result EXPECTED-RESULT." (declare (indent 1)) `(ert-deftest ,test-name () - (with-current-buffer (find-file-noselect ,file) - (,major-mode) - (let* ((cognitive-complexity-results (cognitive-complexity-buffer)) - (total-score (car cognitive-complexity-results)) - (expressions-and-scores (cognitive-complexity-test-utils-expression-scores cognitive-complexity-results))) - (should (equal ,expected-result - (cons total-score - expressions-and-scores))))))) + (with-current-buffer (find-file-noselect ,file) + (,major-mode) + (treesit-langs-major-mode-setup) + (let* ((cognitive-complexity-results (cognitive-complexity-buffer)) + (total-score (car cognitive-complexity-results)) + (expressions-and-scores (cognitive-complexity-test-utils-expression-scores cognitive-complexity-results))) + (should (equal ,expected-result + (cons total-score + expressions-and-scores))))))) (defun cognitive-complexity-test-utils-expression-scores (analyze-result) "Helper method to get a list of simple expression + score pairs to work with." diff --git a/test/typescript-test.el b/test/typescript-test.el index 11b27ab..c1be022 100644 --- a/test/typescript-test.el +++ b/test/typescript-test.el @@ -21,11 +21,12 @@ ;; ;;; Code: + (require 'cognitive-complexity) (cognitive-complexity-test typescript-simple "test/typescript/Simple.ts" - typescript-ts-mode + typescript-mode '(1 ("class_declaration" . 0) ("method_definition" . 0)