sindresorhus/eslint-plugin-unicorn

Rule proposal: `no-unnecessary-rename`

Closed

#1,313 opened on May 24, 2021

 (4 comments) (5 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

Sometimes, when we import a module or destructuring an object, the property name are not available, we have to rename it. But during refactor, the property name is available again, we may want to avoid rename.

- const foo = 1;
const {foo: anotherFoo} = bar;

Since we removed foo, I want to use foo instead of anotherFoo now.

Or

const foo = 1;
+ if (bar) {
	const {foo: anotherFoo} = bar; // `foo` is available in the new scope now, and I don't need access to the outer `foo`.
+ }

Fail

const {foo: anotherFoo} = bar;

// `foo` is avaiable
import {fromPairs as lodashFromPairs} from 'lodash-es';

// No other variable `fromPairs`

Pass

const foo = 1;
const {foo: anotherFoo} = bar;
import {fromPairs as lodashFromPairs} from 'lodash-es';

function fromPairs() {
  // Do something different from `lodash.fromPairs`
}

Contributor guide