Skip to content

Commit d79551c

Browse files
authored
Merge pull request #176 from Fuco1/bugfix/fix-method-fontification
fix(fontlock): fix special forms/ternary mistaken for method declarations
2 parents d1123e0 + b99d893 commit d79551c

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

typescript-mode-general-tests.el

+16
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,22 @@ bbb: Bar,
846846
(should (eq (get-face-at "aaa") 'font-lock-variable-name-face))
847847
(should (eq (get-face-at "bbb") 'font-lock-variable-name-face))))
848848

849+
(ert-deftest font-lock/funargs--method--no-fontification-in-ternary ()
850+
"Do not apply fontification on a function call inside a ternary
851+
operator, which might look like method with return type
852+
declaration."
853+
(test-with-fontified-buffer
854+
"true ? funcall(helloWorld) : false"
855+
(should (eq (get-face-at "helloWorld") nil))))
856+
857+
(ert-deftest font-lock/funargs--method--no-fontification-in-special-form ()
858+
"Do not apply fontification inside a special form paren-form,
859+
such as inside of if/while/switch etc. These look like method
860+
declarations without a return type annotation but are not."
861+
(test-with-fontified-buffer
862+
"if (hello && world) { }"
863+
(should (eq (get-face-at "world") nil))))
864+
849865
(defun flyspell-predicate-test (search-for)
850866
"This function runs a test on
851867
`typescript--flyspell-mode-predicate'. `SEARCH-FOR' is a string

typescript-mode.el

+28-2
Original file line numberDiff line numberDiff line change
@@ -1974,8 +1974,34 @@ and searches for the next token to be highlighted."
19741974
(is-method-def
19751975
(ignore-errors
19761976
(up-list)
1977-
(looking-at-p
1978-
(rx (* (or whitespace ?\n)) (or ":" "{"))))))
1977+
(and
1978+
(or
1979+
;; After the "argument list" is a bracket, this is
1980+
;; either a special form (if, while...) or a method
1981+
;; declaration.
1982+
(looking-at-p (rx (* (or whitespace ?\n)) "{"))
1983+
;; After the "argument list" is a colon, this is
1984+
;; either a method declaration with a return type
1985+
;; annotation or ternary form. We need to discard
1986+
;; the ternary form case.
1987+
(and
1988+
(looking-at-p (rx (* (or whitespace ?\n)) ":"))
1989+
(save-excursion
1990+
(backward-sexp 2)
1991+
(skip-syntax-backward " >")
1992+
(not (eq (char-before) ??)))))
1993+
;; HACK: here we check the fontification of
1994+
;; the "function name". Because the keywords
1995+
;; are fontified before this check runs, a
1996+
;; keyword would already have been fontified
1997+
;; and therefore we can conclude it is not a
1998+
;; function/method definition.
1999+
(save-excursion
2000+
(backward-sexp)
2001+
(backward-word)
2002+
(not (memq
2003+
'font-lock-keyword-face
2004+
(face-at-point nil t))))))))
19792005
(if is-method-def
19802006
(prog1 (point) (goto-char point-orig))
19812007
(point)))

0 commit comments

Comments
 (0)