aboutsummaryrefslogtreecommitdiffstats
path: root/modules/init-js-two.el
blob: 7162eb1fceb6a9a1c97111e769d725358e52ee49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
;;; init-js-two.el --- .Emacs Configuration -*- lexical-binding: t -*-
;;; Commentary:
;;; JavaScript configuration - IDE

;;; Code:

;; js2-mode: enhanced JavaScript editing mode
;; https://github.com/mooz/js2-mode
(use-package js2-mode
  :ensure t
  :pin "MELPA"
  :mode ("\\.js'\\'" . js2-mode)
  :hook (js2-mode . company-mode)
  :custom
  (js2-include-node-externs t)
  (js2-global-externs '("customElements"))
  (js2-highlight-level 3)
  (js2r-prefer-let-over-var t)
  (js2r-preferred-quote-type 2)
  (js-indent-align-list-continuation t)
  :config
  (setq-default flycheck-javascript-eslint-executable "eslint_d")
  (setq js-indent-level 2)
  (advice-add #'js2-identifier-start-p
              :after-until
              (lambda (c) (eq c ?#))))

;; js2-refactor: refactoring options for emacs
;; https://github.com/magnars/js2-refactor.el
(use-package js2-refactor
  :pin "MELPA"
  :after js2-mode
  :hook (js2-mode . js2-refactor-mode)
  :config
  ;; Add keybindings for js2-refactor with prefix "C-c C-r".
  (js2r-add-keybindings-with-prefix "C-c C-r")
  ;; Assign keybindings to kill an element in js2 and to open the js2-refactor's hydra menu.
  (bind-key "C-k" 'js2r-kill js2-mode-map)
  (bind-key "C-c h r" 'js2-refactor-hydra/body js2-mode-map)

  (defhydra js2-refactor-hydra (:color blue :hint nil)
    "
^Functions^                    ^Variables^               ^Buffer^                      ^sexp^               ^Debugging^
------------------------------------------------------------------------------------------------------------------------------
[_lp_] Localize Parameter      [_ev_] Extract variable   [_wi_] Wrap buffer in IIFE    [_k_]  js2 kill      [_lt_] log this
[_ef_] Extract function        [_iv_] Inline variable    [_ig_] Inject global in IIFE  [_ss_] split string  [_dt_] debug this
[_ip_] Introduce parameter     [_rv_] Rename variable    [_ee_] Expand node at point   [_sl_] forward slurp
[_em_] Extract method          [_vt_] Var to this        [_cc_] Contract node at point [_ba_] forward barf
[_ao_] Arguments to object     [_sv_] Split var decl.    [_uw_] unwrap
[_tf_] Toggle fun exp and decl [_ag_] Add var to globals
[_ta_] Toggle fun expr and =>  [_ti_] Ternary to if
[_q_]  quit"
    ("ee" js2r-expand-node-at-point)
    ("cc" js2r-contract-node-at-point)
    ("ef" js2r-extract-function)
    ("em" js2r-extract-method)
    ("tf" js2r-toggle-function-expression-and-declaration)
    ("ta" js2r-toggle-arrow-function-and-expression)
    ("ip" js2r-introduce-parameter)
    ("lp" js2r-localize-parameter)
    ("wi" js2r-wrap-buffer-in-iife)
    ("ig" js2r-inject-global-in-iife)
    ("ag" js2r-add-to-globals-annotation)
    ("ev" js2r-extract-var)
    ("iv" js2r-inline-var)
    ("rv" js2r-rename-var)
    ("vt" js2r-var-to-this)
    ("ao" js2r-arguments-to-object)
    ("ti" js2r-ternary-to-if)
    ("sv" js2r-split-var-declaration)
    ("ss" js2r-split-string)
    ("uw" js2r-unwrap)
    ("lt" js2r-log-this)
    ("dt" js2r-debug-this)
    ("sl" js2r-forward-slurp)
    ("ba" js2r-forward-barf)
    ("k" js2r-kill)
    ("q" nil)))

;; json-mode: Major mode for editing JSON files with emacs
;; https://github.com/joshwnj/json-mode
(use-package json-mode
  :ensure t
  :pin "MELPA"
  :mode ("\\.json\\'" "\\.jsonld\\'")
  :custom
  (json-reformat:indent-width 2)
  (json-reformat:pretty-string? t)
  (js-indent-level 2)
  :hook
  (json-mode . (lambda ()
                 (add-hook 'before-save-hook 'json-mode-before-save nil t)
                 (prettier-js-mode)))
  :bind
  (:map json-mode-map
        ("C-c <tab>" . json-mode-beautify)))

(use-package prettier-js
  :pin "MELPA"
  :hook
  ((js-mode typescript-mode css-mode scss-mode less-mode web-mode json-mode) . prettier-js-mode))

;; eslintd-fix: Emacs minor-mode to automatically fix javascript with eslint_d.
;; https://github.com/aaronjensen/eslintd-fix/tree/master
(use-package eslintd-fix)

(provide 'init-js-two)
;; Local Variables:
;; byte-compile-warnings: (not free-vars)
;; End:
;;; init-js-two.el ends here