-
Notifications
You must be signed in to change notification settings - Fork 399
UseConsistentWhitespace - Create option to ignore assignment operator inside hash table #1566
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
Changes from 3 commits
5dc3cd3
b9aa6e7
759c712
7ef643b
047ae5e
94329b0
cc9ebd7
ff96591
3594ed5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -232,5 +232,85 @@ private bool OnSameLine(Token token1, Token token2) | |
{ | ||
return token1.Extent.StartLineNumber == token2.Extent.EndLineNumber; | ||
} | ||
|
||
/// <summary> | ||
/// Finds the position of a given token in the AST. | ||
/// </summary> | ||
/// <param name="token">The <see cref="Token"/> to search for.</param> | ||
/// <returns>The Ast node directly containing the provided <see cref="Token"/>.</returns> | ||
public Ast GetAstPosition(Token token) | ||
{ | ||
FindAstPostitionVisitor findAstVisitor = new FindAstPostitionVisitor(token.Extent.StartScriptPosition); | ||
ast.Visit(findAstVisitor); | ||
return findAstVisitor.AstPosition; | ||
} | ||
|
||
} | ||
|
||
/// <summary> | ||
/// Provides an efficient way to find the position in the AST corresponding to a given script position. | ||
/// </summary> | ||
public class FindAstPostitionVisitor : AstVisitor2 | ||
{ | ||
private IScriptPosition searchPosition; | ||
|
||
/// <summary> | ||
/// Contains the position in the AST corresponding to the provided <see cref="IScriptPosition"/> upon completion of the <see cref="Ast.Visit(AstVisitor)"/> method. | ||
/// </summary> | ||
public Ast AstPosition { get; private set; } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="FindAstPostitionVisitor"/> class with the postition to search for. | ||
/// </summary> | ||
/// <param name="position">The script position to search for.</param> | ||
public FindAstPostitionVisitor(IScriptPosition position) | ||
{ | ||
this.searchPosition = position; | ||
} | ||
|
||
/// <summary> | ||
/// Traverses the AST based on offests to find the leaf node which contains the provided <see cref="IScriptPosition"/>. | ||
/// This method implements the entire functionality of this visitor. All <see cref="AstVisitor2"/> methods are overridden to simply invoke this one. | ||
/// </summary> | ||
/// <param name="ast">Current AST node to process.</param> | ||
/// <returns>An <see cref="AstVisitAction"/> indicating whether to visit children of the current node.</returns> | ||
private AstVisitAction Visit(Ast ast) | ||
{ | ||
if (ast.Extent.StartOffset > searchPosition.Offset || ast.Extent.EndOffset < searchPosition.Offset) | ||
{ | ||
return AstVisitAction.SkipChildren; | ||
} | ||
else | ||
{ | ||
AstPosition = ast; | ||
return AstVisitAction.Continue; | ||
} | ||
} | ||
|
||
public override AstVisitAction VisitScriptBlock(ScriptBlockAst scriptBlockAst) | ||
{ | ||
return Visit(scriptBlockAst); | ||
} | ||
|
||
public override AstVisitAction VisitNamedBlock(NamedBlockAst namedBlockAst) | ||
{ | ||
return Visit(namedBlockAst); | ||
} | ||
|
||
public override AstVisitAction VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst) | ||
{ | ||
return Visit(assignmentStatementAst); | ||
} | ||
|
||
public override AstVisitAction VisitCommandExpression(CommandExpressionAst commandExpressionAst) | ||
{ | ||
return Visit(commandExpressionAst); | ||
} | ||
|
||
public override AstVisitAction VisitHashtable(HashtableAst hashtableAst) | ||
{ | ||
return Visit(hashtableAst); | ||
} | ||
|
||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
Describe "TokenOperations" { | ||
It "Should return correct AST position for assignment operator in hash table" { | ||
$scriptText = @' | ||
$h = @{ | ||
a = 72 | ||
b = @{ z = "hi" } | ||
} | ||
'@ | ||
$tokens = $null | ||
$parseErrors = $null | ||
$scriptAst = [System.Management.Automation.Language.Parser]::ParseInput($scriptText, [ref] $tokens, [ref] $parseErrors) | ||
$tokenOperations = New-Object Microsoft.Windows.PowerShell.ScriptAnalyzer.TokenOperations -ArgumentList @($tokens, $scriptAst) | ||
$operatorToken = $tokens | Where-Object { $_.Extent.StartOffset -eq 32 } | ||
$hashTableAst = $tokenOperations.GetAstPosition($operatorToken) | ||
$hashTableAst | Should -BeOfType [System.Management.Automation.Language.HashTableAst] | ||
$hashTableAst.Extent.Text | Should -Be '@{ z = "hi" }' | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.