Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support SuggestedCorrections property on DiagnosticRecord for script based rules #1000

Conversation

@bergmeister
Copy link
Collaborator

bergmeister commented May 14, 2018

PR Summary

Closes #619
Populate property and make it public to keep construction of the object easy.

PR Checklist

Note: Tick the boxes below that apply to this pull request by putting an x between the square brackets. Please mark anything not applicable to this PR NA.

  • PR has a meaningful title
    • Use the present tense and imperative mood when describing your changes
  • Summarized changes
  • User facing documentation needed
  • Change is not breaking
  • Make sure you've added a new test if existing tests do not effectively test the code changed
  • This PR is ready to merge and is not work in progress
    • If the PR is work in progress, please add the prefix WIP: to the beginning of the title and remove the prefix when the PR is ready
bergmeister added 2 commits May 14, 2018
… for easier construction in PowerShell
@bergmeister bergmeister changed the title WIP Support SuggestedCorrections property on DiagnosticRecord for script based rules Support SuggestedCorrections property on DiagnosticRecord for script based rules May 14, 2018
@bergmeister bergmeister added this to the 1.17 milestone May 14, 2018
@bergmeister bergmeister self-assigned this May 14, 2018
Copy link
Member

JamesWTruher left a comment

This looks great and really needed. My comment is not blocking as we can go back and change it later.

@@ -28,9 +28,12 @@ function Measure-RequiresRunAsAdministrator
[System.Management.Automation.Language.ScriptBlockAst]
$testAst
)
$dr = New-Object `
$l=(new-object System.Collections.ObjectModel.Collection["Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.CorrectionExtent"])

This comment has been minimized.

@JamesWTruher

JamesWTruher May 17, 2018 Member

in PowerShellCore tests, we're moving away from using new-object in favor of this:

$l = [System.Collections.ObjectModel.Collection[Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.CorrectionExtent]]::new()
$c = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.CorrectionExtent]::new(1,2,3,4,'text','filePath','description')
$null = $l.Add($c)
$dr = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord]::new(
    "This is help", 
    $ast.Extent,$PSCmdlet.MyInvocation.InvocationName,
    "Warning",
    $null,
    $null,
    $l)
return $dr

which I think is much tidier. I know it's inconsistent, but that's ok - we can go back where we need to.

This comment has been minimized.

@bergmeister

bergmeister May 17, 2018 Author Collaborator

@JamesWTruher I tried it at first but then had to revert this change because the [Type]::new() constructor was only introduced in version 5, therefore it failed the WMF4 build and we should not document examples that do not work on supported PS versions. Will therefore merge

This comment has been minimized.

@JamesWTruher

JamesWTruher May 18, 2018 Member

yah - I thought about this again last night and realized this wouldn't work because of the downlevel requirements. We won't be able to support the modern constructor syntax until we drop support for ps v3/4

bergmeister added 3 commits May 17, 2018
…nalyzer into allowSuggestedCorrectionsInScriptRule
Reason: this was only introduced in PowerShell v5 and therefore fails
the PS v4 build and we should not give examples that do not work on any
supported version
This reverts commit aa90ea3.
@bergmeister bergmeister merged commit e5ae9df into PowerShell:development May 17, 2018
2 checks passed
2 checks passed
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
license/cla All CLA requirements met.
Details
@LaurentDardenne
Copy link

LaurentDardenne commented Jan 12, 2019

For information, these changes are a breaking change, because a priori the 'lookup algorithm' of Powershell does not know how to call the code that worked previously.

@bergmeister
Copy link
Collaborator Author

bergmeister commented Feb 21, 2019

@LaurentDardenne I am sorry for that, every release comes with minor breaking changes, which are usually for a better experience, which is to actually propagate the SuggestedCorrections to the end user (one can use them inside vscode now for example). Thank you for your continued and detailed feedback though :)
You could create a DiagnosticRecord as follows to avoid any future breaking changes in the constructor (which is from the examples that I documented here), which will also work on PS versions lower than 5 in contrast to the ::new operator

[Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord]@{
    "Message"              = "This is a rule with a suggested correction"
    "Extent"               = $ast.Extent
    "RuleName"             = $PSCmdlet.MyInvocation.InvocationName
    "Severity"             = "Warning"
    "SuggestedCorrections" = $suggestedCorrections
}
@LaurentDardenne
Copy link

LaurentDardenne commented Mar 2, 2019

"every release comes with minor breaking changes,"
I agree, but it better when the changelog contains this change.
Here, I discover it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

3 participants
You can’t perform that action at this time.