diff options
| author | Jackson Ray Hamilton | 2019-04-07 14:36:47 -0700 |
|---|---|---|
| committer | Jackson Ray Hamilton | 2019-04-08 22:48:24 -0700 |
| commit | 7a9dac5c944432cc2329473bb1dd9db9c0bfdd99 (patch) | |
| tree | b331864efe2500c8e73423398fb5197152028802 | |
| parent | 98e36a3e31da10bf230743d285544305f730b60d (diff) | |
| download | emacs-7a9dac5c944432cc2329473bb1dd9db9c0bfdd99.tar.gz emacs-7a9dac5c944432cc2329473bb1dd9db9c0bfdd99.zip | |
Improve whitespace and unary keyword parsing
* lisp/progmodes/js.el (js--name-start-chars): Remove, adding these
chars back to js--name-start-re.
(js--name-start-re): Add chars back from js--name-start-chars.
(js-jsx--tag-start-re): Improve regexp to capture the tag name (so it
can be disambiguated from a unary keyword), to match newlines (which
are common in this spot), and to require at least one whitespace
character before the attribute name.
(js-jsx--matched-tag-type): Ensure the “tag name” isn’t possibly a
unary keyword.
(js-jsx--self-closing-re, js-jsx--matching-close-tag-pos): Allow
whitespace around “<” and “>”.
* test/manual/indent/jsx-unclosed-2.jsx: Add tests for unary keyword
and whitespace parsing.
| -rw-r--r-- | lisp/progmodes/js.el | 19 | ||||
| -rw-r--r-- | test/manual/indent/jsx-unclosed-2.jsx | 16 |
2 files changed, 27 insertions, 8 deletions
diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el index 21e6b683b78..e42c455c84c 100644 --- a/lisp/progmodes/js.el +++ b/lisp/progmodes/js.el | |||
| @@ -65,10 +65,7 @@ | |||
| 65 | 65 | ||
| 66 | ;;; Constants | 66 | ;;; Constants |
| 67 | 67 | ||
| 68 | (defconst js--name-start-chars "a-zA-Z_$" | 68 | (defconst js--name-start-re (concat "[a-zA-Z_$]") |
| 69 | "Character class chars matching the start of a JavaScript identifier.") | ||
| 70 | |||
| 71 | (defconst js--name-start-re (concat "[" js--name-start-chars "]") | ||
| 72 | "Regexp matching the start of a JavaScript identifier, without grouping.") | 69 | "Regexp matching the start of a JavaScript identifier, without grouping.") |
| 73 | 70 | ||
| 74 | (defconst js--stmt-delim-chars "^;{}?:") | 71 | (defconst js--stmt-delim-chars "^;{}?:") |
| @@ -1907,7 +1904,12 @@ For use by `syntax-propertize-extend-region-functions'." | |||
| 1907 | (if new-start (cons new-start end)))) | 1904 | (if new-start (cons new-start end)))) |
| 1908 | 1905 | ||
| 1909 | (defconst js-jsx--tag-start-re | 1906 | (defconst js-jsx--tag-start-re |
| 1910 | (concat js--dotted-name-re "\\s-*[" js--name-start-chars "{/>]") | 1907 | (concat "\\(" js--dotted-name-re "\\)\\(?:" |
| 1908 | ;; Whitespace is only necessary if an attribute implies JSX. | ||
| 1909 | "\\(?:\\s-\\|\n\\)*[{/>]" | ||
| 1910 | "\\|" | ||
| 1911 | "\\(?:\\s-\\|\n\\)+" js--name-start-re | ||
| 1912 | "\\)") | ||
| 1911 | "Regexp unambiguously matching a JSXOpeningElement.") | 1913 | "Regexp unambiguously matching a JSXOpeningElement.") |
| 1912 | 1914 | ||
| 1913 | (defun js-jsx--matched-tag-type () | 1915 | (defun js-jsx--matched-tag-type () |
| @@ -1918,11 +1920,12 @@ else return `other'." | |||
| 1918 | (cond | 1920 | (cond |
| 1919 | ((= (char-after) ?/) (forward-char) 'close) ; JSXClosingElement/JSXClosingFragment | 1921 | ((= (char-after) ?/) (forward-char) 'close) ; JSXClosingElement/JSXClosingFragment |
| 1920 | ((= (char-after) ?>) (forward-char) 'other) ; JSXOpeningFragment | 1922 | ((= (char-after) ?>) (forward-char) 'other) ; JSXOpeningFragment |
| 1921 | ((looking-at js-jsx--tag-start-re) ; JSXOpeningElement | 1923 | ((and (looking-at js-jsx--tag-start-re) ; JSXOpeningElement |
| 1924 | (not (js--unary-keyword-p (match-string 1)))) | ||
| 1922 | (goto-char (match-end 0)) | 1925 | (goto-char (match-end 0)) |
| 1923 | (if (= (char-before) ?/) 'self-closing 'other)))) | 1926 | (if (= (char-before) ?/) 'self-closing 'other)))) |
| 1924 | 1927 | ||
| 1925 | (defconst js-jsx--self-closing-re "/>" | 1928 | (defconst js-jsx--self-closing-re "/\\s-*>" |
| 1926 | "Regexp matching the end of a self-closing JSXOpeningElement.") | 1929 | "Regexp matching the end of a self-closing JSXOpeningElement.") |
| 1927 | 1930 | ||
| 1928 | (defun js-jsx--matching-close-tag-pos () | 1931 | (defun js-jsx--matching-close-tag-pos () |
| @@ -1933,7 +1936,7 @@ JSXClosingFragment, skipping over any nested JSXElements to find | |||
| 1933 | the match. Return nil if a match can’t be found." | 1936 | the match. Return nil if a match can’t be found." |
| 1934 | (let ((tag-stack 1) tag-pos type last-pos pos) | 1937 | (let ((tag-stack 1) tag-pos type last-pos pos) |
| 1935 | (catch 'stop | 1938 | (catch 'stop |
| 1936 | (while (and (re-search-forward "<" nil t) (not (eobp))) | 1939 | (while (and (re-search-forward "<\\s-*" nil t) (not (eobp))) |
| 1937 | (when (setq tag-pos (match-beginning 0) | 1940 | (when (setq tag-pos (match-beginning 0) |
| 1938 | type (js-jsx--matched-tag-type)) | 1941 | type (js-jsx--matched-tag-type)) |
| 1939 | (when last-pos | 1942 | (when last-pos |
diff --git a/test/manual/indent/jsx-unclosed-2.jsx b/test/manual/indent/jsx-unclosed-2.jsx index 9d80a2e9ae2..be0a605503f 100644 --- a/test/manual/indent/jsx-unclosed-2.jsx +++ b/test/manual/indent/jsx-unclosed-2.jsx | |||
| @@ -19,6 +19,10 @@ if (foo > bar) void 0 | |||
| 19 | if (foo < await bar) void 0 | 19 | if (foo < await bar) void 0 |
| 20 | while (await foo > bar) void 0 | 20 | while (await foo > bar) void 0 |
| 21 | 21 | ||
| 22 | <div> | ||
| 23 | {foo < await bar} | ||
| 24 | </div> | ||
| 25 | |||
| 22 | // Allow unary keyword names as null-valued JSX attributes. | 26 | // Allow unary keyword names as null-valued JSX attributes. |
| 23 | // (As if this will EVER happen…) | 27 | // (As if this will EVER happen…) |
| 24 | <Foo yield> | 28 | <Foo yield> |
| @@ -40,3 +44,15 @@ while (await foo > bar) void 0 | |||
| 40 | // “-” may be used in a JSXAttribute’s name. | 44 | // “-” may be used in a JSXAttribute’s name. |
| 41 | <Foo a-b-c="" | 45 | <Foo a-b-c="" |
| 42 | x-y-z="" /> | 46 | x-y-z="" /> |
| 47 | |||
| 48 | // Weird spaces should be tolerated. | ||
| 49 | < div > | ||
| 50 | < div > | ||
| 51 | < div | ||
| 52 | attr="" | ||
| 53 | / > | ||
| 54 | < div | ||
| 55 | attr="" | ||
| 56 | / > | ||
| 57 | < / div> | ||
| 58 | < / div > | ||