[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Emacs-diffs] master 7a9dac5 22/28: Improve whitespace and unary keyword
From: |
Jackson Ray Hamilton |
Subject: |
[Emacs-diffs] master 7a9dac5 22/28: Improve whitespace and unary keyword parsing |
Date: |
Tue, 9 Apr 2019 02:00:15 -0400 (EDT) |
branch: master
commit 7a9dac5c944432cc2329473bb1dd9db9c0bfdd99
Author: Jackson Ray Hamilton <address@hidden>
Commit: Jackson Ray Hamilton <address@hidden>
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.
---
lisp/progmodes/js.el | 19 +++++++++++--------
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 21e6b68..e42c455 100644
--- a/lisp/progmodes/js.el
+++ b/lisp/progmodes/js.el
@@ -65,10 +65,7 @@
;;; Constants
-(defconst js--name-start-chars "a-zA-Z_$"
- "Character class chars matching the start of a JavaScript identifier.")
-
-(defconst js--name-start-re (concat "[" js--name-start-chars "]")
+(defconst js--name-start-re (concat "[a-zA-Z_$]")
"Regexp matching the start of a JavaScript identifier, without grouping.")
(defconst js--stmt-delim-chars "^;{}?:")
@@ -1907,7 +1904,12 @@ For use by `syntax-propertize-extend-region-functions'."
(if new-start (cons new-start end))))
(defconst js-jsx--tag-start-re
- (concat js--dotted-name-re "\\s-*[" js--name-start-chars "{/>]")
+ (concat "\\(" js--dotted-name-re "\\)\\(?:"
+ ;; Whitespace is only necessary if an attribute implies JSX.
+ "\\(?:\\s-\\|\n\\)*[{/>]"
+ "\\|"
+ "\\(?:\\s-\\|\n\\)+" js--name-start-re
+ "\\)")
"Regexp unambiguously matching a JSXOpeningElement.")
(defun js-jsx--matched-tag-type ()
@@ -1918,11 +1920,12 @@ else return `other'."
(cond
((= (char-after) ?/) (forward-char) 'close) ;
JSXClosingElement/JSXClosingFragment
((= (char-after) ?>) (forward-char) 'other) ; JSXOpeningFragment
- ((looking-at js-jsx--tag-start-re) ; JSXOpeningElement
+ ((and (looking-at js-jsx--tag-start-re) ; JSXOpeningElement
+ (not (js--unary-keyword-p (match-string 1))))
(goto-char (match-end 0))
(if (= (char-before) ?/) 'self-closing 'other))))
-(defconst js-jsx--self-closing-re "/>"
+(defconst js-jsx--self-closing-re "/\\s-*>"
"Regexp matching the end of a self-closing JSXOpeningElement.")
(defun js-jsx--matching-close-tag-pos ()
@@ -1933,7 +1936,7 @@ JSXClosingFragment, skipping over any nested JSXElements
to find
the match. Return nil if a match can’t be found."
(let ((tag-stack 1) tag-pos type last-pos pos)
(catch 'stop
- (while (and (re-search-forward "<" nil t) (not (eobp)))
+ (while (and (re-search-forward "<\\s-*" nil t) (not (eobp)))
(when (setq tag-pos (match-beginning 0)
type (js-jsx--matched-tag-type))
(when last-pos
diff --git a/test/manual/indent/jsx-unclosed-2.jsx
b/test/manual/indent/jsx-unclosed-2.jsx
index 9d80a2e..be0a605 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
if (foo < await bar) void 0
while (await foo > bar) void 0
+<div>
+ {foo < await bar}
+</div>
+
// Allow unary keyword names as null-valued JSX attributes.
// (As if this will EVER happen…)
<Foo yield>
@@ -40,3 +44,15 @@ while (await foo > bar) void 0
// “-” may be used in a JSXAttribute’s name.
<Foo a-b-c=""
x-y-z="" />
+
+// Weird spaces should be tolerated.
+< div >
+ < div >
+ < div
+ attr=""
+ / >
+ < div
+ attr=""
+ / >
+ < / div>
+< / div >
- [Emacs-diffs] master 84b1cfb 15/28: Indent broken arrow function bodies as an N+1th arg, (continued)
- [Emacs-diffs] master 84b1cfb 15/28: Indent broken arrow function bodies as an N+1th arg, Jackson Ray Hamilton, 2019/04/09
- [Emacs-diffs] master 6f53576 05/28: Use js-jsx- prefix for functions and variables, Jackson Ray Hamilton, 2019/04/09
- [Emacs-diffs] master bf37078 12/28: Automatically detect JSX in JavaScript files, Jackson Ray Hamilton, 2019/04/09
- [Emacs-diffs] master cf416d9 28/28: Explain reasonings for JSX syntax support design decisions, Jackson Ray Hamilton, 2019/04/09
- [Emacs-diffs] master 9545519 26/28: Add open/close parenthesis syntax to “<” and “>” in JSX, Jackson Ray Hamilton, 2019/04/09
- [Emacs-diffs] master 7c3ffda 27/28: Move curly functions closer to where they’re used, Jackson Ray Hamilton, 2019/04/09
- [Emacs-diffs] master 1a1ef28 10/28: Indent JSX as parsed in a JS context, Jackson Ray Hamilton, 2019/04/09
- [Emacs-diffs] master 55c80d4 17/28: Indent expressions in JSXAttributes relative to the attribute’s name, Jackson Ray Hamilton, 2019/04/09
- [Emacs-diffs] master 18bbfc4 25/28: Permit non-ASCII identifiers in JS, Jackson Ray Hamilton, 2019/04/09
- [Emacs-diffs] master 98e36a3 21/28: Optimize js-jsx--enclosing-tag-pos, Jackson Ray Hamilton, 2019/04/09
- [Emacs-diffs] master 7a9dac5 22/28: Improve whitespace and unary keyword parsing,
Jackson Ray Hamilton <=
- [Emacs-diffs] master 8b92719 13/28: Improve JSX syntax propertization, Jackson Ray Hamilton, 2019/04/09
- [Emacs-diffs] master afec451 18/28: Split JSX indentation calculation into several functions, Jackson Ray Hamilton, 2019/04/09
- [Emacs-diffs] master e48306f 23/28: Properly set a dynamic, syntactic mode name, Jackson Ray Hamilton, 2019/04/09
- [Emacs-diffs] master 7b2e3c6 20/28: Optimize js-jsx--matching-close-tag-pos, Jackson Ray Hamilton, 2019/04/09