lang/javascript: detect preact import (for rjsx-mode)

This commit is contained in:
Henrik Lissner 2018-04-26 17:56:58 -04:00
parent 306c41e7b8
commit 37b2563dd1
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -47,14 +47,15 @@
:mode "components/.+\\.js$" :mode "components/.+\\.js$"
:init :init
(defun +javascript-jsx-file-p () (defun +javascript-jsx-file-p ()
"Detect React or preact imports early in the file."
(and buffer-file-name (and buffer-file-name
(string= (file-name-extension buffer-file-name) "js") (string= (file-name-extension buffer-file-name) "js")
(re-search-forward "\\(^\\s-*import React\\|\\( from \\|require(\\)[\"']react\\)" (re-search-forward "\\(^\\s-*import +\\(?:pr\\|R\\)eact\\|\\( from \\|require(\\)[\"']p?react\\)"
magic-mode-regexp-match-limit t) magic-mode-regexp-match-limit t)
(progn (goto-char (match-beginning 1)) (progn (goto-char (match-beginning 1))
(not (sp-point-in-string-or-comment))))) (not (sp-point-in-string-or-comment)))))
(push (cons #'+javascript-jsx-file-p 'rjsx-mode) magic-mode-alist) (push '(+javascript-jsx-file-p . rjsx-mode) magic-mode-alist)
:config :config
(set! :electric 'rjsx-mode :chars '(?\} ?\) ?. ?>)) (set! :electric 'rjsx-mode :chars '(?\} ?\) ?. ?>))
(add-hook! 'rjsx-mode-hook (add-hook! 'rjsx-mode-hook