@@ -12,61 +12,7 @@ class Golangcilint(NodeLinter):
1212 defaults = {"selector" : "source.go" }
1313 axis_base = (1 , 1 )
1414
15- def severity (self , issue ):
16- """consider /dev/stderr as errors and /dev/stdout as warnings"""
17- return "error" if issue ["FromLinter" ] == "typecheck" else "warning"
18-
19- def shortname (self , issue ):
20- """find and return short filename"""
21- return os .path .basename (issue ["Pos" ]["Filename" ])
22-
23- def lintissue (self , issue ):
24- return LintMatch (
25- match = issue ,
26- message = issue ["Text" ],
27- error_type = self .severity (issue ),
28- line = issue ["Pos" ]["Line" ] - self .axis_base [0 ],
29- col = issue ["Pos" ]["Column" ] - self .axis_base [1 ],
30- code = issue ["FromLinter" ]
31- )
32-
33- def canonical (self , issue ):
34- mark = issue ["Text" ].rfind ("/" )
35- package = issue ["Text" ][mark + 1 :- 1 ]
36- # Go 1.4 introduces an annotation for package clauses in Go source that
37- # identify a canonical import path for the package. If an import is
38- # attempted using a path that is not canonical, the go command will
39- # refuse to compile the importing package.
40- #
41- # When the linter runs, it creates a temporary directory, for example,
42- # “.golangcilint-foobar”, then creates a symbolic link for all relevant
43- # files, and writes the content of the current buffer in the correct
44- # file. Unfortunately, canonical imports break this flow because the
45- # temporary directory differs from the expected location.
46- #
47- # The only way to deal with this for now is to detect the error, which
48- # may as well be a false positive, and then ignore all the warnings
49- # about missing packages in the current file. Hopefully, the user has
50- # “goimports” which will automatically resolve the dependencies for
51- # them. Also, if the false positives are not, the programmer will know
52- # about the missing packages during the compilation phase, so it’s not
53- # a bad idea to ignore these warnings for now.
54- #
55- # See: https://golang.org/doc/go1.4#canonicalimports
56- return {
57- "FromLinter" : "typecheck" ,
58- "Text" : "cannot lint package “{}” due to canonical import path" .format (package ),
59- "Replacement" : issue ["Replacement" ],
60- "SourceLines" : issue ["SourceLines" ],
61- "Level" : "error" ,
62- "Pos" : {
63- "Filename" : self .filename ,
64- "Offset" : 0 ,
65- "Column" : 0 ,
66- "Line" : 1
67- }
68- }
69-
15+ """match regex against the command output"""
7016 def find_errors (self , output ):
7117 current = os .path .basename (self .filename )
7218 exclude = False
@@ -101,8 +47,8 @@ def find_errors(self, output):
10147 """detect broken canonical imports"""
10248 if ("code in directory" in issue ["Text" ]
10349 and "expects import" in issue ["Text" ]):
104- issue = self .canonical (issue )
105- yield self .lintissue (issue )
50+ issue = self ._canonical (issue )
51+ yield self ._lintissue (issue )
10652 exclude = True
10753 continue
10854
@@ -113,7 +59,62 @@ def find_errors(self, output):
11359 continue
11460
11561 """issues found in the current file are relevant"""
116- if self .shortname (issue ) != current :
62+ if self ._shortname (issue ) != current :
11763 continue
11864
119- yield self .lintissue (issue )
65+ yield self ._lintissue (issue )
66+
67+ def _shortname (self , issue ):
68+ """find and return short filename"""
69+ return os .path .basename (issue ["Pos" ]["Filename" ])
70+
71+ def _severity (self , issue ):
72+ """consider /dev/stderr as errors and /dev/stdout as warnings"""
73+ return "error" if issue ["FromLinter" ] == "typecheck" else "warning"
74+
75+ def _canonical (self , issue ):
76+ mark = issue ["Text" ].rfind ("/" )
77+ package = issue ["Text" ][mark + 1 :- 1 ]
78+ # Go 1.4 introduces an annotation for package clauses in Go source that
79+ # identify a canonical import path for the package. If an import is
80+ # attempted using a path that is not canonical, the go command will
81+ # refuse to compile the importing package.
82+ #
83+ # When the linter runs, it creates a temporary directory, for example,
84+ # “.golangcilint-foobar”, then creates a symbolic link for all relevant
85+ # files, and writes the content of the current buffer in the correct
86+ # file. Unfortunately, canonical imports break this flow because the
87+ # temporary directory differs from the expected location.
88+ #
89+ # The only way to deal with this for now is to detect the error, which
90+ # may as well be a false positive, and then ignore all the warnings
91+ # about missing packages in the current file. Hopefully, the user has
92+ # “goimports” which will automatically resolve the dependencies for
93+ # them. Also, if the false positives are not, the programmer will know
94+ # about the missing packages during the compilation phase, so it’s not
95+ # a bad idea to ignore these warnings for now.
96+ #
97+ # See: https://golang.org/doc/go1.4#canonicalimports
98+ return {
99+ "FromLinter" : "typecheck" ,
100+ "Text" : "cannot lint package “{}” due to canonical import path" .format (package ),
101+ "Replacement" : issue ["Replacement" ],
102+ "SourceLines" : issue ["SourceLines" ],
103+ "Level" : "error" ,
104+ "Pos" : {
105+ "Filename" : self .filename ,
106+ "Offset" : 0 ,
107+ "Column" : 0 ,
108+ "Line" : 1
109+ }
110+ }
111+
112+ def _lintissue (self , issue ):
113+ return LintMatch (
114+ match = issue ,
115+ message = issue ["Text" ],
116+ error_type = self ._severity (issue ),
117+ line = issue ["Pos" ]["Line" ] - self .axis_base [0 ],
118+ col = issue ["Pos" ]["Column" ] - self .axis_base [1 ],
119+ code = issue ["FromLinter" ]
120+ )
0 commit comments