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

Invoke-Formatter do nothing :( #2012

Closed
hitriyvalenok opened this issue Jul 9, 2024 · 3 comments
Closed

Invoke-Formatter do nothing :( #2012

hitriyvalenok opened this issue Jul 9, 2024 · 3 comments

Comments

@hitriyvalenok
Copy link

hitriyvalenok commented Jul 9, 2024

I need to inline variable assignments only. Nothing more. For example, I have:

$var = @{

}

Exptected:

$var = @{}

Full example:

$customConfig = @{
    Rules = @{
        PSUseConsistentIndentation = @{
            Enable = $false
        }
        PSUseConsistentWhitespace = @{
            Enable = $false
        }
        PSAlignAssignmentStatement = @{
            Enable = $true
            CheckHashtable = $false
        }
    }
}
$code = @'
$var = @{

}
'@
Invoke-Formatter -ScriptDefinition $code -Settings $customConfig

But the cmdlet does nothing for me. It returns the code I passed without a tiny change as I've just asked to return $code back untouched. What I'm doing wrong?

@hitriyvalenok hitriyvalenok changed the title Invoke-Formatter change nothing of my code Invoke-Formatter do nothing :( Jul 9, 2024
@liamjpeters
Copy link
Contributor

Hey @hitriyvalenok 👋,

There isn't a rule in PSSA which causes hashtable declarations with no key-value pairs to be collapsed to a single line. The rules that exist will just control the alignment of the braces and any key-value pairs declared.

So for instance, the below:

$var  =       @{

    }

Would be formatted (in the default config) to:

$var = @{

}

Hopefully that makes sense!

@hitriyvalenok
Copy link
Author

hitriyvalenok commented Jul 11, 2024

Thanks! I got it now. I must make all multiline expressions (string concatenation or member chained call) inline. Examples:

$string = 'hello' +`
'world'
===>
$string = 'hello' + 'world'

InokeSomething 'arg1'`
'arg2'
===>
InokeSomething 'arg1' 'arg2'

$var = $true |`
$false
===>
$var = $true | $false

Can I do it with the formatter? My attempts fail :( Thanks!

@liamjpeters
Copy link
Contributor

There aren't currently any rules that enforce the layout of expressions to a single line.

The current crop of layout rules really focus on:

  • spacing (spaces before and after operators, no double spaces, etc)
  • Bracket alignment and placement
  • alignment of code to the current tab-stop based on bracket-depth
  • alignment of elements within hashtables and arrays

Amongst others.

You could put together a pull request to introduce a new rule/edit an existing rule to accommodate your requirements - but I don't know that they'd be particularly popular or that the PR would be accepted. There's value to having whitespace and spacing things out in general for readability. Certainly a rule that enforced single-line expressions would be unpopular.

You could look into creating a custom rule for yourself in PowerShell.

  • There's some good documentation on getting started here.
  • Jason Shirk's ShowPSAst module is really useful to look at the AST of your code to help put rules together.
  • There are some community analyzer rules within the PSSA repo. They may give you an idea of how rules are put together. One of which, Measure-Backtick may get you some of the way towards avoiding the use of backticks.

Hope that helps!

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

No branches or pull requests

3 participants