no-useless-rename

不允许将导入、导出和解构的分配重命名为相同的名称

🔧 Fixable

此规则报告的一些问题可通过 --fix 命令行选项自动修复

ES2015 允许重命名 import 和 export 语句中的引用以及解构赋值。这为程序员在重命名这些引用时执行这些操作提供了简洁的语法:

¥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;

规则详情

¥Rule Details

此规则不允许将导入、导出和解构分配重命名为相同名称。

¥This rule disallows the renaming of import, export, and destructured assignments to the same name.

选项

¥Options

此规则允许使用以下选项进行更细粒度的控制:

¥This rule allows for more fine-grained control with the following options:

  • ignoreImport:设置为 true 时,此规则不检查导入

    ¥ignoreImport: When set to true, this rule does not check imports

  • ignoreExport:设置为 true 时,此规则不检查导出

    ¥ignoreExport: When set to true, this rule does not check exports

  • ignoreDestructuring:设置为 true 时,此规则不检查解构分配

    ¥ignoreDestructuring: When set to true, this rule does not check destructuring assignments

默认情况下,所有选项都设置为 false

¥By 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 { foo1 as foo1 } from "bar";
import { "foo2" as foo2 } from "bar";
export { foo1 as foo1 };
export { foo2 as "foo2" };
export { foo3 as foo3 } from "bar";
export { "foo4" as "foo4" } from "bar";
let { foo3: foo3 } = bar;
let { 'foo4': foo4 } = bar;
function foo({ bar: bar }) {}
({ foo: 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 }) => {}

此规则与 { ignoreImport: true } 的正确代码示例:

¥Examples of correct code for this rule with { ignoreImport: true }:

在线运行
/*eslint no-useless-rename: ["error", { ignoreImport: true }]*/

import { foo as foo } from "bar";

此规则与 { ignoreExport: true } 的正确代码示例:

¥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";

此规则与 { ignoreDestructuring: true } 的正确代码示例:

¥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 }) => {}

何时不使用

¥When Not To Use It

如果你不关心重复地重命名导入、导出和解构分配,则可以安全地禁用此规则。

¥You can safely disable this rule if you do not care about redundantly renaming import, export, and destructuring assignments.

兼容性

¥Compatibility

版本

此规则是在 ESLint v2.11.0 中引入。

资源

ESLint 中文网
粤ICP备13048890号