Disallow renaming import, export, and destructured assignments to the same name
Some problems reported by this rule are automatically fixable by the --fix command line option
ES2015 allows for the renaming of references in import and export statements as well as destructuring assignments. This gives programmers a concise syntax for performing these operations while renaming these references:
import { foo as bar } from "baz";
export { foo as bar };
let { foo: bar } = baz;
With this syntax, it is possible to rename a reference to the same name. This is a completely redundant operation, as this is the same as not renaming at all. For example, this:
import { foo as foo } from "bar";
export { foo as foo };
let { foo: foo } = bar;
is the same as:
import { foo } from "bar";
export { foo };
let { foo } = bar;
This rule disallows the renaming of import, export, and destructured assignments to the same name.
This rule allows for more fine-grained control with the following options:
ignoreImport: When set to true, this rule does not check importsignoreExport: When set to true, this rule does not check exportsignoreDestructuring: When set to true, this rule does not check destructuring assignmentsBy default, all options are set to false:
"no-useless-rename": ["error", {
"ignoreDestructuring": false,
"ignoreImport": false,
"ignoreExport": false
}]
Examples of incorrect code for this rule by default:
/*eslint no-useless-rename: "error"*/
import { } from "bar";
import { } from "bar";
export { };
export { };
export { } from "bar";
export { } from "bar";
let { } = bar;
let { } = bar;
function foo({ }) {}
({ }) => {}
Examples of correct code for this rule by default:
/*eslint no-useless-rename: "error"*/
import * as foo1 from "foo";
import { foo2 } from "bar";
import { foo as bar1 } from "baz";
import { "foo" as bar2 } from "baz";
export { foo };
export { foo as bar1 };
export { foo as "bar2" };
export { foo as bar3 } from "foo";
export { "foo" as "bar4" } from "foo";
let { foo } = bar;
let { foo: bar } = baz;
let { [qux]: qux } = bar;
function foo3({ bar }) {}
function foo4({ bar: baz }) {}
({ foo }) => {}
({ foo: bar }) => {}
Examples of correct code for this rule with { ignoreImport: true }:
/*eslint no-useless-rename: ["error", { ignoreImport: true }]*/
import { foo as foo } from "bar";
Examples of correct code for this rule with { ignoreExport: true }:
/*eslint no-useless-rename: ["error", { ignoreExport: true }]*/
const foo = 1;
export { foo as foo };
export { bar as bar } from "bar";
Examples of correct code for this rule with { ignoreDestructuring: true }:
/*eslint no-useless-rename: ["error", { ignoreDestructuring: true }]*/
let { foo: foo } = bar;
function baz({ bar: bar }) {}
({ foo: foo }) => {}
You can safely disable this rule if you do not care about redundantly renaming import, export, and destructuring assignments.
This rule was introduced in ESLint v2.11.0.
© OpenJS Foundation and other contributors
Licensed under the MIT License.
https://eslint.org/docs/latest/rules/no-useless-rename