Skip to content

Add linting to PSES, with fixes for obvious cases #1155

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

Merged
merged 22 commits into from
Jan 14, 2020

Conversation

rjmholt
Copy link
Contributor

@rjmholt rjmholt commented Jan 13, 2020

  • Adds .ConfigureAwait(false) to all await calls (and turns some into Task<T> pass throughs)
  • Fixes some clear cases of string rendering with culture variance
  • Add suppressions for things like localisation
  • Add null checks to public APIs
  • Make fields readonly where VS suggests
  • Various other syntactic sugar improvements

Copy link
Member

@TylerLeonhardt TylerLeonhardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good!

@PowerShell PowerShell deleted a comment from rjmholt Jan 14, 2020
Copy link
Collaborator

@SeeminglyScience SeeminglyScience left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! One optional

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1819:Properties should not return arrays", Justification = "Cmdlet parameters can be arrays", Scope = "module")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1303:Do not pass literals as localized parameters", Justification = "PSES is not localized", Scope = "module")]

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "PSCmdlet.ThrowTerminatingError() is used instead", Scope = "member", Target = "~M:Microsoft.PowerShell.EditorServices.Commands.StartEditorServicesCommand.EndProcessing")]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could these be placed inline? The ones that target a specific member that is.

Copy link
Contributor Author

@rjmholt rjmholt Jan 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They can be. I've never liked those FxCop attributes that occur all over the code (especially when codebases stop using FxCop like in PowerShell), but will leave it up to others

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can certainly see the pros of having the attribute inline, so you get the justification like a comment

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally agree with @SeeminglyScience about putting them inline so they're "self documenting"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it's far from perfect. I hate seeing them too, but it makes it confusing why one thing warns and another doesn't. Most folks aren't going to know to look in that file.

I usually go with #pragma warning disable CA1031 and #pragma warning restore CA1031 since it's slightly less aggressive, but then you lose a lot of information. Probably not suitable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went for the attribute where possible, just since it's got a field for Justification. Some things required a pragma though

@@ -1569,7 +1581,7 @@ public async Task SetWorkingDirectoryAsync(string path)
/// <param name="isPathAlreadyEscaped">Specify false to have the path escaped, otherwise specify true if the path has already been escaped.</param>
public async Task SetWorkingDirectoryAsync(string path, bool isPathAlreadyEscaped)
{
this.InitialWorkingDirectory = path;
this.InitialWorkingDirectory = path ?? throw new ArgumentNullException(nameof(path));
Copy link
Member

@TylerLeonhardt TylerLeonhardt Jan 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the Validate.IsNotNull?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed this initially, but how do we feel about these methods?

They kind of mess with the call stack a little bit. It's not a huge deal, but it does make debugging slightly more tedious since you need to pick the right frame. And it makes Exception.TargetSite kinda useless.

Obviously I'm not expecting anything in this merged PR to change, just bringing it up in case any of y'all are also leaning that way. WTB macros 🙁

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Util method or the SetWorkingDirectory?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Util methods for argument validation

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah they do seem very unnecessary and even have drawbacks as you've pointed out.

I'd be fine with them being removed but also not a huge problem atm

Copy link
Member

@TylerLeonhardt TylerLeonhardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM besides the above comments

@TylerLeonhardt
Copy link
Member

Codacy Here is an overview of what got changed by this pull request:

Issues
======
+ Solved 88
- Added 2
           

Clones added
============
- src/PowerShellEditorServices/Services/TextDocument/Handlers/CompletionHandler.cs  1
- src/PowerShellEditorServices/Services/TextDocument/Handlers/DocumentHighlightHandler.cs  3
- src/PowerShellEditorServices/Services/TextDocument/Handlers/DefinitionHandler.cs  1
- src/PowerShellEditorServices/Services/TextDocument/Handlers/DocumentSymbolHandler.cs  1
         

See the complete overview on Codacy

@@ -1143,12 +1164,12 @@ public async Task LoadHostProfilesAsync()
{
PSCommand command = new PSCommand();
command.AddCommand(profilePath, false);
await this.ExecuteCommandAsync<object>(command, true, true);
await this.ExecuteCommandAsync<object>(command, sendOutputToHost: true, sendErrorToHost: true).ConfigureAwait(false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int initialPromptCol = await ConsoleProxy.GetCursorLeftAsync(cancellationToken);
// TODO: Are these values used?
int initialPromptRow = await ConsoleProxy.GetCursorTopAsync(cancellationToken).ConfigureAwait(false);
int initialPromptCol = await ConsoleProxy.GetCursorLeftAsync(cancellationToken).ConfigureAwait(false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@TylerLeonhardt TylerLeonhardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@rjmholt rjmholt merged commit 81ba0c0 into PowerShell:master Jan 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants