Error when '=' syntax is used to define UDT (#2721)

Today we allow something like `T = Type(Number)` which is incorrect and
should not be allowed . this PR adds error to that scenario

---------
This commit is contained in:
Adithya Selvaprithiviraj 2024-10-29 11:41:12 -07:00 коммит произвёл GitHub
Родитель 90441fd95d
Коммит bc2b841304
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
4 изменённых файлов: 28 добавлений и 0 удалений

Просмотреть файл

@ -853,5 +853,6 @@ namespace Microsoft.PowerFx.Core.Localization
public static ErrorResourceKey ErrReachedMaxJsonLength = new ErrorResourceKey("ErrReachedMaxJsonLength");
public static ErrorResourceKey ErrUserDefinedTypesDisabled = new ErrorResourceKey("ErrUserDefinedTypesDisabled");
public static ErrorResourceKey ErrUserDefinedTypeIncorrectSyntax = new ErrorResourceKey("ErrUserDefinedTypeIncorrectSyntax");
}
}

Просмотреть файл

@ -362,6 +362,8 @@ namespace Microsoft.PowerFx.Core.Parser
}
else if (_curs.TidCur == TokKind.Equ)
{
var equalToken = _curs.TokCur;
var declaration = script.Substring(declarationStart, _curs.TokCur.Span.Min - declarationStart);
_curs.TokMove();
definitionBeforeTrivia.Add(ParseTrivia());
@ -385,6 +387,12 @@ namespace Microsoft.PowerFx.Core.Parser
definitionBeforeTrivia.Add(ParseTrivia());
var result = ParseExpr(Precedence.None);
if (result is TypeLiteralNode _)
{
CreateError(equalToken, TexlStrings.ErrUserDefinedTypeIncorrectSyntax);
continue;
}
namedFormulas.Add(new NamedFormula(thisIdentifier.As<IdentToken>(), new Formula(result.GetCompleteSpan().GetFragment(script), result), _startingIndex, attribute));
userDefinitionSourceInfos.Add(new UserDefinitionSourceInfo(index++, UserDefinitionType.NamedFormula, thisIdentifier.As<IdentToken>(), declaration, new SourceList(definitionBeforeTrivia), GetExtraTriviaSourceList()));
definitionBeforeTrivia = new List<ITexlSource>();

Просмотреть файл

@ -4846,4 +4846,11 @@
<value>The 'User-defined types' experimental feature is not enabled.</value>
<comment>Error message returned when user uses User-defined types with the User-defined types feature flag turned off.</comment>
</data>
<data name="ErrUserDefinedTypeIncorrectSyntax" xml:space="preserve">
<value>Incorrect syntax: '=' cannot be used to declare user-defined types. Use ':=' instead.</value>
<comment>
Error message returned when user uses '=' syntax to define User-defined type.
Some examples for valid named type declarations - "Point := Type({x: Number, y: Number});" , "T1 := Type(Number);" , "T2 := Type([Boolean]);".
</comment>
</data>
</root>

Просмотреть файл

@ -1014,5 +1014,17 @@ namespace Microsoft.PowerFx.Core.Tests
Assert.Equal(expectErrors, parseResult.HasErrors);
Assert.Equal(isImperative, parseResult.UDFs.First().IsImperative);
}
[Theory]
[InlineData("A = Type(Number);", 0)]
[InlineData("A = \"hello\";B = Type([Boolean]); C = 5;", 2)]
public void TestTypeLiteralInNamedFormula(string script, int namedFormulaCount)
{
var parserOptions = new ParserOptions();
var parseResult = UserDefinitions.Parse(script, parserOptions, Features.PowerFxV1);
Assert.True(parseResult.HasErrors);
Assert.Equal(namedFormulaCount, parseResult.NamedFormulas.Count());
Assert.Contains(parseResult.Errors, e => e.MessageKey.Contains("ErrUserDefinedTypeIncorrectSyntax"));
}
}
}