-
Notifications
You must be signed in to change notification settings - Fork 581
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2bef3df
commit de6594d
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import json | ||
|
||
from coalib.bearlib.abstractions.Linter import linter | ||
from coalib.results.Diff import Diff | ||
from coalib.results.Result import Result | ||
from coalib.results.TextRange import TextRange | ||
|
||
|
||
@linter(executable='powershell') | ||
class PSScriptAnalyzerBear: | ||
""" | ||
Check the quality of PowerShell modules and scripts. | ||
""" | ||
|
||
LANGUAGES = {'PowerShell'} | ||
AUTHORS = {'The coala developers'} | ||
AUTHORS_EMAILS = {'[email protected]'} | ||
LICENSE = 'AGPL-3.0' | ||
CAN_DETECT = CAN_FIX = {'Syntax', 'Formatting', 'Smell'} | ||
SEE_MORE = 'https://github.com/PowerShell/PSScriptAnalyzer' | ||
|
||
@staticmethod | ||
def create_arguments(filename, file, config_file): | ||
""" | ||
Define a ``powershell -Command`` for running ``Invoke-ScriptAnalyzer`` | ||
cmdlet on `filename` and converting result to JSON. | ||
""" | ||
return ('-Command', | ||
'Invoke-ScriptAnalyzer -Path {} | ConvertTo-Json -Depth 3' | ||
.format(filename)) | ||
|
||
def process_output(self, output, filename, file): | ||
violations = output and json.loads(output) | ||
for item in violations or (): | ||
diff = None # for the case that no correction is found below | ||
fixes = item['SuggestedCorrections'] | ||
if fixes: | ||
diff = Diff(list(file)) | ||
for fix in fixes: | ||
diff.replace(TextRange.from_values( | ||
int(fix['StartLineNumber']), | ||
int(fix['StartColumnNumber']), | ||
int(fix['EndLineNumber']), | ||
int(fix['EndColumnNumber']) | ||
), fix['Text']) | ||
yield Result.from_values( | ||
origin='{} ({})'.format( | ||
type(self).__name__, item['RuleName']), | ||
message=item['Message'], | ||
file=filename, | ||
line=int(item['Extent']['StartLineNumber']), | ||
column=int(item['Extent']['StartColumnNumber']), | ||
end_line=int(item['Extent']['EndLineNumber']), | ||
end_column=int(item['Extent']['EndColumnNumber']), | ||
diffs={filename: diff} if diff else None) |