Skip to content

'new' does not recursively transpile constructor arguments #271

Closed
@ninmonkey

Description

@ninmonkey

Line endings

$src | Invoke-Formatter 
    # works

$src.Transpile() | invoke-formatter

    Error: Invoke-Formatter: Cannot determine line endings as the text probably contain mixed line endings. (Parameter 'text')

$src.Transpile() -replace '\r?\n', "`n" | invoke-formatter
    # works

I didn't test whether pipescript is doing anything inconsistant -- I'm assuming it's okay. But you might want to catch that case for a usability aspect.

I've ran into that when testing exact string comparisons in pester. Because, the here-string content is different depending on the line endings chosen. ( So the literal, is a literal, but re-saving the file in another line endings change the herestrings)

About

using .>$srcSb | % ToString outputs the expected XML
However $srcSb.Transpile() doesn't transpile everything

using namespace System.Xml.Linq
Import-module PipeScript
    $srcSb= {    
    new XElement 'Contacts', @(
        new XElement 'Contact', @(
            new XElement 'Name', 'Patrick Hines'
            new XElement 'Phone', '425-555-0145', @(
                new XAttribute 'Type', 'Home'
            )
            new XElement 'Address' @(
                new XElement "Street1", "123 Main St"
                new XElement "City", "Mercer Island"
                new XElement "State", "WA"
                new XElement "Postal", "68042"
            ))) }

first pass

$srcSb.Transpile()
    [XElement]::new('Contacts', @(
            new XElement 'Contact', @(
                new XElement 'Name', 'Patrick Hines'
                new XElement 'Phone', '425-555-0145', @(
                    new XAttribute 'Type', 'Home'
                )
                new XElement 'Address' @(
                    new XElement "Street1", "123 Main St"
                    new XElement "City", "Mercer Island"
                    new XElement "State", "WA"
                    new XElement "Postal", "68042"
                )))) 

Full Pass

$src.Transpile().Transpile().Transpile().Transpile() -replace '\r?\n', "`n" | Invoke-Formatter 
[XElement]::new('Contacts', @(
        [XElement]::new('Contact', @(
                [XElement]::new('Name', 'Patrick Hines')
                [XElement]::new('Phone', '425-555-0145', @(
                        [XAttribute]::new('Type', 'Home')
                    ))
                [XElement]::new('Address', @(
                        [XElement]::new("Street1", "123 Main St")
                        [XElement]::new("City", "Mercer Island")
                        [XElement]::new("State", "WA")
                        [XElement]::new("Postal", "68042")
                    )))))) 

Here's a quick hack to generate the final text

Import-Module PipeScript -MinimumVersion 0.1.7 # maybe earlier
function transpileTillCooked { 
    <#
    .synopsis
        repeat transpiling, until fully done
    .DESCRIPTION
        it's sugar for:

            $src.Transpile().Transpile().Transpile().Transpile() | invoke-formatter

        exit early with max iteration count
        returns script block, while optionally printing each state
    .EXAMPLE
        $finalSrc = transpileTillCooked.2 -ScriptBlock $src -MaxIters 3 -Pretty
    #>
    [OutputType('System.Management.Automation.ScriptBlock')]
    [Alias('Pipes.transpileTillCooked.2')]
    param( 
        [Parameter(Mandatory)]
        [ScriptBlock]$ScriptBlock, [int]$MaxIters = 7,

        # This shows changes before each transpile step
        # with any value, final output is always a [scriptblock]
        [switch]$Pretty
    )
    $last = $ScriptBlock
    $iter = 0

    while($true) { 
        if($iters++ -gt $MaxIters) { break } 
        $next = $last.Transpile()
        if($Pretty) { 
            $InformationPreference = 'continue'
            hr | Write-Information
            $next | Write-Information
            $InformationPreference = 'silentlycontinue'
        }
        if($next.ToString() -eq $last.ToString()) { break }
        $last = $Next
    }
    return $next
}

Metadata

Metadata

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions