Exposing a DisableVariableSubstitution on the Parser object (#1941)

* Cleaning up a few tests; fixing one

* Exposing DisableVariableSubstitution in Parser; fixing NRE; adding tests

* Added another test

* Addressing CR feedback; added tests
This commit is contained in:
Matteo Taveggia
2023-03-13 22:04:04 -07:00
committed by GitHub
parent d5266ef6f3
commit ffed8313d9
4 changed files with 205 additions and 14 deletions

View File

@@ -34,8 +34,18 @@ namespace Microsoft.SqlTools.ServiceLayer.BatchParser
this.variableResolver = variableResolver;
lexer = new Lexer(reader, name);
tokenBuffer = new List<Token>();
DisableVariableSubstitution = variableResolver == null;
}
/// <summary>
/// Whether to enable or disable the variable substitution.
/// </summary>
public bool DisableVariableSubstitution { get; set; }
/// <summary>
/// Whether to throw an error when a variable to be substituted was not specified.
/// </summary>
/// <remarks>When DisableVariableSubstitution is true, this value is ignored, i.e. no exception will be thrown.</remarks>
public bool ThrowOnUnresolvedVariable { get; set; }
private Token LookaheadToken
@@ -110,9 +120,9 @@ namespace Microsoft.SqlTools.ServiceLayer.BatchParser
private void AddVariableReferences(Token token, int offset, IList<VariableReference> variableRefs)
{
if (lexer.RecognizeSqlCmdSyntax == false)
if (!lexer.RecognizeSqlCmdSyntax || DisableVariableSubstitution)
{
// variables are recognized only in sqlcmd mode.
// variables are recognized only in sqlcmd mode and the substitution was not explicitly disabled
return;
}
@@ -649,11 +659,15 @@ namespace Microsoft.SqlTools.ServiceLayer.BatchParser
column: column,
offset: reference.Start - offset,
filename: inputToken.Filename);
string value = variableResolver.GetVariable(variablePos.Value, reference.VariableName);
// If the variableResolver is not defined (=null), then we are a little
// more robust and avoid throwing a NRE. It may just mean that the caller
// did not bother implementing the IVariableResolver interface, presumably
// because they were not interested in any variable replacement.
string value = variableResolver?.GetVariable(variablePos.Value, reference.VariableName);
if (value == null)
{
// Undefined variable
if (ThrowOnUnresolvedVariable == true)
if (ThrowOnUnresolvedVariable)
{
RaiseError(ErrorCode.VariableNotDefined, inputToken,
string.Format(CultureInfo.CurrentCulture, SR.BatchParser_VariableNotDefined, reference.VariableName));