This repository has been archived by the owner on Mar 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flycheck-swagger-tools.el
94 lines (78 loc) · 3.55 KB
/
flycheck-swagger-tools.el
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
;;; flycheck-swagger-tools.el --- Flycheck checker for swagger-tools.
;; Copyright (C) 2017-2018 Marc-André Goyette
;; Author: Marc-André Goyette <[email protected]>
;; URL: https://github.com/magoyette/flycheck-swagger-tools
;; Version: 0.1.0
;; Package-Requires: ((emacs "25"))
;; Keywords: languages
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; For a full copy of the GNU General Public License
;; see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; flycheck-swagger-tools provides a Flycheck checker for swagger-tools.
;; This allows to validate Swagger YAML and JSON files.
;; The checker can be activating by requiring this package.
;; (require 'flycheck-swagger-tools)
;; By default, only the first 4000 characters of a file are scanned to
;; find the swagger 2.0 element. To avoid stack overflow in Emacs
;; multi-line regex, this value is necessary. The defcustom
;; swagger-tools-predicate-regexp-match-limit can be used to change this
;; limit. That could be necessary for YAML files with long initial comments.
;;; Code:
(require 'flycheck)
(defgroup swagger-tools nil
"Validate swagger files with swagger-tools."
:group 'swagger
:prefix "swagger-tools-")
(defcustom swagger-tools-predicate-regexp-match-limit 4000
"Defines the number of characters that will be scanned at the beginning of a
buffer to find the swagger 2.0 element."
:type 'integer
:group 'swagger-tools)
;;;###autoload
(flycheck-define-checker swagger-tools
"A checker that uses swagger tools to validate Swagger2 JSON and YAML files.
See URL `https://github.com/apigee-127/swagger-tools'."
:command ("swagger-tools" "validate" source)
:predicate
(lambda ()
(string-match
"\\(.\\|\n\\)*\\([[:space:]]\\|\"\\|\\'\\)*swagger\\([[:space:]]\\|\"\\|\\'\\)*:[[:space:]]*[\"\\']2.0[\"\\'].*"
;; Need to avoid stack overflow for multi-line regex
(buffer-substring 1 (min (buffer-size)
swagger-tools-predicate-regexp-match-limit))))
:error-patterns
((warning line-start " " (message (one-or-more not-newline)
"is not used"
(one-or-more not-newline))
line-end)
;; js-yaml error with position
(error line-start " error: " (message) " at line " line ", column " column ":" line-end)
;; js-yaml error without position
(error line-start " error: " (message) ": " (one-or-more not-newline) line-end)
;; swagger-tools error (always contain a # symbol in the message)
(error line-start
" "
(message (optional (one-or-more not-newline))
(one-or-more "#")
(one-or-more not-newline))
line-end))
:error-filter
;; Add line number 1 if the error has no line number
(lambda (errors)
(let ((errors (flycheck-sanitize-errors errors)))
(dolist (err errors)
(unless (flycheck-error-line err)
(setf (flycheck-error-line err) 1)))
errors))
:modes (json-mode openapi-yaml-mode yaml-mode))
(add-to-list 'flycheck-checkers 'swagger-tools)
(provide 'flycheck-swagger-tools)
;;; flycheck-swagger-tools.el ends here