sindresorhus/eslint-plugin-unicorn

Rule proposal: Forbidden property key

Closed

#1,283 opened on May 18, 2021

 (1 comment) (4 reactions) (0 assignees)JavaScript (468 forks)user submission
help wantednew rule

Repository metrics

Stars
 (5,022 stars)
PR merge metrics
 (Avg merge 1d 16h) (399 merged PRs in 30d)

Description

  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.

Contributor guide