Skip to content

Commit

Permalink
Update regex in to match word boundaries (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-hughes authored Oct 6, 2024
1 parent 398fae8 commit 7b635dd
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ and [#9](https://github.com/dsccommunity/DscResource.AnalyzerRules/issues/9).
- `DscResource.AnalyzerRules.psd1`
- `CONTRIBUTING.md`
- `Get-LocalizedData.Tests.ps1`
- `Measure-Keyword.ps1`
- Update regex in to match word boundaries. Fixes [#11](https://github.com/dsccommunity/DscResource.AnalyzerRules/issues/11).
- Fix formatting.
- Localization Strings
- Correct url for OneSpaceBetweenKeywordAndParenthesis.
- `Get-TokensFromDefinition.ps1`
- Remove unused variable.

## [0.2.0] - 2019-11-21

Expand Down
5 changes: 3 additions & 2 deletions Source/Public/Measure-Keyword.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
.OUTPUTS
[Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]]
.NOTES
.NOTES
None
#>

function Measure-Keyword
{
[CmdletBinding()]
Expand All @@ -41,7 +42,7 @@ function Measure-Keyword
}
$upperCaseTokens = $keywords.Where{ $_.Text -cMatch '[A-Z]+' }

$tokenWithNoSpace = $keywords.Where{ $_.Extent.StartScriptPosition.Line -match "$($_.Extent.Text)\(.*" }
$tokenWithNoSpace = $keywords.Where{ $_.Extent.StartScriptPosition.Line -match "\b$($_.Extent.Text)\(.*" }

foreach ($item in $upperCaseTokens)
{
Expand Down
2 changes: 1 addition & 1 deletion Source/en-US/DscResource.AnalyzerRules.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ EnumOpeningBraceShouldBeFollowedByOnlyOneNewLine = Opening brace on Enum should
ClassOpeningBraceNotOnSameLine = Class should not have the open brace on the same line as the declaration. See https://dsccommunity.org/styleguidelines/whitespace/#one-newline-before-braces
ClassOpeningBraceShouldBeFollowedByNewLine = Opening brace on Class should be followed by a new line. See https://dsccommunity.org/styleguidelines/whitespace/#one-newline-after-opening-brace
ClassOpeningBraceShouldBeFollowedByOnlyOneNewLine = Opening brace on Class should only be followed by one new line. See https://dsccommunity.org/styleguidelines/whitespace/#one-newline-after-opening-brace
OneSpaceBetweenKeywordAndParenthesis = If a keyword is followed by a parenthesis, there should be single space between the keyword and the parenthesis. See https://dsccommunity.org/styleguidelines/whitespace/#one-newline-after-opening-brace
OneSpaceBetweenKeywordAndParenthesis = If a keyword is followed by a parenthesis, there should be single space between the keyword and the parenthesis. See https://dsccommunity.org/styleguidelines/whitespace/#one-space-between-keyword-and-parenthesis
HashtableShouldHaveCorrectFormat = Hashtable is not correctly formatted. See https://dsccommunity.org/styleguidelines/general/#correct-format-for-hashtables-or-objects
'@
2 changes: 1 addition & 1 deletion tests/Unit/Public/Get-TokensFromDefinition.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function Get-TokensFromDefinition
)

$parseErrors = $token = $null
$definitionAst = [System.Management.Automation.Language.Parser]::ParseInput($ScriptDefinition, [ref] $token, [ref] $parseErrors)
$null = [System.Management.Automation.Language.Parser]::ParseInput($ScriptDefinition, [ref] $token, [ref] $parseErrors)

if ($parseErrors)
{
Expand Down
34 changes: 34 additions & 0 deletions tests/Unit/Public/Measure-Keyword.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,24 @@ Describe 'Measure-Keyword' {
($record | Measure-Object).Count | Should -Be 0
}
}

Context 'When another word contains ''base'' but has other characters preceeding it' {
It 'Should not return an error record' {
$definition = '
class SqlSetupBase
{
SqlSetupBase() : base ()
{
Write-Verbose -Message "Example found."
}
}
'

$token = Get-TokensFromDefinition -ScriptDefinition $definition
$record = Measure-Keyword -Token $token
($record | Measure-Object).Count | Should -Be 0
}
}
}

Context 'When calling PSScriptAnalyzer' {
Expand Down Expand Up @@ -190,6 +208,22 @@ Describe 'Measure-Keyword' {
($record | Measure-Object).Count | Should -Be 0
}
}

Context 'When another word contains ''base'' but has other characters preceeding it' {
It 'Should not return an error record' {
$invokeScriptAnalyzerParameters['ScriptDefinition'] = '
class SqlSetupBase
{
SqlSetupBase() : base ()
{
Write-Verbose -Message "Example found."
}
}
'
$record = Invoke-ScriptAnalyzer @invokeScriptAnalyzerParameters
($record | Measure-Object).Count | Should -Be 0
}
}
}
}
}

0 comments on commit 7b635dd

Please sign in to comment.