Files
doomemacs/modules/lang/javascript/config.el
Henrik Lissner bd0bee92cc feat!(javascript): add treesit support
BREAKING CHANGE: This commit removes a number of core packages and
features from this module and only replaces a handful of them, so that
we can lean more on LSP and tree-sitter. To be specific:

- We used to rely on `rjsx-mode` (derived from js2-mode) for total
  JS/JSX support (though imperfect; Emacs was starved for options at the
  time). This has now been replaced with `js-ts-mode` (built-in after
  Emacs 29), falling back to `js-mode` (very rudimentary, but a decent
  fallback).
- This also meant the removal of `js2-mode`, which `skewer-mode`,
  `js2-refactor`, and `xref-js2` depended on, so those were removed
  too, and have *somewhat* been replaced with LSP integration (offers
  jump-to-definition/references and *some* refactoring actions, but no
  replacement for skewer's functionality).
- Typescript support no longer relies on the jury-rigged, web-mode-derived
  major mode (because TSX support in the upstream `typescript-mode`
  isn't great). We now use `typescript-ts-mode` (built-in into Emacs
  29.1+), falling back to `typescript-mode`.
- JSX/TSX support now *requires* tree-sitter (and Emacs 29.1+), where
  `tsx-ts-mode` is available and outshines all the alternatives (at the
  time of writing).

Due to the absolute chaos that is webdev, this module sacrifices some of
the graceful-degradation I've implemented for other modules and creates
a hard requirement on tree-sitter and Emacs 29.1+ for JSX/TSX. It still
tries to degrade gracefully for plain JS and TS, but the module's doctor
and docs will actively recommend tree-sitter.

Close: #5278
Fix: #6172
Fix: #7042
Close: #8447
Co-authored-by: ribaricplusplus <ribaricplusplus@users.noreply.github.com>
2025-08-29 14:35:49 +02:00

125 lines
3.2 KiB
EmacsLisp

;;; lang/javascript/config.el -*- lexical-binding: t; -*-
(after! projectile
(pushnew! projectile-project-root-files "package.json")
(pushnew! projectile-globally-ignored-directories "node_modules" "flow-typed"))
;;
;;; Major modes
(defun +javascript-common-config (mode)
(unless (eq mode 'nodejs-repl-mode)
(set-repl-handler! mode #'+javascript/open-repl)
(set-electric! mode :chars '(?\} ?\) ?. ?:))
(set-ligatures! mode
;; Functional
:def "function"
:lambda "() =>"
:composition "compose"
;; Types
:null "null"
:true "true" :false "false"
;; Flow
:not "!"
:and "&&" :or "||"
:for "for"
:return "return"
;; Other
:yield "import")
(when (modulep! +lsp)
(add-hook (intern (format "%s-local-vars-hook" mode)) #'lsp! 'append)))
(pcase mode
((or 'js-mode 'js-ts-mode 'nodejs-repl-mode)
(set-docsets! mode "JavaScript"
"AngularJS" "Backbone" "BackboneJS" "Bootstrap" "D3JS" "EmberJS" "Express"
"ExtJS" "JQuery" "JQuery_Mobile" "JQuery_UI" "KnockoutJS" "Lo-Dash"
"MarionetteJS" "MomentJS" "NodeJS" "PrototypeJS" "React" "RequireJS"
"SailsJS" "UnderscoreJS" "VueJS" "ZeptoJS"))
((or 'typescript-mode 'typescript-ts-mode)
(set-docsets! mode :add "TypeScript" "AngularTS")
(set-electric! mode :chars '(?\} ?\)) :words '("||" "&&")))))
(use-package! js-mode
:mode "\\.[mc]?js\\'"
:mode "\\.es6\\'"
:mode "\\.pac\\'"
:config
(setq js-chain-indent t)
(+javascript-common-config 'js-mode))
(use-package! js-ts-mode
:when (modulep! +tree-sitter)
:when (fboundp 'js-ts-mode) ; 29.1+ only
:defer t
:init
(set-tree-sitter! 'js-mode 'js-ts-mode 'javascript)
(+javascript-common-config 'js-ts-mode))
(use-package! typescript-mode
:unless (modulep! +tree-sitter)
:mode "\\.ts\\'"
:config
(+javascript-common-config 'typescript-mode))
(use-package! typescript-ts-mode
:when (modulep! +tree-sitter)
:when (fboundp 'typescript-ts-mode) ; 29.1+ only
:mode "\\.ts\\'"
:init
(set-tree-sitter! 'typescript-mode 'typescript-ts-mode 'typescript)
:config
(+javascript-common-config 'typescript-ts-mode))
(use-package! tsx-ts-mode
:when (modulep! +tree-sitter)
:when (fboundp 'tsx-ts-mode) ; 29.1+ only
:mode "\\.[tj]sx\\'"
:defer t
:config
(+javascript-common-config 'tsx-ts-mode))
;;
;;; Extensions
;; Parse node stack traces in the compilation buffer
(after! compilation
(add-to-list 'compilation-error-regexp-alist 'node)
(add-to-list 'compilation-error-regexp-alist-alist
'(node "^[[:blank:]]*at \\(.*(\\|\\)\\(.+?\\):\\([[:digit:]]+\\):\\([[:digit:]]+\\)"
2 3 4)))
(use-package! nodejs-repl
:defer t
:init
:config
(+javascript-common-config 'nodejs-repl-mode))
;;
;;; Projects
(def-project-mode! +javascript-npm-mode
:modes '(html-mode
css-mode
web-mode
markdown-mode
js-mode ; includes js2-mode and rjsx-mode
json-mode
typescript-mode
solidity-mode)
:when (locate-dominating-file default-directory "package.json")
:add-hooks '(+javascript-add-npm-path-h npm-mode))
(def-project-mode! +javascript-gulp-mode
:when (locate-dominating-file default-directory "gulpfile.js"))