Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d14acf9

Browse files
landakramLefterisJP
authored andcommitted
Support project root and additional allow-paths
solidity-flycheck can now detect projects. Project roots: When `solidity-flycheck-use-project' is t, solidity-flycheck will detect a project and include files in the project during compilation and linting. solidity-flycheck will look for a .soliumrc.json in a parent directory. If found, this directory is considered the project root. If no .soliumrc.json is found, `project-roots' is used. When `solidity-flycheck-solium-checker-active' is t, the .soliumrc.json found in the project root will be used as the solium config, rather than a .soliumrc.json in the same directory as the file being linted. When `solidity-flycheck-solc-checker-active' is t, the project root will be passed to solc using the --allow-paths flag. This means imports to other files inside the project will lint without erorr." Additional allow-paths: The custom variable `solidity-flycheck-solc-additional-allow-paths' can be set to support additional allow-paths. This lets you import .sol files from other directories without causing linting errors. For example, say that you use Brownie (URL `https://github.com/eth-brownie/brownie'), which stores ethPM packages in \"~/.brownie/packages\". You could add \"~/.brownie/packages\" to this variable to import ethPM packages without linting errors. Subdirectories in each allow path are remapped. Note that when `solidity-flycheck-use-project' is t, the project root will be added to --allow-paths in addition to any paths defined here.
1 parent b4fd719 commit d14acf9

File tree

2 files changed

+99
-6
lines changed

2 files changed

+99
-6
lines changed

solidity-flycheck.el

Lines changed: 98 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
;; Author: Lefteris Karapetsas <[email protected]>
66
;; Keywords: languages, solidity, flycheck
7-
;; Version: 0.1.10
8-
;; Package-Requires: ((flycheck "32-cvs") (solidity-mode "0.1.9"))
7+
;; Version: 0.1.11
8+
;; Package-Requires: ((flycheck "32-cvs") (solidity-mode "0.1.9") (dash "2.17.0"))
99

1010
;; This program is free software; you can redistribute it and/or modify
1111
;; it under the terms of the GNU General Public License as published by
@@ -29,6 +29,7 @@
2929

3030
(require 'flycheck)
3131
(require 'solidity-common)
32+
(require 'dash)
3233

3334
(defvar flycheck-solidity-checker-executable)
3435
(defvar flycheck-solium-checker-executable)
@@ -86,6 +87,55 @@ we pass the directory to solium via the `--config' option."
8687
:safe #'stringp
8788
:package-version '(solidity-mode . "0.1.4"))
8889

90+
(defcustom solidity-flycheck-solc-additional-allow-paths nil
91+
"A list of paths that should be passed to solc using the --allow-paths option.
92+
93+
This lets you import .sol files from other directories without causing linting errors.
94+
For example, say that you use Brownie (URL `https://github.com/eth-brownie/brownie'),
95+
which stores ethPM packages in \"~/.brownie/packages\". You could add \"~/.brownie/packages\"
96+
to this variable to import ethPM packages without linting errors. Subdirectories
97+
in each allow path are remapped.
98+
99+
Note that when `solidity-flycheck-use-project' is t, the project root will be
100+
added to --allow-paths in addition to any paths defined here."
101+
:group 'solidity
102+
:type 'list
103+
:safe #'listp
104+
:package-version '(solidity-mode . "0.1.11"))
105+
106+
(defcustom solidity-flycheck-use-project nil
107+
"A boolean flag denoting whether solidity-flycheck should detect projects.
108+
109+
When t, solidity-flycheck will detect a project and include files in the project
110+
during compilation and linting. solidity-flycheck will look for a .soliumrc.json
111+
in a parent directory. If found, this directory is considered the project root. If
112+
no .soliumrc.json is found, `project-roots' is used.
113+
114+
When `solidity-flycheck-solium-checker-active' is t, the .soliumrc.json found in
115+
the project root will be used as the solium config, rather than a .soliumrc.json
116+
in the same directory as the file being linted.
117+
118+
When `solidity-flycheck-solc-checker-active' is t, the project root will be passed
119+
to solc using the --allow-paths flag. This means imports to other files inside the
120+
project will lint without erorr."
121+
:group 'solidity
122+
:type 'boolean
123+
:safe #'booleanp
124+
:package-version '(solidity-mode . "0.1.11"))
125+
126+
(defun solidity-flycheck--find-working-directory (_checker)
127+
"Look for a working directtory to run solium CHECKER in.
128+
129+
This will be a parent directory that contains a `.soliumrc.json' file. If
130+
no .soliumrc.json is found, `project-roots' is used."
131+
(when (and buffer-file-name solidity-flycheck-use-project)
132+
(when-let ((root (or
133+
(locate-dominating-file buffer-file-name ".soliumrc.json")
134+
(let* ((proj (project-current t))
135+
(roots (project-roots proj)))
136+
(car roots)))))
137+
(expand-file-name root))))
138+
89139
(when solidity-flycheck-solium-checker-active
90140
;; define solium flycheck syntax checker
91141
;; expanded the flycheck-define-checker macro in order to eval certain args, as per advice given in gitter
@@ -121,7 +171,7 @@ we pass the directory to solium via the `--config' option."
121171
:predicate #'(lambda nil (eq major-mode 'solidity-mode))
122172
:next-checkers 'nil
123173
:standard-input 'nil
124-
:working-directory 'nil)
174+
:working-directory 'solidity-flycheck--find-working-directory)
125175
(add-to-list 'flycheck-checkers 'solium-checker)
126176
(setq flycheck-solium-checker-executable solidity-solium-path))
127177
(error (format "Solidity Mode Configuration error. Requested solium flycheck integration but can't find solium at: %s" solidity-solium-path)))))
@@ -147,13 +197,56 @@ we pass the directory to solium via the `--config' option."
147197
(patch (string-to-number (nth 2 version))))
148198
(if (and (>= major 0) (>= minor 6)) t nil)))
149199

200+
(defun solidity-flycheck--solc-allow-paths ()
201+
(append
202+
(mapcar #'expand-file-name solidity-flycheck-solc-additional-allow-paths)
203+
'(".")))
204+
205+
(defun solidity-flycheck--only-subdirectories (dir)
206+
(-filter
207+
(lambda (p)
208+
(and (file-directory-p p)
209+
(not (string-prefix-p "." (file-name-nondirectory p)))))
210+
(directory-files dir t)))
211+
212+
(defun solidity-flycheck--solc-remappings ()
213+
(let* ((allow-paths (solidity-flycheck--solc-allow-paths)))
214+
(->> allow-paths
215+
(mapcar
216+
#'solidity-flycheck--only-subdirectories)
217+
218+
-flatten
219+
220+
(mapcar
221+
(lambda (dir)
222+
(let ((prefix (file-name-nondirectory dir)))
223+
(concat prefix "=" dir)))))))
224+
225+
(defun solidity-flycheck--solc-remappings-opt ()
226+
(when-let ((remappings (solidity-flycheck--solc-remappings)))
227+
remappings))
228+
229+
(defun solidity-flycheck--solc-allow-paths-opt ()
230+
(let ((allow-paths (mapconcat 'identity (solidity-flycheck--solc-allow-paths) ",")))
231+
(when (not (string= "" allow-paths))
232+
`("--allow-paths" ,allow-paths))))
233+
234+
(defun solidity-flycheck--solc-cmd ()
235+
(if (solc-gt-0.6.0)
236+
`("solc"
237+
"--no-color"
238+
,@(solidity-flycheck--solc-allow-paths-opt)
239+
,@(solidity-flycheck--solc-remappings-opt)
240+
source-inplace)
241+
'("solc" source-inplace)))
242+
150243
(when solidity-flycheck-solc-checker-active
151244
;; add a solidity mode callback to set the executable of solc for flycheck
152245
;; define solidity's flycheck syntax checker
153246
;; expanded the flycheck-define-checker macro in order to eval certain args, as per advice given in gitter
154247
;; https://gitter.im/flycheck/flycheck?at=5a43b3a8232e79134d98872b
155248
(flycheck-def-executable-var solidity-checker "solc")
156-
(let* ((cmd (if (solc-gt-0.6.0) '("solc" "--no-color" source-inplace) '("solc" source-inplace))))
249+
(let* ((cmd (solidity-flycheck--solc-cmd)))
157250
(if (funcall flycheck-executable-find solidity-solc-path)
158251
(progn
159252
(flycheck-define-command-checker 'solidity-checker
@@ -173,7 +266,7 @@ we pass the directory to solium via the `--config' option."
173266
:predicate #'(lambda nil (eq major-mode 'solidity-mode))
174267
:next-checkers `((,solidity-flycheck-chaining-error-level . solium-checker))
175268
:standard-input 'nil
176-
:working-directory 'nil)
269+
:working-directory 'solidity-flycheck--find-working-directory)
177270
(add-to-list 'flycheck-checkers 'solidity-checker)
178271
(setq flycheck-solidity-checker-executable solidity-solc-path))
179272
(error (format "Solidity Mode Configuration error. Requested solc flycheck integration but can't find solc at: %s" solidity-solc-path)))))

solidity-mode.el

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
;; Author: Lefteris Karapetsas <[email protected]>
66
;; Keywords: languages, solidity
7-
;; Version: 0.1.10
7+
;; Version: 0.1.11
88

99
;; This program is free software; you can redistribute it and/or modify
1010
;; it under the terms of the GNU General Public License as published by

0 commit comments

Comments
 (0)