mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Fix error handling in parsing 'using' statement (#3007)
This commit is contained in:
@@ -4414,8 +4414,11 @@ namespace System.Management.Automation.Language
|
||||
{
|
||||
case TokenKind.EndOfInput:
|
||||
case TokenKind.NewLine:
|
||||
// Example, using module ,FooBar
|
||||
// Example: 'using module ,FooBar'
|
||||
// GetCommandArgument will successfully return an argument for a unary array argument
|
||||
// but we don't want to allow that syntax with a using statement.
|
||||
case TokenKind.Comma:
|
||||
case TokenKind.Semi:
|
||||
{
|
||||
ReportIncompleteInput(After(directiveToken), () => ParserStrings.MissingUsingItemName);
|
||||
return new ErrorStatementAst(ExtentOf(usingToken, directiveToken));
|
||||
@@ -4426,11 +4429,14 @@ namespace System.Management.Automation.Language
|
||||
if (itemAst == null)
|
||||
{
|
||||
ReportError(itemToken.Extent, () => ParserStrings.InvalidValueForUsingItemName, itemToken.Text);
|
||||
// ErrorRecovery: If there is no identifier, skip whole 'using' line
|
||||
SyncOnError(true, TokenKind.Semi, TokenKind.NewLine);
|
||||
return new ErrorStatementAst(ExtentOf(usingToken, itemToken.Extent));
|
||||
}
|
||||
|
||||
if (!(itemAst is StringConstantExpressionAst) && (kind != UsingStatementKind.Module || !(itemAst is HashtableAst)))
|
||||
{
|
||||
ReportError(ExtentOf(usingToken, ExtentFromFirstOf(itemAst, itemToken)), () => ParserStrings.InvalidValueForUsingItemName, itemAst.Extent.Text);
|
||||
ReportError(ExtentFromFirstOf(itemAst, itemToken), () => ParserStrings.InvalidValueForUsingItemName, itemAst.Extent.Text);
|
||||
return new ErrorStatementAst(ExtentOf(usingToken, ExtentFromFirstOf(itemAst, itemToken)));
|
||||
}
|
||||
|
||||
|
||||
@@ -261,6 +261,29 @@ New-Object Foo
|
||||
$err[1].ErrorId | Should Be 'RequiresModuleInvalid'
|
||||
}
|
||||
|
||||
It "report an error when 'using module' terminating by NewLine" {
|
||||
$err = Get-ParseResults "using module"
|
||||
$err.Count | Should Be 1
|
||||
$err[0].ErrorId | Should Be 'MissingUsingItemName'
|
||||
}
|
||||
|
||||
It "report an error when 'using module' terminating by Semicolon" {
|
||||
$err = Get-ParseResults "using module; $testvar=1"
|
||||
$err.Count | Should Be 1
|
||||
$err[0].ErrorId | Should Be 'MissingUsingItemName'
|
||||
}
|
||||
|
||||
It "report an error when a value after 'using module' is a unallowed expression" {
|
||||
$err = Get-ParseResults "using module )"
|
||||
$err.Count | Should Be 1
|
||||
$err[0].ErrorId | Should Be 'InvalidValueForUsingItemName'
|
||||
}
|
||||
|
||||
It "report an error when a value after 'using module' is not a valid module name" {
|
||||
$err = Get-ParseResults "using module 123"
|
||||
$err.Count | Should Be 1
|
||||
$err[0].ErrorId | Should Be 'InvalidValueForUsingItemName'
|
||||
}
|
||||
}
|
||||
|
||||
Context 'short name in case of name collision' {
|
||||
|
||||
Reference in New Issue
Block a user