From aa5bb16d66b1c2b42a773245710c85c99deb3417 Mon Sep 17 00:00:00 2001 From: Konstantin Kharlamov Date: Mon, 10 Feb 2025 23:18:51 +0300 Subject: [PATCH 1/3] Remove `qualified` keyword from sorting imports Such keyword isn't a thing in Purescript, it has instead syntax `import Foo as Bar`. I also checked VSCode purescript highlight mode, it also doesn't have `qualified` in among its sources. --- purescript-sort-imports.el | 8 +++----- tests/purescript-sort-imports-tests.el | 16 ++++++++-------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/purescript-sort-imports.el b/purescript-sort-imports.el index c635ada..741ef04 100644 --- a/purescript-sort-imports.el +++ b/purescript-sort-imports.el @@ -32,8 +32,7 @@ (defvar purescript-sort-imports-regexp (concat "^import[ ]+" - "\\(qualified \\)?" - "[ ]*\\(\"[^\"]*\" \\)?" + "\\(\"[^\"]*\" \\)?" "[ ]*\\([A-Za-z0-9_.']*.*\\)")) ;;;###autoload @@ -68,9 +67,8 @@ within that region." (defun purescript-sort-imports-normalize (i) "Normalize an import, if possible, so that it can be sorted." - (if (string-match purescript-sort-imports-regexp - i) - (match-string 3 i) + (if (string-match purescript-sort-imports-regexp i) + (match-string 2 i) i)) (defun purescript-sort-imports-collect-imports () diff --git a/tests/purescript-sort-imports-tests.el b/tests/purescript-sort-imports-tests.el index e24fec1..e8025e2 100644 --- a/tests/purescript-sort-imports-tests.el +++ b/tests/purescript-sort-imports-tests.el @@ -45,23 +45,23 @@ import B import B "))) (should (with-temp-buffer - (insert "import qualified A + (insert "import A (A, B, C) import B ") (goto-char (point-min)) (purescript-sort-imports) (string= (buffer-string) - "import qualified A + "import A (A, B, C) import B "))) (should (with-temp-buffer - (insert "import qualified \"mtl\" A + (insert "import A (mtl) import B ") (goto-char (point-min)) (purescript-sort-imports) (string= (buffer-string) - "import qualified \"mtl\" A + "import A (mtl) import B ")))) @@ -116,8 +116,8 @@ import Data.Aeson.Types import Data.Aeson.Parser.Internal (decodeWith, decodeStrictWith, eitherDecodeWith, eitherDecodeStrictWith, jsonEOF, json, jsonEOF', json') -import qualified Data.ByteString as B -import qualified Data.ByteString.Lazy as L +import Data.ByteString as B +import Data.ByteString.Lazy as L ") (goto-char (point-min)) (purescript-sort-imports) @@ -127,8 +127,8 @@ import Data.Aeson.Parser.Internal (decodeWith, decodeStrictWith, eitherDecodeWith, eitherDecodeStrictWith, jsonEOF, json, jsonEOF', json') import Data.Aeson.Types -import qualified Data.ByteString as B -import qualified Data.ByteString.Lazy as L +import Data.ByteString as B +import Data.ByteString.Lazy as L ")))) (provide 'purescript-sort-imports-tests) From 0f832dadaa991904a4e2e77480ec748656cb21c6 Mon Sep 17 00:00:00 2001 From: Konstantin Kharlamov Date: Mon, 10 Feb 2025 23:33:30 +0300 Subject: [PATCH 2/3] tests: only use `should` for specific assertions This improves debuggability of fails, because tests would only show the part that failed. There's no reason to wrap full code blocks to `should` because their execution may only exit past `should` if exception is thrown, which would be caught by `ert` anyway. --- tests/purescript-sort-imports-tests.el | 94 +++++++++++++------------- 1 file changed, 47 insertions(+), 47 deletions(-) diff --git a/tests/purescript-sort-imports-tests.el b/tests/purescript-sort-imports-tests.el index e8025e2..fd5de6b 100644 --- a/tests/purescript-sort-imports-tests.el +++ b/tests/purescript-sort-imports-tests.el @@ -21,88 +21,88 @@ (require 'purescript-sort-imports) (ert-deftest empty-buffer () - (should (with-temp-buffer - (purescript-sort-imports) - t))) + (with-temp-buffer + (purescript-sort-imports) + t)) (ert-deftest single-line () - (should (with-temp-buffer - (insert "import A\n") - (goto-char (point-min)) - (purescript-sort-imports) - (string= (buffer-string) + (with-temp-buffer + (insert "import A\n") + (goto-char (point-min)) + (purescript-sort-imports) + (should (string= (buffer-string) "import A\n")))) (ert-deftest two-idem () - (should (with-temp-buffer - (insert "import A + (with-temp-buffer + (insert "import A import B ") - (goto-char (point-min)) - (purescript-sort-imports) - (string= (buffer-string) + (goto-char (point-min)) + (purescript-sort-imports) + (should (string= (buffer-string) "import A import B "))) - (should (with-temp-buffer - (insert "import A (A, B, C) + (with-temp-buffer + (insert "import A (A, B, C) import B ") - (goto-char (point-min)) - (purescript-sort-imports) - (string= (buffer-string) + (goto-char (point-min)) + (purescript-sort-imports) + (should (string= (buffer-string) "import A (A, B, C) import B "))) - (should (with-temp-buffer - (insert "import A (mtl) + (with-temp-buffer + (insert "import A (mtl) import B ") - (goto-char (point-min)) - (purescript-sort-imports) - (string= (buffer-string) + (goto-char (point-min)) + (purescript-sort-imports) + (should (string= (buffer-string) "import A (mtl) import B ")))) (ert-deftest two-rev () - (should (with-temp-buffer - (insert "import B + (with-temp-buffer + (insert "import B import A ") - (goto-char (point-min)) - (purescript-sort-imports) - (string= (buffer-string) + (goto-char (point-min)) + (purescript-sort-imports) + (should (string= (buffer-string) "import A import B ")))) (ert-deftest file-structure () - (should (with-temp-buffer - (insert "module A where + (with-temp-buffer + (insert "module A where import B import A ") - ;; test at line 2 - (goto-char (point-min)) - (forward-line 1) - (purescript-sort-imports) - (string= (buffer-string) + ;; test at line 2 + (goto-char (point-min)) + (forward-line 1) + (purescript-sort-imports) + (should (string= (buffer-string) "module A where import A import B "))) - (should (with-temp-buffer - (insert "module C where + (with-temp-buffer + (insert "module C where import B import A ") - ;; test at line 3 - (goto-char (point-min)) - (forward-line 2) - (purescript-sort-imports) - (string= (buffer-string) + ;; test at line 3 + (goto-char (point-min)) + (forward-line 2) + (purescript-sort-imports) + (should (string= (buffer-string) "module C where import A @@ -110,8 +110,8 @@ import B ")))) (ert-deftest bos-270 () - (should (with-temp-buffer - (insert "import Data.Aeson.Encode (encode) + (with-temp-buffer + (insert "import Data.Aeson.Encode (encode) import Data.Aeson.Types import Data.Aeson.Parser.Internal (decodeWith, decodeStrictWith, eitherDecodeWith, eitherDecodeStrictWith, @@ -119,9 +119,9 @@ import Data.Aeson.Parser.Internal (decodeWith, decodeStrictWith, import Data.ByteString as B import Data.ByteString.Lazy as L ") - (goto-char (point-min)) - (purescript-sort-imports) - (string= (buffer-string) + (goto-char (point-min)) + (purescript-sort-imports) + (should (string= (buffer-string) "import Data.Aeson.Encode (encode) import Data.Aeson.Parser.Internal (decodeWith, decodeStrictWith, eitherDecodeWith, eitherDecodeStrictWith, From 4c2d1883c2a936edb1a704dc5570315e76a27ef6 Mon Sep 17 00:00:00 2001 From: Konstantin Kharlamov Date: Mon, 10 Feb 2025 23:43:03 +0300 Subject: [PATCH 3/3] Don't "autoload" purescript-sort-imports The function will be brought into the scope by purescript-mode by virtue of having a `(require 'purescript-sort-imports)`. No point additionally declaring it as autoload. --- purescript-sort-imports.el | 1 - 1 file changed, 1 deletion(-) diff --git a/purescript-sort-imports.el b/purescript-sort-imports.el index 741ef04..796eee8 100644 --- a/purescript-sort-imports.el +++ b/purescript-sort-imports.el @@ -35,7 +35,6 @@ "\\(\"[^\"]*\" \\)?" "[ ]*\\([A-Za-z0-9_.']*.*\\)")) -;;;###autoload (defun purescript-sort-imports () "Sort the import list at point. It sorts the current group i.e. an import list separated by blank lines on either side.