Skip to content

Commit 528b694

Browse files
committed
Convert Using fixer to a Script Generator
1 parent 2a57d87 commit 528b694

File tree

3 files changed

+68
-80
lines changed

3 files changed

+68
-80
lines changed

Source/Private/MoveUsingStatements.ps1

Lines changed: 0 additions & 77 deletions
This file was deleted.

Source/Public/Move-UsingStatement.ps1

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using namespace System.Management.Automation.Language
2+
3+
function Move-UsingStatement {
4+
<#
5+
.SYNOPSIS
6+
A Script Generator that commetnts out using statements and copies them to the top of the file
7+
.DESCRIPTION
8+
Move-UsingStatement supports having using statements repeated in multiple files that are merged by ModuleBuilder.
9+
When all the files are merged together, the using statements from individual files
10+
don't necessarily end up at the beginning of the PSM1, which creates Parsing Errors.
11+
12+
This function uses the AST to generate TextReplacements to:
13+
1. Comment out the original using statements (to preserve line numbering)
14+
2. Insert the using statements (conserving order, but removing duplicates) at the top of the script
15+
#>
16+
[CmdletBinding()]
17+
[OutputType([TextReplacement])]
18+
param(
19+
# The AST of the original script module to refactor
20+
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
21+
[Alias("Ast")]
22+
[Ast]$InputObject,
23+
24+
# Parser Errors from parsing the original script module
25+
[Parameter(ValueFromPipelineByPropertyName)]
26+
[AllowNull()]
27+
[ParseError[]]$ParseErrors
28+
)
29+
process {
30+
# Avoid modifying the file if there's no Parsing Error caused by Using Statements or other errors
31+
if (!$ParseErrors.Where{ $_.ErrorId -eq 'UsingMustBeAtStartOfScript' }) {
32+
Write-Debug "No using statement errors found."
33+
return
34+
} else {
35+
# as decided https://github.com/PoshCode/ModuleBuilder/issues/96
36+
Write-Debug "Parsing errors found. We'll still attempt to Move using statements."
37+
}
38+
39+
# Find all Using statements including those non erroring (to conserve their order)
40+
$UsingStatementExtents = $InputObject.FindAll(
41+
{ $Args[0] -is [System.Management.Automation.Language.UsingStatementAst] },
42+
$false
43+
).Extent
44+
45+
# Edit the Script content by commenting out existing statements (conserving line numbering)
46+
$StatementsToCopy = [System.Collections.Generic.HashSet[string]]::new([StringComparer]::OrdinalIgnoreCase)
47+
48+
foreach ($UsingSatement in $UsingStatementExtents) {
49+
[TextReplacement]@{
50+
StartOffset = $UsingSatement.StartOffset
51+
EndOffset = $UsingSatement.EndOffset
52+
Text = '# ' + $UsingSatement.Text
53+
}
54+
# Keep track of unique statements we'll need to insert at the top
55+
$null = $StatementsToCopy.Add($UsingSatement.Text)
56+
}
57+
if ($StatementsToCopy.Count -gt 0) {
58+
[TextReplacement]@{
59+
StartOffset = 0
60+
EndOffset = 0
61+
Text = ($StatementsToCopy -join "`r`n")+ "`r`n"
62+
}
63+
}
64+
}
65+
}

Tests/Public/Build-Module.Tests.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ Describe "Build-Module" {
8686
Context "Testing" {
8787
BeforeAll {
8888
InModuleScope ModuleBuilder {
89-
Mock MoveUsingStatements
89+
Mock Move-UsingStatements
9090
Mock SetModuleContent
9191
}
9292
Mock Update-Metadata
@@ -181,8 +181,8 @@ Describe "Build-Module" {
181181
Assert-MockCalled ConvertToAst -Scope Context
182182
}
183183

184-
It "Should call MoveUsingStatements to move the using statements, just in case" {
185-
Assert-MockCalled MoveUsingStatements -Parameter {
184+
It "Should call Move-UsingStatement to move the using statements, just in case" {
185+
Assert-MockCalled Move-UsingStatement -Parameter {
186186
$AST.Extent.Text -eq "{ }"
187187
} -Scope Context
188188
}

0 commit comments

Comments
 (0)