Skip to content

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

Merged
merged 2 commits into from
Apr 21, 2018

Conversation

Kingwl
Copy link
Contributor

@Kingwl Kingwl commented Jan 30, 2018

Fixes #20784

@mhegazy
Copy link
Contributor

mhegazy commented Jan 30, 2018

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 getEnumKind.

also @RyanCavanaugh, did we decide to reflect the concatenation in the type, i.e. is enum E { a = "value" + "one" } the same type as the literal const v: "valueone" = E.a ?

@@ -22962,9 +22962,16 @@ namespace ts {
case SyntaxKind.AsteriskAsteriskToken: return left ** right;
}
}
if (typeof left === "string" && typeof right === "string") {
switch ((<BinaryExpression>expr).operatorToken.kind) {
Copy link
Contributor

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.

@@ -22962,9 +22962,16 @@ namespace ts {
case SyntaxKind.AsteriskAsteriskToken: return left ** right;
}
}
if (typeof left === "string" && typeof right === "string") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else if

break;
case SyntaxKind.StringLiteral:
return (<StringLiteral>expr).text;
case SyntaxKind.NoSubstitutionTemplateLiteral:
Copy link
Contributor

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.

@RyanCavanaugh
Copy link
Member

also @RyanCavanaugh, did we decide to reflect the concatenation in the type, i.e. is enum E { a = "value" + "one" } the same type as the literal const v: "valueone" = E.a ?

@mhegazy Seems right. I don't see what the alternative would even be

@mhegazy
Copy link
Contributor

mhegazy commented Jan 30, 2018

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

@Kingwl Kingwl force-pushed the concat-string-in-enum-member branch from 6b9bf89 to aa10919 Compare January 31, 2018 01:48
@Kingwl
Copy link
Contributor Author

Kingwl commented Jan 31, 2018

EnumKind only have Numeric and Literal kind, may i append a String Kind?

const enum EnumKind {
    Numeric,                            // Numeric enum (each member has a TypeFlags.Enum type)
    Literal                             // Literal enum (each member has a TypeFlags.EnumLiteral type)
}

@mhegazy
Copy link
Contributor

mhegazy commented Jan 31, 2018

Well.. if we do what @RyanCavanaugh was suggestion it should be a Literal enum, just the type will be the concatenation of all the parts.

@Kingwl Kingwl force-pushed the concat-string-in-enum-member branch 2 times, most recently from 03d9f72 to 6a2f996 Compare January 31, 2018 10:49
@Kingwl
Copy link
Contributor Author

Kingwl commented Feb 22, 2018

ping @mhegazy
still need review 😅

@joseSantacruz
Copy link

+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

Copy link
Member

@DanielRosenwasser DanielRosenwasser left a 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);
Copy link
Member

@DanielRosenwasser DanielRosenwasser Apr 16, 2018

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

@Kingwl
Copy link
Contributor Author

Kingwl commented Apr 16, 2018

seems this future can be merged with #6805

@mhegazy
Copy link
Contributor

mhegazy commented Apr 18, 2018

Can u add a test for declaration emit.

}
else if (expr.kind === SyntaxKind.BinaryExpression) {
const binaryExpression = <BinaryExpression>expr;
return binaryExpression.operatorToken.kind === SyntaxKind.PlusToken && isStringConcatExpression(binaryExpression.left) && isStringConcatExpression(binaryExpression.right);
Copy link
Contributor

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.

@Kingwl
Copy link
Contributor Author

Kingwl commented Apr 18, 2018

Can u add a test for declaration emit.

could you take a example? i'm not sure that i catch your point

@mhegazy
Copy link
Contributor

mhegazy commented Apr 18, 2018

A test with // @declaration: true
This will generate the .d.ts file for the enum, which should use the constant value.

@Kingwl Kingwl force-pushed the concat-string-in-enum-member branch from 6764b30 to 3190b57 Compare April 18, 2018 10:37
@mhegazy
Copy link
Contributor

mhegazy commented Apr 18, 2018

One failing test that needs attention.

@Kingwl Kingwl force-pushed the concat-string-in-enum-member branch from 3190b57 to 2689a99 Compare April 19, 2018 01:19
@Kingwl
Copy link
Contributor Author

Kingwl commented Apr 19, 2018

Everything is ok in my pc... i'm trying to fix that

@Kingwl Kingwl force-pushed the concat-string-in-enum-member branch from 2689a99 to 3371f3d Compare April 19, 2018 01:41
@Kingwl
Copy link
Contributor Author

Kingwl commented Apr 19, 2018

what the fuck is happened 😂

@Kingwl Kingwl force-pushed the concat-string-in-enum-member branch from 3371f3d to f658dbd Compare April 19, 2018 10:53
@Kingwl Kingwl force-pushed the concat-string-in-enum-member branch from f658dbd to 2455405 Compare April 19, 2018 13:49
@mhegazy
Copy link
Contributor

mhegazy commented Apr 20, 2018

i think you just undid your change to add // @declaration: true to the test. is that intentional? anything i can help with there?

@mhegazy mhegazy merged commit 7f34340 into microsoft:master Apr 21, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
@Kingwl Kingwl deleted the concat-string-in-enum-member branch April 28, 2019 15:43
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants