branch: elpa/typescript-mode
commit 330c887241cf585aa4e2e3f3e9d851f28b92b08e
Author: Louis-Dominique Dubeau <[email protected]>
Commit: Louis-Dominique Dubeau <[email protected]>
Fix indentation of functions with numbers in return type.
Prior to this commit, a function with a return type consisting of
numbers would not be indented properly. This commit fixes the issue.
---
typescript-mode-tests.el | 17 +++++++++++++++++
typescript-mode.el | 20 ++++++++++++++++++++
2 files changed, 37 insertions(+)
diff --git a/typescript-mode-tests.el b/typescript-mode-tests.el
index 45c43a0a83..da8f9a3aa8 100644
--- a/typescript-mode-tests.el
+++ b/typescript-mode-tests.el
@@ -99,6 +99,23 @@ a severity set to WARNING, no rule name."
(should (string-equal (nth 4 matches) "1"))
(should (string-equal (nth 5 matches) "83"))))
+(ert-deftest typescript--number-literal-re-matches-numbers ()
+ "`typescript--number-literal-re' matches numbers."
+ (dolist (to-match '("NaN" "Infinity" "-Infinity" "-1" "1" "0.1" ".1" "-.1"
"8e23"
+ "9E-2" ".1e23" "0b1" "-0B1" "0o7" "-0O13" "0xaf"
"-0XAF"))
+ (should (string-match typescript--number-literal-re to-match))
+ ;; The regular expression does not begin with ^ and end with $ so
+ ;; we need to check ourselves that the whole string is matched.
+ (should (string-equal (match-string 0 to-match) to-match))))
+
+(ert-deftest typescript--number-literal-re-does-not-match-non-numbers ()
+ "`typescript--number-literal-re' does not match non-numbers."
+ (dolist (to-match '("NaNa" "Inf" "1." "." "0xPQ" "e" "2.3e2.4"))
+ ;; For the same reason as for the positive test above, what we want is
either no match
+ ;; or a match that fails to match the whole string.
+ (should-not (and (string-match typescript--number-literal-re to-match)
+ (string-equal (match-string 0 to-match) to-match)))))
+
(ert-deftest correctly-indents-lines-with-wide-chars ()
"Otsuka Ai and other multi-char users should be a happy to write typescript."
diff --git a/typescript-mode.el b/typescript-mode.el
index 880aafba6e..e342e3e7bf 100644
--- a/typescript-mode.el
+++ b/typescript-mode.el
@@ -1963,6 +1963,21 @@ This performs fontification according to
`typescript--class-styles'."
(concat "[-+*/%<>=&^|?:.]\\([^-+*/]\\|$\\)\\|" typescript--indent-keyword-re)
"Regexp matching operators that affect indentation of continued
expressions.")
+;;
+;; We purposely do not allow the plus symbol as a prefix here, as this
+;; regex is used to check number literal in type annotations, and TS
+;; does not allow to use a plus symbol to prefix numbers there: you
+;; can use 1, but not +1 in a type annotation.
+;;
+;; This is meant to match NaN, floats, decimals, the two infinities
+;; and numbers recorded in binary, octal and hex.
+;;
+;; This regular expression was derived from:
+;; https://stackoverflow.com/a/30987109/
+;;
+(defconst typescript--number-literal-re
+
"\\(?:NaN\\|-?\\(?:0[Bb][01]+\\|0[Oo][0-7]+\\|0[Xx][0-9a-fA-F]+\\|Infinity\\|\\(?:[[:digit:]]*\\.[[:digit:]]+\\|[[:digit:]]+\\)\\(?:[Ee][+-]?[[:digit:]]+\\)?\\)\\)"
+ "Regexp that matches number literals.")
(defun typescript--looking-at-operator-p ()
"Return non-nil if point is on a typescript operator, other than a comma."
@@ -2159,6 +2174,11 @@ moved on success."
(condition-case nil
(backward-sexp)
(scan-error nil)))
+ ((looking-back typescript--number-literal-re
+ ;; We limit the search back to the previous
space or end of line (if possible)
+ ;; to prevent the search from going over the
whole buffer.
+ (save-excursion (re-search-backward
"\\(?:\\s-\\|\n\\)" nil t)) t)
+ (goto-char (match-beginning 0)))
;; Otherwise, we failed to find a location.
(t
(cl-return-from search-loop nil)))))