Skip to content

Commit a187291

Browse files
committed
use enum member initializer if present
1 parent 7562951 commit a187291

File tree

3 files changed

+299
-7
lines changed

3 files changed

+299
-7
lines changed

src/compiler/checker.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8127,8 +8127,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
81278127

81288128
function typeParameterToDeclaration(type: TypeParameter, context: NodeBuilderContext, constraint = getConstraintOfTypeParameter(type)): TypeParameterDeclaration {
81298129
const constraintNode = constraint && typeToTypeNodeHelperWithPossibleReusableTypeNode(constraint, getConstraintDeclaration(type), context);
8130-
const typeParam = typeParameterToDeclarationWithConstraint(type, context, constraintNode);
8131-
return typeParam;
8130+
return typeParameterToDeclarationWithConstraint(type, context, constraintNode);
81328131
}
81338132

81348133
function typePredicateToTypePredicateNodeHelper(typePredicate: TypePredicate, context: NodeBuilderContext): TypePredicateNode {
@@ -9878,12 +9877,24 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
98789877
// I hate that to get the initialized value we need to walk back to the declarations here; but there's no
98799878
// other way to get the possible const value of an enum member that I'm aware of, as the value is cached
98809879
// _on the declaration_, not on the declaration's symbol...
9881-
const initializedValue = p.declarations && p.declarations[0] && isEnumMember(p.declarations[0]) ? getConstantValue(p.declarations[0]) : undefined;
9882-
const initializer = initializedValue === undefined ? undefined :
9883-
typeof initializedValue === "string" ? factory.createStringLiteral(initializedValue) :
9884-
factory.createNumericLiteral(initializedValue);
9880+
const memberDecl = p.declarations && p.declarations[0] && isEnumMember(p.declarations[0]) ?
9881+
p.declarations[0] :
9882+
undefined;
9883+
let initializer: Expression | undefined;
9884+
let initializerLength: number;
9885+
if (isUnfolding(context) && memberDecl && memberDecl.initializer) {
9886+
initializer = visitNode(memberDecl.initializer, factory.cloneNode, isExpression);
9887+
initializerLength = memberDecl.initializer.end - memberDecl.initializer.pos;
9888+
}
9889+
else {
9890+
const initializedValue = memberDecl && getConstantValue(memberDecl);
9891+
initializer = initializedValue === undefined ? undefined :
9892+
typeof initializedValue === "string" ? factory.createStringLiteral(initializedValue) :
9893+
factory.createNumericLiteral(initializedValue);
9894+
initializerLength = (initializer as StringLiteral | NumericLiteral | undefined)?.text.length ?? 0
9895+
}
98859896
const memberName = unescapeLeadingUnderscores(p.escapedName);
9886-
context.approximateLength += 4 + memberName.length + (initializer?.text.length ?? 0); // `member = initializer,`
9897+
context.approximateLength += 4 + memberName.length + initializerLength; // `member = initializer,`
98879898
const member = factory.createEnumMember(
98889899
memberName,
98899900
initializer,

tests/baselines/reference/quickinfoVerbosityEnum.baseline

Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,27 @@
5858
// | const y: Direction
5959
// | (verbosity level: 0)
6060
// | ----------------------------------------------------------------------
61+
// enum Flags {
62+
// ^^^^^
63+
// | ----------------------------------------------------------------------
64+
// | enum Flags {
65+
// | None = 0,
66+
// | IsDirectory = 1 << 0,
67+
// | IsFile = 1 << 1,
68+
// | IsSymlink = 1 << 2
69+
// | }
70+
// | (verbosity level: 1)
71+
// | ----------------------------------------------------------------------
72+
// ^^^^^
73+
// | ----------------------------------------------------------------------
74+
// | enum Flags
75+
// | (verbosity level: 0)
76+
// | ----------------------------------------------------------------------
77+
// None = 0,
78+
// IsDirectory = 1 << 0,
79+
// IsFile = 1 << 1,
80+
// IsSymlink = 1 << 2,
81+
// }
6182

6283
[
6384
{
@@ -651,5 +672,257 @@
651672
"canIncreaseVerbosityLevel": false,
652673
"verbosityLevel": 1
653674
}
675+
},
676+
{
677+
"marker": {
678+
"fileName": "/tests/cases/fourslash/quickinfoVerbosityEnum.ts",
679+
"position": 161,
680+
"name": "f"
681+
},
682+
"item": {
683+
"kind": "enum",
684+
"kindModifiers": "",
685+
"textSpan": {
686+
"start": 156,
687+
"length": 5
688+
},
689+
"displayParts": [
690+
{
691+
"text": "enum",
692+
"kind": "keyword"
693+
},
694+
{
695+
"text": " ",
696+
"kind": "space"
697+
},
698+
{
699+
"text": "Flags",
700+
"kind": "enumName"
701+
}
702+
],
703+
"documentation": [],
704+
"canIncreaseVerbosityLevel": true,
705+
"verbosityLevel": 0
706+
}
707+
},
708+
{
709+
"marker": {
710+
"fileName": "/tests/cases/fourslash/quickinfoVerbosityEnum.ts",
711+
"position": 161,
712+
"name": "f"
713+
},
714+
"item": {
715+
"kind": "enum",
716+
"kindModifiers": "",
717+
"textSpan": {
718+
"start": 156,
719+
"length": 5
720+
},
721+
"displayParts": [
722+
{
723+
"text": "enum",
724+
"kind": "keyword"
725+
},
726+
{
727+
"text": " ",
728+
"kind": "space"
729+
},
730+
{
731+
"text": "Flags",
732+
"kind": "text"
733+
},
734+
{
735+
"text": " ",
736+
"kind": "space"
737+
},
738+
{
739+
"text": "{",
740+
"kind": "punctuation"
741+
},
742+
{
743+
"text": "\n",
744+
"kind": "lineBreak"
745+
},
746+
{
747+
"text": " ",
748+
"kind": "space"
749+
},
750+
{
751+
"text": "None",
752+
"kind": "text"
753+
},
754+
{
755+
"text": " ",
756+
"kind": "space"
757+
},
758+
{
759+
"text": "=",
760+
"kind": "operator"
761+
},
762+
{
763+
"text": " ",
764+
"kind": "space"
765+
},
766+
{
767+
"text": "0",
768+
"kind": "stringLiteral"
769+
},
770+
{
771+
"text": ",",
772+
"kind": "punctuation"
773+
},
774+
{
775+
"text": "\n",
776+
"kind": "lineBreak"
777+
},
778+
{
779+
"text": " ",
780+
"kind": "space"
781+
},
782+
{
783+
"text": "IsDirectory",
784+
"kind": "text"
785+
},
786+
{
787+
"text": " ",
788+
"kind": "space"
789+
},
790+
{
791+
"text": "=",
792+
"kind": "operator"
793+
},
794+
{
795+
"text": " ",
796+
"kind": "space"
797+
},
798+
{
799+
"text": "1",
800+
"kind": "stringLiteral"
801+
},
802+
{
803+
"text": " ",
804+
"kind": "space"
805+
},
806+
{
807+
"text": "<<",
808+
"kind": "operator"
809+
},
810+
{
811+
"text": " ",
812+
"kind": "space"
813+
},
814+
{
815+
"text": "0",
816+
"kind": "stringLiteral"
817+
},
818+
{
819+
"text": ",",
820+
"kind": "punctuation"
821+
},
822+
{
823+
"text": "\n",
824+
"kind": "lineBreak"
825+
},
826+
{
827+
"text": " ",
828+
"kind": "space"
829+
},
830+
{
831+
"text": "IsFile",
832+
"kind": "text"
833+
},
834+
{
835+
"text": " ",
836+
"kind": "space"
837+
},
838+
{
839+
"text": "=",
840+
"kind": "operator"
841+
},
842+
{
843+
"text": " ",
844+
"kind": "space"
845+
},
846+
{
847+
"text": "1",
848+
"kind": "stringLiteral"
849+
},
850+
{
851+
"text": " ",
852+
"kind": "space"
853+
},
854+
{
855+
"text": "<<",
856+
"kind": "operator"
857+
},
858+
{
859+
"text": " ",
860+
"kind": "space"
861+
},
862+
{
863+
"text": "1",
864+
"kind": "stringLiteral"
865+
},
866+
{
867+
"text": ",",
868+
"kind": "punctuation"
869+
},
870+
{
871+
"text": "\n",
872+
"kind": "lineBreak"
873+
},
874+
{
875+
"text": " ",
876+
"kind": "space"
877+
},
878+
{
879+
"text": "IsSymlink",
880+
"kind": "text"
881+
},
882+
{
883+
"text": " ",
884+
"kind": "space"
885+
},
886+
{
887+
"text": "=",
888+
"kind": "operator"
889+
},
890+
{
891+
"text": " ",
892+
"kind": "space"
893+
},
894+
{
895+
"text": "1",
896+
"kind": "stringLiteral"
897+
},
898+
{
899+
"text": " ",
900+
"kind": "space"
901+
},
902+
{
903+
"text": "<<",
904+
"kind": "operator"
905+
},
906+
{
907+
"text": " ",
908+
"kind": "space"
909+
},
910+
{
911+
"text": "2",
912+
"kind": "stringLiteral"
913+
},
914+
{
915+
"text": "\n",
916+
"kind": "lineBreak"
917+
},
918+
{
919+
"text": "}",
920+
"kind": "punctuation"
921+
}
922+
],
923+
"documentation": [],
924+
"canIncreaseVerbosityLevel": false,
925+
"verbosityLevel": 1
926+
}
654927
}
655928
]

tests/cases/fourslash/quickinfoVerbosityEnum.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,17 @@
1313
//// }
1414
//// const y/*y*/: Direction = Direction.Up;
1515

16+
//// enum Flags/*f*/ {
17+
//// None = 0,
18+
//// IsDirectory = 1 << 0,
19+
//// IsFile = 1 << 1,
20+
//// IsSymlink = 1 << 2,
21+
//// }
22+
1623
verify.baselineQuickInfo({
1724
c: [0, 1],
1825
x: [0, 1],
1926
d: [0, 1],
2027
y: [0, 1],
28+
f: [0, 1],
2129
});

0 commit comments

Comments
 (0)