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

Extra parameters #57

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

stinos
Copy link

@stinos stinos commented Feb 11, 2020

We use a slightly different style for formatting PS code but still would like to use this formatter so here's a bunch of changes which gets it to the style we use. Summary:

  • adding spaces after each comma (pretty common I think)
  • treating all groups the same for consistency (so both @() and @{} for instance are formatted the same instead of the latter having spaces but not the former)
  • custom function for spacing simply because we're so used to having e.g. $x | %{...} instead of $x | % {}
    All of these are optional so existing users shouldn't be affected, I think.

For reference, example of how we use this now:

$psBeautify = Get-Module PowerShell-Beautifier;
# Super-common aliases shouldn't be replaced
 @('?', '%', 'rm', 'cd') | %{$psBeautify.PrivateData.ValidCommandNames.Remove($_)}

# All of this is just so that Object gets turned into object, just like e.g. String is turned into string
$accel = [PowerShell].Assembly.GetType("System.Management.Automation.TypeAccelerators")
$accel::Add("object",[System.Object])
$builtinField = $accel.GetField("builtinTypeAccelerators", [System.Reflection.BindingFlags]"Static,NonPublic")
$builtinField.SetValue($builtinField, $accel::Get)

# Don't add space after ? and %
$customSpacing = {
  param($SourceTokens, $TokenIndex)
  if ($SourceTokens[$TokenIndex].Type -eq 'Command' -and (@('?', '%') -eq $SourceTokens[$TokenIndex].Content)) {
    return $false
  }
  $null
}

# Invoke beautifier with our options
Edit-DTWBeautifyScript -NewLine LF -SpaceAfterComma -TreatAllGroupsEqual -AddSpaceAfter $customSpacing $args

Having a space after commas is a rather common style so
make it configurable.
Note a spurious [<CommonParameters>] in the README has been removed.
This makes it easy to tweak 'add space after' behavior.
This makes the spacing in all groups (arrays, hashtables, script blocks, ...)
the same which is more consistent overall: when removing spaces in arrays
like @(1) it makes sense doing that also for e.g. @{a = 1}.
Default is still $false for backwards compatibility.
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.

1 participant