📄 shared.ts¶
📊 Analysis Summary¶
| Metric | Count |
|---|---|
| 🔧 Functions | 4 |
| 📦 Imports | 1 |
| 📊 Variables & Constants | 5 |
📚 Table of Contents¶
🛠️ File Location:¶
📂 packages/eslint-plugin/src/rules/enum-utils/shared.ts
📦 Imports¶
| Name | Source |
|---|---|
isTypeFlagSet |
../../util |
Variables & Constants¶
| Name | Type | Kind | Value | Exported |
|---|---|---|---|---|
symbol |
any |
const | type.getSymbol()! |
✗ |
memberDeclaration |
ts.EnumMember |
const | symbol.valueDeclaration as ts.EnumMember |
✗ |
enumDeclaration |
any |
const | memberDeclaration.parent |
✗ |
memberNameIdentifier |
any |
const | memberDeclaration.name |
✗ |
enumName |
any |
const | enumDeclaration.name.text |
✗ |
Functions¶
getBaseEnumType(typeChecker: ts.TypeChecker, type: ts.Type): ts.Type¶
Code
function getBaseEnumType(typeChecker: ts.TypeChecker, type: ts.Type): ts.Type {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const symbol = type.getSymbol()!;
if (!tsutils.isSymbolFlagSet(symbol, ts.SymbolFlags.EnumMember)) {
return type;
}
return typeChecker.getTypeAtLocation(
(symbol.valueDeclaration as ts.EnumMember).parent,
);
}
- Parameters:
typeChecker: ts.TypeCheckertype: ts.Type- Return Type:
ts.Type - Calls:
type.getSymboltsutils.isSymbolFlagSettypeChecker.getTypeAtLocation- Internal Comments:
getEnumLiterals(type: ts.Type): ts.LiteralType[]¶
Code
-
JSDoc:
/** * Retrieve only the Enum literals from a type. for example: * - 123 --> [] * - {} --> [] * - Fruit.Apple --> [Fruit.Apple] * - Fruit.Apple | Vegetable.Lettuce --> [Fruit.Apple, Vegetable.Lettuce] * - Fruit.Apple | Vegetable.Lettuce | 123 --> [Fruit.Apple, Vegetable.Lettuce] * - T extends Fruit --> [Fruit] */ -
Parameters:
type: ts.Type- Return Type:
ts.LiteralType[] - Calls:
tsutils .unionConstituents(type) .filterisTypeFlagSet (from ../../util)
getEnumTypes(typeChecker: ts.TypeChecker, type: ts.Type): ts.Type[]¶
Code
-
JSDoc:
-
Parameters:
typeChecker: ts.TypeCheckertype: ts.Type- Return Type:
ts.Type[] - Calls:
getEnumLiterals(type).mapgetBaseEnumType
getEnumKeyForLiteral(enumLiterals: ts.LiteralType[], literal: unknown): string | null¶
Code
export function getEnumKeyForLiteral(
enumLiterals: ts.LiteralType[],
literal: unknown,
): string | null {
for (const enumLiteral of enumLiterals) {
if (enumLiteral.value === literal) {
const { symbol } = enumLiteral;
const memberDeclaration = symbol.valueDeclaration as ts.EnumMember;
const enumDeclaration = memberDeclaration.parent;
const memberNameIdentifier = memberDeclaration.name;
const enumName = enumDeclaration.name.text;
switch (memberNameIdentifier.kind) {
case ts.SyntaxKind.Identifier:
return `${enumName}.${memberNameIdentifier.text}`;
case ts.SyntaxKind.StringLiteral: {
const memberName = memberNameIdentifier.text.replaceAll("'", "\\'");
return `${enumName}['${memberName}']`;
}
case ts.SyntaxKind.ComputedPropertyName:
return `${enumName}[${memberNameIdentifier.expression.getText()}]`;
default:
break;
}
}
}
return null;
}
-
JSDoc:
/** * Returns the enum key that matches the given literal node, or null if none * match. For example: * ```ts * enum Fruit { * Apple = 'apple', * Banana = 'banana', * } * * getEnumKeyForLiteral([Fruit.Apple, Fruit.Banana], 'apple') --> 'Fruit.Apple' * getEnumKeyForLiteral([Fruit.Apple, Fruit.Banana], 'banana') --> 'Fruit.Banana' * getEnumKeyForLiteral([Fruit.Apple, Fruit.Banana], 'cherry') --> null * ``` */ -
Parameters:
enumLiterals: ts.LiteralType[]literal: unknown- Return Type:
string | null - Calls:
memberNameIdentifier.text.replaceAllmemberNameIdentifier.expression.getText