Skip to content

Latest commit

Β 

History

History
142 lines (101 loc) Β· 2.69 KB

File metadata and controls

142 lines (101 loc) Β· 2.69 KB

number-literal-case

πŸ“ Enforce proper case for numeric literals.

πŸ’Ό This rule is enabled in the following configs: βœ… recommended, β˜‘οΈ unopinionated.

πŸ”§ This rule is automatically fixable by the --fix CLI option.

Differentiating the casing of the identifier and value clearly separates them and makes your code more readable.

  • Lowercase radix identifier 0x 0o 0b for Number and BigInt.
  • Uppercase or lowercase hexadecimal value for Number and BigInt.
  • Lowercase e for exponential notation.

Examples

Hexadecimal

// ❌
const foo = 0XFF;

// ❌
const foo = 0xff;

// ❌
const foo = 0Xff;

// βœ…
const foo = 0xFF;
// ❌
const foo = 0Xffn;

// βœ…
const foo = 0xFFn;

Binary

// ❌
const foo = 0B10;

// βœ…
const foo = 0b10;
// ❌
const foo = 0B10n;

// βœ…
const foo = 0b10n;

Octal

// ❌
const foo = 0O76;

// βœ…
const foo = 0o76;
// ❌
const foo = 0O76n;

// βœ…
const foo = 0o76n;

Exponential notation

// ❌
const foo = 2E-5;

// βœ…
const foo = 2e-5;
// ❌
const foo = 2E+5;

// βœ…
const foo = 2e+5;
// ❌
const foo = 2E5;

// βœ…
const foo = 2e5;

Options

Type: object

hexadecimalValue

Type: 'uppercase' | 'lowercase'
Default: 'uppercase'

Specify whether the hexadecimal number value (ABCDEF) should be in uppercase or lowercase.

Note: 0x is always lowercase and not controlled by this option to maintain readable code.

Example:

{
	'unicorn/number-literal-case': [
		'error',
		{
			hexadecimalValue: 'lowercase',
		}
	]
}
// ❌
const foo = 0XFF;
const foo = 0xFF;
const foo = 0XFFn;
const foo = 0xFFn;

// βœ…
const foo = 0xff;
const foo = 0xffn;