Skip to content

No Useless Constants

ErrorAuto-fixable
small-rules/no-useless-constants

Disallow constants that do not add value.

Diagnostic Messages

uselessConstant
Constant '{{name}}' is only referenced once in the same scope. Inline it directly, or move it to a higher scope if reference stability is needed.
uselessConstantNoFix
Constant '{{name}}' is only referenced once in the same scope. It cannot be auto-inlined because the initializer is not safely movable or the declaration has attached comments. Inline it manually, or move it to a higher scope if reference stability is needed.
uselessConstants
Constants '{{names}}' are only referenced once in the same scope. Inline them directly, or move them to a higher scope if reference stability is needed.

Configuration

This rule accepts one options object after the severity.

ignoreCallPatternsOptional

Regular expression patterns for call expressions that should be ignored.

{
"jsPlugins": [
"@pobammer-ts/small-rules"
],
"rules": {
"small-rules/no-useless-constants": [
"error",
{
"ignoreCallPatterns": [
"^Array\\b",
"^Object\\b",
"^Map\\b",
"^Set\\b",
"^WeakMap\\b",
"^WeakSet\\b"
]
}
]
}
}

Examples

Inline adjacent constant
const TITLE_OFFSET = 225;
const TEXT_NATIVE = { Offset: TITLE_OFFSET };

After auto-fix

const TEXT_NATIVE = { Offset: 225 };
Already inlined constant
const TEXT_NATIVE = { Offset: 225 };