sindresorhus/eslint-plugin-unicorn

Rule proposal: Forbidden property key

Chiusa

#1283 aperta il 18 mag 2021

 (1 commento) (4 reazioni) (0 assegnatari)JavaScript (468 fork)user submission
help wantednew rule

Metriche repository

Star
 (5022 stelle)
Metriche merge PR
 (Merge medio 1g 16h) (399 PR mergiate in 30 g)

Descrizione

  1. Forbid known object / array / bigint as property key
  2. Fordid unsafe number as property key

Fail

// May lead to unexpected result.
const array = [];
const object = {};

const a = {
	[array]: 1,
	[object]() {}
};

class A {
	[array]: 1
	[object]() {}
}
// `4n` is used as `4` not `'4n'`
const array = [];
array[4n] = '';
// array.length === 5
const a = {
  1.00000000000000001: 1
};

(no-loss-of-precision should already covered.)

const a = {
  1234567891012345678910: 1
};

Pass

const array = [];
const object = {};

const a = {
	[array.join('')]: 1,
	[toString(object)]: 2
};
const array = [];
array[Number(4n)] = '';
const symbol = Symbol("key")
const a = {
	[symbol]: 1
};

This rule need the type info by static analyse.

Guida contributor