Skip to content

Instantly share code, notes, and snippets.

@mykhas
Created March 29, 2020 21:17
Show Gist options
  • Save mykhas/60d2469f494470dfe3ebd209a701b2db to your computer and use it in GitHub Desktop.
Save mykhas/60d2469f494470dfe3ebd209a701b2db to your computer and use it in GitHub Desktop.
;;; tern-lint.el --- JavaScript type-checker using Tern-lint and Flycheck.
;;; -*- lexical-binding: t -*-
;; Author: m.kobernyk@gmail.com
;; Author: katspaugh
;; Keywords: tools
;; URL: https://github.com/katspaugh/tern-lint.el
;; Version: 0.0.21
;; Package-Requires: ((emacs "26") (flycheck "2020.*") (tern "*"))
;;; Commentary:
;; Older version the tern-lint integration, developed
;; by katspaugh (see https://gist.github.com/katspaugh/7d646074e547cf59dd6c)
;; is not supported by modern flycheck/emacs. As a skeleton for the
;; `flycheck-parse-tern-lint` function was used flycheck's
;; `flycheck-parse-tslint`.
;; 1) Install the tern.lint plugin. See
;; https://github.com/angelozerr/tern-lint.
;; 2) Enable the plugin in your .tern-project.
;; 3) Require this file in your .emacs.d.
;; 4) Run `flycheck-tern-lint-setup' when `tern' loads:
;; (require 'tern-lint)
;; (with-eval-after-load 'tern
;; (flycheck-tern-lint-setup))
;;; Code:
(require 'tern)
(require 'flycheck)
(require 'json)
(defun flycheck-parse-tern-lint (output checker buffer)
"Parse `tern-lint` errors from JSON OUTPUT.
CHECKER and BUFFER denoted the CHECKER that returned OUTPUT and
the BUFFER that was checked respectively."
(let ((json-array-type 'list))
(seq-map (lambda (message)
(let-alist message
(flycheck-error-new-at-pos
(+ 1 .from)
(pcase .severity
("error" 'error)
("warning" 'warning)
(_ 'warning))
.message
:id .ruleName
:checker checker
:buffer buffer
:filename .file
:end-pos (+ 1 .to))))
;; Don't try to parse empty output as JSON
(and (not (string-empty-p output))
(cdr (car (car (flycheck-parse-json output))))))))
(flycheck-define-checker tern-lint
"A type checker for JavaScript using Tern-lint."
:command ("tern-lint" source)
:error-parser flycheck-parse-tern-lint
:modes (js2-mode js-mode tern-mode))
;;;###autoload
(defun flycheck-tern-lint-setup ()
"Enable the tern-lint checker."
(interactive)
(message "in flycheck-tern-lint-setup")
(add-to-list 'flycheck-checkers 'tern-lint))
(provide 'tern-lint)
;;; tern-lint ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment