This repository was archived by the owner on Feb 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSemanticLintProvider.coffee
366 lines (305 loc) · 12.2 KB
/
SemanticLintProvider.coffee
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
module.exports =
##*
# The (provider) that handles indexing errors using an indie linter.
##
class IndexingProvider
###*
* The service (that can be used to query the source code and contains utility methods).
###
service: null
###*
* The indie linter.
###
indieLinter: null
###*
* Contains global package settings.
###
config: null
###*
* Keeps track of whether a linting operation is currently running.
###
isLintingInProgress: false
###*
* Whether to ignore the next linting result.
###
ignoreLintingResult: false
###*
* The next editor to start a linting task for.
###
nextEditor: null
###*
* Constructor.
*
* @param {Config} config
###
constructor: (@config) ->
###*
* Initializes this provider.
*
* @param {mixed} service
###
activate: (@service) ->
@attachListeners(@service)
###*
* Deactives the provider.
###
deactivate: () ->
###*
* Sets the indie linter to use.
*
* @param {mixed} indieLinter
###
setIndieLinter: (@indieLinter) ->
@messages = []
###*
* Attaches listeners for the specified base service.
*
* @param {mixed} service
###
attachListeners: (service) ->
service.onDidFinishIndexing (response) =>
editor = @findTextEditorByPath(response.path)
return if not editor?
return if not @indieLinter?
@semanticLint(editor)
service.onDidFailIndexing (response) =>
editor = @findTextEditorByPath(response.path)
return if not editor?
return if not @indieLinter?
@semanticLint(editor)
###*
* @param {TextEditor} editor
*
* @return {Promise}
###
semanticLint: (editor) ->
if @isLintingInProgress
# This file is already being linted, but by the time it finishes, the results will be out of date and we
# will then need to perform a new lint (we don't do it now to avoid spawning an excessive amount of
# linting processes).
@ignoreLintingResult = true
@nextEditor = editor
return
@isLintingInProgress = true
doneHandler = () =>
ignoreResult = @ignoreLintingResult
@isLintingInProgress = false
@ignoreLintingResult = false
if ignoreResult
# The result was ignored because there is more recent data, run again.
@semanticLint(@nextEditor)
return ignoreResult
successHandler = (response) =>
return if doneHandler()
@processSuccess(editor, response)
failureHandler = (response) =>
return if doneHandler()
@processFailure()
return @invokeSemanticLint(editor.getPath(), editor.getBuffer().getText()).then(
successHandler,
failureHandler
)
###*
* @param {String} path
* @param {String} source
*
* @return {Promise}
###
invokeSemanticLint: (path, source) ->
options = {
noUnknownClasses : not @config.get('showUnknownClasses')
noUnknownMembers : not @config.get('showUnknownMembers')
noUnknownGlobalFunctions : not @config.get('showUnknownGlobalFunctions')
noUnknownGlobalConstants : not @config.get('showUnknownGlobalConstants')
noUnusedUseStatements : not @config.get('showUnusedUseStatements')
noDocblockCorrectness : not @config.get('validateDocblockCorrectness')
}
return @service.semanticLint(path, source, options)
###*
* @param {TextEditor} editor
* @param {Object} response
###
processSuccess: (editor, response) ->
return if not @indieLinter
messages = []
if response.errors.syntaxErrors?
for item in response.errors.syntaxErrors
messages.push @createLinterMessageForSyntaxErrorOutputItem(editor, item)
if response.errors.unknownClasses?
for item in response.errors.unknownClasses
messages.push @createLinterMessageForOutputItem(
editor,
item,
'Error',
"<strong>#{item.name}</strong> was not found."
)
if response.errors.unknownMembers?
for item in response.errors.unknownMembers.expressionHasNoType
messages.push @createLinterMessageForOutputItem(
editor,
item,
'Error',
"The member <strong>#{item.memberName}</strong> could not be found because the expression has no type."
)
for item in response.errors.unknownMembers.expressionIsNotClasslike
messages.push @createLinterMessageForOutputItem(
editor,
item,
'Error',
"Type <strong>#{item.expressionType}</strong> does not have a member <strong>#{item.memberName}</strong>."
)
for item in response.errors.unknownMembers.expressionHasNoSuchMember
messages.push @createLinterMessageForOutputItem(
editor,
item,
'Error',
"The member <strong>#{item.memberName}</strong> does not exist for type <strong>#{item.expressionType}</strong>."
)
if response.warnings.unknownMembers?
for item in response.warnings.unknownMembers.expressionNewMemberWillBeCreated
messages.push @createLinterMessageForOutputItem(
editor,
item,
'Warning',
"The member <strong>#{item.memberName}</strong> was not explicitly defined for type <strong>#{item.expressionType}</strong>."
)
if response.errors.unknownGlobalFunctions?
for item in response.errors.unknownGlobalFunctions
messages.push @createLinterMessageForOutputItem(
editor,
item,
'Error',
"The global function <strong>#{item.name}</strong> was not found."
)
if response.errors.unknownGlobalConstants?
for item in response.errors.unknownGlobalConstants
messages.push @createLinterMessageForOutputItem(
editor,
item,
'Error',
"The global constant <strong>#{item.name}</strong> was not found."
)
if response.warnings.unusedUseStatements? and response.errors.syntaxErrors?.length == 0
for item in response.warnings.unusedUseStatements
messages.push @createLinterMessageForOutputItem(
editor,
item,
'Warning',
"<strong>#{item.name}</strong> is not used anywhere."
)
if response.warnings.docblockIssues?
for item in response.warnings.docblockIssues.varTagMissing
messages.push @createLinterMessageForOutputItem(
editor,
item,
'Warning',
"The docblock for <strong>#{item.name}</strong> is missing a @var tag."
)
if @config.get('showMissingDocs')
for item in response.warnings.docblockIssues.missingDocumentation
messages.push @createLinterMessageForOutputItem(
editor,
item,
'Warning',
"Documentation for <strong>#{item.name}</strong> is missing."
)
for item in response.warnings.docblockIssues.parameterMissing
messages.push @createLinterMessageForOutputItem(
editor,
item,
'Warning',
"The docblock for <strong>#{item.name}</strong> is missing a @param tag for <strong>#{item.parameter}</strong>."
)
for item in response.warnings.docblockIssues.parameterTypeMismatch
messages.push @createLinterMessageForOutputItem(
editor,
item,
'Warning',
"The docblock for <strong>#{item.name}</strong> has an incorrect @param type for <strong>#{item.parameter}</strong>."
)
for item in response.warnings.docblockIssues.superfluousParameter
parameters = item.parameters.join(', ')
messages.push @createLinterMessageForOutputItem(
editor,
item,
'Warning',
"The docblock for <strong>#{item.name}</strong> contains superfluous @param tags for: <strong>#{parameters}</strong>."
)
for item in response.warnings.docblockIssues.deprecatedCategoryTag
messages.push @createLinterMessageForOutputItem(
editor,
item,
'Warning',
"The docblock for <strong>#{item.name}</strong> contains a deprecated @category tag."
)
for item in response.warnings.docblockIssues.deprecatedSubpackageTag
messages.push @createLinterMessageForOutputItem(
editor,
item,
'Warning',
"The docblock for <strong>#{item.name}</strong> contains a deprecated @subpackage tag."
)
link = 'https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpdoc.md#710-link-deprecated'
for item in response.warnings.docblockIssues.deprecatedLinkTag
messages.push @createLinterMessageForOutputItem(
editor,
item,
'Warning',
"The docblock for <strong>#{item.name}</strong> contains a deprecated @link tag. See also <a href=\"#{link}\">#{link}</a>"
)
@indieLinter.setMessages(messages)
###*
*
###
processFailure: () ->
return if not @indieLinter
@indieLinter.setMessages([])
###*
* @param {TextEditor} editor
* @param {Object} item
*
* @return {Object}
###
createLinterMessageForSyntaxErrorOutputItem: (editor, item) ->
startLine = if item.startLine then item.startLine else 1
endLine = if item.endLine then item.endLine else 1
startColumn = if item.startColumn then item.startColumn else 1
endColumn = if item.endColumn then item.endColumn else 1
return {
type : 'Error'
html : item.message
range : [[startLine - 1, startColumn - 1], [endLine - 1, endColumn]]
filePath : editor.getPath()
}
###*
* @param {TextEditor} editor
* @param {Object} item
* @param {string} type
* @param {string} html
*
* @return {Object}
###
createLinterMessageForOutputItem: (editor, item, type, html) ->
text = editor.getBuffer().getText()
startCharacterOffset = @service.getCharacterOffsetFromByteOffset(item.start, text)
endCharacterOffset = @service.getCharacterOffsetFromByteOffset(item.end, text)
startPoint = editor.getBuffer().positionForCharacterIndex(startCharacterOffset)
endPoint = editor.getBuffer().positionForCharacterIndex(endCharacterOffset)
return {
type : type
html : html
range : [startPoint, endPoint]
filePath : editor.getPath()
}
###*
* Retrieves the text editor that is managing the file with the specified path.
*
* @param {string} path
*
* @return {TextEditor|null}
###
findTextEditorByPath: (path) ->
for textEditor in atom.workspace.getTextEditors()
if textEditor.getPath() == path
return textEditor
return null