diff options
| author | Tom Tromey | 2017-02-05 11:40:18 -0700 |
|---|---|---|
| committer | Tom Tromey | 2017-02-11 12:30:32 -0700 |
| commit | 862d6438cfa6c6c035033697751f3d002357b024 (patch) | |
| tree | cc7eab8482c2fe5fabb37660d1d5a6353aefd159 | |
| parent | c2e19a73401eb37d5c13f6c8589dc1e5d706d239 (diff) | |
| download | emacs-862d6438cfa6c6c035033697751f3d002357b024.tar.gz emacs-862d6438cfa6c6c035033697751f3d002357b024.zip | |
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.
| -rw-r--r-- | lisp/progmodes/js.el | 43 | ||||
| -rw-r--r-- | test/lisp/progmodes/js-tests.el | 17 |
2 files changed, 42 insertions, 18 deletions
diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el index e42e01481b6..b42b2bca822 100644 --- a/lisp/progmodes/js.el +++ b/lisp/progmodes/js.el | |||
| @@ -1687,29 +1687,36 @@ This performs fontification according to `js--class-styles'." | |||
| 1687 | js--font-lock-keywords-3) | 1687 | js--font-lock-keywords-3) |
| 1688 | "Font lock keywords for `js-mode'. See `font-lock-keywords'.") | 1688 | "Font lock keywords for `js-mode'. See `font-lock-keywords'.") |
| 1689 | 1689 | ||
| 1690 | (defconst js--syntax-propertize-regexp-syntax-table | 1690 | (defconst js--syntax-propertize-regexp-regexp |
| 1691 | (let ((st (make-char-table 'syntax-table (string-to-syntax ".")))) | 1691 | (rx |
| 1692 | (modify-syntax-entry ?\[ "(]" st) | 1692 | ;; Start of regexp. |
| 1693 | (modify-syntax-entry ?\] ")[" st) | 1693 | "/" |
| 1694 | (modify-syntax-entry ?\\ "\\" st) | 1694 | (0+ (or |
| 1695 | st)) | 1695 | ;; Match characters outside of a character class. |
| 1696 | (not (any ?\[ ?/ ?\\)) | ||
| 1697 | ;; Match backslash quoted characters. | ||
| 1698 | (and "\\" not-newline) | ||
| 1699 | ;; Match character class. | ||
| 1700 | (and | ||
| 1701 | "[" | ||
| 1702 | (0+ (or | ||
| 1703 | (not (any ?\] ?\\)) | ||
| 1704 | (and "\\" not-newline))) | ||
| 1705 | "]"))) | ||
| 1706 | (group "/")) | ||
| 1707 | "Regular expression matching a JavaScript regexp literal.") | ||
| 1696 | 1708 | ||
| 1697 | (defun js-syntax-propertize-regexp (end) | 1709 | (defun js-syntax-propertize-regexp (end) |
| 1698 | (let ((ppss (syntax-ppss))) | 1710 | (let ((ppss (syntax-ppss))) |
| 1699 | (when (eq (nth 3 ppss) ?/) | 1711 | (when (eq (nth 3 ppss) ?/) |
| 1700 | ;; A /.../ regexp. | 1712 | ;; A /.../ regexp. |
| 1701 | (while | 1713 | (goto-char (nth 8 ppss)) |
| 1702 | (when (re-search-forward "\\(?:\\=\\|[^\\]\\)\\(?:\\\\\\\\\\)*/" | 1714 | (when (and (looking-at js--syntax-propertize-regexp-regexp) |
| 1703 | end 'move) | 1715 | ;; Don't touch text after END. |
| 1704 | (if (nth 1 (with-syntax-table | 1716 | (<= (match-end 1) end)) |
| 1705 | js--syntax-propertize-regexp-syntax-table | 1717 | (put-text-property (match-beginning 1) (match-end 1) |
| 1706 | (let ((parse-sexp-lookup-properties nil)) | 1718 | 'syntax-table (string-to-syntax "\"/")) |
| 1707 | (parse-partial-sexp (nth 8 ppss) (point))))) | 1719 | (goto-char (match-end 0)))))) |
| 1708 | ;; A / within a character class is not the end of a regexp. | ||
| 1709 | t | ||
| 1710 | (put-text-property (1- (point)) (point) | ||
| 1711 | 'syntax-table (string-to-syntax "\"/")) | ||
| 1712 | nil)))))) | ||
| 1713 | 1720 | ||
| 1714 | (defun js-syntax-propertize (start end) | 1721 | (defun js-syntax-propertize (start end) |
| 1715 | ;; JavaScript allows immediate regular expression objects, written /.../. | 1722 | ;; JavaScript allows immediate regular expression objects, written /.../. |
diff --git a/test/lisp/progmodes/js-tests.el b/test/lisp/progmodes/js-tests.el index 7cb737c30e2..d61f084e0df 100644 --- a/test/lisp/progmodes/js-tests.el +++ b/test/lisp/progmodes/js-tests.el | |||
| @@ -23,6 +23,7 @@ | |||
| 23 | 23 | ||
| 24 | (require 'ert) | 24 | (require 'ert) |
| 25 | (require 'js) | 25 | (require 'js) |
| 26 | (require 'syntax) | ||
| 26 | 27 | ||
| 27 | (ert-deftest js-mode-fill-bug-19399 () | 28 | (ert-deftest js-mode-fill-bug-19399 () |
| 28 | (with-temp-buffer | 29 | (with-temp-buffer |
| @@ -99,6 +100,22 @@ if (!/[ (:,='\"]/.test(value)) { | |||
| 99 | (forward-line) | 100 | (forward-line) |
| 100 | (should (looking-at " \\* test")))) | 101 | (should (looking-at " \\* test")))) |
| 101 | 102 | ||
| 103 | (ert-deftest js-mode-regexp-syntax-bug-25529 () | ||
| 104 | (dolist (regexp-contents '("[^[]" | ||
| 105 | "[/]" | ||
| 106 | ;; A comment with the regexp on the next | ||
| 107 | ;; line. | ||
| 108 | "*comment*/\n/regexp")) | ||
| 109 | (with-temp-buffer | ||
| 110 | (js-mode) | ||
| 111 | (insert "let x = /" regexp-contents "/;\n") | ||
| 112 | (save-excursion (insert "something();\n")) | ||
| 113 | ;; The failure mode was that the regexp literal was not | ||
| 114 | ;; recognized, causing the next line to be given string syntax; | ||
| 115 | ;; but check for comment syntax as well to prevent an | ||
| 116 | ;; implementation not recognizing the comment example. | ||
| 117 | (should-not (syntax-ppss-context (syntax-ppss)))))) | ||
| 118 | |||
| 102 | (provide 'js-tests) | 119 | (provide 'js-tests) |
| 103 | 120 | ||
| 104 | ;;; js-tests.el ends here | 121 | ;;; js-tests.el ends here |