Skip to content

Commit

Permalink
Add powershell/PSScriptAnalyzerBear
Browse files Browse the repository at this point in the history
  • Loading branch information
userzimmermann committed Aug 1, 2017
1 parent 2bef3df commit 264f35a
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions bears/powershell/PSScriptAnalyzerBear.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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 ():
location = item['Extent']
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(location['StartLineNumber']),
column=int(location['StartColumnNumber']),
end_line=int(location['EndLineNumber']),
end_column=int(location['EndColumnNumber']),
diffs={filename: diff} if diff else None)

0 comments on commit 264f35a

Please sign in to comment.