-
Notifications
You must be signed in to change notification settings - Fork 13.1k
allow string concat in enum member declaration #21476
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
allow string concat in enum member declaration #21476
Conversation
|
if the first entry in the enum is a string literal you will still get the error for string enums not allowing expressions. you need to revisit the implementation of also @RyanCavanaugh, did we decide to reflect the concatenation in the type, i.e. is |
src/compiler/checker.ts
Outdated
| } | ||
| } | ||
| if (typeof left === "string" && typeof right === "string") { | ||
| switch ((<BinaryExpression>expr).operatorToken.kind) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i would make this a simple if statement.. and consider merging it with typeof left === "string" && typeof right === "string.
src/compiler/checker.ts
Outdated
| case SyntaxKind.AsteriskAsteriskToken: return left ** right; | ||
| } | ||
| } | ||
| if (typeof left === "string" && typeof right === "string") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
else if
src/compiler/checker.ts
Outdated
| break; | ||
| case SyntaxKind.StringLiteral: | ||
| return (<StringLiteral>expr).text; | ||
| case SyntaxKind.NoSubstitutionTemplateLiteral: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make sure getEnumKind handles NoSubstitutionTemplateLiteral correctly, and add a test for an literal enum with only template literals. I would recommend doing this in a separate PR instead, since this is not really the change originally intended.
@mhegazy Seems right. I don't see what the alternative would even be |
well, it will be a non-union (non-literal type) enum.. I am asking cause we do not do this literal type generation from parts any where else |
6b9bf89 to
aa10919
Compare
|
EnumKind only have Numeric and Literal kind, may i append a String Kind? |
|
Well.. if we do what @RyanCavanaugh was suggestion it should be a |
03d9f72 to
6a2f996
Compare
|
ping @mhegazy |
|
+1 to this new feature is useful when you want to restrict something that changes base on env variables for example database tables names on dynamoDB where you don't have the database context only a full list of tables |
DanielRosenwasser
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Almost good to go!
| return true; | ||
| } | ||
| else if (expr.kind === SyntaxKind.BinaryExpression) { | ||
| return isStringConcatExpression((<BinaryExpression>expr).left) && isStringConcatExpression((<BinaryExpression>expr).right); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll be pedantic even though this is only part of what determines whether an enum type is a literal enum type: but think you should also check whether the token is a PlusToken
|
seems this future can be merged with #6805 |
|
Can u add a test for declaration emit. |
src/compiler/checker.ts
Outdated
| } | ||
| else if (expr.kind === SyntaxKind.BinaryExpression) { | ||
| const binaryExpression = <BinaryExpression>expr; | ||
| return binaryExpression.operatorToken.kind === SyntaxKind.PlusToken && isStringConcatExpression(binaryExpression.left) && isStringConcatExpression(binaryExpression.right); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use isBinaryExpression and avoid the cast.
could you take a example? i'm not sure that i catch your point |
|
A test with // @declaration: true |
6764b30 to
3190b57
Compare
|
One failing test that needs attention. |
3190b57 to
2689a99
Compare
|
Everything is ok in my pc... i'm trying to fix that |
2689a99 to
3371f3d
Compare
|
what |
3371f3d to
f658dbd
Compare
f658dbd to
2455405
Compare
|
i think you just undid your change to add |
Fixes #20784