β¬ οΈ Back to Table of Contents
π no-magic-numbers.ts
¶
π Analysis Summary¶
Metric | Count |
---|---|
π§ Functions | 10 |
π¦ Imports | 8 |
π Variables & Constants | 5 |
π Type Aliases | 2 |
π Table of Contents¶
π οΈ File Location:¶
π packages/eslint-plugin/src/rules/no-magic-numbers.ts
π¦ Imports¶
Name | Source |
---|---|
TSESTree |
@typescript-eslint/utils |
JSONSchema4 |
@typescript-eslint/utils/json-schema |
AST_NODE_TYPES |
@typescript-eslint/utils |
InferMessageIdsTypeFromRule |
../util |
InferOptionsTypeFromRule |
../util |
createRule |
../util |
deepMerge |
../util |
getESLintCoreRule |
../util/getESLintCoreRule |
Variables & Constants¶
Name | Type | Kind | Value | Exported |
---|---|---|---|---|
schema |
JSONSchema4 |
const | `deepMerge( | |
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument -- https://github.com/microsoft/TypeScript/issues/17002 | ||||
Array.isArray(baseRule.meta.schema) | ||||
? baseRule.meta.schema[0] | ||||
: baseRule.meta.schema, | ||||
{ | ||||
properties: { | ||||
ignoreEnums: { | ||||
type: 'boolean', | ||||
description: 'Whether enums used in TypeScript are considered okay.', | ||||
}, | ||||
ignoreNumericLiteralTypes: { | ||||
type: 'boolean', | ||||
description: | ||||
'Whether numbers used in TypeScript numeric literal types are considered okay.', | ||||
}, | ||||
ignoreReadonlyClassProperties: { | ||||
type: 'boolean', | ||||
description: 'Whether readonly class properties are considered okay.', |
||||
}, | ||||
ignoreTypeIndexes: { | ||||
type: 'boolean', | ||||
description: 'Whether numbers used to index types are okay.', | ||||
}, | ||||
}, | ||||
}, | ||||
) as unknown as JSONSchema4` | β | |||
ignored |
Set<unknown> |
const | new Set((options.ignore ?? []).map(normalizeIgnoreValue)) |
β |
isAllowed |
boolean | undefined |
let/var | *not shown* |
β |
fullNumberNode |
TSESTree.Literal | TSESTree.UnaryExpression |
let/var | node |
β |
raw |
any |
let/var | node.raw |
β |
Functions¶
normalizeIgnoreValue(value: bigint | number | string): bigint | number
¶
Code
-
JSDoc:
-
Parameters:
value: bigint | number | string
- Return Type:
bigint | number
- Calls:
BigInt
value.slice
normalizeLiteralValue(node: TSESTree.BigIntLiteral | TSESTree.NumberLiteral, value: bigint | number): bigint | number
¶
Code
function normalizeLiteralValue(
node: TSESTree.BigIntLiteral | TSESTree.NumberLiteral,
value: bigint | number,
): bigint | number {
if (
node.parent.type === AST_NODE_TYPES.UnaryExpression &&
['-', '+'].includes(node.parent.operator) &&
node.parent.operator === '-'
) {
return -value;
}
return value;
}
-
JSDoc:
-
Parameters:
node: TSESTree.BigIntLiteral | TSESTree.NumberLiteral
value: bigint | number
- Return Type:
bigint | number
- Calls:
['-', '+'].includes
getLiteralParent(node: TSESTree.Literal): TSESTree.Node | undefined
¶
Code
-
JSDoc:
-
Parameters:
node: TSESTree.Literal
- Return Type:
TSESTree.Node | undefined
- Calls:
['-', '+'].includes
isGrandparentTSTypeAliasDeclaration(node: TSESTree.Node): boolean
¶
Code
-
JSDoc:
-
Parameters:
node: TSESTree.Node
- Return Type:
boolean
isGrandparentTSUnionType(node: TSESTree.Node): boolean
¶
Code
-
JSDoc:
-
Parameters:
node: TSESTree.Node
- Return Type:
boolean
- Calls:
isGrandparentTSTypeAliasDeclaration
isParentTSEnumDeclaration(node: TSESTree.Literal): boolean
¶
Code
-
JSDoc:
-
Parameters:
node: TSESTree.Literal
- Return Type:
boolean
- Calls:
getLiteralParent
isParentTSLiteralType(node: TSESTree.Node): boolean
¶
Code
-
JSDoc:
-
Parameters:
node: TSESTree.Node
- Return Type:
boolean
isTSNumericLiteralType(node: TSESTree.Node): boolean
¶
Code
function isTSNumericLiteralType(node: TSESTree.Node): boolean {
// For negative numbers, use the parent node
if (
node.parent?.type === AST_NODE_TYPES.UnaryExpression &&
node.parent.operator === '-'
) {
node = node.parent;
}
// If the parent node is not a TSLiteralType, early return
if (!isParentTSLiteralType(node)) {
return false;
}
// If the grandparent is a TSTypeAliasDeclaration, ignore
if (isGrandparentTSTypeAliasDeclaration(node)) {
return true;
}
// If the grandparent is a TSUnionType and it's parent is a TSTypeAliasDeclaration, ignore
if (isGrandparentTSUnionType(node)) {
return true;
}
return false;
}
-
JSDoc:
-
Parameters:
node: TSESTree.Node
- Return Type:
boolean
- Calls:
isParentTSLiteralType
isGrandparentTSTypeAliasDeclaration
isGrandparentTSUnionType
- Internal Comments:
isParentTSReadonlyPropertyDefinition(node: TSESTree.Literal): boolean
¶
Code
-
JSDoc:
-
Parameters:
node: TSESTree.Literal
- Return Type:
boolean
- Calls:
getLiteralParent
isAncestorTSIndexedAccessType(node: TSESTree.Literal): boolean
¶
Code
function isAncestorTSIndexedAccessType(node: TSESTree.Literal): boolean {
// Handle unary expressions (eg. -4)
let ancestor = getLiteralParent(node);
// Go up another level while weβre part of a type union (eg. 1 | 2) or
// intersection (eg. 1 & 2)
while (
ancestor?.parent?.type === AST_NODE_TYPES.TSUnionType ||
ancestor?.parent?.type === AST_NODE_TYPES.TSIntersectionType
) {
ancestor = ancestor.parent;
}
return ancestor?.parent?.type === AST_NODE_TYPES.TSIndexedAccessType;
}
-
JSDoc:
-
Parameters:
node: TSESTree.Literal
- Return Type:
boolean
- Calls:
getLiteralParent
- Internal Comments: