1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-15 10:30:25 -08:00

Recognize JS regexp literals more correctly

Bug#25529
* lisp/progmodes/js.el (js--syntax-propertize-regexp-regexp): New
constant.
(js-syntax-propertize-regexp): Use it.  Remove "end" argument.
(js--syntax-propertize-regexp-syntax-table): Remove.
(js-syntax-propertize): Update.
* test/lisp/progmodes/js-tests.el (js-mode-regexp-syntax-bug-25529):
New test.
This commit is contained in:
Tom Tromey 2017-02-05 11:40:18 -07:00
parent c2e19a7340
commit 862d6438cf
2 changed files with 42 additions and 18 deletions

View file

@ -1687,29 +1687,36 @@ This performs fontification according to `js--class-styles'."
js--font-lock-keywords-3)
"Font lock keywords for `js-mode'. See `font-lock-keywords'.")
(defconst js--syntax-propertize-regexp-syntax-table
(let ((st (make-char-table 'syntax-table (string-to-syntax "."))))
(modify-syntax-entry ?\[ "(]" st)
(modify-syntax-entry ?\] ")[" st)
(modify-syntax-entry ?\\ "\\" st)
st))
(defconst js--syntax-propertize-regexp-regexp
(rx
;; Start of regexp.
"/"
(0+ (or
;; Match characters outside of a character class.
(not (any ?\[ ?/ ?\\))
;; Match backslash quoted characters.
(and "\\" not-newline)
;; Match character class.
(and
"["
(0+ (or
(not (any ?\] ?\\))
(and "\\" not-newline)))
"]")))
(group "/"))
"Regular expression matching a JavaScript regexp literal.")
(defun js-syntax-propertize-regexp (end)
(let ((ppss (syntax-ppss)))
(when (eq (nth 3 ppss) ?/)
;; A /.../ regexp.
(while
(when (re-search-forward "\\(?:\\=\\|[^\\]\\)\\(?:\\\\\\\\\\)*/"
end 'move)
(if (nth 1 (with-syntax-table
js--syntax-propertize-regexp-syntax-table
(let ((parse-sexp-lookup-properties nil))
(parse-partial-sexp (nth 8 ppss) (point)))))
;; A / within a character class is not the end of a regexp.
t
(put-text-property (1- (point)) (point)
'syntax-table (string-to-syntax "\"/"))
nil))))))
(goto-char (nth 8 ppss))
(when (and (looking-at js--syntax-propertize-regexp-regexp)
;; Don't touch text after END.
(<= (match-end 1) end))
(put-text-property (match-beginning 1) (match-end 1)
'syntax-table (string-to-syntax "\"/"))
(goto-char (match-end 0))))))
(defun js-syntax-propertize (start end)
;; JavaScript allows immediate regular expression objects, written /.../.

View file

@ -23,6 +23,7 @@
(require 'ert)
(require 'js)
(require 'syntax)
(ert-deftest js-mode-fill-bug-19399 ()
(with-temp-buffer
@ -99,6 +100,22 @@ if (!/[ (:,='\"]/.test(value)) {
(forward-line)
(should (looking-at " \\* test"))))
(ert-deftest js-mode-regexp-syntax-bug-25529 ()
(dolist (regexp-contents '("[^[]"
"[/]"
;; A comment with the regexp on the next
;; line.
"*comment*/\n/regexp"))
(with-temp-buffer
(js-mode)
(insert "let x = /" regexp-contents "/;\n")
(save-excursion (insert "something();\n"))
;; The failure mode was that the regexp literal was not
;; recognized, causing the next line to be given string syntax;
;; but check for comment syntax as well to prevent an
;; implementation not recognizing the comment example.
(should-not (syntax-ppss-context (syntax-ppss))))))
(provide 'js-tests)
;;; js-tests.el ends here